I have a few buttons (ranges between 1-5) that i would like to show in a horizontal space.
Each space should take up equal amount of space.
The buttons, depending on the state of the current user, will be hidden and shown dynamically.
Ideally, in my java code, i only wish to control the visibility of buttons via setVisibility()
Is there a way to organize my layout, so that the width of my button is always EVENLY distributed across the width of screen?
I thought about using a relative layout, and buttons would line up one by one, but i never get that to work. Alternatively, i could use linearLayout with layout sum, and assign each button a weight of 1. But when i am hiding buttons, the rest of the button do not readjust and take up the remaining space.
Use a LinearLayout with a horizontal orientation. For each of your Buttons, use these attributes:
android:layout_width="0dp"
android:layout_weight="1"
Do not put a weightSum attribute on the parent LinearLayout. Omitting this attribute will cause the buttons to resize as some are shown and hidden.
Just use LinearLayout with horizontal orientation (should give you an error when you place more than 1 element in LinearLayout and don't mention the orientation attribute) and simply place your buttons. Just include the weight = 1 attribute in your buttons to distribute the space equally.
XML File
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.nero.myapplication.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="bottom">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:layout_weight="1"/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:layout_weight="1"/>
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:layout_weight="1"/>
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:layout_weight="1"/>
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
android:layout_weight="1"/>
</LinearLayout>
MainActivity Class
public class MainActivity extends AppCompatActivity {
Button btn1, btn2, btn3, btn4, btn5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = findViewById(R.id.button1);
btn2 = findViewById(R.id.button2);
btn3 = findViewById(R.id.button3);
btn4 = findViewById(R.id.button4);
btn5 = findViewById(R.id.button5);
btn1.setVisibility(View.GONE);
}
}
By setting the visibility of a button to GONE with automatically adjust the size of the remaining buttons with visible visibility. I've tested this piece of code myself to ensure it's functionality.
An alternate approach to using LinearLayout would be to use a ConstraintLayout. Everything in a horizontal chain will be allocated the same amount of space.
Pseudocode below
<ConstraintLayout>
<View
android:id="#+id/one"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_leftToLeftOf="parent"
app:layout_rightToLeftOf="#id/two"
/>
<View
android:id="#+id/two"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_leftToRightOf="#id/one"
app:layout_rightToLeftOf="#id/three"
/>
<View
android:id="#+id/three"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_leftToRightOf="#id/two"
app:layout_rightToRightOf="parent"
/>
</ConstraintLayout>
Related
in the below xml layout, i have two buttons at the same position and they will do their function based on the visibilty set. now i tried to place two textviews
below the buttons, i want the text views to be below both buttons so I used
android:layout_below="#id/actConnect2_btn_connect"
but at run time when the connect-button is visible the text view appears below it, and if pair-button is visible it overlap
how to display the textview below both buttons?
Note: i know that i can use android:layout:marginTop but i want to solve it without it
code:
<Button
android:id="#+id/actConnect2_btn_pair"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/actConnect2_tv_label_devClass"
android:layout_centerInParent="true"
android:text="#string/str_pair"/>
<Button
android:id="#+id/actConnect2_btn_connect"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/str_connect"
android:layout_below="#+id/actConnect2_tv_label_devClass"
android:layout_alignParentStart="true" />
<TextView
android:id="#+id/actConnect2_tv_label_uuids"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/actConnect2_btn_connect"
android:text="Service's UUID: ">
</TextView>
<TextView
android:id="#+id/actConnect2_tv_uuids"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/actConnect2_tv_label_uuids">
</TextView>
Put both buttons in a LinearLayout then put the textview below the LinearLayout
take both button in one layout
<RelativeLayout android:id="#+id/relativeButton"
android:layout_width="wrap_content"
android:layout_below="#id/actConnect2_tv_label_devClass"
android:layout_height="wrap_content">
<Button
android:id="#+id/actConnect2_btn_pair"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="#string/str_pair"/>
<Button
android:id="#+id/actConnect2_btn_connect"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/str_connect"
android:layout_alignParentStart="true" />
</RelativeLayout>
and use this for your textview
android:layout_below="#id/relativeButton"
You have to place the buttons in a Layout (any type and arrange them accordingly)
Assign an ID to that layout.
Place the textView below that layout ID.
I am using the Linear Layout with 6 buttons in it.
Based on the permissions the Buttons are set visible or invisible.
Button1
Button2
Button3
Button4
Button5
Button6
Based on User Permissions
Button1
Button3
Button4
Button6
My question is: is there a way to make the display as
Button1
Button3
Button4
Button6
Can android xml for layouts manage this, and if yes how?
Here is your layout Please check.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="6" >
<Button
android:id="#+id/btn1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 1" />
<Button
android:id="#+id/btn2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 2"
android:visibility="gone" />
<Button
android:id="#+id/btn3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 3" />
<Button
android:id="#+id/btn4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 4" />
<Button
android:id="#+id/btn5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 5"
android:visibility="gone" />
<Button
android:id="#+id/6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 6" />
</LinearLayout>
You just set the orientation horizontal in linearlayout
android:orientation="horizontal"
To achieve your goal:
1 - Set your LinearLayout width to match_parent and its orientation to horizontal:
android:layout_width="match_parent"
android:orientation="horizontal"
This will make your buttons lay on a row.
2 - Set all the buttons weight to 1 and their widths to 0dp:
android:layout_weight="1"
android:layout_width="0dp"
This will make your buttons fill the space horizontally, in percentage (each one will be given the same width)
3 - If you programmatically set the visibility to View.GONE, the invisible buttons will take no space, leaving the other buttons more space to fill (they will automatically grow in size). By setting the visibility to View.VISIBLE, again, the visible Button will reclaim its space and the Buttonn widths will be narrowed
You can do it using Layout Weight from LinearLayout http://developer.android.com/guide/topics/ui/layout/linear.html#Weight
But as you are dynamically changing the set ot buttons, you might have to do it programmatically, for that you can only do it using the LinearLayout.LayoutParams like this:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 0.5f);
button.setLayoutParams(params);
I have a custom dialog which has 3 buttons and sometimes it has one button only. When I have 3 buttons, the LinearLayout fits those buttons well in itself. But when I have just one button, it gives the whole width available to a single button making that button look too big. I want that, if there's only one button, it should only take half the the complete width available or should wrap content (Button image.) See following images for reference-
Refer this XML file which is similar to your requirement. Just visibility to gone to not required button.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" android:weightSum="3" android:gravity="center_horizontal">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button"/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button"/>
</LinearLayout>
In order to have one button take up half the available screen width you need to
set android:weightSum="2" in the parent LinearLayout
set android:layout_weight="1" in the Button
I'm using Relative Layout to create a calculator screen, and i have a row of buttons labeled 1, 2, and 3. I want them evenly spaced, but i'm not sure how to do this with Relative Layout
I'm used to using the android:layout_weight function in LinearLayout, and give each of them a value of android:layout_width="fill_parent", as well as a layout_weight="1"
any way to get this to happen on calculator screen (without specifying DP or PX)
this is what i have set up at the moment, and they are aligned in order from left to right, under the TextView (calculator output screen). any suggestions/solutions to evenly space, filling the width of the screen?
<TextView
android:id="#+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/edit_text"
android:layout_margin="6px"
/>
<Button
android:id="#+id/button1"
android:text="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView"
/>
<Button
android:id="#+id/button2"
android:text="2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView"
android:layout_toRightOf="#+id/button1"/>
<Button
android:id="#+id/button3"
android:text="3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView"
android:layout_toRightOf="#+id/button2"
/>
If you want equal spacing, you do not want a RelativeLayout. Use a LinearLayout, as you suggest in your question. If needed, put the Buttons in a LinearLayout and put the LinearLayout in the RelativeLayout.
The easiest would be to manipulate the view during OnCreate().
Something like:
OnCreate(Context ctx){
int w = getWidth();
int h = getHeight();
Button B1 = findViewById(R.id.button1);
B1.setWidth(w/3);
--repeat for button2,3 textview--
}
Another option would be to use a TableLayout where you stretch the columns and force the buttons to fill parent. This would require you to use
<TableLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="0,1,2"
android:layout_below="#+id/textView">
<TableRow><Button
android:id="#+id/button1"
android:text="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/><Button
android:id="#+id/button2"
android:text="2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</TableRow>
</TableLayout>
How can i make a layout in XML with four button on the same line. I need the first and last button to be of specific width (40px) and the middle two to be of their content width (wrap_content)?
Sample how I would like to position buttons...
|-b1-|....|--b2--||---b3---|....|-b4-|
Thanks in advance!
Just put the four buttons in a horizontal LinearLayout. Define b1 with a layout_width of 40px, b2 and b3 with a layout_width of wrap_content, then b4 with a layout_width of 40px. If you want it centered, just set the LinearLayout's layout_width to fill_parent and give it a gravity of center_horizontal. Something like:
EDIT: Oh, if you're wanting the two 40px buttons on the sides, and the two wrap_content buttons centered, you could go two ways. Simplest method would be to add some blank Views to your LinearLayout (I'll demonstrate below), while a more lengthy way would be to use a RelativeLayout and put the two center buttons within a LinearLayout within that RelativeLayout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_width="wrap_content"
>
<Button
android:layout_width="40px"
android:layout_height="wrap_content"
android:text="Button 1"
/>
<View
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3"
/>
<View
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<Button
android:layout_width="40px"
android:layout_height="wrap_content"
android:text="Button 4"
/>
</LinearLayout>