Dynamically setting an Image on a ImageButton - android

I looked around but didn't find an answer to my problem anywhere. I have an imageButton, and would like to change the image when the button is pressed. I'm trying to figure out a way to get Eclipse to interpret my code literally.
String card = nextcard.toString(); //Becomes "Ace_Of_Hearts", for example
theImageButton.setImageResource(R.drawable.card); //R.drawable.Ace_Of_Hearts is an image in the correct folder
Sadly this doesn't work. But because the card variable changes dynamically, I can't hard code a specific image to set the button to. So how should I set the image?

Get the id of all the cards in the drawable dir:
Integer ace_of_hearts_id = R.drawable.ace_of_hearts;
Integer ace_of_spades_id = R.drawable.ace_of_spades;
Integer queen_of_hearts_id = R.drawable.queen_of_hearts;
Integer ten_of_hearts_id = R.drawable.ten_of_hearts;
....
Put the ids and the string names in a HashMap as key value pairs:
HashMap cardMap = new HashMap<String,Integer>();
cardMap.put("ace_of_hearts",ace_of_hearts_id);
cardMap.put("ace_of_spades" ace_of_spades_id);
......
Then add your code:
String card = nextcard.toString(); //Becomes "Ace_Of_Hearts", for example
theImageButton.setImageResource(cardMap.get(card)); /
Havent actually compiled the code, so there might be errors, but this should give you a basic idea to go about.

You can use imagebutton selector for doing this.
First create selector in res/drawable btn_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<item android:drawable="#drawable/button_bg_selected" android:state_selected="true"></item>
<item android:drawable="#drawable/button_bg_normal"></item>
Now you can use this selctor as src of your image button.
<ImageButton
android:id="#+id/button1"
android:src="#drawable/btn_selector"
android:layout_width="200dp"
android:layout_height="126dp"/>

Related

Get the name of the image resource used in image button

I'm new to Android programming and I want to know that how can I get the name of the image resource that is currently set on the image button.
eg : b1.setImageResource(R.drawable.img);
//where b1 is the image button and img is the name of the png file present in res/drawable.
Now I would like to know how would I get the name of the image file i.e "img".
Thanks in advance.
You can use setTag() and getTag() this way you can store any data you want.
imageView.setTag("imageNameHere");
String imageName = (String) imageView.getTag();
Let me know! ;)
I am guessing that you are simply looking to add an image as a background to your button.
To do this, add your chosen image to your res/drawable folder (there are many ways to do this). You can rename it if you like, but I recommend lowercase with underscores ( _ ), like "img_1", or just "img" as you have said.
Then, in your XML view, add the android:background line to your button...
<Button
android:id="#+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/img"/>

Assign R.id to dynamically created Edit Text

I am new to this. wanted to know if there is a way to assign R.id to dynamically created edit text so i can move the data with in to SQLite db. tried few diff ways but fail. Any help would be appreciated. Thanks
In android id should be unique. Setting any arbitrary integer value can lead to duplicate ids. The correct way is to define id like below.
Create a new xml file named ids.xml inside res/values folder.
Add new item like:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="id" name="button_group_cancel" />
</resources>
Now you can set the id to edittext as:
edittext.setId(R.id.button_group_cancel);
You can assign an id using .setId(int) before you add the EditText to the layout. However this does not place the id into the R.java file.
You can reference the EditText with findViewById(int) instead of using findViewById(R.id.editTextId), where int is the same int used in the set method.

android drawable selection custom

To better explain: For example i have two languages available. Thats why i will have image for English and image for Russian. Both of them i reference in one drawable xml file.
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/image_eng"></item>
<item android:drawable="#drawable/image_ru"></item>
</selector>
What type of drawable xml files i should use ?
How can i set and detect state and switch image ? Basicly it works like android:state_
If I understand correctly, you want to use one image when the phone is in English, and the other when the phone is in Russian. You should use the language modifiers on the drawable folder, not in the drawable name.
So, you would have a folder named drawable-en, which would contain your image for English (drawable-en/image.png, for example). Then, have another folder named drawable-ru which contains your Russian version of the image, but it must have the same name (drawable-ru/image.png).
Then, in your layout XML (or wherever you're referencing the drawable) just reference #drawable/image ... Android will automatically choose the appropriate image.
Please read how Android resource Localization work.
You don't have to use selector for that , or force anything.
just put your image file in the following folders:
res/drawable-en
res/drawable-ru
and in your code you can use:
Drawable draw = getResources().getDrawable(R.drawable.image);
The app will select the suitable one based on the phone's language

How to set ImageButton's foreground image to nothing

I have some ImageButtons being used as a segmented control each has a background set, and the forground image will be a checkmark showing which one of the 3 is currently selected. The other 2 buttons should have no forground image. The images are defined in XML (see below).
<ImageButton
android:id="#+id/style_light_button"
android:layout_width="#dimen/style_color_segment_width"
android:layout_height="#dimen/style_button_segment_height"
android:background="#drawable/button_segmented_light"
android:src="#drawable/icons_checkmark_dark" />
<ImageButton
android:id="#+id/style_sepia_button"
android:layout_width="#dimen/style_color_segment_width"
android:layout_height="#dimen/style_button_segment_height"
android:background="#drawable/button_segmented_sepia"
android:src="#drawable/icons_checkmark_dark" />
<ImageButton
android:id="#+id/style_dark_button"
android:layout_width="#dimen/style_color_segment_width"
android:layout_height="#dimen/style_button_segment_height"
android:background="#drawable/button_segmented_dark"
android:src="#drawable/icons_checkmark_light" />
In code when one is clicked I will clear the checkmark from the 2 that were not clicked and make sure its added to the one that was clicked.
ImageButton lightModeButton = (ImageButton)findViewById(R.id.style_light_button);
ImageButton sepiaModeButton = (ImageButton)findViewById(R.id.style_sepia_button);
ImageButton darkModeButton = (ImageButton)findViewById(R.id.style_dark_button);
I have tried both the setImageBitmap(null) and setImageDrawable(null) but they both crash.
lightModeButton.setImageBitmap(null);
sepiaModeButton.setImageDrawable(null);
darkModeButton.setImageResource(R.drawable.icons_checkmark_light);
How can I clear the image, or at just hide the foreground image while leaving the background image showing?
from android documentation :
public void setBackgroundResource (int resid)
Set the background to a given resource. The resource should refer to a Drawable object or 0 to remove the background.
i don't know if it will work with setImageResource(int resId) so give this a try :
sepiaModeButton.setImageResource(0);
instead of
sepiaModeButton.setImageDrawable(null);
You can create a Transclude drawable for example, on you Drawable directory create file
transclude_drawable.xml like this:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#android:color/transparent" />
</selector>
After that just set ImageButton.setImageResource(R.drawable.transclude_drawable);
Do not forget to change the ImageButton with your references, for example lightModeButton.

Setting background colour of Android layout element

I am trying to, somewhat clone the design of an activity from a set of slides on Android UI design. However I am having a problem with a very simple task.
I have created the layout as shown in the image, and the header is a TextView in a RelativeLayout. Now I wish to change the background colour of the RelativeLayout, however I cannot seem to figure out how.
I know I can set the android:background property in the RelativeLayout tag in the XML file, but what do I set it to? I want to define a new colour that I can use in multiple places. Is it a drawable or a string?
Additionally I would expect there to be a very simple way to this from within the Eclipse Android UI designer that I must be missing?
I am a bit frustrated currently, as this should be an activity that is performed with a few clicks at maximum. So any help is very appreciated. :)
You can use simple color resources, specified usually inside res/values/colors.xml.
<color name="red">#ffff0000</color>
and use this via android:background="#color/red". This color can be used anywhere else too, e.g. as a text color. Reference it in XML the same way, or get it in code via getResources().getColor(R.color.red).
You can also use any drawable resource as a background, use android:background="#drawable/mydrawable" for this (that means 9patch drawables, normal bitmaps, shape drawables, ..).
The above answers are nice.You can also go like this programmatically if you want
First, your layout should have an ID. Add it by writing following +id line in res/layout/*.xml
<RelativeLayout ...
...
android:id="#+id/your_layout_id"
...
</RelativeLayout>
Then, in your Java code, make following changes.
RelativeLayout rl = (RelativeLayout)findViewById(R.id.your_layout_id);
rl.setBackgroundColor(Color.RED);
apart from this, if you have the color defined in colors.xml, then also you can do programmatically :
rl.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.red));
You can use android:background="#DC143C", or any other RGB values for your color. I have no problem using it this way, as stated here
The
res/values/colors.xml.
<color name="red">#ffff0000</color>
android:background="#color/red"
example didn't work for me, but the
android:background="#(hexidecimal here without these parenthesis)"
worked for me in the relative layout element as an attribute.
If you want to change a color quickly (and you don't have Hex numbers memorized) android has a few preset colors you can access like this:
android:background="#android:color/black"
There are 15 colors you can choose from which is nice for testing things out quickly, and you don't need to set up additional files.
Setting up a values/colors.xml file and using straight Hex like explained above will still work.
4 possible ways, use one you need.
1. Kotlin
val ll = findViewById<LinearLayout>(R.id.your_layout_id)
ll.setBackgroundColor(ContextCompat.getColor(this, R.color.white))
2. Data Binding
<LinearLayout
android:background="#{#color/white}"
OR more useful statement-
<LinearLayout
android:background="#{model.colorResId}"
3. XML
<LinearLayout
android:background="#FFFFFF"
<LinearLayout
android:background="#color/white"
4. Java
LinearLayout ll = (LinearLayout) findViewById(R.id.your_layout_id);
ll.setBackgroundColor(ContextCompat.getColor(this, R.color.white));
Android studio 2.1.2 (or possibly earlier) will let you pick from a color wheel:
I got this by adding the following to my layout:
android:background="#FFFFFF"
Then I clicked on the FFFFFF color and clicked on the lightbulb that appeared.
Kotlin
linearLayout.setBackgroundColor(Color.rgb(0xf4,0x43,0x36))
or
<color name="newColor">#f44336</color>
-
linearLayout.setBackgroundColor(ContextCompat.getColor(vista.context, R.color.newColor))
The answers above all are static. I thought I would provide a dynamic answer. The two files that will need to be in sync are the relative foo.xml with the layout and activity_bar.java which corresponds to the Java class corresponding to this R.layout.foo.
In foo.xml set an id for the entire layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+id/foo" .../>
And in activity_bar.java set the color in the onCreate():
public class activity_bar extends AppCompatActivty {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.foo);
//Set an id to the layout
RelativeLayout currentLayout =
(RelativeLayout) findViewById(R.id.foo);
currentLayout.setBackgroundColor(Color.RED);
...
}
...
}
I hope this helps.

Categories

Resources