So, I'm currently in the process of creating my first Android app. It's been a difficult battle, but I'm somehow hanging in there. Stuck on the design now though.
<?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" >
<TextView
android:id="#+id/phid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<ImageView
android:id="#+id/color"
android:contentDescription="paintable circle"
android:layout_width="18dp"
android:layout_height="fill_parent"
android:layout_gravity="left"
android:layout_marginLeft="6dip"
android:layout_marginTop="6dip"
android:src="#drawable/circle" />
<TextView
android:id="#+id/datetime"
android:layout_width="wrap_content"
android:layout_height="60dip"
android:paddingBottom="6dip"
android:paddingLeft="6dip"
android:paddingRight="10dp"
android:paddingTop="10dip"
android:textSize="12sp"
android:textStyle="bold" />
This gives a nice list, with the title in the top left, and the date in the far top right. But I want a textview under the title, same font size as the datetime, and another textview under the datetime with the same font size. I've tried adding another LinearLayout, a RelativeLayout, without any extra Layouts, but it just doesn't seem to work. What am I missing here? Thanks.
You could use a RelativeLayout
place the title on the top left
android:layout_alignParentLeft = "true"
place the date time on the right with
android:layout_alignParentRight = "true"
Then for each of the TextViews you want to place under a component, use
android:layout_below = (give the id of the control )
Cheers
Related
I'm attempting to create a a Heading + button similar to the Google Music App, e.g. where there is a "Songs" Header on the Left and then on the right there is a Button with the text "X more"..
I've using a RelativeLayout for the TextView and Button
My problem is that the button is taking up the size of the layout that contains the text the height is all wrong and the padding doesn't seem to do anything.
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
[REMOVED for clarity]
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/list_foreground"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="#string/photos"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/photo_button"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#color/actionbar_background"
android:padding="10dp"
android:text="test" />
</RelativeLayout>
What am I doing wrong here?
RelativeLayouts are designed to have children in the layout "relative" to each other. In other words, if you want the Button to the right of the Textview, you need to tell it.
Because you are aligning relative to the parent LEFT / RIGHT, it appears that things are "kind of" working.
You may be better off with a LinearLayout, depending on your needs. LinearLayouts use "orientation" not RelativeLayouts.
You should look over some tutorials (like this one: http://mobile.tutsplus.com/tutorials/android/android-layout/) but ultimately you will probably put your button in first and then your text view so that the textview content will wrap appropriately.
To get the same effect as the Music App I ended up using a RelativeLayout but instead of a Button I'm using another TextView, this is giving the impression it's a button but it gives me more scope to format the background etc. I think just setup a OnClickListener in the code
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/photo_title">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:text="#string/photos"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/more_photo_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:background="#color/actionbar_background"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="10 MORE"
android:textColor="#color/button_text"
android:textSize="12sp" />
</RelativeLayout>
I have a custom info window for Google Maps API v2 that uses a RelativeLayout added by overwritting getInfoWindow in my class. This layout has a TextView with a title in the first line and three TextViews in the next line, one aligned to the left, another to the center and the thrid one to the right. It works, but the window takes the whole of the screen, which is very ugly in big screens like tablets.
I have tried to set the width of the RelativeLayout to some fixed value (let's say "50dp") or to "wrap_content", but it always takes the whole of the screen.
This is the XML for the layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/bus_stop_info_window"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffffff" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="A title" >
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/title"
android:gravity="left"
android:text="Test1" >
</TextView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/title"
android:gravity="center"
android:text="Test2" >
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/title"
android:gravity="left"
android:text="Test3" >
</TextView>
</RelativeLayout>
How can I achieve the desired effect?
For that make root element as Linear-layout and put Relative-layout inside it.so that window will not fill the whole screen in tablet.This will not affect whether Relative-layout width is wrap_content or fixed size.
U can try my answer here
https://docs.google.com/document/d/1SDDnAG-jkQjC6F85iPgfv3Et1pYl8t03k-TkCN3YcCw/edit
I have an Android preference page containing a mixture of stock, and custom List Items. The custom list items "Mood" and "Mixed Mood" can be seen in the screen shot in the referenced Google Doc, and function in the following way. (The external link is because, without a reputation of at leat 10, I'm not allowed to post images in stackoverflow...)
1) Before a user has chosen a value, they simply display their title. (either Mood or MixedMood)
2) After a user has chosen a value, they display their title AND a custom tool tip (the little colored squares), indicating their choice.
THE PROBLEM: You can see from the screen shot that before a user has chosen a value, the "Mood" text is not centered. I would like for it to be centered, and then dynamically make room for the custom tool tip after a choice is made. In other words, I would like
<TextView android:id="#+android:id/title"
To respond to
android:gravity="center_vertical".
Is this possible?
My layout file looks like this.
<?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:minHeight="60dip"
android:gravity="center_vertical"
android:paddingRight="?android:attr/scrollbarSize">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_marginLeft="15dip"
android:layout_marginRight="6dip">
<TextView android:id="#+android:id/title"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="18sp"/>
<!-- This image represents the dropdown arrow -->
<ImageView
android:gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="#android:id/title"
android:scaleType="fitEnd"
android:src="#drawable/ic_btn_round_more_normal_cropped" />
<monarca_rct.client.customcomponents.MoodScalePresentation
android:id="#+android:id/hpmp_mood_scale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#android:id/title"
android:paddingTop="4.0dp"/>
</RelativeLayout>
</LinearLayout>
If i get your question right:
You can set android:layout_alignWithParentIfMissing="true" in <TextView android:id="#+android:id/title" for more info visit http://developer.android.com/resources/articles/layout-tricks-efficiency.html
I can't exactly try this out but I think it might be something like this:
<?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:minHeight="60dip"
//REMOVED GRAVITY
android:paddingRight="?android:attr/scrollbarSize">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
//REMOVED GRAVITY
android:layout_marginLeft="15dip"
android:layout_marginRight="6dip">
<TextView android:id="#+android:id/title"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_vertical|left"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="18sp"/>
<!-- This image represents the dropdown arrow -->
<ImageView
android:gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="#android:id/title"
android:scaleType="fitEnd"
android:src="#drawable/ic_btn_round_more_normal_cropped" />
<monarca_rct.client.customcomponents.MoodScalePresentation
android:id="#+android:id/hpmp_mood_scale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#android:id/title"
android:paddingTop="4.0dp"/>
</RelativeLayout>
</LinearLayout>
The reason to go from center_vertical to center_vertical|left is because your parent layout is like a grid and you can "choose" your position. So if you have:
(1) (2) (3)
(4) (5) (6)
(7) (8) (9)
[Don't mind the white spaces between each line, for some reason if I don't do it like that it gets displayed all in one line.]
The 4 is where you want your text. As you can see, the 4 is on the left but also vertically centered hence the combination:
android:gravity="center_vertical|left"
Quick question, why do you need a RelativeLayout inside a LinearLayout? You could do without the LinearLayout altogether from the looks of it.
I'm pretty sure I've done this before, but I've forgotten how.
Here's the problem:
I've got a button and a textview, and I want the textview to be centered, while the button is on the left side.
No problem? Just put them in a relativelayout, make the textview centerinparent, and the button alignparentleft.
But now I'm going to dynamically change the text, so it can potentially be written on top of the button! I'll just add toRightOf="#id/button" on the textview. No, now it's no longer centered.
I wish I could provide a screenshot, but it seems the computer is out of memory and can't do that.
Here's some code: http://pastebin.com/3N70Vjre (Since I can't paste xml...?)
<RelativeLayout
android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
<Button
android:id="#+id/leftbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="text!"
/>
<TextView
android:id="#+id/toptext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:layout_toRightOf="#id/leftbutton"
android:layout_centerInParent="true"
android:textSize="16sp"
android:textStyle="bold"
android:text="Text!"
android:singleLine="true"
/>
</RelativeLayout>
Try this (unfortunately I'm at work so can't jump into Eclipse to get you some code) -
Change the layout_width of the TextView to fill_parent.
Set the gravity of the TextView to center (so the text centers inside the TextView)
Set the layout_weight of the Button to 1 and the layout_weight of the TextView to 2. Note that you may have to fudge with these numbers to get the layout you're looking for.
This should center the text of the TextView after the Button, though it will not center the TextView itself. You can accomplish that by replacing the TextView with a container (Linear/Relative Layout) and doing the same method as above on the Layout instead of the TextView. You would then put your TextView inside the container and set the container's gravity to "center".
Hope this helps point you in the right direction :)
You can try this (pseudo-code):
<RelativeLayout>
<Button>
<LinearLayout toLeftOf="toptext" type="horizontal">
<TextView gravity="center">
</LinearLayout>
</RelativeLayout>
You might have to have the LinearLayout as width="fill_parent". Not sure if that will work nor not. You can subsequently try some of the things listed here: http://thinkandroid.wordpress.com/2010/01/14/how-to-position-views-properly-in-layouts/
Try declaring the TextView first, then aligning the button to the left of the text view. Keep in mind you may run into issues if the TextView becomes too wide.
EDIT: I see, so you're trying to do something sort of like the iPhone's header with back/next buttons (similar anyway). Try this modification. I still believe you're going to run into issues if the TextView gets large enough to hit the Button, though.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
>
<TextView
android:id="#+id/toptext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:layout_alignParentCenter="true"
android:textSize="16sp"
android:textStyle="bold"
android:text="Text!"
android:singleLine="true"
/>
<Button
android:id="#+id/leftbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="text!"
/>
</RelativeLayout>
Try this FrameLayout instead. This may do more what you're expecting:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="#+id/toptext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#android:color/white"
android:textSize="16sp"
android:textStyle="bold"
android:text="Text!"
android:singleLine="true"
/>
<Button
android:id="#+id/leftbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text!"
/>
</FrameLayout>
I have a problem with two Textviews on the same height in a RelativeLayout running into each other.
I use the following Layout.
<?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">
<ImageView
android:id="#+id/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:src="#drawable/icon" />
<TextView
android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="NameNameNameNameNameNameName"
android:layout_alignParentTop="true"
android:layout_toRightOf="#id/logo"
android:gravity="clip_horizontal"
android:lines="1" />
<TextView
android:id="#+id/information"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Distance"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
<TextView
android:id="#+id/nrcoupons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Number"
android:layout_alignRight="#id/information"
android:layout_alignBottom="#id/logo" />
<TextView
android:id="#+id/subcategory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Subcategory"
android:layout_alignLeft="#id/name"
android:layout_alignBottom="#id/logo" />
</RelativeLayout>
This gives me this view:
alt text http://janusz.de/~janusz/view.png
Everything is as I need it except the two textviews name and information are displayed on the same screen space with the one on top of the other.
How can I avoid this?
For your #+id/name TextView, add android:layout_toLeftOf="..." for whatever TextView is on the right. The screenshot and the XML do not seem to line up (screenshot appears to have "Distance" in the overwritten TextView, but the XML does not), so I'm not completely certain which widget this is.
If you are targeting Android 1.5, you will need to order the widgets in the XML such that the widgets are defined before they are referenced from android:layout_toLeftOf or android:layout_toRightOf. If you are targeting Android 1.6 and newer only, you can have them be in any order, but the first occurrence of any distinct ID must have the + sign, even if that first occurrence is in an android:layout_toLeftOf attribute instead of an android:id attribute.
Your namenamename textview is set with width = fill_parent so you can't put anything to its right ;)