I have to create a horizontally aligned textview and an imageview that is placed below of another image view. The problem is they are not getting horizontally aligned. What should i be doing to avoid this problem?..
Whats happening right now:
It should be like this:
My Code :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="5dp"
>
<ImageButton
android:id="#+id/imgBtnItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:background="#android:color/transparent">
</ImageButton>
<TextView
android:id="#+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="9sp"
android:layout_below="#+id/imgBtnItem">
</TextView>
<ImageButton
android:id="#+id/imgBtnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imgBtnItem"
android:layout_toRightOf="#+id/txtName"
android:background="#android:color/transparent"
>
</ImageButton>
</RelativeLayout>
Use as (edit dimens according to use):
<?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:gravity="center"
android:padding="5dp"
>
<ImageButton
android:id="#+id/imgBtnItem"
android:layout_width="150dp"
android:layout_height="150dp"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:background="#color/colorAccent">
</ImageButton>
<TextView
android:id="#+id/txtName"
android:layout_width="50dp"
android:layout_height="50dp"
android:textStyle="bold"
android:textSize="9sp"
android:layout_marginTop="4dp"
android:layout_below="#+id/imgBtnItem"
android:background="#color/colorPrimary">
</TextView>
<ImageButton
android:id="#+id/imgBtnAdd"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/colorPrimaryDark"
android:layout_below="#+id/imgBtnItem"
android:layout_alignTop="#id/txtName"
android:layout_toEndOf="#id/txtName"
android:layout_alignEnd="#+id/imgBtnItem">
</ImageButton>
</RelativeLayout>
Output:
Use this Tested code. dont use Relative layout for simple layout. It's not good practise
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="5dp">
<ImageButton
android:id="#+id/imgBtnItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="#android:color/transparent"
android:scaleType="fitCenter"
android:src="#drawable/amojee_logo"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:id="#+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="9sp"
android:text="hi how are you"
android:textStyle="bold"/>
<ImageButton
android:id="#+id/imgBtnAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/amojee_logo"
android:background="#android:color/transparent" />
</LinearLayout>
</LinearLayout>
You may try out the following XML code. I have run this and it's doing ok. I have added some icons and text for testing you may change according to your need.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="5dp"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:id="#+id/imgBtnItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:background="#android:color/transparent"
android:src="#mipmap/ic_launcher">
</ImageButton>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1">
<TextView
android:id="#+id/txtName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="9sp"
android:layout_below="#+id/imgBtnItem"
android:text="Hello"
android:layout_weight="0.7">
</TextView>
<ImageButton
android:id="#+id/imgBtnAdd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="#+id/imgBtnItem"
android:layout_toRightOf="#+id/txtName"
android:background="#android:color/transparent"
android:src="#drawable/abc_btn_check_to_on_mtrl_015"
android:layout_weight="0.3">
</ImageButton>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Related
Below is the image what I required.
You can see there is blue background and inside that, it has
Trailer Number 1 which is textview and 01010 is editext (input field)
Also outside the background, there is delete button near to the blu background.
What I tried is,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#color/app_white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RelativeLayout
android:id="#+id/relative_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/blue_light"
android:layout_marginRight="60dp"
android:layout_marginLeft="60dp">
<TextViewÃ’
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Trailer number"
android:padding="#dimen/margin_5"
android:layout_margin="5dp"
android:layout_alignParentLeft="true"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:text="01010"
android:layout_alignParentRight="true"/>
</RelativeLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="0dp"
android:layout_toRightOf="#+id/relative_1"
android:background="#drawable/ic_delete_cion" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
But delete icon is not at all displaying and also line is displaying in 01010 eddittext.
This is my screenshot.
I made some changes in ur layout and it is fit in ur requirement and fit for every device.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/relative_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#color/default_blue_light">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center|start"
android:padding="10dp"
android:text="Trailer number" />
<EditText
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:background="#null"
android:inputType="number"
android:padding="5dp"
android:text="01010"
android:textSize="14sp" />
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/ic_delete_icon" />
</LinearLayout>
you can achive this by only putting android:weightSum into linear layout.
try below code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/app_white"
android:orientation="horizontal"
android:padding="10dp"
android:weightSum="1">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight=".9"
android:background="#color/blue_light"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".7"
android:text="Trailer number" />
<EditText
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".2"
android:background="#null"
android:inputType="number"
android:text="01010"
android:textSize="14sp" />
</LinearLayout>
<ImageView
android:layout_width="0dp"
android:layout_height="20dp"
android:layout_weight=".1"
android:layout_gravity="center"
android:background="#drawable/ic_delete_cion" />
</LinearLayout>
Try this xml code : (Please replace dimensions, padding, styles with your own)
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#FFF"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RelativeLayout
android:id="#+id/relative_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:layout_weight="6"
android:background="#f5f5f5">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_margin="5dp"
android:gravity="center_vertical"
android:padding="5dp"
android:text="Trailer number" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="0dp"
android:layout_marginRight="0dp"
android:inputType="number"
android:text="01010" />
</RelativeLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginEnd="8dp"
android:src="#drawable/ic_delete_black_24dp" />
</LinearLayout>
</LinearLayout>
I want to set an imageview to the right side inside the cardview but aligning is not working... please suggest me
Layout
<FrameLayout
android:id="#+id/framelayout"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/lin1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="05dp"
android:layout_marginTop="15dp"
android:id="#+id/casardviewvisit">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#482048"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/omg"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textColor="#fff"
android:textStyle="bold"
android:layout_marginLeft="20dp"
android:layout_gravity="center"
android:text="Add Order"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sent"
android:layout_gravity="right"
android:id="#+id/imageButtsdon" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</framelayout>
Like this way i am trying but it is not working and i don't know why this is not working
Please use this code to set Imageview to right side inside CardView, just using the Relative layout.
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/casardviewvisit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="05dp"
android:layout_marginTop="15dp">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#482048">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:text="Add Order"
android:textColor="#fff"
android:textSize="20dp"
android:textStyle="bold" />
<ImageButton
android:id="#+id/imageButtsdon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_gravity="right"
android:src="#drawable/beautiful" />
</RelativeLayout>
</android.support.v7.widget.CardView>
Use RelativeLayout instead of LinearLayout
and add
android:layout_alignParentRight="true"
to the ImageView
thats all !!
Make your TextView stretched by doing
android:layout_width="0dp"
android:layout_weight="1"
You can use following code.
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="05dp"
android:layout_marginTop="15dp"
android:id="#+id/casardviewvisit">
<LinearLayout
android:weightSum="1"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#482048"
android:orientation="horizontal">
<ImageView
android:layout_weight=".2"
android:layout_width="0dp"
android:layout_height="50dp"
android:src="#drawable/test"/>
<TextView
android:layout_weight=".5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textColor="#fff"
android:textStyle="bold"
android:layout_marginLeft="20dp"
android:layout_gravity="center"
android:text="Add Order"/>
<ImageView
android:layout_weight=".2"
android:layout_width="0dp"
android:layout_height="50dp"
android:src="#drawable/test"
android:layout_gravity="right"
android:id="#+id/imageButtsdon" />
</LinearLayout>
</android.support.v7.widget.CardView>
You can copy and paste it your xml file
Just use simple gravity for your image view.
<FrameLayout
android:id="#+id/framelayout"
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/lin1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="05dp"
android:layout_marginTop="15dp"
android:id="#+id/casardviewvisit">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#482048"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textColor="#fff"
android:textStyle="bold"
android:layout_marginLeft="20dp"
android:layout_gravity="center"
android:text="Add Order"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sent"
android:layout_gravity="right"
android:id="#+id/imageButtsdon" />
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_gravity="end"
android:layout_height="match_parent"
android:layout_marginRight="16dp"
android:src="#drawable/omg"/>
</android.support.v7.widget.CardView>
</LinearLayout>
</LinearLayout>
</FrameLayout>
Please check the following xml.
<FrameLayout
android:id="#+id/framelayout"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/lin1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="05dp"
android:layout_marginTop="15dp"
android:id="#+id/casardviewvisit">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#482048"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#482048"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textColor="#fff"
android:textStyle="bold"
android:layout_marginLeft="20dp"
android:layout_gravity="center"
android:text="Add Order"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sent"
android:layout_gravity="right"
android:id="#+id/imageButtsdon" />
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/omg"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</framelayout>
Inside the Cardview, the container LinearLayout just give Weightsum of 1 and give both the Imageviews a weight of .25 and textview a weight of .5 as shown below
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/casardviewvisit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="05dp"
android:layout_marginTop="15dp" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#482048"
android:orientation="horizontal"
android:weightSum="1" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:src="#drawable/omg" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_weight=".5"
android:text="Add Order"
android:textColor="#fff"
android:textSize="20dp"
android:textStyle="bold" />
<ImageButton
android:id="#+id/imageButtsdon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight=".2"
android:src="#drawable/sent" />
</LinearLayout>
</android.support.v7.widget.CardView>
my Internal Linear Layout (with blue background) seems to have some margin at bottom with white space, do not know how to remove this.
The External Linear layout also have no padding or margin , from where this whitespace came
[![<?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:background="#fff">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#E78F23">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:clickable="true"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:onClick="dashboard"
android:src="#drawable/menu"/>
<TextView
android:id="#+id/ticket_reciever_header_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="25sp"
android:textColor="#ffffff"
android:text="Mike Smith"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#20000000"
android:layout_alignParentBottom="true">
</RelativeLayout>
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="35%0FF"
android:gravity="center_vertical"
android:layout_marginTop="55dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="35dp"
android:textSize="40sp"
android:textColor="#color/background_material_dark"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:src="#drawable/layer"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:orientation="horizontal"
android:background="#91A4AB"
android:weightSum="3">
<RelativeLayout
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="55dp">
<TextView
android:id="#+id/product_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="AMG 020 ladies shirt"
android:layout_marginTop="6dp"
android:layout_marginLeft="12dp"
android:textSize="20sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/product_name"
android:text="Purple heather"
android:layout_marginLeft="12dp"
android:textSize="12sp"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:clickable="true"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:layout_alignParentRight="true"
android:src="#drawable/share_upper"/>
</RelativeLayout>
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/qr_code"
android:layout_marginTop="30dp"
android:layout_gravity="center"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_gravity="center"
android:background="#3E41B2"
>
<ImageButton
android:id="#+id/shedule"
android:layout_width="0dp"
android:layout_height="match_parent"
android:src="#drawable/tecket_recieved_shedule"
android:layout_weight="1"
android:background="#00000000"/>
<ImageButton
android:id="#+id/location"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="#drawable/ticket_received_location"
android:background="#00000000"/>
<ImageButton
android:id="#+id/favourite"
android:layout_width="0dp"
android:layout_height="match_parent"
android:src="#drawable/favourite"
android:layout_weight="1"
android:background="#00000000"/>
<ImageButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:src="#drawable/share"
android:layout_weight="1"
android:background="#00000000"/>
</LinearLayout>
</LinearLayout>]
1]1
Just update your last Linear Layout Height . Set match_parent instead Hard-Coded 50dp .
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#3E41B2"
>
<ImageButton
android:id="#+id/shedule"
android:layout_width="0dp"
android:layout_height="match_parent"
android:src="#drawable/tecket_recieved_shedule"
android:layout_weight="1"
android:background="#00000000"/>
<ImageButton
android:id="#+id/location"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="#drawable/ticket_received_location"
android:background="#00000000"/>
<ImageButton
android:id="#+id/favourite"
android:layout_width="0dp"
android:layout_height="match_parent"
android:src="#drawable/favourite"
android:layout_weight="1"
android:background="#00000000"/>
<ImageButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:src="#drawable/share"
android:layout_weight="1"
android:background="#00000000"/>
</LinearLayout>
try
android:layout_height="match_parent"
instead of
android:layout_height="50dp"
in your last LinearLayout.
hope this helps!
You have set the height of the layout as a fixed value (50dp). Change it so it takes all available space changing
android:layout_height="50dp"
for
android:layout_height="match_parent"
For future ui issues like this you can enable "Show layout bounds" in Developer Options. It will show the size of every view in the screen
I have a listview which contains a linearlayout. I am trying to get the linearlayout to the center of the screen(horizontally) but it doesn't work.
How it looks -
Portrait -
Landscape -
I want the linearlayout(which has the text "text" and the two images) to come to the center of the screen(horizontally) in both the orientations.
I've tried using layout_gravity on the linearlayout but it doesn't work.
Here's my code -
<?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="match_parent"
android:orientation="vertical"
android:background="#EEEEEE" >
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/parentListContainer">
</ListView>
</LinearLayout>
Each listview item -
<?xml version="1.0" encoding="utf-8"?>
<!-- I want this layout centered. -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:textColor="#000000"
android:text="text"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/ivBottle1"
android:scaleType="fitXY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"/>
<TextView
android:id="#+id/tvBottle1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="top|center"
android:text="vino"
android:textSize="22sp" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/ivBottle2"
android:scaleType="fitXY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"/>
<TextView
android:id="#+id/tvBottle2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="top|center"
android:text="vino"
android:textSize="22sp" >
</TextView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Please help.
EDIT: Neither gravity="center" works on the listview, nor does layout_gravity="center" on the linearlayout.
<?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="match_parent"
android:orientation="vertical"
android:background="#EEEEEE" >
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#+id/parentListContainer">
</ListView>
</LinearLayout>
See here for explanation : https://stackoverflow.com/a/3482757/4706693
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:textColor="#000000"
android:text="text"/>
<LinearLayout
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/ivBottle1"
android:scaleType="fitXY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"/>
<TextView
android:id="#+id/tvBottle1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="top|center"
android:text="vino"
android:textSize="22sp" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/ivBottle2"
android:scaleType="fitXY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"/>
<TextView
android:id="#+id/tvBottle2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="top|center"
android:text="vino"
android:textSize="22sp" >
</TextView>
</LinearLayout>
</LinearLayout>
I am using scrollview on my activity. The scrollview contains a LinearLayout and some other views inside that layout. There is another LinearLayout android:id="#+id/linLayoutImagesContainer" used to put images items with 3 images dynamically just like in Gridview with three columns.
All other things are working perfectly. But, If I add more than 12 rows (i,e.36 images) into LinearLayout dynamically then the scrollview becomes blank with no view.
Can anybody help me? Thanks in advance.
My xml layout:
<?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">
<RelativeLayout
android:id="#+id/relLayoutActionBar"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="#drawable/background_action_bar">
<TextView
android:id="#+id/textViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:text="Activity Detail"
android:textColor="#ffffff"
android:textSize="20sp"/>
<ImageView
android:id="#+id/ivButtonBack"
android:layout_width="35dp"
android:layout_height="20dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:background="#drawable/back_icon"
android:padding="8dp"
android:layout_marginLeft="10dp"
android:textColor="#FFFFFF"
android:textSize="14sp"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/relLayoutTripBanner"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/relLayoutActionBar">
<ImageView
android:id="#+id/imageViewActivityBanner"
android:layout_width="match_parent"
android:layout_height="220dp"
android:scaleType="centerCrop"
android:background="#drawable/default_activity_banner"/>
<!--<Button
android:id="#+id/buttonActivityBanner"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#null"/>-->
</RelativeLayout>
<ScrollView
android:id="#+id/parallaxScroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:layout_below="#+id/relLayoutActionBar"
android:layout_above="#+id/relLayoutActionFooter">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="220dp">
<Button
android:id="#+id/buttonActivityBanner"
android:layout_width="match_parent"
android:layout_height="220dp"
android:background="#null"
android:layout_marginTop="-220dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#ffffffff"
android:alpha="0.9">
<TextView
android:id="#+id/textViewTripDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:padding="10dp"
android:alpha="0.9"
android:background="#ffffffff"
android:text="Activity (Description)"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_margin="10dp"
android:background="#drawable/layout_round_border">
<TextView
android:id="#+id/textViewImages"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_alignParentLeft="true"
android:gravity="center"
android:text="Images (0)"
android:textSize="18sp"
android:textColor="#android:color/white"
android:background="#drawable/layout_left_round_colored"
android:layout_toLeftOf="#+id/viewDate"
/>
<View
android:id="#+id/viewDate"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
/>
<TextView
android:id="#+id/textViewVideos"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_alignEnd="#id/textViewImages"
android:gravity="center"
android:text="Videos (0)"
android:textColor="#ffec6221"
android:textSize="18sp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="#+id/viewDate"/>
</RelativeLayout>
<LinearLayout
android:id="#+id/linLayoutImagesContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
Try this way,hope this will help you to solve your problem.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/relLayoutActionBar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#drawable/background_action_bar">
<TextView
android:id="#+id/textViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:text="Activity Detail"
android:textColor="#ffffff"
android:textSize="20sp"/>
<ImageView
android:id="#+id/ivButtonBack"
android:layout_width="35dp"
android:layout_height="20dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:background="#drawable/back_icon"
android:padding="8dp"
android:layout_marginLeft="10dp"
android:textColor="#FFFFFF"
android:textSize="14sp"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/relLayoutTripBanner"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageViewActivityBanner"
android:layout_width="match_parent"
android:layout_height="220dp"
android:scaleType="centerCrop"
android:background="#drawable/default_activity_banner"/>
</RelativeLayout>
<ScrollView
android:id="#+id/parallaxScroll"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="220dp">
<Button
android:id="#+id/buttonActivityBanner"
android:layout_width="match_parent"
android:layout_height="220dp"
android:background="#null"
android:layout_marginTop="-220dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#ffffffff"
android:alpha="0.9">
<TextView
android:id="#+id/textViewTripDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:padding="10dp"
android:alpha="0.9"
android:background="#ffffffff"
android:text="Activity (Description)"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_margin="10dp"
android:background="#drawable/layout_round_border">
<TextView
android:id="#+id/textViewImages"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_alignParentLeft="true"
android:gravity="center"
android:text="Images (0)"
android:textSize="18sp"
android:textColor="#android:color/white"
android:background="#drawable/layout_left_round_colored"
android:layout_toLeftOf="#+id/viewDate"
/>
<View
android:id="#+id/viewDate"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
/>
<TextView
android:id="#+id/textViewVideos"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_alignEnd="#id/textViewImages"
android:gravity="center"
android:text="Videos (0)"
android:textColor="#ffec6221"
android:textSize="18sp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="#+id/viewDate"/>
</RelativeLayout>
<LinearLayout
android:id="#+id/linLayoutImagesContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>