I'm trying to create a menu on my home screen but I cannot see the second button that I created. I tried android:layout_below="#+id/score_button" but it gives warning "invalid layout parameter"
Here is my xml file
<LinearLayout 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:orientation="horizontal" >
<Button
android:id="#+id/score_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="scoreGame"
android:text="#string/score_button" />
<Button
android:id="#+id/settings_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/action_settings" />
</LinearLayout>
Something that I've noticed is that whichever button is placed first in the xml file is the one that is displayed when I run the app on my device.
Thanks in advance for any help, much appreciated.
use RelativeLayout instead of LinearLayout
then you can use android:layout_below="#+id/score_button"
and remove android:orientation="horizontal" when using RelativeLayout
Change the layout_width to wrap_content:
<Button
android:id="#+id/score_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="scoreGame"
android:text="#string/score_button" />
<Button
android:id="#+id/settings_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/action_settings" />
Also if you want equal width buttons, give layout_weight:
android:layout_weight="1"
to both the buttons.
try below code:-
<LinearLayout 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:orientation="horizontal" >
<Button
android:id="#+id/score_button"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:onClick="scoreGame"
android:text="#string/score_button" />
<Button
android:id="#+id/settings_button"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="#string/action_settings" />
</LinearLayout>
Repace your xml code by this code...
<LinearLayout 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:orientation="horizontal" >
<Button
android:id="#+id/score_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="scoreGame"
android:text="#string/score_button" />
<Button
android:id="#+id/settings_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/action_settings" />
</LinearLayout>
User orientation as Vertical. In linear layout there is no layout_below attribute. Use Relative layout instead.
To force same width for both buttons:
<Button
android:layout_width="0dp"
android:layout_weight="1"
... />
<Button
android:layout_width="0dp"
android:layout_weight="1"
... />
To use natural buttons width:
<Button
android:layout_width="wrap_content"
... />
<Button
android:layout_width="wrap_content"
... />
Instead of "match_parent" for width, put weight
<Button
android:id="#+id/score_button"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:onClick="scoreGame"
android:text="#string/score_button" />
<Button
android:id="#+id/settings_button"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="#string/action_settings" />
IF you want to show the menu in horizontal
<LinearLayout 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:orientation="horizontal" >
<Button
android:id="#+id/score_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="scoreGame"
android:layout_weight="1"
android:text="#string/score_button" />
<Button
android:id="#+id/settings_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/action_settings" />
</LinearLayout>
And if you want to show one blow the other change orientation to Vertical
Try this..
Change this orientation horizontal to vertical like below
android:orientation="horizontal"
to
android:orientation="vertical"
Adjust the button width and height appropriately and check again .. All the best
The android:layout_below="" attribute can only be used with a Relative Layout. You have chosen a Linear Layout.
Also both the buttons choose the "match_parent" for the android:layout_width attribute.
Either change "match_parent" to "wrap_content", otherwise change the android:orientation tag of the linear layout to "vertical".
Also if you change the layout to relative, it will work.
Related
So, I am trying to use a linear layout to hold three different buttons, each should take up 33% of the width on one line. This linear layout will be below all the other content in my relative layout, which holds all the other widgets on this activity. Unfortunately, when I add the third button into the layout, the other two have a white bar along the bottom of them and the third button (in this case the home button) is positioned higher than the others.
Can someone explain this behavior and how to rectify it? Thanks.
This is the XML file for the linear layout, I've removed all the text for the other widgets. If that would be helpful, I can post it as well.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
<Button
android:layout_width="0dp"
android:layout_weight=".33"
android:layout_height="wrap_content"
android:text="#string/adfazsdfasdfadfsagjlfkdlgjklfsadgfjgps"
android:onClick="resetDates"
android:background="#drawable/sumbitstyleing"
android:id="#+id/resetDatesButton" />
<Button
android:layout_width="0dp"
android:layout_weight=".33"
android:layout_height="wrap_content"
android:text="#string/home"
android:onClick="home"
android:id="#+id/homeButtonSearch"
android:background="#drawable/generalbutton" />
<Button
android:layout_weight=".33"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/submitchanges"
android:onClick="submitChanges"
android:background="#drawable/sumbitstyleing"
android:id="#+id/submitchanges" />
</LinearLayout>
The first picture is WITHOUT the third button, the second picture is WITH the third button.
Try this, I have removed Properties you should use with RelativeLayout and unnecessary with LinearLayout, and buttons aligned horizontally,
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
and assigned Weightsum to LinearLayout along with button height to match parents
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:weightSum="1">
<Button
android:id="#+id/resetDatesButton"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".33"
android:onClick="resetDates"
android:text="Rese check dates" />
<Button
android:id="#+id/homeButtonSearch"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".33"
android:onClick="home"
android:text="home" />
<Button
android:id="#+id/submitchanges"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".33"
android:onClick="submitChanges"
android:text="submit changes" />
</LinearLayout>
</RelativeLayout>
Result
Here you can go this way :
<?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"
android:background="#color/white"
>
<LinearLayout
android:layout_width="300dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerHorizontal="true"/>
<!--Buttons -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="3"
>
<android.support.v7.widget.AppCompatButton
android:id="#+id/motor_team_send_sms_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/send_sms"
/>
<android.support.v7.widget.AppCompatButton
android:id="#+id/motor_team_sleep_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/sleep"
/>
<android.support.v7.widget.AppCompatButton
android:id="#+id/motor_team_rise_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/rise"
/>
</LinearLayout>
Output is:
This might help you.
Try this, in your LinearLayout add android:gravity="center"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center">
...
</LinearLayout>
I have a list of elements and a layout at the bottom of the screen. This layout contains some ImageButtons. It looks in such a way:
Here is my markup:
<?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" >
<ListView
android:id="#+id/cloudListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/cloud_list_layout"
android:layout_alignParentTop="true" >
</ListView>
<LinearLayout
android:id="#+id/cloud_list_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:paddingBottom="3dp"
android:paddingTop="3dp"
android:visibility="visible" >
<ImageButton
android:id="#+id/btn_cloud_copy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/cloud_copy_selector" />
<ImageButton
android:id="#+id/btn_cloud_paste"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/cloud_paste_selector" />
<ImageButton
android:id="#+id/btn_cloud_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/cloud_delete_selector" />
</LinearLayout>
</RelativeLayout>
I want to align my ImageButtons uniformly across the entire width. I now have three buttons but they may be more or less. Is it possible to apply some universal markup to the bottom layout that provides uniform aligning across the with for any number of buttons? For three buttons it must look like this:
Thanks!
Apply android:layout_weight="1" to all the ImageButton and change
android:layout_width="wrap_content"
to
android:layout_width="match_parent"
take a look at the weight property.
<ImageButton
android:id="#+id/btn_cloud_copy"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#drawable/cloud_copy_selector"
android:layout_weight="1"
/>
<ImageButton
android:id="#+id/btn_cloud_paste"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#drawable/cloud_paste_selector"
android:layout_weight="1"
/>
<ImageButton
android:id="#+id/btn_cloud_delete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#drawable/cloud_delete_selector"
android:layout_weight="1"
/>
i am desinging a xml
<?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="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.05"
android:background="#000000"
android:orientation="horizontal" >
<Button
android:id="#+id/btncancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center"
android:background="#CCCCCC"
android:text="Cancel" />
<Button
android:id="#+id/btnadd"
android:layout_width="82dp"
android:layout_height="wrap_content"
android:gravity="right"
android:background="#3399FF"
android:text="Add" />
</LinearLayout>
</LinearLayout>
for button "Add" android:gravity=right is not working.i have give a layout:margin,then only its moving right.can anyone help me out.
It's a little unclear what you want, but do notice the following:
gravity affects the contents of the widget i.e. your button text.
layout_gravity affects positioning of the widget in the parent layout
So if you want the button to be on right, change gravity to layout_gravity.
Use RelativeLayout instead of LinearLayout.
and use android:layout_alignParentRight="true" for btnadd.
EDIT :
Code :
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.05"
android:background="#000000"
android:orientation="horizontal" >
<Button
android:id="#+id/btncancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center"
android:background="#CCCCCC"
android:text="Cancel" />
<Button
android:id="#+id/btnadd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_gravity="right"
android:background="#3399FF"
android:text="Add" />
</RelativeLayout>
Change the LinearLayout to be as RelativeLayout
and instead of android:gravity="right" use android:layout_alignParentRight="true"
It is because you use LinearLayout (orientation horizontal)
Try to use relative layout
you are using linear layout in linear layout your layout depends on orientation like you are using vertical:
use relative layout instead to solve your problem,and give margins you want
also to align your button right use:
android:layout_alignParentRight="true"
your problem would definetly be solved with this.
it was because of you are using android:orientation="horizontal"in Linear Layout
use this android:orientation="vertical"
or use this i modified your code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.05"
android:background="#000000"
android:orientation="horizontal" >
<Button
android:id="#+id/btncancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#CCCCCC"
android:text="Cancel" />
<Button
android:id="#+id/btnadd"
android:layout_width="82dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#3399FF"
android:text="Add" />
</RelativeLayout>
If you want to achieve something like this:
You can do that either with RelativeLayout or LinearLayout.
For LinearLayout you explicitely add space between the buttons. This is because the android:layout_gravitypositions the view only within the space, that LinearLayout has given. And this space is not the LinearLayout itself, but the sum of all views, that were layed out linearly (as the name says).
Here's my way of putting the two buttons:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<Button
android:id="#+id/btnadd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add" />
<ImageView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#android:color/transparent" />
<Button
android:id="#+id/btncancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel" />
</LinearLayout>
I always prefer LinearLayout over RelativeLayout as I experienced LinearLayout as more robust, especially on Android's 2.x versions.
BTW: The difference between android:layout_gravity and android:gravity:
The layout_gravity tells the parent layout of a view to position the whole view within the bounds that the layout defines. In case of LinearLayout this is the space that was allocated by linearly putting the views one after the other.
The gravity tells the view to position its foreground within the available space. You might have seen, that your Add-Button has shown the text on the right side.
I have a layout like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include layout="#layout/basicinfo" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="profile" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="item" />
</LinearLayout >
the problem is that it only shows the include part,no buttons showing up. if I remove the include layout, buttons will show up. any idea what I am doing wrong here? thanks.
update: the basicinfo layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:src="#drawable/icon"/>
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/imageView1"
android:layout_toRightOf="#id/imageView"
android:orientation="vertical" >
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text="Name:" />
<TextView
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text="Catogery:" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text="Description" />
<TextView
android:id="#+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text="Owner" />
</LinearLayout>
</RelativeLayout>
Define your include file layout_height and width for eample:
<include layout="#layout/basicinfo"
android:layout_width="100dp"
android:layout_height="100dp"/>
I fount out the problem. it was because I used match_parent in basic layout. I just changed that from
android:layout_width="match_parent"
android:layout_height="match_parent"
to
android:layout_width="wrap_content"
android:layout_height="wrap_content"
thanks for help.
I would check layout_width and layout_height. This is mostly the point in displaying errors.
included layout is as big as the screen dimension, so the buttons are outside view area. you need to give dimension to this include layout
Change the outer LinearLayout width and height to fill_parent and also place a scroll bar inside it.
One of the main issues with an include layout is that BOTH android:layout_width and android:layout_height must be included or the layout will simply fill the screen (if for instance only the height is set, it will be ignored without setting the width as well).
So something like this should be done:
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="#layout/myLayout" />
Use Merge...
use the element as the root view for the re-usable layout
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/add"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/delete"/>
</merge>
Developer Site
Pictures: http://img838.imageshack.us/img838/1402/picse.png
How do I make the layout in Pic. 2 using RelativeLayout ONLY. Below is the xml layout of the first screenshot.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/timer_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="10dip"
android:textSize="30sp"
android:layout_alignParentTop="true"
android:text="#string/timer_start" />
<ListView
android:id="#+id/questions_listview"
android:layout_below="#id/timer_textview"
android:layout_width="wrap_content"
android:layout_height="fill_parent" />
<Button android:id="#+id/true_btn"
android:text="#string/true_option"
android:layout_alignParentBottom="true"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/false_btn"
android:text="#string/false_option"
android:layout_toRightOf="#id/true_btn"
android:layout_alignBaseline="#id/true_btn"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
You could use LinearLayout at bottom to achieve this. But you want to use RelativeLayout layout only so:
<RelativeLayout android:layout_width="fill_parent" ... >
...
<View android:id="#+id/helper"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<Button android:id="#+id/true_btn"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/helper"/>
<Button
android:id="#+id/false_btn"
android:layout_alignBaseline="#id/true_btn"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/helper" />
</RelativeLayout>
Can't you really use nested layouts?
If you change the buttons to
<Button android:id="#+id/true_btn"
android:text="#string/true_option"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/false_btn"
android:text="#string/false_option"
android:layout_toRightOf="#id/true_btn"
android:layout_alignBaseline="#id/true_btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
you use at least the full width although the false_btn gets all remaining space.
To increase the width of the first true_btn you could set the minWidth attribute - either use a fixed value or get the screen width and make the minWidth the half of it