Manage ClickListener of button in ListView - android

I have a custom ArrayAdapter to show items in a ListView. It has a Button and a TextView, I want that when the button gets pressed, change the text view's color to red, but if I click the button of another item of the list, besides of setting the textview of the item to red, it will set the textview's color of the rest of the items, to their original color.
It would be different if I were using android.R.layout.simple_list_item_1 as the layout of the ListView's childs. I would only have to override the setOnItemClickListener of my list like this:
myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView tv1 = (TextView)view;
if(tv1.getCurrentTextColor() != Color.RED){
for(int i = 0; i < myList.getChildCount(); i++){
TextView tv2 = (TextView)myList.getChildAt(i);
tv2.setTextColor(tv1.getCurrentTextColor());
}
tv1.setTextColor(Color.RED);
}
}
});
But since I'm using a custom layout(also a custom adapter), I do not know how and where should I manage this, in the OnItemClick() method or in the getView() method of my custom adapter.

Related

Getting views of a custom view in android

Well I have a listview with an OnItemClickListener on it, each item of listview is a customview with two textviews, I want to get text of first textview when an item is clicked, So how can I do that? when an item is clicked I can just get the view that is clicked but how can I get textview on this customview?
Traverse that view's hierarchy:
TextView textView = (TextView) clickedView.findViewById(R.id.first_text_view);
clickedView is provided to you by OnItemClickListener.
yourListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
textFromFirstView = yourArray.get(position).getTextFromFirst();
}
});
whre getTextFromFirst() is getter for your textview for example

Custom Listview - determine if the image is clicked?

I have a custom ListView which has an ImageView, Textview and CheckBox.
In the activity I can register if the ListView Item is pressed by the following code, it they press an item anywhere in the ListView item it just checks a CheckBox and my adapter is updated.
lstView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long row) {
//code
}
But is there a way to determine if the ImageView was pressed? So if they press the image I can do something else?

Enable next row in ListView via checkbox in current row

I have a ListView with a TextView and a Checkbox on every row. I'm trying to create a sequence where the user has to click on the Checkbox of the first row to enable the next row and remove a grayed out effect. Clicking on the Checkbox in this row will do the same to the next row and so on.
Is this possible? and how?
Create a custom Object to keep the state of your row. For example:
class Row {
private TextView text;
private CheckBox box;
private boolean grayedOut;
//implementation...
}
Inside your Activity you make an ArrayList<Row>() and give it as source to the Adapter of your ListView.
onCreate(..){
mArrayList = new ArrayList<Row>();
// fill arraylist
Adapter mAdapter = new CustomAdapter(... , ... , mArrayList);
ListView lv = findView..
lv.setAdapter(mAdapter);
}
The Adapter for your ListView is custom. You can follow the example found here. In your case some adjustments will be as followed:
public class CustomAdapter extends ArrayAdapter<Row>{
public CustomAdapter(...) {
// do stuff
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// ....
if (item at position has gray state){
set layout to gray
} else {
set layout to active
}
// ....
return row;
}
Now we have a Listview, a CustomAdapter and for each item in the list we have an Row which provides a certain state. Default all the states will be set to gray-state except the first item. Using the onItemClickedListener, we can unlock other items..
lv.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View v, int position, Long id){
// do stuff...
mArrayList.get(position+1).setGrayedOut(false);
}
}
Good luck!

Change the selected item font size in listview android

In my list view inflator i have an image and a textview. When the user selects the any item in the listview, I want to increase the font size of the text in the textview. I am using a custom adapter. Can I do this in getview method ? And also is it possible to do this without calling notifydatasetchanged() always ?
You can use setTag() and getTag().
setTag() in your adapter's getView()
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView= getLayoutInflater().inflate(R.layout.layoutfile, null);
TextView mTextViewNumber= (TextView) convertView.findViewById(R.id.TvId);
ImageView mImageView= (ImageView) convertView.findViewById(R.id.ImgId)
mTextViewNumber.setText("Hi hello");
mImageView.setBackgroundResource(R.x.xxxx);
convertView.setTag(mTextViewNumber, R.id.TvId);
convertView.setTag(mImageView, R.id.ImgId);
return convertView;
}
Change the Textsize by getTag() OnItemClickListener().
mListView=(ListView) findViewById(R.id.list_call_log_listview);
mListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2,
long arg3) {
TextView mTextView= (TextView) view.getTag(R.id.TvId);
mTextView.setTextSize(20);
ImageView mImageView= (ImageView) view.getTag(R.id.ImgId);
mImageView.setBackgroundResource(R.x.xxxx);
}
});
I think you can use this in your onItemClick method it's rather simple solution
View v=myListView.getChildAt(position);
TextView tv=(TextView)v.findViewById(R.id.myTextView);
tv.setTextSize(...);
in adapter in getView function you can check the current item is selected item then can change the font and color in that if that is ture ...
or can use selector
how to change font color in selected/focused ListView items?

Android Listview simpleadapter onClick, how to find which view was clickon?

I have a list view that is built using a simpleAdapter, each row is defined by a Layout containing a TableRow and a few TextView.
What should I do to know on which TextView (in the layout) was clicked on?
I am able to get the listview row (position) if I use a OnItemClickListener on the Listview, however it dosen't tell me on which TextView on that row was clicked on.
I would like to take different action depending on which TextView a user click on in a listview
listview
-----------------------
TextView1 : TextView2 | <--Row1
-----------------------
TextView1 : TextView2 | <--Row2
-----------------------
TextView1 | TextView2 | <--Row3
-----------------------
I would like to be able to tell, that row 2's TextView2 was click on.
Hopefully I am explaining myself well enough.
Thanks you
you can use below code in your List Activity
setListAdapter(new ArrayAdapter<String>(this, R.layout.your name, String Name));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
}
If the click is being handled by onItemClick, then you cannot know from this which View WITHIN that View was clicked. An OnItemClickListener just handles clicking a list item. What you might try is using a custom adapter in which you grab hold of all the Views in the row in getView() and set onClickListeners on them there. Or you can define the onClickListeners directly in your View class if possible.
you can handle the click of different view's in Adapter.The Adapter must be customized
I needed to do something similar to set a tag on elements within the list view, so I could find which button was clicked
myAdapter = new SimpleAdapter(this, myArrayList, R.layout.list_item,
new String[] {"ItemID", "ItemText", "ItemImage"},
new int[] {R.id.item_id, R.id.item_text, R.id.item_image})
{
#Override
public View getView (int row, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
view.findViewById(R.id.item_id).setTag(row);
view.findViewById(R.id.item_text).setTag(row);
view.findViewById(R.id.item_image).setTag(row);
return view;
}
};
I added an onClick event for each item in the layout
<TextView
...
android:clickable="true"
android:onClick="myClickEvent"
...
/>
And then in my onClick events I had something like
public void myClickEvent(View v) {
Object o = v.getTag();
if (o instanceof Integer) {
int row = (Integer) o;
// do something
}
You can either use one onClick event per item in the view to determine which item was clicked, or you can have one onClick event for all and modify the tag to contain both the item and the row.

Categories

Resources