I have a GridView with a BaseAdapter. I would highlight an element with its focus or selected or checked state.
I tried to set grid.setSelection(0) to highlight the first element, but nothing happened.
I set my drawable states background.
put this code in GetView() Method of base adaptor
If(items[position] == selectedIndex){
// Change background or something alpha or anything so that it looks like it is selected
}
And in onItemSelected() Method
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position,long id) {
selectedIndex = position;
adapter.notifyDataSetChanged();
}
Feel free to ask if you not get anything
Related
In my android application i'm having a list view and some items in that,now my requirement is when I select a particular item from that list the list item should be highlighted with a red color, and that selection should not be disappear because in my application i should choose an item from the list view and i should click a button(submit).
So in order to know to the user that he has selected an item from the list view it should be highlighted until he clicks submit button.
I have used choice Mode attribute and set the value to single choice and i have changed the color for highlighting everything works fine but i need it not to be disappeared until user clicks the submit button
The variable val consists of some names which i retrieved from database.
Please help me to solve this Thanks in advance.
ArrayAdapter<String> adptr= new ArrayAdapter<String>(this,R.layout.row,R.id.member_name,array);
lv.setAdapter(adptr);
lv.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View v, int pos,
long id) {
// TODO Auto-generated method stub
val=lv.getItemAtPosition(pos).toString();
//Toast.makeText(getApplicationContext(), val, 5000).show();
}
});
use OnItemClickListener
ListView lview = getListView();
int pos =0;
lview.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position,
long arg3)
{
// set the background of v here
if (position == pos)
// reset the background color
// and pos = position;
else
get the previous listitem view and reset it's background to original
}
});
Actually I had the same problem sometime back. I was working on non touch andorid ui application for google tv.
The solution is like this.
1) create a custom ArrayAdapter (extending ArrayAdapter)
2) onItemClick get the position of the item.
3) send that position to your adapter by some public method say setCurrentPosition(int pos)
4) in getView() of the adapter check for the position and set the background to red for that view.
Hope this will work as it worked for me.
Hello guys I want to highlight multiple items in a list view.
So I tried SngList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); but it didn't help
I am using a custom adapter and extending BaseAdapter
I am using ListView and not AbsListView
I don't want to use CAB because it doesn't go well with the design of my app
I don't want to use the getView method of the adapter as well.
I don't want to use checkboxes either, I guess I will use a boolean for each item and pass it to getviews if I don't get a solution here, but that doesn't seem too elegant and neat. I believe there is a proper built-in way of doing it without using getview() of the adapter
I tried:
android:drawSelectorOnTop="false"
android:listSelector="#android:color/darker_gray"
in the xml but it is highlighting only one of the items and as soon as I click on another item, it highlights it instead...
So is there any proper way of doing it?
Here is how my app looks:
You can make the same logic as CAB but avoid using CAB.
Your list item should have the FrameLayout at root like
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:foreground="?android:attr/activatedBackgroundIndicator">
....
Set onItemClickListener to change choice mode on long press
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
if (mInMultiChoiceMode) {
// if already in multi choice - do nothing
return false;
}
mInMultiChoiceMode = true;
// set checked selected item and enter multi selection mode
final AbsListView list = (AbsListView) arg0;
list.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
list.setItemChecked(arg2, true);
return true;
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
if (mInMultiChoiceMode) {
//exit multi choice mode if number of selected items is 0
if (((AbsListView) arg0).getCheckedItemCount() == 0) {
((AbsListView) arg0).setChoiceMode(AbsListView.CHOICE_MODE_NONE);
mInMultiChoiceMode = false;
}
} else {
// do whatever you should as in normal non-multi item click
System.out.println("CLICK");
}
}
});
SngList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
It should be enough, but you have to use getView, to differentiate the selected and unselected state.
You can use isItemChecked() method to determine weather the item is selected or not, so you don't have to introduce a boolean variable for each items.
Edit:
Something like this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ListView list = (ListView) parent;
if(list.isItemChecked(position)){
//...
}
else{
//...
}
use
SngList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
then manually do it in the adapter.
my problem:
ring5Views[i]
.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,
View arg1, int position, long arg3) {
arg1.setBackgroundColor(Color.parseColor("#444444"));
}
});
if I click on one Item (child) every second item (%2) will change its bg color.
Why ?
The hole thing I want is a list with items and different colors for each but getChild dont solve the prob because NULL every time. And getItem dont help because it only gives back the value (string)
This is how it should work...
for(int i=0; i<oneList;i++)
{
oneList.getChildAt(i).setBackgroundColor(Color.blue);
}
thanks
Extend the ArrayAdapter and override the getView method. And inside the getView method based on the condition you can change the color
you can see this Example for reference
I have a ListView that is populated with 50 items.
This is the xml that I use for my ArrayAdapter.
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:minHeight="?android:attr/listPreferredItemHeight"
android:background="#drawable/listview_background"
android:textColor="#ff000000">
</TextView>
I type
ArrayAdapter<String> list = new ArrayAdapter<String>(this, R.layout.my_simple_list);
to create a new ArrayAdapter. Then I add a String[] items to the list. Then I call
setListAdapter(list);
To add Strings from the String[] items to list, I use a for loop to loop through the String array and add each String to list using command: list.add(items.get(i)); where i is my for loop counter.
for(int i=0;i<items.size();i++){
list.add(items.get(i));
}
This gets my ListView created and populated. I also have an onListItemClick function.
public void onListItemClick(ListView parent, View v, int position, long id) {
String Select = this.getListAdapter().getItem(position).toString();
if(copyitems.contains(Select)){
v.setBackgroundResource(R.drawable.listview_background);
copyitems.remove(Select);
}else{
copyitems.add(Select);
v.setBackgroundColor(Color.GRAY);
}
}
In the onListItemClick function copyitems is an ArrayAdapter variable that I declare as a global variable. So what this function does is store the text from the selected items of the listview so later I can copy that selected text. So if an item is selected it's text is added to copyitems and if the same item is selected it will be removed form copyitems. Every time an item is added to copyitems the background of that item is changed to Gray and if an item is removed from copyitems the background of that item is set back to original. All this work fine. But when I have lots of items(like 50) they don't all fit to the screen so if I want to select an item that is not on screen then I have to scroll down or up to get to the item. The wierd this is that when I scroll away from a selected item and comeback to it the Background changes to original and a different item has the Gray background. So basically if my screen fits 10 items and I select the 1st one and scroll to select others that are passed 10(for example 11). When the 1st item leaves the screen and the 11 comes in, it is already colored Gray but I didn't selected. When I scroll back up to see the 1st item, the background may or may not be Gray. If its not Gray so other item's Background is set to Gray like the 2nd one. Is this happening because the items are recycled when they go off the screen and because of it the positions change of the items? If it is how can I disable that so I only have items background being gray if I have selected that item. If there is a better approach to what I'm trying to do please tell me. I'm new to android.
You should write your own Adapter. I would recommend you to extend from BaseAdapter. Code would look like this:
// These are members of your activity
private List<String> mList = null;
private int mNewlyAddedItem;
private class Adapter extends BaseAdapter {
#Override
public int getCount() {
return mList.size();
}
#Override
public Object getItem(int position) {
return mList.get(position);
}
#Override
public long getItemId(int position) {
return (long)position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(position == mNewlyAddedItem) {
convertView.setBackgroundColor(Color.GRAY);
} else {
convertView.setBackgroundResource(R.drawable.listview_background);
}
// Set text to our TextView
TextView nameText = (TextView)convertView.findViewById(R.id.text1);
nameText.setText(mList.get(position));
return convertView;
}
}
Instead of background changing inside your onListItemClick handler you should just assign mNewlyAddedItem:
public void onListItemClick(ListView parent, View v, int position, long id) {
String Select = this.getListAdapter().getItem(position).toString();
if(copyitems.contains(Select)){
copyitems.remove(Select);
mNewlyAddedItem = -1;
}else{
copyitems.add(Select);
mNewlyAddedItem = position;
}
}
mNewlyAddedItem = position;
I hope I understood your task right. Anyway, you got the point: you should change adapter code to draw your views based on some state (which you can set using Activity member variables), change the state and your views will update (you can also call getListView().invalidateViews() to force redrawal of all views if it is not happening at some point).
i have two spinners.
If in the first one the Item "Diesel" is selected i want to display the second one.
sKraftstoffArt.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if(sKraftstoffArt.getSelectedItem().toString() == "Diesel"){
sPartikelfilterArt.setVisibility(sPartikelfilterArt.VISIBLE);
}
}
public void onNothingSelected(AdapterView<?> adapterView) {
return;
}
});
I've implemented this code in the onCreate method. When i select a item during the runtime i'm not getting the selected item text... It works only if the activity gets created and the default value gets selected....
Where else do i have to implement it?
Regards,
float
Unless your sKraftstoffArt object is not a final one, the check against it's selected item text inside an anonymous class won't work.
The adapterView among the parameters is your ListView instance to which you've assigned the AdapterView.OnItemClickListener.
The view parameter is the actual item (renderer) inside your ListView that has been clicked. This item is provided by your adapter's getView(int position, View convertView, ViewGroup parent) method.
Also, you should use the equals method of String to check whether two String values are equal.
So this won't work:
if(sKraftstoffArt.getSelectedItem().toString() == "Diesel")
Use insetad
if (adapterView.getSelectedItem().toString().equals("Diesel"))
You might also want to add an else clause after this if, to hide the sPartikelfilterArt spinner when the selected item in the previous spinner is not "Diesel".
Please note, that every time you assign a new adapter to this list (which probably you don't, i still mention it just in case...), you should add the AdapterView.OnItemClickListener to it again.