Unselect the ListView Item onClick in android - android

What is happening i have a listview on which i putting background color change on selection.As well as by default i am putting the first listview item selected as
public class OneWayFlightResult extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public OneWayFlightResult(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.row, null);
TextView flightTime = (TextView)vi.findViewById(R.id.flightTime); // title
TextView flightCompanyName = (TextView)vi.findViewById(R.id.flightCompanyName); // title
TextView flightNumber = (TextView)vi.findViewById(R.id.flightNumber); // title
ImageView flightLogo = (ImageView)vi.findViewById(R.id.flightLogo);
HashMap<String, String> flight = new HashMap<String, String>();
flight = data.get(position);
flightTime.setText(flight.get(TestActivity.FlightTime));
flightCompanyName.setText(TestActivity.FlightCompanyName);
flightNumber.setText(TestActivity.FlightNumber);
if(position == 0){
vi.setBackgroundResource(R.drawable.selection_effect);
vi.setSelected(true);
}
return vi;
}
This is XML file i am using in this selection_effect.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true">
<shape>
<solid android:color="#ffffff" />
</shape>
</item>
<item>
<shape>
<solid android:color="#00a7eb" />
</shape>
</item>
</selector>
So by default this the first list view is selected.Now when the user select the another listview item the first one remains selected and the other one also got the same effect.So how could change the effect on click of the listview item dynamically .Means by default first item comes up selected when the user selects other item other one get selected the effect from the default one get removed

I got solution for your problem. Do as following.
1) open your main layout file where ListView you have created.
Add android:choiceMode="singleChoice". This will look like below.
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:choiceMode="singleChoice" >
</ListView>
2) Open your list_item.xml layout file. In which, to your root view, add android:background="?android:attr/activatedBackgroundIndicator". In my sample project, its look like below.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/activatedBackgroundIndicator"
android:orientation="vertical" >
//your views
</LinearLayout>
3) Open your activity file. After setting adapter to your ListView, add list.setSelector(R.drawable.selection_effect);. This will look like below.
ListView ls = (ListView) findViewById(R.id.listView1);
ListAdapter adapter = new ListAdapter(this, data);
ls.setAdapter(adapter);
ls.setSelector(R.drawable.selection_effect);
Here, selection_effect is drawable file which you have created in drawable directory.
I tested my code. Which is working fine.
4) To select first view by default, remove your code in BaseAdapter and put following code after completing 3rd step.
ls.setItemChecked(0,true);
You need to put it after above code like below.
ListAdapter adapter = new ListAdapter(data);
ls.setAdapter(adapter);
ls.setSelector(R.drawable.selection_effect);
ls.setItemChecked(0, true);
Explanation
ls.setSelector(R.drawable.selection_effect);
This will select row item based on selector you have defined in drawable directory.
ls.setItemChecked(0, true);
This will select first item by default at first time run. After you can select other items by clicking on them.

You could just declare an int for item clicked which defaults to the first item that starts clicked, then in onclick update accordingly.
int selected = <default>;
set in oncreate etc.
Then you can have onItemClicked listener and do this,
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if (selected != i){
<listView>.getChildAt(selected).setBackgroundColor(getResources().getColor(android.R.color.background_dark));
<listView>.getChildAt(i).setBackgroundColor(getResources().getColor(android.R.color.holo_blue_light));
selected = i;
}
}
This will change the background colour of the previously selected item back to (in my case) the default android dark theme colour, set the newly selected one to a nice highlighted light blue, and update the selected int to reflect the newly selected item.

You can try like this:
after setting adapter for ListView set position to 0 for default selection i.e. listView.setSelection(0); and in onItemClick you get selected position (3rd parameter) of item, so inside onItemClick write listView.setSelection("that 3rd param");

I have had similar problem with ListView all you do is when item is clicked call a method on adapter, adadpter.SetPosition(position) {mSelectedPsoiton = position; notifyDataSetChanged();}, and in your getView function check if the position is equal to selectedPosition, then set background accordingly this never fails, focus does not work as ur in touch mode always
//adapter class
public override View GetView(int position, View convertView, ViewGroup parent) { TextView view = null; int lookupPos = position; if (null == convertView) { view = new TextView(_context); view.SetTextSize(ComplexUnitType.Sp, 20); view.SetPadding(_pixels, _pixels, _pixels, _pixels); } else { view = convertView as TextView; }
if (position == mSelectedPos )
{
view.SetBackgroundResource(Android.Resource.Color.HoloBlueDark);
}
else
{
view.SetBackgroundResource(Resource.Drawable.listItemSelector);
}
return view;
}
public void SetSelectedPosition(int position) { mSelectedPos = position;
}
private int mSelectedPos = -1;
// ListView code, _adapter is adapter of listview
listView.SetOnItemClickListener (new OnItemClickListener() { #Override public void onItemClick(AdapterView adapter, View view, int pos, long id) { _adapter.SetSelectionPostion(pos); }
}

Related

Highlighted item changes position when scrolling listview

Highlighted item changes position when scrolling listview. If i choose first item in listview and scrolling to the end, between every 7-8 item is selected. And even highlighted item lose its position and highlighting 3rd item. i dont use custom adapter for listview, only for textview. Here is code:
lv=(ListView) findViewById(R.id.listView);
myadaptersong = new ArrayAdapter<String>(this, R.layout.custom_item, R.id.menu, music);
lv.setAdapter(myadaptersong);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(menuItem != null) menuItem.setBackgroundColor(Color.parseColor("#ffffff"));
menuItem = view;
menuItem.setBackgroundColor(Color.parseColor("#a2aed3"));
});
}
UPDATE:
Custom Adapter is this
public class CustomAdapter extends BaseAdapter {
String music[];
int flag[];
LayoutInflater inflater;
public CustomAdapter(Context context, String[]music, int []flag)
{
this.music=music;
this.flag=flag;
inflater=(LayoutInflater.from(context));
}
#Override
public int getCount() {
return music.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View view, ViewGroup viewGroup) {
view=inflater.inflate(R.layout.custom_item,viewGroup,false);
TextView name= (TextView)view.findViewById(R.id.menu);
name.setText(music[position]);
return view;
}
}
what should i do here?
The code you posted won't work the way you intend (as you already noticed). Let's say your ListView shows 4 items on screen and you clicked on the first item (item 0, highlighting it by changing its background color). When you scroll down to see more views and item 0 goes off the screen, the view from item 0 will be recycled to show a later item (say, item 6).
Now item 6 is using the view whose background color you already changed, so it will be highlighted. As the view continues to go off-screen and be re-used, other random rows in the ListView will be highlighted.
You will need to implement a custom adapter to do what you are trying to do.

Select listview item on second click

The code populates the listview with items using baseadapter. On clicking on the item it changes background color and goes to the next activity. Now i want to change the back ground color on first click and it should stay selected. Then on next click it should go to the next activity. Is it possible to do so.
Activity
nameList = (ListView) findViewById(R.id.list_names);
nameList.setAdapter(listAdapter);
listAdapter.notifyDataSetChanged();
nameList.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long viewId)
{
TextView PCNtv = (TextView) view.findViewById(R.id.value_Name);
String P_Name = PCNtv.getText().toString();
Intent intents = new Intent(getApplicationContext(), Activity2.class);
intents.putExtra("P_Name", P_Name);
view.setSelected(true);
startActivity(intents);
}
});
In drawable
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="#drawable/blue_color"/>
<item android:state_pressed="true" android:drawable="#drawable/white_color"/>
<item android:state_focused="true" android:drawable="#drawable/red_color"/>
</selector>
Use the views selected property to detect the second click. First click will select the view, the second will open the activity.
nameList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long viewId)
{
TextView PCNtv = (TextView) view.findViewById(R.id.value_Name);
String P_Name = PCNtv.getText().toString();
if ( view.isSelected() ) {
Intent intents = new Intent(getApplicationContext(), Activity2.class);
intents.putExtra("P_Name", P_Name);
startActivity(intents);
}
view.setSelected(true);
});
}
Update
The selection is lost when the users presses the view again, so the code above does not work. Here is a solution involving the adapter tracking the currently selected position.
The click event:
nameList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
// Check to see which item in the adapter is already
// selected, if the same has been selected twice, start new activity
if ( i == ((TestAdapter)adapter).getSelectedIndex()) {
// Start your new activity here.
Log.d("TAG", "second push");
}
((TestAdapter)adapter).setSelectedIndex(i);
}
});
You did not show the code for your adapter, so here is a simple one with the selected index added:
public class TestAdapter extends ArrayAdapter<String> {
private final Activity context;
private int mSelection = -1;
public TestAdapter(Activity context, String[] objects) {
super(context, R.layout.text_layout, objects);
this.context = context;
}
// Call this when an item is clicked, this sets the
// internal index for the selected item and notifies the view
// that the data has changes, this will force the view to draw
// allowing it to correctly highlight the selected item
public void setSelectedIndex(int index) {
mSelection = index;
notifyDataSetChanged();
}
// This is needed for the click listener to check to see which
// item is already selected
public int getSelectedIndex() {
return mSelection;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.text_layout, null);
TextView tv = (TextView)rowView.findViewById(R.id.text3);
tv.setText(getItem(position));
// Adds or removes the selection on the view so
// it will display as selected
if ( mSelection == position ) {
tv.setSelected(true);
} else {
tv.setSelected(false);
}
return rowView;
}
}
Updated: added inline comments

Highlight the selected item in Horizontal List view

By default i need to show one item as highlighted in horizontal list view and when the user selected another item in the horizontal list view i want to highlight that item(removing the earlier and highlight the currently selected) for that i'm trying with the following code,in my adapter
Adapter:-
int selectedIndex;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
v = LayoutInflater.from(parent.getContext()).inflate(
R.layout.hlist_rowitem, null);
if (position == selectedIndex) {
v.setBackgroundColor(Color.parseColor("#abcdef"));
}
}
and after selecting another item from activity in from the list view what to do in activity to change highlighting position of the item.
Activity:-
int sIndex;
sIndex = getIntent().getIntExtra("POSITION", 0);
hlAdapter = new HSelectedAdapter(InsuranceCard.this, rowItems, sIndex);
hListView.setAdapter(hlAdapter);
hListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
//other code goes here
}
});
I'd would use a color state list resource and have the ListView handle the selection with setSelection(position).
The color list would look something like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#aabbcc"/> <!-- pressed -->
<item android:state_activated="true"
android:color="#fedcba"/> <!-- selected -->
<item android:color="#abcdef"/> <!-- default -->
</selector>
and it should be set as background of the R.layout.hlist_rowitem or as listSelector on the listview.
Edit:
To change the selection when receiving a click event:
hListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
hListView.setSelection(position);
}
});
The ListView will deselect the old/default item and selects the new item at the specified position.
Edit 2: By default the ListView don't have a choice mode set so make sure you either set it in xml or in code: listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
you can achieve this in two ways.
manually clear all item and set selected in onItemClick()
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
for (int i = 0; i < adapter.getCount(); i++) {
View item = listview.getChildAt(i);
if (item != null) {
item.setBackgroundResource(R.drawable.unselected);
}
arg1.setBackgroundResource(R.drawable.selected);
}
}
});
use selector and let listview do itself.
/drawable/selector_list.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/selected" android:state_selected="true"/>
<item android:drawable="#drawable/selected" android:state_activated="true"/>
<item android:drawable="#drawable/unselected"/>
</selector>
and add android:listSelector="#drawable/selector_list" to your listview
add listitemclick.xml
in your drawblw folder this is the code.
2)then in your hlist_rowitem.xml set background="#drawable/listitemclick"
follow these steps:
1)declare one boolean array.
public static boolean ClickItem[];
2)inside oncreate
ClickItem=new boolean[your array size];
Arrays.fill(ClickItem, false);
in your adapter write this code
a)
if ClickItem[pos]
{
v.setBackgroundColor(Color.parseColor("#abcdef"));
}else
a)
v.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Arrays.fill(ClickItem, false);
ClickItem[pos]=true;
adapter1.notifyDataSetChanged();
}
});

Custom Spinner not hiding the dropdown menu after selection

(Android API version 9)I created a spinner with a custom adapter and overrided getView() to inflate it with my xml file which has a text view. But now, my spinner is not closing the dropdown list after user selects an item. Is there anyway to close the spinner dropdown upon selecting an item?
Code
//Code in onCreate function
Spinner list = (Spinner) findViewById(R.id.spn_purchaseList);
listAdapter = new ItemListAdapter(this, new MyItemList());
list.setAdapter(listAdapter);
listAdapter.item_list.addItem(new MyItem("Test", "Test Item"));
listAdapter.notifyDataSetChanged();
//onCreate end
//the class below is inside "MainActivity extends Activity"
class ItemListAdapter extends BaseAdapter
{
Context context;
MyItemList item_list;
MyItem selectedItem;
ItemListAdapter(Context con,MyItemList k)
{
super();
this.context=con;
this.item_list=k;
selectedItem=null;
}
#Override
public int getCount() {
return item_list.getCount();
}
#Override
public MyItem getItem(int arg0) {
return this.item_list.getList().get(arg0);
}
#Override
public long getItemId(int arg0) {
return this.item_list.getPosition(this.item_list.getList().get(arg0));
}
#Override
public View getView(int position, View arg1, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View spinner_item = inflater.inflate(R.layout.spinner_layout, parent, false);
TextView tx = (TextView)spinner_item.findViewById(R.id.txt_spinner);
tx.setId((int) item_list.getPosition(item_list.getList().get(position)));
tx.setText(this.item_list.getList().get(position).name.toString());
tx.setBackgroundResource(R.drawable.spinner_item);
tx.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
selectedItem = item_list.getItem(v.getId());
list.setSelection(v.getId());
}
});
return spinner_item;
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
{
return getView(position,convertView,parent);
}
}
Calling setVisibility(View.GONE) works to hide the dropdown but it seems to cause issues with the Spinner state, i.e. you will be unable to reopen the dropdown after it has been closed.
The preferred way is to get a handle to the Spinner and call its onDetachedFromWindow() from your onClick() listener.
#Override
public void onClick(View v) {
// code here to get selected item and do something with it
// hide the spinner dropdown
Spinner spinner = (Spinner) findViewById(R.id.mySpinner);
if (spinner != null) {
try {
Method method = Spinner.class.getDeclaredMethod("onDetachedFromWindow");
method.setAccessible(true);
method.invoke(spinner);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Too late, but for my case I had a custom layout for spinner items. The clickable="true" or adding onClickListeners, onItemSelectedListeners didn't work because I was adding them to root layout.
When I changed my code as following, I added android:background="?attr/selectableItemBackground" to child of parent layout, and set OnItemSelectedListener() on Spinner, and it worked. Spinner dialog or dropdown hides when item is tapped.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground">
<!-- your custom spinner item view -->
</LinearLayout>
</LinearLayout>
just call spinner.dismissDropDown() method of spinner, inside on item click of spinner. Your problem will be solved.

Android: ListView add gradient for each row

I want to add gradient for each row. What I've tried to do is on listview label put this attribut, android:background="#drawable/mygradient". But All I get is this gradient displayed all along the list. What I want is this gradient to be displayed for each item.
Thanks in advance
You can provide a custom adapter for your list view and set the gradient as the background in its getView method. Something like this:
public class MyAdapter extends BaseAdapter {
List<MyDataType> myData;
Context context;
public MyAdaptor(Context ctx, List<MyDataType> myData) {
this.myData = myData;
this.context = ctx;
}
public int getCount() { return myData.size() };
public Object getItem(int pos) { return myData.get(pos); }
public long getItemId(int pos) { return 0L; }
public View getView(int pos, View convertView, ViewGroup parent) {
//this is where you can customise the display of an individual
//list item by setting its background, etc, etc.
...
//and return the view for the list item at the end
return <List item view>;
}
}
Then you can set this adapter as the adapter for your list:
ListView myList = <initialise it here>
myList.setAdapter(new MyAdapter(getContext(), listData);
Now whenever a list item needs to be displayed, getView method will be called, where you'll perform all the necessary display customisation, including setting the background.
Instead of setting background property set Selector of list, as below:
android:listSelector="#drawable/list_selector_background"

Categories

Resources