Various items in a ListView row - screenshot example - android

i want to achieve this kinda of row look in my app.
I already have a ImageView/TextView layout, but i want that 'positive' little image in the right always being displayed at the exact same place!
http://cache.ctoof.com/2010/03/gowalla-1.png
Thanks!

Probably by using gravity will do the job
android:layout_gravity="center_vertical|right"
See this sample taken from one of my views
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/LinearLayout01"
android:gravity="center_vertical"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dip"
android:layout_weight="1"
android:paddingRight="8dip"
android:layout_height="wrap_content"
android:id="#+id/LinearLayout01"
android:orientation="vertical">
....
</LinearLayout>
<ImageView
android:id="#+id/imgArrowRight"
android:src="#drawable/tab_right_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dip"
android:layout_gravity="center_vertical|right"></ImageView>
</LinearLayout>

Related

One of two Android ListView filling too much space

I would like to obtain this layout for an Android app for mobile phones:
Icon - Object1
List with entries related to Object1
Icon - Object2
List with entries related to Object2
So far I have used the following layout tree (edited graphically with the editor in Android Studio):
Root-LinearLayout
Vertical LinearLayout
Horizontal LinearLayout with icon and text
ListView
Vertical LinearLayout
Horizontal LinearLayout with icon and text
ListView
May be this is not the best way to organize such layout (may be I should use lists with header, but suggestions very welcome), however it can be a good case for understanding deeper how ListView works.
This is the graphical layout generated:
the blue row corresponds to the first LinearLayout. As you can see from the second screenshot that follows, the second list goes all the way down to Hell, bringing me with her. Is there any way to make the lists respect the wrap_content+ weight behaviour?
The XML code follows. I have tried several combos (both reasonable and unreasonable) of layout:weights but none works. I also tried to set the min-width of the first LinearLayout (the hidden one), but nothing changes.
Could you please help me?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="50dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView15"
android:src="#drawable/abc_ic_menu_share_mtrl_alpha" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Object1"
android:id="#+id/textView24"
android:textSize="26dp"
android:paddingLeft="10dp" />
</LinearLayout>
</LinearLayout>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView2"
android:layout_weight="1" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView16"
android:src="#drawable/abc_ic_commit_search_api_mtrl_alpha" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Object2"
android:id="#+id/textView25"
android:textSize="26dp"
android:paddingLeft="10dp" />
</LinearLayout>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_weight="1" />
</LinearLayout>
It should work if you put your ListViews inside of the child LinearLayouts which hold the LinearLayout that has the TextView and ImageView. You also should be using "0dp" for the height when using weight with a vertical layout.
Something like this, I believe, should work
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:gravity="center_vertical"
android:minHeight="50dp"
android:layout_weight=".2">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView15"
android:src="#drawable/abc_ic_menu_share_mtrl_alpha" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Object1"
android:id="#+id/textView24"
android:textSize="26dp"
android:paddingLeft="10dp" />
</LinearLayout>
<ListView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:id="#+id/listView2"
android:layout_weight=".8" />
</LinearLayout>
Note the other changes: I gave the inner-LinearLayout an arbitrary weight of ".2" then the ListView a weight of ".8". And, of course, set the height to "0dp". You may need to play with those weights a bit but I think doing something like that for both first child LinearLayouts should get you close.
That may get your current layout to work but using headers and/or an ExpandableListView may be a better option.

Layout: background picture stretched

I have difficulties with my layout.
As my first dev project, I'm trying to make a simple chronometer.
I want to have a picture of a chronometer in the background and the start and stop button
at the bottom of the application, side by side and filling the width of the screen.
Here is my best shot, but I'm not satisfied.
All widgets are placed properly but ...
My background is streched and my buttons seem vertically compressed, even the text at the bottom is a bit cropped.
I found that if I change these lines
android:layout_width="fill_parent"
android:layout_height="fill_parent"
by
android:layout_width="wrap_content"
android:layout_height="wrap_content"
The background is ok, the buttons too .. but all widgets are placed in the center of the Activity.
How can I fix this ?
By the way if I want to have my buttons side by side, did I choose the better solution ?
Thanks for your help !
Charles.
... and now my xml ....
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/background"
tools:context=".Chronosv1" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="2" >
<Chronometer
android:id="#+id/chronometer1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1" >
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="50"
android:text="START" />
<Button
android:id="#+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="50"
android:text="STOP" />
</LinearLayout>
I tried relativeLayout.
I don't know how to have two buttons of the same size without using a padding, I don't think it's a good idea if you want your app to run on different screens.
Anyway I come up with this, but I still have my streched image and my buttons don't have the same size.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background"
tools:context=".Chronosv1" >
<Chronometer
android:id="#+id/chronometer1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Start" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="#id/button1"
android:text="Stop" />
Try RelativeLayout ;) I think this is much easier to handle than a LinearLayout. Maybe you can upload a picture how it looks like and I will give you a relativelayout solution ;)
Can you try this out? Should work IMO.
It is a better/fool-proof way to obtain your layout.
<RelativeLayout 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:background="#drawable/background"
tools:context=".Chronosv1" >
<LinearLayout
android:id="#+id/layout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2"
android:layout_alignParentBottom="true">
<Button
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="5dp"
android:text="Start" />
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="5dp"
android:text="Stop" />
</LinearLayout>
<Chronometer
android:id="#+id/chronometer1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/layout1" />
</RelativeLayout>
I finally found the solution.
First problem: my 9 patch image hid my other component.
Solution: add android:padding="0dip" in your linearlayout ... here the subject that gave me my answer (Android layout broken with 9-patch background)
Second problem: why my picture was streched whereas I designed it at the exact size of the screen. This one was a silly question ... it was because of the action bar and the other bar above (with battery level and other stuff).
To get rid of theses bars (included in the theme) use a different version of the Theme in the manifest.
I choose: Theme.Light.NoTitleBar.Fullscreen.
Problems solved.

How does one align views at certain positions of the screen, staying consistent across multiple screen resolutions?

I've got a bit of a problem aligning my buttons. I want to get them at (roughly) 1/3rd and 2/3rd of my screen (I provided a screenshot below to make things a bit more clear)
My code is the following:
<?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"
android:background="#drawable/mainbg">
<Button
android:text="#string/topbutton"
android:background="#drawable/silvertablesbuttonbgkort"
android:id="#+id/topbutton"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center|center_vertical">
</Button>
<Button
android:text="#string/midbutton"
android:background="#drawable/silvertablesbuttonbglang"
android:id="#+id/midbutton"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center|center_vertical">
</Button>
<RelativeLayout
android:id="#+id/underbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<Button
android:text="#string/underbartekst"
android:background="#drawable/silvertablesunderbar"
android:id="#+id/underbarbutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:textSize="20dp"
android:gravity="left|center_vertical" >
</Button>
</RelativeLayout>
I'm trying to get it to look like this: (I'm sorry if posting screenshot mock-ups is frowned upon here, I just couldn't think of a better way to clear up what I'm trying to do)
It seems that I can't post images because I'm too new here... So here is a link to the screenshot: http://i.imgur.com/l3b3z.png
My initial idea was just wrapping a vertical linear layout around the two buttons and putting empty textviews above and in between the two, which would actually work if this app was meant to be running on one screen size, but I'm pretty sure it'd mess up on every phone with another screen resolution. I solved this problem for the bar at the bottom by using a relativelayout with android:layout_alignParentBottom="true", but I can't really think of a way to snap to 1/3rd or 2/3rd. Is there a way to do this that will work with various screen resolutions?
Edit:
I solved my problem! I tried to post it as an answer, but I can't until in 8 hours. Will do then, but for now, I'll just post it as an edit here:
I've placed a TextView of 0x0 in the middle of the screen, and put RelativeLayouts on top of and below it, filling the screen. Then I placed two TextViews of 0x0 in the middle of those layouts, and within those layouts, two new RelativeLayouts. One below the highest TextView, one above the lowest. I placed my buttons in the center of those layouts. It works like a charm, and does not rely on anything but the code itself. This is my code now:
<?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"
android:background="#drawable/mainbg">
<TextView
android:text=" "
android:id="#+id/ankermidden"
android:layout_centerVertical="true"
android:layout_width="0dp"
android:layout_height="0dp">
</TextView>
<RelativeLayout
android:id="#+id/ankerveldboven"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/ankermidden">
<TextView
android:text=" "
android:id="#+id/ankerboven"
android:layout_centerVertical="true"
android:layout_width="0dp"
android:layout_height="0dp">
</TextView>
<RelativeLayout
android:id="#+id/ankerveldmidboven"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/ankerboven">
<Button
android:text="#string/topbutton"
android:background="#drawable/silvertablesbuttonbgkort"
android:id="#+id/topbutton"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center|center_vertical">
</Button>
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/ankerveldonder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/ankermidden">
<TextView
android:text=" "
android:id="#+id/ankeronder"
android:layout_centerVertical="true"
android:layout_width="0dp"
android:layout_height="0dp">
</TextView>
<RelativeLayout
android:id="#+id/ankerveldmidonder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/ankeronder">
<Button
android:text="#string/midbutton"
android:background="#drawable/silvertablesbuttonbglang"
android:id="#+id/midbutton"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center|center_vertical">
</Button>
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/underbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<Button
android:text="#string/underbartekst"
android:background="#drawable/silvertablesunderbar"
android:id="#+id/underbarbutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:textSize="20dp"
android:gravity="left|center_vertical" >
</Button>
</RelativeLayout>
So yeah, that was easier than I thought.
I have found a solution! One that is completely relative to itself, and does not rely on pixels or density pixels at all.
I've placed a TextView of 0x0 in the middle of the screen, and put RelativeLayouts on top of and below it, filling the screen.
Then I placed two TextViews of 0x0 in the middle of those layouts, and within those layouts, two new RelativeLayouts. One below the highest TextView, one above the lowest. I placed my buttons in the center of those layouts.
It works like a charm, and does not rely on anything but the code itself.
This is my code now:
<?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"
android:background="#drawable/mainbg">
<TextView
android:text=" "
android:id="#+id/ankermidden"
android:layout_centerVertical="true"
android:layout_width="0dp"
android:layout_height="0dp">
</TextView>
<RelativeLayout
android:id="#+id/ankerveldboven"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/ankermidden">
<TextView
android:text=" "
android:id="#+id/ankerboven"
android:layout_centerVertical="true"
android:layout_width="0dp"
android:layout_height="0dp">
</TextView>
<RelativeLayout
android:id="#+id/ankerveldmidboven"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/ankerboven">
<Button
android:text="#string/topbutton"
android:background="#drawable/silvertablesbuttonbgkort"
android:id="#+id/topbutton"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center|center_vertical">
</Button>
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/ankerveldonder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/ankermidden">
<TextView
android:text=" "
android:id="#+id/ankeronder"
android:layout_centerVertical="true"
android:layout_width="0dp"
android:layout_height="0dp">
</TextView>
<RelativeLayout
android:id="#+id/ankerveldmidonder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/ankeronder">
<Button
android:text="#string/midbutton"
android:background="#drawable/silvertablesbuttonbglang"
android:id="#+id/midbutton"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center|center_vertical">
</Button>
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/underbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<Button
android:text="#string/underbartekst"
android:background="#drawable/silvertablesunderbar"
android:id="#+id/underbarbutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:textSize="20dp"
android:gravity="left|center_vertical" >
</Button>
</RelativeLayout>
So yeah, that was easier than I thought.
Bill Gary suggested using a margin in dp which would keep the same proportions on different screen sizes, but after a lot of experimenting, things end up looking weird for me on different screens again.
I'll do some more experimenting before I'll get back to that, because this whole dip-margins thing is weirding me out... Things that should be displayed properly are not, and things that just shouldn't, do, on some screen resolutions.
I'll start doing my homework on that stuff, but for now, albeit it being a bit long, the code I posted above works flawlessly for me.
try this, you may have to adjust the dp android:layout_marginTop="150dp"
<Button
android:text="#string/topbutton"
android:background="#drawable/silvertablesbuttonbgkort"
android:id="#+id/topbutton"
android:layout_alignParentRight="true"
android:layout_marginTop="150dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center|center_vertical">
</Button>

LinearLayout layout_weight

I have a ListView that is populated with rows. These rows come from an XML file that looks like:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="8dip"
android:weightSum="100">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/columnA"
android:layout_weight="30"
android:layout_gravity="center"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/columnB"
android:layout_weight="30"
android:layout_gravity="center"/>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:src="#drawable/icon"
android:id="#+id/columnC"
android:layout_weight="10"
android:layout_gravity="center"
>
</ImageView>
<CheckBox
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/columnD"
android:layout_weight="30"
android:layout_gravity="center"/>
The problem is that I want the columnC item and columnB item to be very close together, so that my rows are equally spaced out in three parts, i.e.: columnA, (columnB+columnC) and then columnD. I tried to achieve this by using layout_weight as you can see, however the above code seems to have the opposite effect. columnA and columnB are very squished on the left, column C seems to be floating in a large space on its own, and then columnD is located close to columnC, with too much space on its right. What am I doing wrong? :s
Give this a try. You can basically figure how much space each view will "want" by weight/total weight. You don't have to try to make them equal 100.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="8dip"
android:weightSum="100">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/columnA"
android:layout_weight="2"
android:layout_gravity="center"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/columnB"
android:layout_weight="1"
android:layout_gravity="center"/>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:src="#drawable/icon"
android:id="#+id/columnC"
android:layout_weight="1"
android:layout_gravity="center"
>
</ImageView>
<CheckBox
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/columnD"
android:layout_weight="2"
android:layout_gravity="center"/>
I would think that you want columnC and columnB to have the same weight, but you have it set with them different.
Try
columnA:weight=2
columnB:weight=1
columnC:weight=1
columnD:weight=2
I have very limited experience with the weight attribute but I think this is how you can get your desired result.
If you're still having trouble with it, may help us help you if you can post a screen shot of how it looks, and how you want it to look.

Android Linear Layout Problem

Hi all,
I'm a newbie to Android and having problems displaying items using a Linear layout and wondering if anyone can help me. The item list and pictures display fine but the textbox and search button are displayed for each item rather than getting displayed once. My code looks like:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon"/>
<TextView
android:id="#+id/selection"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/searchHeader"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/searchItem"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/search"
android:onClick="Search"/>
</LinearLayout>
</LinearLayout>
With this XML you should only see exactly one imageview and not a list of images as you discribed. Do you alter this layout programmatically in order do display various images? Maybe there you also add again the items you only want once.
Try moving the textview and search button outside of the linearlayouts they are in.

Categories

Resources