How to show that ImageButton is pressed? - java

How to show that ImageButton is pressed if i add image to it like this:
Drawable testPic = getResources().getDrawable(R.drawable.test_pic);
button.setImageDrawable( testPic );
And make transparent background so that there would be just image:
button.setBackgroundColor(Color.TRANSPARENT);
So when i press image button i don't see that it is pressed. I want that image would be highlighted or something when it is pressed.
Also every my button is created dynamically by code and i don't know what image would be and how many button there are.
So any ideas ?
Thanks.

you should use a selector for choosing all the different appearances of a UI element, including an ImageButton.
first comes the state_Pressed=true which means what does it look like when pressed. after that comes the regular which is the normal state of the button.

I think you should use State List for that...

You have to change Image at run time. You can achieve these with the help of xml file.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:drawable="#drawable/category_listing_bg_img" />
<item android:state_pressed="true" android:drawable="#drawable/category_listing_bg_img_pressed" />
</selector>
Copy this xml to your drawable folder

Related

How to Change Color of Button when FireTV remote navigates over it

I am making my first Android App. This is for a firestick.
All done except for the color of the buttons.
The default light gray color is good enough (though I may change this when I find out how to).
When navigating over buttons with the remote control, the color of the button only changes to a slightly darker color. It makes it near impossible to tell which button is being navigated over.
I have gone all through the code and can find nowhere to change this to a darker color.
Changing just the background color stops the color changing at all when it is navigated over.
Where would I change this color?
As luck would have it I managed to find the answer just after I posted.
Create xml file in drawable (I used button_events.xml):
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="true"
android:drawable="#color/primary_dark"/>
<item
android:state_pressed="true"
android:drawable="#color/primary_dark"/>
<item android:drawable="#color/primary" />
</selector>
Then I set my buttons background to use this drawable:
<Button
android:background="#drawable/button_events"/>

How can I have more than one imageview on the screen at the same time when I click the screen? (Circle one imageview)

Imageview wherever I touch on Android
I want to show. Can you help me?
In the article I wrote below, my project is how I want to use it.
You have to get screen pressure as we want from you, you should create a new component on the screen every time you click on the screen, you can do this on the java side, every component that is produced will appear and disappear on the screen for 1 second, you can use the timer, you can use the native animations of the android. In other words, what we want right now is to create a new circle with each click on the screen and to have more than one circle at the same time on the screen, and each will have its own start and end time, the components will be removed when the time is up.
use selector background:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- pressed -->
<item android:drawable="#drawable/button_1_selected" android:state_pressed="true"/>
<!-- focused -->
<item android:drawable="#drawable/button_1_normal" android:state_focused="true"/>
<!-- default -->
<item android:drawable="#drawable/button_1_normal"/>
Use state drawable on your image view. Set drawable what you want state_pressed="true" and state_pressed="false". This will do the magic what you want
State list drawable and disabled state

Android (eclipse) image button vs background image

I'm trying to figure out which is best to use.
I'm trying to develop a simple game and designing the starting activity. to start the game I plan to have a start button.
I want to use a custom design though.
My question is, should I use an ImageButton (which I believe is an imported image that you can click on, or a textview with android:background="#drawable/imageName".
Both seem to allow onClick if I'm not mistaken so what's the pros and or one over the other?
Hope this isn't duplicate question, all I found was info on how to upload images which I don't need. Just need to know advantages to using one versus the other.
also, Does onclick for textview apply to background or only text?
I think imagebutton maybe better.
because you must have a click effect for feedback . if you use background for the image ,you have to write a selector.xml for change the click and unclick effects.
so, i think imagebutton may be better.
You can use android:src="#drawable/imageName"
So that the image will be perfect fit. Also you can use
android:scaleType="fitCenter"
android:adjustViewBounds="true"
If you are using TextView then your background may not be good
From : Difference between a clickable ImageView and ImageButton
There's no differences, except default style. ImageButton has a non-null background by default.
Also, ImageButton.onSetAlpha() method always returns false, scaleType is set to center and it's always inflated as focusable.
Here's ImageButton's default style:
<style name="Widget.ImageButton">
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
<item name="android:scaleType">center</item>
<item name="android:background">#android:drawable/btn_default</item>
</style>

Highlighting ListView Selection

I've got a (hopefully) simple question. My program has a ListView to display some data. When an item is pressed, I call the setChecked method on it to mark it as checked. How do I have this "checked" item appear highlighted or in a different style? I was doing some reading on this and found a lot of things that had to do with check-boxes, but I just want to highlight an item that is "selected" by tapping it.
EDIT: I do have a selector setup as follows:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/category_tab_s" android:state_checked="true"/>
<item android:drawable="#drawable/category_tab"/>
</selector>
However, it never applies the checked state. When I press items the category_tab state is activated.
Please check below link :
http://android-codes-examples.blogspot.in/2011/03/customized-listview-items-selection.html
Or on the onclick event you just set
view.setBackground("Any Color");
If your going t some other activity after clicking on any of the row and coming back again to the listview activity again refresh listview so that color get removed and you will get fresh listview.

how to make imagebutton in android stay highlighted?

if you click on a button in android, then it become orange, how would you make it stay like that in java? I tried setBackgroundcolor, but it change the shape of my button to the default shape when i use it and i dont want that.
It not Possible for button as it does not have permanent focus but you can take one radio button and use android:button="#drawable/yourselector". In Radio button you will get checked event and Radio Button checked is permanent state
Make a layout in res/draw able
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false"
android:state_pressed="true"
android:drawable="#drawable/btn_attending_h" /> <!--clicked-->
<item android:drawable="#drawable/btn_attending"/> <!--not clicked-->
</selector>

Categories