I would like to have an image set to background a text on it and an icon on the left side of the text. Very easy in iPhone, but can't figure out how to do it at Android, to be resizable that button and keep the icon + text position and distance properly.
iPhone:
Android I have this:
The xml code is:
<Button
android:id="@+id/btSettings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/tvWhatever"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:background="@drawable/bt_general"
android:drawableTop="@drawable/settings_selected"
android:text="@string/settings"
android:textColor="#000000" />
Took the code from here.
If I use android:drawableLeft
, than the icon will go to most left part.
If I start playing with semi hardcoded paddings, than I will have different look at diff devives: ( phone and table)
If I add the android:gravity="left|center_vertical"
than it will look like this:
The text is variable: it will change, when the user change the language.
How to do it properly?
I don't want to downvote anybody's answer, but please read the question and don't suggest what I have tryed already. Also told the hardcoded fixes doesn't work well.
This is not a homework, but a commercial software part.
Here is a suggested code from answers:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/bt_general"
android:padding="20dip" >
<ImageView
android:id="@+id/xIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dip"
android:src="@drawable/settings_selected" />
<TextView
android:id="@+id/xSettingsTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/settings"
android:textColor="#000000" />
</RelativeLayout>
What do you think how it will look like the android:layout_marginLeft="10dip"
on Galaxy s4? Here is a preview:
This is not, what I have asked. "dip" or "dp" or "px" shouldn't be used anywhere as distance from left, top, because phones has HPDI, smaller screen and Tablets has MDPI and wide resolutions. Simple doesn't work on mdpi, and xxhdpi.
Nizam answer is very close to a good solution:
Try below layout
and drawable/round_corner_drawable is as below:
Also, if you want to use your image as a background, give
android:layout_height="wrap_content"
and set it as a background for relative layout.p.s. If you put different color values for
startColor
,centerColor
andendColor
, you will get that gradient effect for your background drawable. So change the color values accordingly.EDIT:
Try below layout, i edited it to fit in different screen sizes with rounded corner drawable.
I had achieved that with following code. May be used as an easier alternative to above solutions.
the button view is marked in red(this is not a list item but a button with above code):
I just implemented this kind of button (and it has been deployed in a large scale app), it is not really complicated : content of large_button.xml :
Then, to use it I just have to use the include tag :
large_button_background is a selector that points to the different drawables to use for each button state
I used a LinearLayout, it allows to simply center both text & icons in the button. It should also be doable with a GridLayout. If you want to center just on the text (I don't think it looks great though but that's your choice), use a RelativeLayout.
If your button width:
android:layout_width="wrap_content"
then your code will be like this:Else if your button width:
android:layout_width="match_parent"
then your code will be like this:By the way
android:paddingLeft="100dp"
, change100
with your suitable value.You can use paddingLeft and paddingRight to get button as the one in iphone
Try to use TextView,
In Java
where Drawable image is,