Center Controls in Android View..HowTo? - android

i have a Fragment with following Layout:
<?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="?android:attr/listPreferredItemHeight"
android:orientation="horizontal"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:background="#ffffff"
android:id="#+id/dateItemRow"
>
<TextView
android:id="#+id/timeField"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:textStyle="bold"
android:layout_marginTop="3dp"
android:paddingBottom="0dp"
android:textColor="#5d6265"
android:textSize="16sp" />
<LinearLayout
android:id="#+id/rowBackground"
android:layout_width="3dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#33b5e6"
>
<TextView
android:id="#+id/seperator"
android:layout_width="2dp"
android:layout_height="fill_parent"
android:textColor="#000000"
android:text=""
/>
</LinearLayout>
<CheckBox
android:id="#+id/mycheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:button="#drawable/custom_checkbox_design"
/>
</LinearLayout>
How you (hopefully) see this is a Layout where on the Left site is a Time Field then kind a Border to separate the TimeField from the CheckBox. What i'm looking for is to set all of them to Center.
Actual everything is on the Left Site.
All this is in a ListView what i have described like this:
<?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:background="#e8e8e8" >
android:orientation="vertical" >
<ListView
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#e8e8e8" >
</ListView>
</LinearLayout>
I really have NO Idea how i can get this. THX for your HELP!!

Try adding
android:gravity="center"
to your outer LinearLayout.

Related

Set 1 textview and 2 buttons independently in a Relativelayout

I have this XML:
<?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">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/upperBar"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/darker_gray"
>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/txtForum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="25dp"
android:textColor="#255094"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_alignParentLeft="true"
android:maxLines="2"
android:scrollbars="vertical"
>
</TextView>
<ImageButton
android:id="#+id/btnBrowser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_alignParentRight="true"
android:src="#drawable/icon_browser"
android:contentDescription="Open Browser"
>
</ImageButton>
<Button
android:id="#+id/btnNewThread"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NewThread"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_toLeftOf="#+id/btnBrowser">
</Button>
</RelativeLayout>
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="#00000000"
android:padding="10dp"
android:layout_below="#+id/upperBar">
</ListView>
</LinearLayout>
And looks like this:
The problem is that, when the TextView is too long, it overlaps the buttons. I'd like to have those elements independently that the text is long. What should I add to my code?
Thank you.
Add following code android:layout_toLeftOf="#+id/btnBrowser" for the text view.

Align a layout to the top of another layout in order to make a status bar

I have a layout like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#e4e8ed"
android:gravity="top" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/app"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="0px"
android:orientation="vertical"
android:padding="0px" >
<include
android:id="#+id/tabBar"
layout="#layout/tab" />
<Button
android:id="#+id/nist"
android:layout_width="match_parent"
android:layout_height="67dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="3dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:layout_marginTop="3dp"
android:background="#drawable/ready"
android:textColor="#FFFFFF" />
<ListView
android:id="#+id/lastCases"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2.08"
android:longClickable="true" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="7dp"
android:layout_below="#id/app"
android:background="#drawable/dropshadow_custom" >
<TextView
android:id="#+id/error"
style="#style/AudioFileInfoOverlayText"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="120dp"
android:gravity="center"
android:text="No Cases"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#D6D8D9"
android:textSize="40dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/app"
android:background="#000000"
android:gravity="bottom"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
I try to align the last LinearLayout on top of the layout which holds the ListView by using this tag: android:layout_alignTop="#+id/app" But it doesn't seem to work. How can accomplish this?
Many Thanks!
Without any content a Layout with layout_width="wrap_content" and layout_height="wrap_content" is not shown as its width and height have a value of 0! Even if you have set some background color nothing will appear! You either have to add some content to the layout or you have to define another layout dimension. The position of your layout with android:layout_alignTop="#+id/app" should be correct. Hope that helps.
You can set "layout_below" on the first layout targeting to the last layout.
The code is that, I put a button inside the top layout as an exemple (Obs: In your listview you should use 0dp in layout_weight cause you already use layout_weight ^^) :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#e4e8ed"
android:gravity="top" >
<LinearLayout
android:id="#+id/app"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="0px"
android:orientation="vertical"
android:layout_below="#+id/top"
android:padding="0px" >
<Button
android:id="#+id/nist"
android:layout_width="match_parent"
android:layout_height="67dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="3dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:layout_marginTop="3dp"
android:textColor="#FFFFFF" />
<ListView
android:id="#+id/lastCases"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2.08"
android:longClickable="true" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="7dp"
android:layout_below="#+id/app" >
<TextView
android:id="#+id/error"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="120dp"
android:gravity="center"
android:text="No Cases"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#D6D8D9"
android:textSize="40dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/top"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:background="#000000"
android:gravity="bottom"
android:orientation="vertical" >
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Top"
/>

Android listview inside relative layout

Have somebody implemented ExpandableListView inside layout? I'm trying to do it and I have problems with height. I want to make it "wrap_content" in order to see fully expanded View when it is expanded. But unfortunately with "wrap_content" parameter I cannot achieve this result. Can somebody help?
<?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="fill_parent" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/user"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentTop="true"
android:layout_below="#+id/rl"
android:layout_toRightOf="#+id/user"
android:paddingLeft="3dp"
android:text="Top User"
android:textColor="#000000"
android:textSize="22dp" />
<ExpandableListView
android:id="#+id/elvMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/user"
android:layout_above="#+id/about" >
</ExpandableListView>
<TextView
android:id="#+id/about"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:text="Bottom About"
android:textColor="#6f6f6f"
android:textSize="24dp"
android:textStyle="bold" />
</RelativeLayout>
</ScrollView>
</RelativeLayout>
Group row in ExpandableListView:
<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="#+id/groupname"
xmlns:android="http://schemas.android.com/apk/res/android"
android:textStyle="bold"
android:textSize="20dp"
android:paddingLeft="40dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:background="#drawable/expanded_upper_selector"
android:gravity="center_vertical"/>
Child Row:
<?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="wrap_content"
android:background="#drawable/ingredients_selector">
<TextView android:id="#+id/ingredients_second_layer_text"
android:textStyle="bold"
android:textSize="20dp"
android:paddingLeft="50dp"
android:paddingRight="50dp"
android:textColor="#color/dark_orange"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"/>
Check out this link - http://about-android.blogspot.in/2010/04/steps-to-implement-expandablelistview.html
Here only one custom text is added.in similar way you can add second one.Try this and if any query you can feel free to ask.Hope this will help you.
Try your XML edited some-much:
Your_XYZ_Main_XML::
<?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" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#000000" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00ff00" >
<TextView
android:id="#+id/user"
android:layout_width="match_parent"
android:layout_height="100dp"
android:paddingLeft="3dp"
android:text="Top User"
android:textColor="#111111"
android:background="#eeeeee"
android:textSize="22dp" />
<ExpandableListView
android:id="#+id/elvMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ExpandableListView>
<TextView
android:id="#+id/about"
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="Bottom About"
android:textColor="#6f6f6f"
android:textSize="24dp"
android:textStyle="bold" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
Group_Row_XML ::
<?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:orientation="vertical" >
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/groupname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#159357" // YOUR_BACKGROUND
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingLeft="40dp"
android:text="Parent text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="20dp"
android:textStyle="bold" />
</RelativeLayout>
Child_Row_XML ::
<?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:orientation="vertical" >
<TextView android:id="#+id/ingredients_second_layer_text"
android:textStyle="bold"
android:textSize="20dp"
android:paddingLeft="50dp"
android:paddingRight="50dp"
android:textColor="#ff2211" // YOUR_COLOR
android:text="Child Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"/>
</RelativeLayout>

Android LinearLayouts conflicting with each other

I have two LinearLayout
One contains a EditText with a patch9 image as a speech bubble and another with a TextView and ImageView
When I try typing into the EditText is begins to expand towards the right hand side then starts to reduce the width of the second LinearLayout (crushing the image)
XML is
<?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="fill_parent"
android:background="#color/white"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:orientation="vertical" >
<EditText
android:id="#+id/summaryMessage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/greeting_bubble_white"
android:gravity="center" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:orientation="vertical" >
<ImageView
android:id="#+id/summaryImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/picture_frame" />
<TextView
android:id="#+id/summaryText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingLeft="10dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Looks like this
Any ideas?
its expanding since you have specified that layout_width of your LinearLayout should be "wrap_content", you should specify a fixed dimension with dps so it doesn't expand beyond.
Alternative use the weight tag
try out this modification:
<?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="fill_parent"
android:background="#color/white"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:layout_weight="0.7">
<EditText
android:id="#+id/summaryMessage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/greeting_bubble_white"
android:gravity="center" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:gravity="right"
android:orientation="vertical" >
<ImageView
android:id="#+id/summaryImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/picture_frame" />
<TextView
android:id="#+id/summaryText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingLeft="10dp" />
</LinearLayout>
however I would suggest more that you use this one:
<?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:background="#color/white"
android:gravity="center"
android:orientation="horizontal" >
<EditText
android:id="#+id/summaryMessage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.7"
android:background="#drawable/greeting_bubble_white"
android:gravity="center" />
<TextView
android:id="#+id/summaryText"
android:layout_weight="0.3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingLeft="10dp" />
</LinearLayout>
and that you use compound drawables on your summaryText here is a link that should help you: Using Compund Drawables
Use a relative layout, the layout editor is in fact pretty good so use it to position your views

Can we place two textviews in footer xml file in android?

I am displaying a header and footer for a listview. Here I am displaying all the data dynamically. Now I want to keep a title to my footer...So I tried to keep another textview in my footer.xml file. But it is not accepting. How can I place a title for my footer data?
My Code:
<?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" >
<TextView
android:id="#+id/exptext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="#000000"
android:textSize="12dip" >
</TextView>
</LinearLayout>
Help me regarding this...Thanks in Advance
Try this
using two layout one is using the textview and other is using the listview
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="welcome"
android:textColor="#ffffff"
android:layout_gravity="center_horizontal" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="welcome"
android:textColor="#ffffff"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</LinearLayout>
Another XML:
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>
Do I get you right? You want to place a TextView above and one below a ListView?
Than you have to do it like this:
<?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" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="TextView" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="TextView" />
The Important part ist the layout_weight attributes.
Try this
you can us two xml one is set the two textview and other xml used the listview
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="welcome"
android:textColor="#ffffff"
android:layout_gravity="center_horizontal" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="welcome"
android:textColor="#ffffff"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</LinearLayout>
Another XML:
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>

Categories

Resources