I want to add two buttons in a single linear layout with horizontal orientation, I want my buttons to be stretched if the screen is bigger and contracted if the screen is small, what should I do to keep those exactly the same size together?
Fill parent or match parent causes only one to be displayed on the screen, what should be done to make both of them visible at the same time on any screen size?
give both of them the following properties :
weight =1
layout_width=fill_parent
here is a working code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 1" >
</Button>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 2" >
</Button>
</LinearLayout>
Related
i'm looking for an option to scroll through multiple lists (horizontally) like in the Picture in the attachment. You have the possibility to swipe left or Right to get to the next listview. On top there should be some buttons to click or to scroll
I tried putting ListViews in something like this Code but it doesn't work properly.
<HorizontalScrollView
android:id="#+id/horizontalScroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbarFadeDuration="250">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="0px">
<Button
android:id="#+id/buttonOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/Black"
android:text="hallo"
android:textColor="#color/White">
</Button>
<Button
android:id="#+id/buttonTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/Black"
android:text="hallo"
android:textColor="#color/White">
</Button>
</LinearLayout>
</HorizontalScrollView>
I got it. The Tool I was looking for is a ViewPager.
i am beginner to android programming and trying to build a simple app program.
whenever i try to make more than one views they stack on each other.
I am trying to make a text field and a button, but when i run it, the text field and button overlap each other however, i want them to be separated by some distance.
i am using the following code.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context="com.example.new1.MainActivity" >
<EditText android:id="#+id/edit_message"
android:layout_weight= "1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send" />
</RelativeLayout>
i am checking this code on Samsung Galaxy S2.
does anybody knows the solution to this problem and can guide me where i am doing it wrong.
Either use LinearLayout so that elements stack horizontally or vertically, or use attributes such as android:layout_toRightOf="#id/edit_message" to control placement within a RelativeLayout.
Find out more about LinearLayout here: http://developer.android.com/guide/topics/ui/layout/linear.html and RelativeLayout here: http://developer.android.com/guide/topics/ui/layout/relative.html
RelativeLayout positions views relative to each other. So if you do not specify the relationship between view, all the views will be put one above the other. You can either use LinearLayout with orientation attribute or define relationship between views. Following can be your working code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<EditText
android:id="#+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:hint="#string/edit_message"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/edit_message"
android:text="#string/button_send" />
</RelativeLayout>
Here android:layout_toRightOf="#id/edit_message" lets your button to be positioned to right of your edittext
Depending on what you want, a horizontal layout or vertical, you need a fitting layout xml.
You use relativeLayout, where you have to specify the parents to layout like:
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
If your beginner better take a look at a linear layout in vertical or horizontal mode.
You dont have to specify this then.
Like this:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="100"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/bResults"
android:text="Try Command"
android:layout_weight="20" />
<ToggleButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/tbPassword"
android:layout_weight="80"
android:checked="true"
/>
</LinearLayout>
This will put your two buttons nexto each other
Here is my layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/outerBox"
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
<GridLayout
android:id="#+id/checksHere"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:columnCount="5"
android:columnWidth="100dp" />
<GridLayout
android:id="#+id/textHere"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/checksHere"
android:columnCount="4" />
<Button
android:id="#+id/submitButton"
android:layout_width="185dip"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/outerBox"
android:layout_below="#id/textHere"
android:layout_margin="3dip"
android:onClick="submit"
android:text="#string/submit" />
<Button
android:id="#+id/cancelButton"
android:layout_width="185dip"
android:layout_height="wrap_content"
android:layout_below="#id/textHere"
android:layout_margin="3dip"
android:layout_toRightOf="#id/submitButton"
android:onClick="next"
android:text="#string/cancel" />
<Button
android:id="#+id/backButton"
android:layout_width="185dip"
android:layout_height="wrap_content"
android:layout_below="#id/textHere"
android:layout_margin="3dip"
android:layout_toRightOf="#id/cancelButton"
android:onClick="next"
android:text="test" />
</RelativeLayout>
I have a relative layout. The layout has two grid layouts at the top, which can be populated in my java code with text or checks. They can be blank. Then I have three buttons. The first two buttons display on the screen just fine. The third button just sits on top of the first button:
I changed the third button (backButton) to be toRightOf submitButton just to see what would happen. It goes on top of the cancelButton, as expected. I feel like I'm missing a simple fundamental, but I have not been able to figure it out.
Well this doesn't necessarily fix the problem with RelativeLayout, but you could always group the three buttons together in a single LinearLayout. This should prevent the buttons from overlapping.
I am not sure but when testing in eclipse changing all the android:layout_width="185dip" to android:layout_width="wrap_content" for buttons worked for me.
I'm creating a car dock application for myself and I have 6 buttons to allow me to place shortcuts to applications. I've designed a layout for landscape and portrait mode. I'm having issues with screen sizes in portrait mode. I have my icons lined up like so:
Text up here
------------
| 1 | 2 |
------------
| 3 | 4 |
------------
| 5 | 6 |
------------
Here's the XML for the buttons:
<Button
android:id="#+id/btnLaunchSlotOne"
style="#style/launchButton"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_weight="1"
android:layout_marginRight="8dp"
android:padding="10dp"
android:text="Navigation"
android:textColor="#ffffff" />
Now the problem I'm having is when I test the app on a smaller screen size, the buttons don't seem to resize and they go off the screen at the bottom. How can I get them to scale properly? I thought using "dp" took care of all that?
Do I need to calculate the screen size programatically?
Thanks for the suggestions / help!
Use something like the layout below to correctly nest the buttons:
<LinearLayout
android:orientation="vertical">
<TextView>Put your Text here</TextView>
<LinearLayout
android:orientation="horizontal"
android:layout_weightSum="2">
<LinearLayout
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="0dp"
android:layout_weightSum="3"
android:layout_weight="1">
<Button
android:layout_weight="1"/>
<Button
android:layout_weight="1"/>
<Button
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_weightSum="3"
android:layout_height="fill_parent"
android:layout_width="0dp"
android:layout_weight="1">
<Button
android:layout_weight="1"/>
<Button
android:layout_weight="1"/>
<Button
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Then each button needs height of 0dp (this lets the weight apply and scale the button horizontally) and width of fill parent.
The trick is that the weight of a child element will cause the dimension that is set to 0dp to fill to a percentage of of the weightSum of the parent element.
Eg i have a linear layout with weightSum 3 and 3 child buttons of weight 1. When height is set to 0dp they will each take 1/3 of the height of the parent as their height. When width is set to 0dp they will each take 1/3 of the width of the parent as their own.
This is the best way to organise items according to a percentage width or height.
As an aside if you had two buttons one with weight 2 and one with weight 1 then 1 button would be 33% of the parents height/width and the other 66% of the parents height/width. Handy little trick and works on all screens.
If you want the buttons to always take up the full screen you could use a GridView which will automatically handle scaling on all screen sizes.
you can use the below xml file:::
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:text="Text 1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<LinearLayout
android:orientation="horizantal" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:weight_sum = "2">
<Button android:layout_width="fill_parent" android:layout_weight="1"
android:layout_height="fill_parent" android:text="test" />
<Button android:layout_width="fill_parent" android:layout_weight="1"
android:layout_height="fill_parent" android:text="test" />
</LinearLayout>
<LinearLayout
android:orientation="horizantal" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:weight_sum = "2">
<Button android:layout_width="fill_parent" android:layout_weight="1"
android:layout_height="fill_parent" android:text="test" />
<Button android:layout_width="fill_parent" android:layout_weight="1"
android:layout_height="fill_parent" android:text="test" />
</LinearLayout>
<LinearLayout
android:orientation="horizantal" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:weight_sum = "2">
<Button android:layout_width="fill_parent" android:layout_weight="1"
android:layout_height="fill_parent" android:text="test" />
<Button android:layout_width="fill_parent" android:layout_weight="1"
android:layout_height="fill_parent" android:text="test" />
</LinearLayout>
</LinearLayout>
See an example here. Specifically the Dashboard Layout
I have some problem with multiple screen support, I work with dp(dpi) for specify the layout_heigth and layout_width and I hope that is the better way to support multiple screen, but when I tried with two smartphone I meet two different result.
I give an example, this is a layout I use:
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/cities_main_layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="#+id/citieslist"
android:layout_width="wrap_content"
android:layout_height="320dip"
android:layout_gravity="center_vertical"
android:layout_below="#id/cities_main_layout"
/>
<LinearLayout
android:id="#+id/cities_button_layout"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/citieslist"
android:layout_gravity="center_vertical">
<Button
android:id="#+id/bycountry"
android:layout_height="50dip"
android:layout_width="105dip"
android:background="#drawable/buttonmarket"
android:text="#string/button_bycountry"
/>
<Button
android:id="#+id/top10"
android:layout_height="50dip"
android:layout_width="105dip"
android:background="#drawable/buttonmarket"
android:text="#string/button_top10"
/>
<Button
android:id="#+id/recommended"
android:layout_height="50dip"
android:layout_width="105dip"
android:background="#drawable/buttonmarket"
android:text="#string/button_recommended"
/>
</LinearLayout>
</RelativeLayout>
The button are at the bottom of the layout, and I see two different result:
http://img600.imageshack.us/img600/5513/htcmagicg2.png http://img600.imageshack.us/img600/5513/htcmagicg2.png
http://img441.imageshack.us/img441/6440/samsunggalaxys.png http://img441.imageshack.us/img441/6440/samsunggalaxys.png
In the last smartphone I can see the buttons, instead in the first I cannot...what's wrong?
I have to write a layout for any set of screen??!!!
Your ListView has
android:layout_height="320dip"
Now if the phone screen is smaller, it will not fit.
Try doing this instead: (Edited due to comments. This is displayed correcty in eclipse)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relative"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF0000"
android:layout_above="#+id/linlay">
</ListView>
<LinearLayout
android:id="#+id/linlay"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="30dip"
android:background="#00FF00"
android:layout_alignParentBottom="true">
</LinearLayout>
</RelativeLayout>
Thant should fix it I think.
Cheers
As others have indicated, your problem is that you hardwired in a size for the ListView. If you want a business rule of "have the buttons at the bottom and have the ListView fill up the rest", you actually need to write code that implements "have the buttons at the bottom and have the ListView fill up the rest".
There are two main approaches here:
Use a LinearLayout parent for the buttons and the ListView. Use
android:layout_height="0px" and android:layout_weight="1" for the
ListView. Use a regular android:layout_height for the buttons (presumably in their own LinearLayout) and no
android:layout_weight for for them
Use a RelativeLayout parent for the buttons and the ListView.
Define the buttons as having android:layout_alignParentBottom="true".
Define the ListView as having android:layout_alignParentTop="true"
and android:layout_above="...", where the ... is the ID of the buttons' LinearLayout.
I would say it's because you are specifically declaring a height for your ListView and then laying the LinearLayout that holds your buttons at the bottom. Try changing it instead of being at the bottom of the ListView to something like
<LinearLayout
android:id="#+id/cities_button_layout"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:alignparentbottom="true"
android:layout_gravity="center_vertical">
I'm not entirely sure if align_parent_bottom is the 100% correct spelling of that.
Well, others have beaten me to it while I was typing, haha, but yeah, you're hardwiring a lot of things that shouldn't be, both the ListView and the Buttons. Take a look at this:
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/cities_main_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:id="#+id/cities_button_layout"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
>
<Button
android:id="#+id/bycountry"
android:layout_height="50dip"
android:layout_width="fill_parent"
android:layout_weight="1"
android:background="#drawable/buttonmarket"
android:text="#string/button_bycountry"
/>
<Button
android:id="#+id/top10"
android:layout_height="50dip"
android:layout_width="fill_parent"
android:layout_weight="1"
android:background="#drawable/buttonmarket"
android:text="#string/button_top10"
/>
<Button
android:id="#+id/recommended"
android:layout_height="50dip"
android:layout_width="fill_parent"
android:layout_weight="1"
android:background="#drawable/buttonmarket"
android:text="#string/button_recommended"
/>
</LinearLayout>
<ListView
android:id="#+id/citieslist"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#id/cities_button_layout"
/>
</RelativeLayout>
You have android:orientation on your RelativeLayout, which isn't actually an attribute that RelativeLayout contains.
You should use the layout_weight attribute rather than hardwiring sizes for the Buttons. In my example, all buttons have a width of fill_parent, and a weight of 1. This makes them distribute the space evenly.
List the fixed button layout first, setting it to alignParentBottom="true". Then set the ListView to fill_parent, and layout_above your button layout. This keeps the button layout at the bottom, and makes the ListView take all the space above your buttons.
Tada!