Android - ImageButton in RelativeLayout not matching parent size - android

Fairly new to Android dev, I'm having trouble with a RelativeLayout. In the following XML example, I would expect my ImageButton (on the right, with the '+') to match the RelativeLayout height. However, it doesn't, as shown on this image:
Why isn't the right button with the '+' matching the maximum possible height ?
Here's the related code:
<?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/artist_cell_background">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_margin="10dp"
android:src="#drawable/ic_music_note_24dp">
</ImageView>
<ImageButton
android:id="#+id/artist_button"
android:src="#drawable/ic_add_24dp"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="#drawable/artist_cell_playlist_button_background">
</ImageButton>
<TextView
android:id="#+id/artist_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:lines="1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/black"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:layout_toRightOf="#id/imageView"
android:layout_toEndOf="#id/imageView"
android:layout_toLeftOf="#id/artist_button"
android:layout_toStartOf="#id/artist_button">
</TextView>
<TextView
android:id="#+id/number_of_albums"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:lines="1"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_marginBottom="10dp"
android:layout_toRightOf="#id/imageView"
android:layout_toEndOf="#id/imageView"
android:layout_toLeftOf="#id/artist_button"
android:layout_toStartOf="#id/artist_button"
android:layout_below="#id/artist_name">
</TextView>
</RelativeLayout>
Thanks for any help or idea.
EDIT: What I want is the right button to occupy the height of the line, i.e. have the same height than the two text zones of the middle plus top and bottom margins, no more no less.

As I can understand from the XML, it seems you want to make the "+" button more clickable.
I would suggest you to have some padding and that should do the job.
EDIT 1 : You may do the following without padding, by aligning Top and Bottom of the ImageView to the relevant TextView
<?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/artist_cell_background">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_margin="10dp"
android:src="#drawable/ic_music_note_24dp">
</ImageView>
<TextView
android:id="#+id/artist_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:lines="1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/black"
android:layout_alignParentTop="true"
android:paddingTop="10dp"
android:layout_toRightOf="#id/imageView"
android:layout_toEndOf="#id/imageView"
android:layout_toLeftOf="#id/artist_button"
android:layout_toStartOf="#id/artist_button">
</TextView>
<TextView
android:id="#+id/number_of_albums"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:lines="1"
android:textAppearance="?android:attr/textAppearanceSmall"
android:paddingBottom="10dp"
android:layout_toRightOf="#id/imageView"
android:layout_toEndOf="#id/imageView"
android:layout_toLeftOf="#id/artist_button"
android:layout_toStartOf="#id/artist_button"
android:layout_below="#id/artist_name">
</TextView>
<ImageButton
android:id="#+id/artist_button"
android:src="#drawable/ic_add_24dp"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignTop="#id/artist_name"
android:layout_alignBottom="#id/number_of_albums"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="#drawable/artist_cell_playlist_button_background">
</ImageButton>
</RelativeLayout>

Put your relative layout inside a linear layout that should do it
<?xml version="1.0" encoding="utf-8"?>
<LinearLaout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/artist_cell_background">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_margin="10dp"
android:src="#drawable/ic_music_note_24dp">
</ImageView>
<ImageButton
android:id="#+id/artist_button"
android:src="#drawable/ic_add_24dp"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="#drawable/artist_cell_playlist_button_background">
</ImageButton>
<TextView
android:id="#+id/artist_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:lines="1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/black"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:layout_toRightOf="#id/imageView"
android:layout_toEndOf="#id/imageView"
android:layout_toLeftOf="#id/artist_button"
android:layout_toStartOf="#id/artist_button">
</TextView>
<TextView
android:id="#+id/number_of_albums"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:lines="1"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_marginBottom="10dp"
android:layout_toRightOf="#id/imageView"
android:layout_toEndOf="#id/imageView"
android:layout_toLeftOf="#id/artist_button"
android:layout_toStartOf="#id/artist_button"
android:layout_below="#id/artist_name">
</TextView>
</RelativeLayout>
</LinearLayout>

Your image may be small. Use android:scaleType="fitCenter" in the ImageButton.
Also try removing default padding and margin from the ImageButton by setting them to 0dp.

With the help of #rex, I figured out how to do this: enclose the left picture and the 2 TextViews in a RelativeLayout (this sets the size of the main RelativeLayout), and align top and bottom the ImageButton to this layout.
The code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/artist_cell"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/cell_background">
<ImageButton
android:id="#+id/artist_plus_button"
android:src="#drawable/ic_add_24dp"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_alignTop="#+id/artist_infos"
android:layout_alignBottom="#+id/artist_infos"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="#drawable/plus_button_background"
android:contentDescription="#string/artistPlusButtonCD">
</ImageButton>
<RelativeLayout
android:id="#id/artist_infos"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/artist_plus_button"
android:layout_toStartOf="#id/artist_plus_button">
<ImageView
android:id="#+id/album_cover"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:src="#drawable/ic_music_note_24dp"
android:contentDescription="#string/albumCoverContentDescription">
</ImageView>
<TextView
android:id="#+id/artist_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:lines="1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/black"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:layout_toRightOf="#id/album_cover"
android:layout_toEndOf="#id/album_cover">
</TextView>
<TextView
android:id="#+id/number_of_albums"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:lines="1"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_marginBottom="10dp"
android:layout_toRightOf="#id/album_cover"
android:layout_toEndOf="#id/album_cover"
android:layout_below="#id/artist_name">
</TextView>
</RelativeLayout>
</RelativeLayout>

Related

How to keep the ImageView inside the bounds of screen

I have an imageview left aligned with a textview. The width attribute of the textview has value "wrap_content". The problem is whenever the length of the text inside the textview is exceeds a limit the image view goes out of the visible screen. How to set a limit to the width of textview so the imageview remains inside the bounds of screen?
XML layout:
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimaryText"
android:paddingBottom="10dp">
<RelativeLayout
android:id="#+id/user_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/question_title"
android:layout_marginTop="5dp"
android:minHeight="30dp">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/user_dp"
android:layout_width="34dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:src="#drawable/united_logo"
app:civ_border_color="#color/colorBlack"
app:civ_border_width="1dp" />
<TextView
android:id="#+id/user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="#+id/user_dp"
android:layout_toRightOf="#+id/user_dp"
android:text="A small text"
android:textColor="#color/colorBlack" />
<ImageView
android:id="#+id/button"
android:layout_width="15dp"
android:layout_height="17dp"
android:layout_centerVertical="true"
android:layout_toEndOf="#+id/user_name"
android:layout_toRightOf="#+id/user_name"
android:src="#mipmap/ic_launcher_round" />
</RelativeLayout>
<TextView
android:id="#+id/question_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:text="TextView"
android:textColor="#color/colorBlack"
android:textSize="30sp"
android:textStyle="bold" />
<TextView
android:id="#+id/posted_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/user_info"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:text="5th July, 10:23 IST"
android:textSize="10sp" />
</RelativeLayout>
If all you need is a drawable image at the end of the textview, drop the ImageView and use this for your TextView:
android:drawableEnd="#mipmap/ic_launcher_round"
android:gravity="center"

Use a transparent button in TableView cells on Android

Please take a look at the following layout. This layout is used in a TableView and is applied in each row. The question is why the button in each tablerow is only clickable in the upper half of the tableviewcell when transparency is applied.
Without transparency the button fills up the correct amount of space.
<?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:paddingBottom="10dp"
android:paddingTop="10dp" >
<ImageButton
android:id="#+id/btnSelect"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="#color/white"
android:contentDescription="Geselecteerde rekening"
android:src="#drawable/checkbox_akkoord_uit" />
<ImageView
android:id="#+id/imgPhoto"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignBottom="#+id/btnSelect"
android:layout_marginLeft="4dp"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/btnSelect"
android:scaleType="fitCenter"
android:src="#drawable/icon_foto" />
<ImageView
android:id="#+id/imgPencil"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignBottom="#+id/btnSelect"
android:layout_marginLeft="4dp"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/btnSelect"
android:src="#drawable/potlood" />
<ImageView
android:id="#+id/ivDrag"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:layout_centerVertical="true"
android:src="#drawable/settings" />
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="8dip"
android:layout_toLeftOf="#+id/ivDrag"
android:layout_toRightOf="#+id/imgPhoto"
android:orientation="vertical" >
<TextView
android:id="#+id/txtDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:text="Medium Text Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/green"
android:textSize="#dimen/text_size_1" />
<TextView
android:id="#+id/txtProductname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/green"
android:textSize="#dimen/text_size_2" />
<TextView
android:id="#+id/txtAccountnumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/green"
android:textSize="#dimen/text_size_3" />
</LinearLayout>
<Button
android:id="#+id/btnModify"
android:background="#android:color/transparent"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_toRightOf="#+id/btnSelect"
android:layout_toLeftOf="#+id/ivDrag" />
Any help is greatly appreciated!
Regards, Raoul
Turns out that the Button needs a minimum height. So the altered XML definition could be:
<Button
android:id="#+id/btnModify"
android:background="#android:color/transparent"
android:minHeight="40dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/btnSelect"
android:layout_toLeftOf="#+id/ivDrag" />
This worked for me!
Regards, Raoul.

Android Textview not showing multiple lines within a list

May be this is a duplicate question,but I am asking it again because the solutions here is not working for me.I have a listview and within the list view I have 2 imageviews and a textview.Refer to the image below:
As you can see I cannot split the textviews into 2 lines and as a result my textview is overlapping with that of the imageview.Here is my code:
uplodefilelist.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rl_filelist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:paddingBottom="20dp"
android:paddingTop="20dp" >
<ImageView
android:id="#+id/fileimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:src="#drawable/default_avatar" />
<RelativeLayout
android:id="#+id/ll_cntactcont"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/fileimage" >
<TextView
android:id="#+id/tvfilename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Srina Banerjee"
android:textColor="#000000"
android:textStyle="bold"
android:ellipsize="end"
android:maxLines="2"
android:singleLine="false"
/>
<ImageView
android:id="#+id/btnSettings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#drawable/settings"
android:textColor="#000000"
android:focusable="false"
android:focusableInTouchMode="false" />
</RelativeLayout>
</RelativeLayout>
I cannot use any html format within the setText() method or I cannot do anything from java file.I have to do it from within the xml file only.I consulted our very own stack overflow and added this lines in my textview.
android:ellipsize="end"
android:maxLines="2"
android:singleLine="false"
But as you can see the image none of them are working.Help.
just add 1 tag in to Textview That
android:layout_toLeftOf="#+id/btnSettings"
So this old one is
<TextView
android:id="#+id/tvfilename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Srina Banerjee"
android:textColor="#000000"
android:textStyle="bold"
android:ellipsize="end"
android:maxLines="2"
android:singleLine="false"/>
Change to :
<TextView
android:id="#+id/tvfilename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Srina Banerjee"
android:textColor="#000000"
android:layout_toLeftOf="#+id/btnSettings"
android:textStyle="bold"
android:ellipsize="end"
android:maxLines="2"
android:singleLine="false"/>
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="wrap_content"
android:padding="10dp"
android:gravity="center">
<ImageView
android:id="#+id/fileimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:adjustViewBounds="true"/>
<LinearLayout
android:id="#+id/ll_cntactcont"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp">
<TextView
android:id="#+id/tvfilename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Srina Banerjee"
android:textColor="#000000"
android:textStyle="bold"
android:ellipsize="end"
android:maxLines="2"/>
</LinearLayout>
<ImageView
android:id="#+id/btnSettings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:textColor="#000000"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_marginLeft="5dp"/>
</LinearLayout>
add android:layout_toLeftOf="#+id/btnSettings" in your textview as
<TextView
android:id="#+id/tvfilename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Srina Banerjee"
android:textColor="#000000"
android:textStyle="bold"
android:ellipsize="end"
android:maxLines="2"
android:singleLine="false"
android:layout_toLeftOf="#+id/btnSettings"
/>
And don't forget to clear the project.
Add android:layout_toLeftOf="#+id/btnSettings" in textview and remove its parent relative layout
try this layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rl_filelist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:paddingBottom="20dp"
android:paddingTop="20dp" >
<ImageView
android:id="#+id/fileimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:src="#drawable/default_avatar" />
<TextView
android:id="#+id/tvfilename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_toLeftOf="#+id/btnSettings"
android:layout_toRightOf="#+id/fileimage"
android:ellipsize="end"
android:maxLines="2"
android:singleLine="false"
android:text="Srina Banerjee"
android:textColor="#000000"
android:textStyle="bold" />
<ImageView
android:id="#+id/btnSettings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="#drawable/settings"
android:textColor="#000000" />
</RelativeLayout>
you just have to add one line to your TextView
android:layout_toLeftOf="#+id/btnSettings"
Try this Code...
<ImageView
android:id="#+id/fileimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:src="#drawable/default_avatar" />
<RelativeLayout
android:id="#+id/ll_cntactcont"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/fileimage" >
<TextView
android:id="#+id/tvfilename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Srina Banerjee "
android:layout_toLeftOf="#+id/btnSettings" <!-- use this for setting textview properly -->
android:textColor="#000000"
android:lines="2" <!-- put this line if you always want to show 2 lines -->
android:textStyle="bold"
android:maxLines="2" <!-- max number of size... used when text is long -->
/>
<ImageView
android:id="#+id/btnSettings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#drawable/settings"
android:textColor="#000000"
android:focusable="false"
android:focusableInTouchMode="false" />
</RelativeLayout>
Your textview needs to be a specific width for it to wrap not wrap content. You probably want to do a LinearLayout and not Relative for those 3 views.

How to align a widget at bottom_vertical | center_horizontal with RelativeLayout

This is the page:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true">
<ImageView
android:id="#+id/imageView1"
android:contentDescription="#string/Image_BG_desrp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/background_main" />
<TextView
android:id="#+id/a1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="#string/1"
android:layout_centerHorizontal="true"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:textSize="18sp"
android:focusable="true"
android:focusableInTouchMode="true" />
<TextView
android:id="#+id/a2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="#string/2"
android:layout_centerHorizontal="true"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:textSize="28sp"
android:focusable="true"
android:focusableInTouchMode="true" />
<TextView
android:id="#+id/a3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:layout_centerHorizontal="true"
android:text="#string/3"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:textSize="18sp" />
I want to put this button at bottom_vertical | center_horizontal (center of bottom) page:
<Button
android:id="#+id/btn_xml"
android:layout_width="85dp"
android:layout_height="35dp"
android:gravity="center"
android:text="#string/Continue"
android:textColor="#FFFFFF"
android:textSize="16dp"/>
I put this Button in LinearLayout and use Margin-Top and use some dimension, but with different screen-size it's gonna be problem.
Is there any way to align with RelativeLayout without giving any dimension.
Note: I have to use RelativeLayout!!
Add This in Button Tag:
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
Add
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
to your Button and it will be centered in the bottom of the screen.
try this one
<Button
android:id="#+id/btn_xml"
android:layout_width="85dp"
android:layout_height="35dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="#string/Continue"
android:textColor="#FFFFFF"
android:textSize="16dp" />

Center textview below image view

I need to center a textview just below an image view.
This is a row screenshot from what I a getting now:
I need to put the textview with the text "30" below the image view with the thumbs up hand, and centered. The height position is fine as it is now.
Like this:
This is my layout file:
<?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="#5981b2"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="2dp"
android:src="#drawable/facebook" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/valoracion" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView2"
android:layout_centerHorizontal="true"
android:text="30"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:lines="2"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_toRightOf="#id/imageView1"
/>
</RelativeLayout>
Any help is welcome.
Remove this line
android:layout_centerHorizontal="true"
and add
android:layout_alignParentRight="true"
and you are good to go
If you don't wanna use margins, which can behave differently on other screens, you could for example use the TextView with an compoundDrawable like this:
<?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="#5981b2"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="2dp"
android:src="#drawable/facebook" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerHorizontal="true"
android:text="30"
android:drawablePadding="10dp"
android:gravity="center"
android:drawableTop="#drawable/valoracion"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:lines="2"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_toRightOf="#id/imageView1"
/>
</RelativeLayout>
Take away
android:layout_centerHorizontal="true"
The "below" is good. Then use
android:layout_toLeftOf="#id/imageView2"
android:layout_marginLeft="xxdp"
Set the margin until it appears correct.
Try this..
Remove android:layout_centerHorizontal="true" add android:layout_alignParentRight="true" and give android:layout_marginRight="15dp" for textView3 TextView
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView2"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp"
android:text="30"
android:textAppearance="?android:attr/textAppearanceSmall" />

Categories

Resources