I'm trying to create a layout (which I could include in other layouts), which contains 3 Image buttons (back, menu, forward).
Those 3 Image buttons should be on the same line (because later I would include this layout to other layouts in the bottom of each layout)
I don't understand what I'm doing wrong, I can't see the all 3 Buttons, and they are not in the same row (same horizontal line)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageButton
android:id="#+id/buttomMenuBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:background="#drawable/back_button"
android:onClick="onClickButtomMenu"/>
<ImageButton
android:id="#+id/buttomMenuMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Menu"
android:background="#drawable/HomButton"
android:onClick="onClickButtomMenu"/>
<ImageButton
android:id="#+id/buttomMenuForward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Forward"
android:background="#drawable/forward_button"
android:onClick="onClickButtomMenu"/>
</LinearLayout>
it won't fit because it looks like some of your images are way too big, but we can proportionally weight it so it'll fit.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="3">
<ImageButton
android:id="#+id/buttomMenuBack"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Back"
android:background="#drawable/back_button"
android:onClick="onClickButtomMenu"/>
<ImageButton
android:id="#+id/buttomMenuMenu"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Menu"
android:background="#drawable/HomButton"
android:onClick="onClickButtomMenu"/>
<ImageButton
android:id="#+id/buttomMenuForward"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Forward"
android:background="#drawable/forward_button"
android:onClick="onClickButtomMenu"/>
</LinearLayout>
using weightSum and layout_weight, 3 and 1, we ensure they all each take 1/3 of the space in your linearlayout (oh, and layout_width is 0 because layout_weight overrides it)
You can use LinearLayout with orientation as "horizontal". This will make all the views appear in a row. This LinearLayout can reside as a nested layout inside a another layout, like for example RelativeLayout. You can use this layout design by importing it in other XMLlayouts too.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:id="#+id/buttomMenuBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:background="#drawable/back_button"
android:onClick="onClickButtomMenu"/>
<ImageButton
android:id="#+id/buttomMenuMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Menu"
android:background="#drawable/HomButton"
android:onClick="onClickButtomMenu"/>
<ImageButton
android:id="#+id/buttomMenuForward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Forward"
android:background="#drawable/forward_button"
android:onClick="onClickButtomMenu"/>
</LinearLayout>
Looks like different sized images.
Assuming you have these in a horizontal linear layout; Try changing the height to a fixed dp and put the weight to 1 for all three imagebuttons
Example:
<ImageButton
android:id="#+id/buttomMenuBack"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="35dp"
android:text="Back"
android:background="#drawable/back_button"
android:onClick="onClickButtomMenu"/>
If you do this for all three imagebuttons, they will fill the screen and all be the same height, right beside each other.
Related
I am trying to place two ImageButton in the corners of a LinearLayout as shown in figure below:
I have tried using the android:layout_gravity attribute with the value set to left and right for the two buttons. However, they show just next to each other as shown in image below:
How can I place the two image buttons in the corners of the LinearLayout? I have looked up a lot of previous answers and tried them but nothing seems to work. How can this placement of image buttons be done?
The XML of the layout is in the snippet below:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|fill_horizontal"
android:orientation="horizontal">
<ImageButton
android:id="#+id/prev_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:contentDescription="#string/prev_button"
android:src="#drawable/arrow_left"/>
<ImageButton
android:id="#+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:contentDescription="#string/next_button"
android:src="#drawable/arrow_right"/>
</LinearLayout>
Use a RelativeLayout:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageButton
android:id="#+id/prev_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:contentDescription="#string/prev_button"
android:src="#drawable/arrow_left"/>
<ImageButton
android:id="#+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:contentDescription="#string/next_button"
android:src="#drawable/arrow_right"/>
</RelativeLayout>
A relativelayout is better for positioning views at the edges of the screen
there is another way to do this instead of using RelativeLayout by using a third View , layout xml code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<View
android:layout_width="0dp"
android:layout_weigth="1"
android:layout_height="0.5dp"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
this way is better than using RelativeLayout and makes your layout smoother
I am trying to create a layout as per this picture (paint'ed).
Layout Design
Target level is API 17. This has to be created programmatically, not using XML. This has to be a responsive design.
I extensively researched and attempted other partially similar situations on stackoverflow, using GridLayout, TableLayout, GridView, various layout parameters, gravity, weight, view nesting and so on. However,
(a) I can't get the text buttons width to fill the available width real estate as per device screen size and orientation. Buttons with shorter texts are coming with shorter width.
(b) The plus, minus and number buttons are of fixed height and width irrespective of screen sizes and orientation. But they are not aligning with the text button in the left on the same row. only the bottom few pixels are visible.
I would appreciate any code snippet that can achieve the above layout. Thanks a million.
UPDATE:
Following inputs from #tiny-sunlight, I did this. Next I will recreate this programmatically.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/layoutTable"
android:padding="5dp"
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/layoutRow"
android:padding="5dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_marginEnd="10dp"
android:layout_weight="17"
android:text="This is my button"
android:textSize="15sp"
android:textAllCaps="false"
android:gravity="start"
android:layout_width="0dp"
android:layout_height="40dp" />
<Button
android:layout_marginEnd="5dp"
android:layout_weight="1"
android:text="-"
android:textSize="15sp"
android:textStyle="bold"
android:layout_width="40dp"
android:layout_height="40dp" />
<TextView
android:layout_marginEnd="5dp"
android:layout_weight="1"
android:layout_marginTop="0dp"
android:paddingTop="10dp"
android:text="0"
android:textSize="15sp"
android:gravity="center"
android:layout_width="40dp"
android:layout_height="40dp" />
<Button
android:layout_weight="1"
android:text="+"
android:textSize="15sp"
android:textStyle="bold"
android:layout_width="40dp"
android:layout_height="40dp" />
</LinearLayout>
</LinearLayout>
</ScrollView>
Try to build layout below programmatically.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:padding="5dp"
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:padding="5dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_marginRight="10dp"
android:layout_weight="1"
android:text="1111"
android:layout_width="0dp"
android:layout_height="40dp" />
<ImageView
android:layout_marginRight="5dp"
android:src="#mipmap/ic_launcher"
android:layout_width="40dp"
android:layout_height="40dp" />
<TextView
android:layout_marginRight="5dp"
android:background="#44bb11"
android:layout_marginTop="0dp"
android:paddingTop="10dp"
android:text="11"
android:layout_width="40dp"
android:layout_height="40dp" />
<ImageView
android:src="#mipmap/ic_launcher"
android:layout_width="40dp"
android:layout_height="40dp" />
</LinearLayout>
</LinearLayout>
</ScrollView>
You can use a recycler view, which will provide you scrolling option plus bind views on the fly for you.
So load a linear layout (orientation horizonal) into the recycler view. now for the layout, it then becomes easier. You need a textview, button,textview,button.
For each of these textview and buttons, set widht = 0dp and weight as 7,1,1,1 respectively i.e. your leftmost textview occupy 70% of width whereas all other occupy 10%. Obviously you can change these weights as per your requirements. Thats it for the layout.
Now just fill your data using the adapter.
I was thinking it would be something like android:layout_startOf"centerHoriztonal" or something like that but I have searched and couldn't find anything. If it helps I am trying to make a soundboard app where all the buttons can be the same size across multiple device screens.
try this xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/button1"
android:text="Here is button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
/>
<Button
android:id="#+id/button2"
android:text="Here is button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
/>
</RelativeLayout>
You can wrap the button(s) inside a horizontal LinearLayout and use layout_weight to divide the screen in half and then layout_alignParentLeft/layout_alignParentRight to align the buttons inside a RelativeLayout that takes up exactly half the screen width.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Button Text"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1">
</RelativeLayout>
</LinearLayout>
Another way to do it is to create a dummy view object that is centered horizontally in the parent view, and align your buttons to the left or right of it:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="#+id/dummyview"
android:layout_width="0px"
android:layout_height="0px"
android:layout_centerHorizontal="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/dummyview"
android:text="Button Text"/>
</RelativeLayout>
Are you sure you want the same size of buttons in all devices? People try to avoid it and make make view elements flexible.
Anyways if you want to set same size just directly write in layout
android:layout_width = "#dimen/valueForButton"; //some absolute value
For starting at same point, just add all buttons inside LinearLayout and make some operations there, such as
android:layout_gravity = "center_horizontal";
Your layout must be something like this:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="#dimen/valueForButton"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_width="#dimen/valueForButton"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/button3"
android:layout_width="#dimen/valueForButton"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
I have a problem with a layout in android. I want that two views should have the equal width, and they should almost use the full width of the screen. Each view should contain a centered label.
It should look like this when it's done:
Here is what I have so far:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<View
android:id="#+id/view1"
android:layout_width="10dp"
android:layout_height="90dp"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp" />
<View
android:id="#+id/view2"
android:layout_width="10dp"
android:layout_height="90dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp" />
</RelativeLayout>
I just have placeholder values for the width right now.
Thanks.
You have to use android:layout_weight attribute in xml so try below code hope it will resolve your problem :-
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:text="Register"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="10dip"
android:layout_margin="5dp"
android:layout_weight="1" />
<Button
android:text="Not this time"
android:id="#+id/cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="10dip"
android:layout_margin="5dp"
android:layout_weight="1" />
</LinearLayout>
To create a linear layout in which each child uses the same amount of
space on the screen, set the android:layout_height of each view to
"0dp" (for a vertical layout) or the android:layout_width of each view
to "0dp" (for a horizontal layout). Then set the android:layout_weight
of each view to "1"
For more info you need to read Linear layout.
It will be easier with LinearLayout, set the layout to match_parent with horizontal orientation.
After that, set layout_width to 0px and layout_weight=1 in both of your view.
See this for good explanation :
Layout buttons so each divides up the space equally
You have to use layout_weight attribute for this.
<Button
android:text="Register"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="10dip"
android:layout_margin="5dp"
android:layout_weight="1" />
<Button
android:text="Not this time"
android:id="#+id/cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="10dip"
android:layout_margin="5dp"
android:layout_weight="1" />
</LinearLayout>
I am going through the tutorials at developer.android.com and they do not cover something I want to try.
In my XML file (LinearLayout) I have 3 elements, a textblock and 2 buttons.
I would like to have the TextBlock taking up all available width. IN HTML terms I would like it to be block.
Then underneath I would like the 2 buttons to be on the same line. In HTML terms I would like it to be inline.
My XML:
<LinearLayout android:orientation="horizontal">
<TextView
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name" />
<Button
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Another option:
Create a linearlayout and add textview and another linearlayout with orientation horizontal within that the 2 buttons.
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
you can sort of do it in one linear layout, but it's not going to be very flexible. You'd either want to use a relative layout and position the button below the text view or you can use two linear layouts (first is vertical orientation containing the textview and the second linear layout, that one is a horizontal linear layout containing the two buttons)
Simple fixes:
<TextView
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/app_name" />
<Button android:id = "#+id/thisbutton"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="thisbutton"
/>
That's the basic idea.
Look up all the features that are available in the layouts. Because there are a lot, and they are quite flexible.