Android: ActionBar layout not centering - android

In my action bar, I want the icon and title text to be centered horizontally. Here's what I'm getting:
The action bar is using a RelativeLayout. The icon image and title text are in a LinearLayout which has android:layout_centerHorizontal="true", but the linear layout is not centering. Here's the layout file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dip"
android:background="#dddddd" >
<ImageButton
android:id="#+id/leftButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:background="#null"
android:contentDescription=""
android:scaleType="centerInside"
android:src="#drawable/ic_drawer" />
<LinearLayout
android:id="#+id/titleParentLayout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerHorizontal="true" >
<ImageView
android:id="#+id/titleImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:scaleType="centerInside"
android:src="#drawable/icon_orig" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:singleLine="true"
android:text="#string/app_name"
android:textColor="#484848"
android:textSize="18dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/rightSideParentLayout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:background="#dddddd"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/rightButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:background="#null"
android:contentDescription=""
android:scaleType="centerInside"
android:src="#drawable/addbutton" />
</LinearLayout>
</RelativeLayout>
EDIT: Both answers give me this:
As you can see, the icon and title are still not centered.

You can try using LinearLayout as follows...
<?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="50dip"
android:background="#dddddd"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/leftButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:background="#null"
android:contentDescription=""
android:scaleType="centerInside"
android:src="#drawable/ic_drawer" />
<LinearLayout
android:id="#+id/titleParentLayout"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center" >
<ImageView
android:id="#+id/titleImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:scaleType="centerInside"
android:src="#drawable/ic_orig" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:singleLine="true"
android:text="#string/app_name"
android:textColor="#484848"
android:textSize="18dp" />
</LinearLayout>
<ImageButton
android:id="#+id/rightButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:background="#null"
android:contentDescription=""
android:scaleType="centerInside"
android:src="#drawable/addbutton" />
</LinearLayout>

in java code use this for android-version 3.0 to upper
getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getActionBar().setCustomView(R.layout.custom_actionbar);
xml file custom_actionbar
<?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="wrap_content"
android:orientation="horizontal"
android:background="#dddddd">
<ImageButton
android:id="#+id/leftButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dip"
android:background="#null"
android:contentDescription=""
android:scaleType="centerInside"
android:src="#drawable/ic_drawer" />
<LinearLayout
android:id="#+id/titleParentLayout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center" >
<ImageView
android:id="#+id/titleImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:scaleType="centerInside"
android:src="#drawable/icon_orig" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:singleLine="true"
android:text="#string/app_name"
android:textColor="#484848"
android:textSize="18sp" />
</LinearLayout>
<ImageButton
android:id="#+id/rightButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="10dip"
android:gravity="center_vertical"
android:background="#null"
android:contentDescription=""
android:scaleType="centerInside"
android:src="#drawable/addbutton" />
</LinearLayout>
EDIT : Screenshot from Samsung tab2

Related

How to give only bottom border to RelativeLayout in Android

I have created a relative layout in which it requires only bottom border. How can I do that? Below is the RelativeLayout that I have used.
I have added view inside relative layout but it shows at bottom of screen
<?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="#EAEAEA"
android:orientation="vertical" >
<TextView
android:id="#+id/auth_lbl_register_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#EAEAEA"
android:clickable="true"
android:ellipsize="none"
android:fontFamily="FNB Sans"
android:gravity="left"
android:maxLines="50"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:scrollHorizontally="false"
android:text="#string/auth_into"
android:textColor="#color/labelColor"
android:textSize="20sp" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#drawable/border"
android:orientation="horizontal" >
<TextView
android:id="#+id/cellnum_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:fontFamily="FNB Sans"
android:text="#string/cell_num_label"
android:textColor="#color/labelColor"
android:textSize="15sp" />
<EditText
android:id="#+id/auth_txt_msisdn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="25dp"
android:layout_toRightOf="#+id/cellnum_text"
android:ellipsize="start"
android:ems="10"
android:gravity="center_horizontal"
android:hint="Enter cell number"
android:inputType="number"
android:singleLine="true"
android:textSize="15sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="5sp"
android:paddingRight="5sp" >
<CheckBox
android:id="#+id/auth_cbx_agree_terms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:button="#drawable/check_box"
android:clickable="true"
android:ellipsize="none"
android:fontFamily="FNB Sans"
android:gravity="left"
android:maxLines="50"
android:paddingLeft="10dp"
android:paddingStart="30dp"
android:scrollHorizontally="false"
android:text="#string/auth_agree_terms"
android:textColor="#color/labelColor"
android:textSize="20sp" />
</RelativeLayout>
<!-- next -->
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/auth_cmd_validate"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_weight="0.9"
android:background="#F49B00"
android:gravity="center"
android:paddingLeft="0dp"
android:text="Next"
android:textColor="#color/labelColor"
android:textSize="20dp"
android:textStyle="normal" />
<ImageView
android:layout_width="40dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:src="#drawable/next_arrow_white" />
</RelativeLayout>
<!-- end of next -->
<TextView
android:id="#+id/or_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:background="#EAEAEA"
android:clickable="false"
android:fontFamily="FNB Sans"
android:gravity="center"
android:padding="10sp"
android:text="OR"
android:textColor="#color/labelColor"
android:textSize="20sp" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/auth_cmd_register"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_weight="0.9"
android:background="#color/white"
android:gravity="center"
android:paddingLeft="0dp"
android:text="Register Online"
android:textColor="#color/labelColor"
android:textSize="20dp"
android:textStyle="normal" />
<ImageView
android:id="#+id/imgArrowRight"
android:layout_width="40dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:gravity="center"
android:src="#drawable/line_arrow" />
<View
android:id="#+id/view"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_alignParentBottom="true"
android:background="#color/black" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/auth_cmd_terms"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_weight="0.9"
android:background="#ffffff"
android:gravity="center"
android:paddingLeft="0dp"
android:text="Terms and Condition"
android:textColor="#color/labelColor"
android:textSize="20dp"
android:textStyle="normal" />
<ImageView
android:id="#+id/imgArrowRight1"
android:layout_width="40dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:gravity="center"
android:src="#drawable/line_arrow" />
</RelativeLayout>
You need to specify android:layout_height="N" where N is your height in dp when using android:layout_alignParentBottom="true" :
<?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
android:id="#+id/relativeLayoutOne"
android:layout_width="fill_parent"
android:layout_height="200dp">
<!-- Bottom Border shows -->
<View android:layout_alignParentBottom="true"
android:layout_height="1dp"
android:background="#F00"
android:layout_width="fill_parent"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeLayoutTwo"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:text="Text View 1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeLayoutThree"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:text="Text View 2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
</LinearLayout>
You can use View to give bottom border to your RelativeLayout.
Add this in your RelativeLayout.
<View
android:id="#+id/view"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_alignParentBottom="true"
android:background="#color/black" />
I hope it helps!
You can add a new ImageView to your RelativeLayout, set the height to 1 px, align it to the bottom, then this ImageView can be 'considered' as the border.
pleas see following xml:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="55dp" >
<TextView
android:id="#+id/txtCategoryName"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_weight="0.9"
android:background="#ffffff"
android:gravity="center"
android:paddingLeft="0dp"
android:text="Register"
android:textColor="#color/labelColor"
android:textSize="20dp"
android:textStyle="normal" />
<ImageView
android:id="#+id/imgArrow"
android:layout_width="40dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:gravity="center"
android:src="#drawable/line_arrow" />
<ImageView
android:id="#+id/imgBorder"
android:layout_width="fill_parent"
android:layout_height="1px"
android:layout_alignParentBottom="true"
android:background="#ff0000"/>
</RelativeLayout>
#inner_class7 is correct, you need to give a value as the height of the RelativeLayout, or, it will show on the bottom of the screen.

how to align one image button and one textview in leftside

I want to align one image button and one TextView with linear layout right side side one button like. But, I am try to align but not work and below xml layout code:I have tried the code given bellow.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp" >
<TextView
android:id="#+id/ph_txt"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:singleLine="true"
android:text="#string/phone_text"
android:textSize="20sp"
/>
<ImageButton
android:layout_width="20dp"
android:layout_height="match_parent"
android:layout_weight="0.3"
android:background="#null"
android:gravity="center_vertical"
android:scaleType="centerInside"
android:src="#drawable/phoneicon" />
<TextView
android:id="#+id/welcome_txt"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_weight="1.25"
android:background="#null"
android:gravity="center_vertical"
android:text="#string/welcome_text"
android:textColor="#color/welcome_txt_color"
android:textSize="35sp"
/>
</LinearLayout>
Give orientation to linearlayout and keep it horizontal
Try this way
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp" >
<TextView
android:id="#+id/ph_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:layout_alignParentLeft="true"
android:singleLine="true"
android:text="#string/phone_text"
android:textSize="20sp"
/>
<ImageButton
android:layout_width="20dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:background="#null"
android:gravity="center_vertical"
android:layout_below="#+id/ph_txt"
android:scaleType="centerInside"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/welcome_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1.25"
android:layout_alignParentRight="true"
android:background="#null"
android:gravity="center_vertical"
android:text="#string/welcome_text"
android:textColor="#color/welcome_txt_color"
android:textSize="35sp"
/>
</RelativeLayout>
Try this
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="left|center_vertical" >
<ImageView
android:id="#+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:gravity="center_vertical"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
</LinearLayout>
try this its works fine :)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:gravity="center">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:gravity="center" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:gravity="center" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
</LinearLayout>

Android Design 2 Images and text LinearLayout

I'm trying to create a layout like this:
Basically I have 2 Images that I would like to stick to either side of the LinearLayout and in the center have some text. I'm setting the width of the images in code based on the density of the screen, so that is of variable width.
Here is the code I have so far:
<RelativeLayout
android:id="#+id/user_header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/user_loading_wrapper"
android:layout_alignWithParentIfMissing="true"
android:layout_alignParentTop="true"
android:layout_margin="#dimen/default_spacing">
<!--User Poster-->
<ImageView
android:id="#+id/user_poster"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="fitXY"
android:contentDescription="User Poster"
android:layout_alignParentLeft="true"/>
<!--Review Details-->
<LinearLayout
android:id="#+id/screen_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/user_poster"
android:layout_alignParentTop="true"
android:gravity="center"
android:layout_centerInParent="true"
android:layout_marginLeft="#dimen/default_spacing_max"
android:orientation="vertical">
<!--User Title-->
<TextView
android:id="#+id/user_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#333"
android:singleLine="false"
android:layout_gravity="center"/>
<!--Location-->
<TextView
android:id="#+id/user_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#333"
android:singleLine="false"
android:layout_gravity="center"/>
<!--User 2 Title-->
<TextView
android:id="#+id/user_secondary_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#333"
android:layout_marginTop="4dp"
android:singleLine="false"
android:layout_gravity="center"
tools:text="Life of Pi"/>
</LinearLayout>
<!--Poster-->
<ImageView
android:id="#+id/user_poster_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitEnd"
android:layout_toRightOf="#id/screen_details"
android:contentDescription="Poster"
android:layout_alignParentRight="true"/>
</RelativeLayout>
But it doesn't work at all. I want the Center Layout to expand and occupy all the space in the middle between the two images.
i fixed it .... check output it in image and flow in code.
And Code is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/user_header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true" >
<ImageView
android:id="#+id/user_poster"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="#android:color/black"
android:scaleType="fitXY" />
<LinearLayout
android:id="#+id/screen_details"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/user_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#android:color/darker_gray"
android:paddingLeft="10dp"
android:singleLine="false"
android:text="Life of Pi"
android:textColor="#333" />
<TextView
android:id="#+id/user_location"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="4dp"
android:background="#android:color/darker_gray"
android:paddingLeft="10dp"
android:singleLine="false"
android:text="Life of Pi"
android:textColor="#333" />
<TextView
android:id="#+id/user_secondary_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="4dp"
android:background="#android:color/darker_gray"
android:paddingLeft="10dp"
android:singleLine="false"
android:text="Life of Pi"
android:textColor="#333" />
</LinearLayout>
<ImageView
android:id="#+id/user_poster2"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_weight="0"
android:background="#android:color/black"
android:scaleType="fitXY" />
</LinearLayout>
try this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/user_header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignWithParentIfMissing="true"
android:layout_alignParentTop="true"
android:layout_margin="5dp">
<!--User Poster-->
<ImageView
android:id="#+id/user_poster"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="fitXY"
android:contentDescription="User Poster"
android:layout_alignParentLeft="true"/>
<!--Review Details-->
<LinearLayout
android:id="#+id/screen_details"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="70dp"
android:layout_toRightOf="#id/user_poster"
android:gravity="center"
android:orientation="vertical" >
<!--User Title-->
<TextView
android:id="#+id/user_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#333"
android:singleLine="false"
android:layout_gravity="center"/>
<!--Location-->
<TextView
android:id="#+id/user_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#333"
android:singleLine="false"
android:layout_gravity="center"/>
<!--User 2 Title-->
<TextView
android:id="#+id/user_secondary_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#333"
android:layout_marginTop="4dp"
android:singleLine="false"
android:layout_gravity="center"
android:text="Life of Pi"/>
</LinearLayout>
<!--Poster-->
<ImageView
android:id="#+id/user_poster_2"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:contentDescription="Poster" />
</RelativeLayout>

How to align Buttons under a ListView?

I'm trying to align a number of buttons under a list of items.
It's appearing as follows:
But I need it to appear as follows:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="7dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/myImage" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="[TextView text]"
android:textSize="20sp"
android:layout_gravity="center" />
</LinearLayout>
<TextView
android:id="#+id/lblOverviewText"
android:textStyle="bold"
android:textSize="15sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/linearLayout1" />
<TextView
android:id="#+id/lblEngineer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/lblOverviewText" />
<TextView
android:id="#+id/lblAlarmEvents"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/lblEngineer" />
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/lblAlarmEvents" />
<LinearLayout
android:id="#+id/linearLayoutSetUnsetButtons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/list">
<ImageButton
android:id="#+id/btnFullSet"
android:background="#null"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="Full Set"
android:layout_margin="5dp"
android:src="#drawable/lock_128"
android:scaleType="fitCenter" />
<ImageButton
android:id="#+id/btnPartsetA"
android:background="#null"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="Part A"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:src="#drawable/PartSetA_128"
android:scaleType="fitCenter" />
<ImageButton
android:id="#+id/btnPartsetB"
android:background="#null"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="Part B"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:src="#drawable/PartSetB_128"
android:scaleType="fitCenter" />
<ImageButton
android:id="#+id/btnUnset"
android:background="#null"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="Unset"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:src="#drawable/Unset_128"
android:scaleType="fitCenter"
android:visibility="gone" />
</LinearLayout>
</RelativeLayout>
Try this way
Make your listview wrap_content
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/lblAlarmEvents" />
<LinearLayout
android:id="#+id/linearLayoutSetUnsetButtons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_alignParentBottom="true"
android:layout_below="#+id/list">
XML file will be like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="7dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/myImage" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="[TextView text]"
android:textSize="20sp"
android:layout_gravity="center" />
</LinearLayout>
<TextView
android:id="#+id/lblOverviewText"
android:textStyle="bold"
android:textSize="15sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/linearLayout1" />
<TextView
android:id="#+id/lblEngineer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/lblOverviewText" />
<TextView
android:id="#+id/lblAlarmEvents"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/lblEngineer" />
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/lblAlarmEvents" />
<LinearLayout
android:id="#+id/linearLayoutSetUnsetButtons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/list">
<ImageButton
android:id="#+id/btnFullSet"
android:background="#null"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="Full Set"
android:layout_margin="5dp"
android:src="#drawable/lock_128"
android:scaleType="fitCenter" />
<ImageButton
android:id="#+id/btnPartsetA"
android:background="#null"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="Part A"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:src="#drawable/PartSetA_128"
android:scaleType="fitCenter" />
<ImageButton
android:id="#+id/btnPartsetB"
android:background="#null"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="Part B"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:src="#drawable/PartSetB_128"
android:scaleType="fitCenter" />
<ImageButton
android:id="#+id/btnUnset"
android:background="#null"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="Unset"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:src="#drawable/Unset_128"
android:scaleType="fitCenter"
android:visibility="gone" />
</LinearLayout>
Or If you want your image button's parent layout as a footer set the xml like this:
XML file will be like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="7dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/myImage" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="[TextView text]"
android:textSize="20sp"
android:layout_gravity="center" />
</LinearLayout>
<TextView
android:id="#+id/lblOverviewText"
android:textStyle="bold"
android:textSize="15sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/linearLayout1" />
<TextView
android:id="#+id/lblEngineer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/lblOverviewText" />
<TextView
android:id="#+id/lblAlarmEvents"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/lblEngineer" />
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/lblAlarmEvents" />
<LinearLayout
android:id="#+id/linearLayoutSetUnsetButtons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true">
<ImageButton
android:id="#+id/btnFullSet"
android:background="#null"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="Full Set"
android:layout_margin="5dp"
android:src="#drawable/lock_128"
android:scaleType="fitCenter" />
<ImageButton
android:id="#+id/btnPartsetA"
android:background="#null"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="Part A"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:src="#drawable/PartSetA_128"
android:scaleType="fitCenter" />
<ImageButton
android:id="#+id/btnPartsetB"
android:background="#null"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="Part B"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:src="#drawable/PartSetB_128"
android:scaleType="fitCenter" />
<ImageButton
android:id="#+id/btnUnset"
android:background="#null"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="Unset"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:src="#drawable/Unset_128"
android:scaleType="fitCenter"
android:visibility="gone" />
</LinearLayout>

Listview item fits to row

I have a listview with two textviews in a row like name and result. I want to fit the textview to the row because I need to set a background color to the second textview.I have tried "match_parent"fill_parent"..etc
//items.xml
<ImageView
android:id="#+id/imgViewLogo"
android:layout_width="22dip"
android:layout_height="22dip"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:scaleType="center"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/txtViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/txtViewDescription"
android:layout_alignBottom="#+id/txtViewDescription"
android:layout_marginLeft="6dp"
android:layout_toRightOf="#+id/imgViewLogo"
android:text="TextView"
android:textStyle="bold" />
<MyTextView
android:id="#+id/txtViewDescription"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/imgViewLogo"
android:layout_marginRight="33dp"
android:layout_gravity="center"
android:text="TextView"
android:textStyle="bold" />
<ImageView
android:id="#+id/temperrr"
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/resultarrawnocolor" />
</RelativeLayout>
// listview.xml
<?xml version="1.0" encoding="utf-8"?>
<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" >
<Button
android:id="#+id/gl1"
style="?android:attr/buttonStyleSmall"
android:layout_width="64dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#drawable/back"
android:gravity="center_vertical"
android:onClick="backMan" />
<TextView
android:id="#+id/textVie3"
android:layout_width="match_parent"
android:layout_height="4dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/gl1"
android:background="#drawable/header_line" />
<Button
android:id="#+id/gl2"
style="?android:attr/buttonStyleSmall"
android:layout_width="64dp"
android:layout_height="40dp"
android:layout_above="#+id/textVie3"
android:layout_alignParentRight="true"
android:background="#drawable/info"
android:gravity="center_vertical"
android:onClick="info" />
<TextView
android:id="#+id/textVie1"
android:layout_width="135dp"
android:layout_height="30dp"
android:layout_alignBaseline="#+id/gl2"
android:layout_alignBottom="#+id/gl2"
android:layout_centerHorizontal="true"
android:background="#drawable/uchek_header"
android:gravity="center" />
<TextView
android:id="#+id/bgnf1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textVie3"
android:background="#drawable/main_bag" />
<TextView
android:id="#+id/myTextView1"
android:layout_width="375dp"
android:layout_height="35dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/bgnf1"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:background="#drawable/textviewtableheader"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="Result"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold" />
<Button
android:id="#+id/btnemail"
android:layout_width="60dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#drawable/email"/>
<Button
android:id="#+id/btnsave"
android:layout_width="60dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#drawable/save"
/>
<Button
android:id="#+id/btnrec"
android:layout_width="60dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/myTextView1"
android:background="#drawable/rec"
/>
<ListView
android:id="#+id/lv1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/btnemail"
android:layout_alignLeft="#+id/myTextView1"
android:layout_alignRight="#+id/myTextView1"
android:layout_below="#+id/myTextView1"
android:background="#color/White"
android:divider="#drawable/tabledivider"
android:dividerHeight="1dp"
android:listSelector="#drawable/listcolor"
tools:listitem="#android:layout/simple_list_item_1" >
</ListView>
</RelativeLayout>
If you want the height to be of icon size you can use this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:weightSum="1" >
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight=".5"
android:background="#F00"
android:gravity="center_vertical"
android:text="Envelope" />
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight=".5"
android:background="#990"
android:gravity="center_vertical"
android:text="1000ml/Dl" />
</LinearLayout>
</LinearLayout>
Try distributing the weight of ImageView , TextView , TextView and ImageView to get the desired result.
<?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="match_parent" >
<ImageView
android:id="#+id/imgViewLogo"
android:layout_width="22dip"
android:layout_height="22dip"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:scaleType="center"
android:src="#drawable/ic_launcher" />
<LinearLayout
android:id="#+id/linear_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/temperrr"
android:layout_toRightOf="#+id/imgViewLogo"
android:orientation="horizontal"
android:weightSum="1.0" >
<TextView
android:id="#+id/txtViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/txtViewDescription"
android:layout_alignBottom="#+id/txtViewDescription"
android:layout_toRightOf="#+id/imgViewLogo"
android:layout_weight="0.5"
android:background="#ff0000"
android:text="TextView"
android:textStyle="bold" />
<TextView
android:id="#+id/txtViewDescription"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/imgViewLogo"
android:layout_gravity="center"
android:layout_weight="0.5"
android:background="#ffff00"
android:text="TextView"
android:textStyle="bold" />
</LinearLayout>
<ImageView
android:id="#+id/temperrr"
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
may be this will help, i edited the answer,check now

Categories

Resources