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.
Related
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"/>
I made a Contextual Action Bar which inflates directly while long clicking on some item of the RecyclerView.
I did not create a toolbar or an appbarlayout in the xml file so the app starts without any toolbars or actionbars but when I long click on some item it inflates the contextual action bar.
My problem is, I tried all the possible methods to change the black color of the back arrow in the contextual action bar, but all in vain.
I'm not sure that you mean home up button, because it isn't part of contextual menu, it's part of activity menu.
If yes, just put this into your style
<item name="homeAsUpIndicator">#drawable/your_drawable</item>
your_drawable may look like this
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
...
</shape>
</item>
<item>
<bitmap
android:src="#android:drawable/your_image"
android:tint="#color/your_color"
/>
</item>
If no, without an image of this arrow I can't tell more.
fairly new to Android development and I've got a question I've Google'd the crap out of and can't find a solid answer on.
I simply want to know how to determine when the position in a ListView changes. Specifically, I've got a ListView whose TextView text changes color when I click on it. The problem is, when I click on a different position in the ListView, the color of the text of the position I was just on stays what it was changed to.
Basically, is there a way to listen for a position change in a ListView, and then execute code when that position changes?
Thanks a lot!
If you only want one item to be selected at a time make sure you set the choice mode to single when you're setting up your ListView.
myListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE)
You'll need to set an onItemClickListener.
myListView..setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
// Your Code Here
}
});
When you set up your ArrayAdapter you can use one of the default Android item layouts such as simple_list_item_activated_1, which highlights the background of the selected item.
myArrayAdapter = new ArrayAdapter<String>(getContext(),
android.R.layout.simple_list_item_activated_1, myArrayList);
If you want your own custom behavior such as changing the text color rather than the background, then simply replace the item layout with one of your own. Have a look at the default Android item layouts to see how they write them. For example, below is the default Android item layout android.R.layout.simple_list_item_activated_1, where you can see they've used a selector called activatedBackgroundIndicator to define the background color.
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:background="?android:attr/activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
/>
<Additional>
In your case, to get the text color to change instead of the background, you will want to use a selector for your text color. Make a color folder in your res directory. Create a selector in it:
my_color_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:color="#color/my_red_color />
<item android:drawable="#color/my_blue_color" /> <!-- color when not selected -->
</selector>
Then in your custom item layout set the color of your text to my_color_selector
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
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>