Android RadioButton dynamic icon change - android

I'm doing a multiple choice quiz app. It has 4 radio buttons. What I want to have is have 3 icons -> No answer yet, right answer, wrong answer. So before any choice is clicked, all are non-answer, when an answer is clicked, if it is right, it light up green, if wrong - light up red. Is it possible to do with radio buttons? Or do I have to find another way out? Like using an ImageView?

Create one XML in "drawable" folder "radio.xml"
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/radio_on" android:state_checked="true"/>
<item android:drawable="#drawable/radio_off" android:state_checked="false"/>
</selector>

Take a look On this Answer.
But u want verification for selected answer. I recognized to u that..Use ImageButton with TextView in your xml file Instead Of RadioButton.Initially Set notChecked image as background of ImageButtons .when User click on Any of your ImageButton. U can verify it On its OnClick event that is it r8 answer or not and then u can set Checked image(red/green) as background image of selected ImageButton.

Related

How to change drawable image background while click

I am new to Android, I have an image, want to change background color programmatically while clicking that icon. How to do. Please, anyone help me.
I have icon like this, before click:
I want to change like this while click, after click:
You have to see:
how to apply click event listener to image in android
and inside of the click event listener:
backgroundImg.setBackgroundColor(Color.parseColor("#FFFFFF"));
or
backgroundImg.setBackgroundColor(Color.rgb(100, 100, 50));
What you need here is a selector.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/numpad_button_bg_selected" android:state_selected="true"></item>
<item android:drawable="#drawable/numpad_button_bg_pressed" android:state_pressed="true"></item>
<item android:drawable="#drawable/numpad_button_bg_normal"></item>
</selector>
And then you need to create the two images, because from what I see in the examples you also set the text color.
If you later want to change the overall color of the icon you can do that using AppCompatImageView and tinting.
Two Solution Available
Use Two Images and update on android:state_selected, android:state_pressed. Similar to dzsonni answer.
Use SVG Image.Update Programmatically.See Below link
Dynamically changing SVG Image colours on Android

How to add a click effect to a button on click event?

How would I add a click effect similar to the below image for a button click event?
The button in my activity already has it's background set to an image so I'm not sure how I would add a background of a state also as in this tutorial:
Android Button color changing on onClick?
You need two image, one for normal state and another for pressed state. At first create a selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/pressed_image" android:state_pressed="true"/>
<item android:drawable="#drawable/normal_image"/>
</selector>
Add this selector in your button as a background.
Here's something similar. I am not sure though, if this fits your solution.
EDIT: It uses selector, just like #Raghunandan suggested above.

Android: how add opacity on ImageButton click?

I thought to use OnTouchListener and tracking down and up events, setting then an alpha value, but I have to add this listener to all ImageButtons (they are really a lot). I wonder if there is a shortcut to achieve this result.
In case that when user click on button and this cause to change the opacity of that button you can do:
In your xml file on button declaration add this line:
android:onClick = "clickMethod"
and in the java file you need to implement the clickMethod,
public void clickMethod(View view)
{
// change opacity
}
so, if you want to do the same process(change button opacity) for each button, so in the xml file
for each button add the line
android:onClick="clickMethod"
If you want to give users better expirince when clicking ImageButton, I recommend you to use selection drawable as the background of your ImageButtons. It gives better user experience, then setting opacity while performing click and it’s really easy to achieve.
First you need to create in your drawable folder file with name f.e. image_button_selection.xml. In which you should define:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="#drawable/your_focused_background_drawable" />
<item android:state_pressed="true" android:drawable="#drawable/your_pressed_background_drawable " />
<item android:drawable="#android:color/transparent" />
</selector>
You should also put into drawable two png with alpha channel that will be displayed on focused button and pressed button. In this example they should be named respectively your_focused_background_drawable and your_pressed_background_drawable.
When you do this you should in every use of your ImageButton in xml use following statement:
android:background="#drawable/image_button_selection"

Android - How to create image button of different states, default , pressed and focussed

In my android application i want to create three different image of a single image for each state pressed , default and focussed. Is there any tool that can help me to create these images.
I think you misunderstood my problem, i knows that how to use three different image for three different states. I only wants to know that how two create those three images.
here http://code.google.com/p/iosched/source/browse/android/#android%2Fres%2Fdrawable-hdpi
you can see that there are three different images for three differenct states
home_btn_announcements_default.png
home_btn_announcements_pressed.png
home_btn_announcements_selected.png
I wants to know that is there any tool that can create the above three images. I think now my question is clear to everyone.
My Question was that how to add white border for default image , gray border for focussed image. I know that how to change image according to different states. I want to ask you that is there any tool that can put white or gray border around my image.
Use selector save this file in drawable/click.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="false" android:drawable="#drawable/notPressedImage ></item>
<item android:state_pressed="true" android:drawable="#drawable/PressedImage" ></item>
</selector>
use click.xml (click) as button src...

custom android button look and feel

The two buttons in the bottom of the screen are Scan and Leads, if we click on any button, the view of the button is visible like pressed as u see in image for lead button, this view will change according to the button click, and here i am unable to create that kind of look and feel for buttons. please provide me the sample code or suggest me to achieve this look and feel.
Thanking you
this is custom button .A custom button is very easy to create, you just have to make different images for the different state of the button and place it into your res/drawable folder.
below link contain the step by step description for creating this:
http://androidemulator.wordpress.com/2011/11/03/creating-custom-buttons-in-android-applications/
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_some_name_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/button_some_name_default"/>
</selector>
Have this in your /drawable/ folder as button_some_name.xml
Then have the button_some_name_pressed.png & button_some_name_pressed.png in your drawable-ldpi drawable-mdpi etc
There are other states to consider as well
ref: http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

Categories

Resources