I have a Button (btn). Is set the Button's style using a drawable:
btn.setBackgroundResource(R.drawable.mybtn_style);
This Button has some text. In some occasions I want to use an image instead of text.
Using the following:
if (useImagesinButtons==true){
btn.setBackgroundResource(R.drawable.ic_myImage);
btn.setText("");
}
replaces the style with the image (myImage).
But I don't want to change the overall style of the Button. I just want to set an image instead of text. I want to do that programmatically.
Could I change the drawable's background image before calling
btn.setBackgroundResource(R.drawable.mybtn_style);
if (useImagesinButtons==true){
btn.setBackgroundResource(R.drawable.ic_myImage);
}
else{
btn.setBackgroundResource(R.drawable.mybtn_style);
}
->First set the button background in the XML file using " android:background" attribute.
-> then when ever you want text on button just set the text on button.
You can't change a style programmatically. However, you can create two different button instances for the two specific case you have. One text-only button and one with an image. Ofcourse you will be using one of the two at a time. Whenever the condition changes, you remove/add buttons.
Related
I notice that for Android ImageView there is an attribute which is android:text = "Hello", but when I run the code, I can't see the text, I'm wondering why there is a text attribute if you can't see it. Do I miss something on how to use it? I'm thinking it can be used to help automation testing, when user switch drawable and text for the ImageView, automation code can compare the text to see if certain image is displayed or not.
You shouldn't be putting text in an ImageView like that. If you want to display text, just use a TextView. Just because you can put the attribute doesn't mean it's useful for something.
Android ImageView is a child of View class. So it doesn't have a property called text. However, while editing the layout manually, you can add it. It'll have no impact. If you try to set the text programmatically or through attributes window, you'll not be able to. It gives you a compilation error.
so don't use text attribute in imageview
Hi all I am trying to show an Android spinner, but not show it as grayed out. I tried using setFocusable(false), which works fine on edit boxes, but not on spinners. I don't want the spinner to changeable unless I tell it to be. Basically I have a screen displaying data that I want to put in "update mode". Until the user has chosen to update the data, it should be view only. Any ideas?
maybe you can create a custom selector and set it to the spinner background,
check out this link for the selectors
I ended up cheating a little. I got the background image before disabling the field, then set the background to that image after disabling the field.
// Deal with spinners. I don't want to gray out the background
// because it looks bad against our dark theme.
Drawable d = type.getBackground().getCurrent(); // the enabled background
type.setEnabled(enabled);
// set all of the spinners back to the enabled background
if(!enabled){
type.setBackgroundDrawable(d);
}
I have a group of buttons which enable user to select certain views on the touch screen.
I want to have the user know which view is active by showing a halo (or any other style) around the button. I mean when the button is clicked it's normal state is changed to different style but it is still normal.
I googled and checked in stackoverflow, that it is not possible to change the style at runtime.
How can I achieve the desired effect?
I have some silly ideas which I won't implement unless as last resort, like making two buttons at same positions and changing their visibility.
Or I can put each button in it's own linear layout and then set the background color of each of them, if it is possible.
Or some ImageButton manipulation maybe.
I have already overridden the default style for button for normal pressed states.
And setting focus(when in focusInTouchMode) causes problem, so that's a NO (already tried that).
Even if style can't be changed, please advise some tip to achieve the effect, it's not necessary to do it the style way. I just want o let the user know which view is selected through buttons.
use state list and set its focussed state drawable as the button with a ring around it and then at runtime say button.requestFocus(). see here for more on state list...
Go for selector xml background for each button. Best Resort
Check this link
How to set image button backgroundimage for different state?
Hpoe this helps
or you can use like ::
Button button;
public void onClick(View v)
{
button.button.setBackgroundColor(**color**);
button.setBackgroundResource(**res**)
}
I have one screen with some image buttons and image view. when user click on these buttons or image view i need to show selected effect not replace any image instead of these. only selected dark shade effect on button or imageview same as iphone. How it's possible ?
i have not any selected image source , i checked these code ...
http://developer.android.com/reference/android/widget/ImageButton.html
But here we set another image on pressed event.
Anyone know any property of android to directly set select dark shadow
effect without image ?
Please anyone suggest how its possible ?
Thanks
you can use setAlpha method of imageView class to do this.
imgView.setAlpha(90);
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
Thats the link for adding images to show different state of buttons. You create a xml file for this and set this file as the background for the button.
how could i get an image and a text from the Internet and made a button for my android application dynamically?And draw them in th position i want.
i.e by Java code
thinks
Download the image and the text, then:
button.setText(downloadedText);
button.setBackgroundDrawable(drawable);
You'll have to create a Drawable from the image, which you can probably do using Drawable#createFromStream.
Drawing the button in the position you want is no different from a normal button. Just declare it in your XML (possible set to Visibility.GONE at first), get a reference to it using View.findViewById and then do the above operations on it.