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);
Related
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>
I have two different LinearLayout(vertical). 1st LinearLayout has 3 buttons named btnX, btnY, btnZ. 2nd LinearLayout has 2 buttons named btnA, btnB. btnB top alignment should follow btnY. If I add a new button btnX(suppose), btnB height will be increased as given picture. You can get a clear idea from it.
You could use layout weight.
In your LinearLayouts, add
android:weightSum=3
Then, assign this to each layout:
android:layout_weight=1
That way, any button will take up 1/3 of the height, regardless of how many layouts you have. If you want a button to take 2/3 of the height, set layout_weight to 2.
The most efficient solution is to implement a relative layout... but you could try putting your button B inside another independent linearlayout and put them inside a relativelayout
Explain more about it, but i hope it helps. change width and height as you like, height of button B is ((Height of button A)*2)+margin top of buttons
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/btnZ"
android:layout_width="216dp"
android:layout_height="86dp"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
android:text="Button Z"/>
<Button
android:id="#+id/btnY"
android:layout_width="216dp"
android:layout_height="86dp"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
android:text="Button Y"
android:layout_below="#+id/btnZ"/>
<Button
android:id="#+id/btnX"
android:layout_width="216dp"
android:layout_height="86dp"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
android:text="Button X"
android:layout_below="#+id/btnY"/>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/btnA"
android:layout_width="216dp"
android:layout_height="86dp"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
android:text="Button A"/>
<Button
android:id="#+id/btnB"
android:layout_width="216dp"
android:layout_height="196dp"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
android:text="Button B"
android:layout_below="#+id/btnA"/>
I have a layout in which buttons placed on a screen, i want that if the screen has more space then buttons should be three per row, whatever the space they get, also their sizes should be adjusted with screen and i want maximum three buttons per row in the centre of the screen. Right now i have used linear layout for each row of two buttons, please guide me how to make adjustable buttons so if the screen size is big, it should display three buttons max, if size is very small it should display only two buttons per row with small button sizes and as explained on default screen it should display three buttons per row.
See my code example here
<LinearLayout android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="25dip"
android:paddingRight="25dip"
android:paddingTop="25dip"
android:layout_width="fill_parent">
<Button
android:id="#+id/Car"
android:layout_width="110dip"
android:layout_height="110dip"
android:layout_marginLeft="10dip"
android:text="#string/car" />
<Button
android:id="#+id/Taxi"
android:layout_width="110dip"
android:layout_height="110dip"
android:layout_marginLeft="10dip"
android:text="#string/taxi" />
</LinearLayout>
You can use layout_weight to set the width of the button automatically according to the width of LinearLayout.
<LinearLayout
android:id="#+id/wrapper"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="25dip"
android:paddingRight="25dip"
android:paddingTop="25dip"
android:layout_width="match_parent">
<Button
android:id="#+id/Car"
android:layout_width="0dp"
android:layout_height="110dip"
android:layout_marginLeft="10dip"
android:layout_weight="1"
android:text="#string/car" />
<Button
android:id="#+id/Taxi"
android:layout_width="0dp"
android:layout_height="110dip"
android:layout_marginLeft="10dip"
android:layout_weight="1"
android:text="#string/taxi" />
</LinearLayout>
However, I don't think you can set the LinearLayout to have three buttons depends on the width by XML. You can only achieve it programmatically.
if(getCurrentScreenWidth() > SPECIFIED_WIDTH_FOR_3_BUTTONS){
LinearLayout layout = (LinearLayout)findViewById("wrapper");
Button button = createNewButton();
layout.addView(button)
}
BUT the best practices will be to load different layout according to screen as described in android document
This is how I would do this.
For each of your buttons set width like:
<Button
android:id="#+id/Car"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="110dip"
android:layout_marginLeft="10dip"
android:text="#string/car" />
If you set the width to 0dp and the weight to 1 for each of your buttons then the buttons will expand to fill the space.
Using fix value for height and width may causes the problem with different screen. use below xml code to align button as per your requirement.
<?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="wrap_content"
android:orientation="horizontal"
android:weightSum="3" >
<Button
android:id="#+id/Car"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/car" />
<Button
android:id="#+id/Taxi"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/taxi" />
<Button
android:id="#+id/bus"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/bus" />
</LinearLayout>
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>