Issue
I have some XML. In this XML there is a single button inside a relative layout. The relative layout takes up the entire screen. I want the button to have 1/3 of the screen width and height. How could I do this?
Here is the XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:paddingBottom="0dp"
tools:context="com.vroy.trapper.MainMenuActivity"
android:layout_weight="2">
<Button
android:layout_width="0dp"
android:layout_height="0dp"
android:text="@string/menu_button_text"
android:background="@drawable/round_button"
android:id="@+id/button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
I looked at this question, but even though I assigned a weight to the relative layout itself I could not assign a weight to the button.
Solution
you can do this easily in java code.
write this code in oncreate.
Button btn = (Button) findViewById(R.id.button);
int width = getResources().getDisplayMetrics().widthPixels/3;
int height = getResources().getDisplayMetrics().heightPixels/3;
btn.setLayoutParams(new RelativeLayout.LayoutParams(width, heigth));
Answered By - Jayanth
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.