Install Android. The width of the button on half the screen.

How to configure (in XML format) that the button will have a width of half the screen. I found only to wrap the contents, match the parent (fills the entire screen) and the exact amount of dp, for example: 50dp. How to set it exactly while holding the screen?

<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:weightSum="2" > <Button android:id="@+id/buttonCollect" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:paddingLeft="8dp" android:paddingRight="8dp" android:text="przycisk" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:text="Button" /> 

+4
source share
3 answers

This can be done if there are two widgets on your layout: use LinearLayout and set layout_width="fill_parent" in both widgets (Button and another widget) and set layout_weight also to the same value. and LinearLayout will share the width between the two widgets equally, and your button will occupy half the screen.

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/buttonCollect" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:paddingLeft="8dp" android:paddingRight="8dp" android:text="przycisk" /> <Button android:id="@+id/button2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:text="Button" /> 
+13
source

This is not possible in XML. However, you can do this in Java by getting the width of the screen using DisplayMetrics , dividing it by 2, and setting the button as the width. Something like that:

 Button button = (Button) findViewById(R.id.button); DisplayMetrics displaymetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); int width = displaymetrics.widthPixels; int buttonWidth = width/2; //Apply this to your button using the LayoutParams for whichever layout you have. 
+7
source

Use the text box.

 <TextView android:id="@+id/textViewHelper" android:layout_width="0dp" android:layout_height="0dp" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" /> 

Then add two buttons or only one of them to the left and / or to the right of it.

 <Button android:id="@+id/saveList" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_alignTop="@+id/deleteAllPlaylists" android:layout_toRightOf="@+id/textViewHelper" android:text="@string/save_temp_playlist" /> <Button android:id="@+id/deleteAllPlaylists" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_toLeftOf="@+id/textViewHelper" android:text="@string/delete_all_playlists" /> 

an answer was found here

My first post on the stack, I hope I helped XD

0
source

All Articles