I need to create a button with the background and place it on the picture. I tried to create LinearLayout Horizontal bet on it, and put the background image and button. but it looks not nice. Tell me how to do this?
No layout is needed here, any TextViews can have drawableLeft attribute. Use appropriate XML attributes to set picture, padding, gravity of the text, etc.
Use button's xml attributes to set a drawable according to your need, and in your case it is drawableLeft. See the example below to make a think like your purpose :
<Button
android:id="#+id/backButton"
android:drawableLeft="#drawable/back_btn"
android:background="#android:color/transparent"
android:text="#string/home_screen_sell_new_card_back_button_text"
android:textColor="#026281"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
And for the little enhancements, put paddings and other stuff.
Related
How would one go about drawing a shape, then adding multiple elements to it?
At first I thought I should make use of either a textview, or a button, then simply apply a rectangle as a background. The problem with that is my text is all the same size.
I would also like to eventually add an image to the below purple button.
My end result should look like this:
What would be very nice, is if there was some sort of container that one could use... unfortunately I am new to android, so I do not know what to search for.
Just use a LinearLayout and put two TextViews in it:
<LinearLayout
android:orientation="vertical"
android:height="wrap_content"
android:width="wrap_content"
android:padding="20dp">
<TextView
android:text="123,150"
android:width="wrap_content"
android:height="wrap_content" />
<TextView
android:text="TOTAL PAGE VIEWS"
android:width="wrap_content"
android:height="wrap_content" />
</LinearLayout>
This is an untested example to give you something to start with, you'll have to adjust font size/color and background yourself.
you should look for a layout combinaison :
FrameLayout to have layout superposition
RelativeLayout to position items
Please refer to this image:
The Top image is a TextView with no compound drawable and the padding all around seems perfect.
The second image has a compound image using android:drawableRight="#drawable/ic_action_expand" and the image messes up all the padding.
How to get rid of this unwanted padding in the second image ?
Here's the XML
<TextView
android:id="#+id/moreinfo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/translucent_black"
android:drawableRight="#drawable/ic_action_expand"
android:drawablePadding="#dimen/content_heading_padding"
android:singleLine="true"
android:padding="#dimen/content_heading_padding"
android:textSize="#dimen/content_size"
android:text="#string/more_info" />
As you can see in the XML i have used android:drawablePadding
Including or removing that line does changes the padding for the drawable but the mess that has happened for the textview still remains.
I have tried negative padding values for android:drawablePadding but it does not correct the issue.
One of the reasons could be that it must be set autoresizable(true) in your android manifest file set to autoresizable(true). Check your manifest.
I am having one contact form in which I would like to add some icons to my edit text for a better look. I want to create EditText something like below.
I have EditText like below. And I got the result something like. I want the image to be absolutely on the left side as it is in the above image. I know that internally EditText is using Nine Patch image and i think that is why the result is some what different. Can anyone know how to resolve this?
<EditText
android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="51dip"
android:hint="#string/name"
android:inputType="text"
android:drawableLeft="#drawable/name_icon_edittext" />
Instead of using android:drawableLeft="#drawable/name_icon_edittext", create separateImageView for the name_icon.
This way you have more control on placing it on the layout
Note: I am assuming your layout is RelativeLayout
if you want to add some space between you text and image then use'drawablePadding' property in xml
android:drawablePadding="10dp"
here is complete example how to use in edittext in xml file
<EditText
android:id="#+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/ic_left"
android:drawablePadding="10dp" />
You can use android:drawablePadding="10dp" property in the xml file.
There's no need to create separate imagview for the icon.
A bit old, but for future reference maybe:
I would do a 9-patch image myself with the appearance you want, keeping the proportions and all but the icon, so you can add it yourself (and reuse the background) with drawableLeft property of EditText. That way only the middle part of the image will be stretched but the rest will stay the same.
I created a button with a 9patch image as a background. In the 9patch image I used the bottom and right lines to specify the content area/padding, to position the text on the button. This works well on a TextView, but on a Button the content lines seem to have no effect on the text position. Setting "paddingTop"/"paddingBottom" also seem to have no effect - only "padding" has any effect.
This is my layout XML:
<Button
android:layout_width="450dp"
android:layout_height="198dp"
android:text="Some text here"
android:gravity="center"
android:background="#drawable/my_button"/>
Is there any way to set the padding? Maybe I could use a different View instead of a Button? For example, I could try to just use a TextView and make it clickable, but I'm not sure what other side-effects this will have.
Explicitly setting android:padding="#null" solved my issue.
The issue seemed to be that a Button (or one of the styles in the theme I'm using) is setting the padding, which overrides all other padding in the button - padding in the background drawable, as well as paddingTop, paddingLeft etc.
My button layout is now defined like this:
<Button
android:layout_width="450dp"
android:layout_height="198dp"
android:text="Some text here"
android:gravity="center"
android:padding="#null"
android:background="#drawable/my_button"/>
My IDE (IntelliJ IDEA 11.1) picked up the #null as an error, even though it compiled. Setting it to -1px also worked.
If you use a 9-patch image as background, both the padding and the stretched areas are defined in the background.
It's quite graphic if you use the draw9patch tool: the padding is defined (indirectly) with the content area (lines on right and bottom):
http://developer.android.com/guide/developing/tools/draw9patch.html
In examples found on the net I saw that the text is set from XML file only. I need to attach the text from another View, and I tried to find any setter that I can use to set text to ImageButton. I didn't succeed. I even tried using this
<ImageButton
android:background="#ffffff"
android:text="setText()"
/>
hoping that I can use setText() in the code, but it did not work as well.
How can I set the text for ImageButton programmatically?
Thanks
PS. This is a custom ImageView which inherits ImageView.
ImageButtons can't have text (or, at least, android:text isn't listed in its attributes). It looks like you need to use Button (and look at drawableTop or setCompoundDrawablesWithIntrinsicBounds(int,int,int,int)).
You cannot set text to ImageButton because it has no method as setText() or android:text property.
Here is workaround, using a Button and android:drawableTop / Left / Right or Bottom like this :
<Button android:layout_height="wrap_content"
android:drawableTop="#drawable/icon" android:text="Button"
android:layout_weight="1" android:layout_width="fill_parent"
android:textSize="11sp"
android:layout_margin="1sp" />