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" />
Related
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.
I have two EditText views on a layout containing a date and a time. On being clicked these open up my custom DatePickerFragment and a TimePickerFragments. I would like to style the EditTexts with a picker drop corner as the following image shows.
however I have not been able to do this, my text fields look like this:
The pickers all work and are hooked up and return values to the EditTexts, I just want to do the styling correctly. I am happy to change the class of the containers from EditText if they should be something else to get the desired behaviour. I think this should be possible as the first image comes from Google's own design documentation. Looking through the EditText and TextView class documentation and xml attibute documentation. Is there a way to do this?
I am using Xamarin to compile this down but that should make no difference to the answer.
set the Style attribute to the following:
<EditText
style="?android:attr/spinnerStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
use following image as background of your edittext
make it 9 patch with whatever colour you want.
find an original image and Create a nine patch image like below..
Note : Black Lines are patches
And then add image to background of EditText like below..
<EditText
android:id="#+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/box_edittext"
android:padding="10dp" />
OR
You can use this simple way...
Define style="?android:attr/spinnerStyle" to EditText..
<EditText
style="?android:attr/spinnerStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
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 want to draw an image on the left of an EditText. I don't want the image appear insde the EditText though.
I use this:
<EditText
android:id="#+id/firstNameTxt"
style="#style/UserInfoInputs"
android:drawablePadding="20dp"
android:drawableLeft="#drawable/first_name" >
</EditText>
It displays the image inside of the EditText. However I use this on TextView and it works fine:
<TextView
android:id="#+id/positionValue"
style="#style/userInfo"
android:drawableLeft="#drawable/position" />
How this can be done for an EditText?
try this
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/go_image"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="#drawable/icon"
/>
<EditText
android:id="#+id/url"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:lines="1"
android:layout_weight="1.0" />
</LinearLayout>
let me know if this works.
The difference between the two use-cases you describe are simple. There is no difference. With an EditText, there are lines that are easily discernible. With a TextView there is not. Try setting the background property of the textview, and you'll see that the drawable is, in fact, drawn on the left side, but still 'inside', the TextView.
The simplest way to accomplish your task (as described) is to utilize an ImageView. Depending on what ViewGroup (LinearLayout, RelativeLayout, etc) you're using, the code may be a bit different; so, update your question with the appropriate info and I'll make my answer a bit more specific.
I should note that, another method you could use is to create your own custom component, which is really very easy to do. See this article Custom Components | Android Developer, be sure to scroll down to the Compound Controls heading title "Compound Controls". This would be especially helpful when this is a 'common' format of controls that you'll use often (I.E. you have an image next to a TextView throughout your app).
That is because the background part of the EditText stretches behind the entire contents of the view, including the drawables.
If you are using a RelativeLayout you can just add a separate ImageView:
<ImageView
layout_width="wrap_content"
layout_height="wrap_content"
layout_toLeftOf="#+id/firstNameTxt"
src="#drawable/first_name"
other imageview attributes as neccessary...
/>
<EditText
android:id="#+id/firstNameTxt"
style="#style/UserInfoInputs"
/>
Or if you use another kind of layout, create a LinearLayout-wrapper:
<LinearLayout
layout_width="wrap_content"
layout_height="wrap_content">
<ImageView
layout_width="wrap_content"
layout_height="wrap_content"
layout_toLeftOf="#+id/firstNameTxt"
src="#drawable/first_name"
other imageview attributes as neccessary...
/>
<EditText
android:id="#+id/firstNameTxt"
style="#style/UserInfoInputs"
/>
</LinearLayout>
I am trying to port my WP7 app to android.
Does anyone know how I can layout the text on a single button so that some text appears aligned left and other text appears aligned right? (See below). I need access to be able to dynamically change the percentage number on the right side using code but the text on the right is just static.
Anyone know the answer to this?
The image is here:
http://i.imgur.com/zW7YV.png
Yes you could make it two buttons.
Remove all padding and margin from between them.
Set the same background drawable.
And just ensure when the left is clicked it invokes the right's onPress method (so it looks as if they depress together).
Or wrap the buttons/imageviews/textviews in a layout and perform the onClick on that.
I would use a RelativeLayout for this.
<RelativeLayout
android:width="fill_parent"
android:height="wrap_content"
android:clickable="true"
android:background="#18a2e7"
android:padding="10dip">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Something" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_align_parentRight="true"
android:text="0%" />
</RelativeLayout>