i have one Fragment in that i defined one GridView,so iam attaching listener for the LinearLayout from GridView Adapter so i click the LinearLayout of the Adapter it should change the background color of selected layout..
First in your girdview adapter class take on variable exp.
int selected;
then make on method in grid view like that
public void selectedPosition(int postion)
{
selected = postion;
}
then you can also put a code in getview method of gridview. Like this
if(position==selected)
{
imageView.setBackgroundColor(Color.WHITE);
}
else
{
imageView.setBackgroundColor(Color.parseColor("#578FFF"));
}
now in onitemclick of grid view you can post like that
adapter.selectcrop(position);
adapter.notifyDataSetChanged();
here is a solution example I will give you step by step i think you can solve your problem using that.
Related
I have a listview. What I've implemented in that listview is that when user clicks a list item a 2 button view is inflated to replace the content of that list item like this:
This works fine but what I want is when I click the second list item the first one should come back to its original layout. Currently, it is like this:
This is my code implemented in onClick method of listview:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView planName = view.findViewById(R.id.planNameText);
TextView planDate = view.findViewById(R.id.planDateText);
ImageView planImage = view.findViewById(R.id.homePlanImageView);
planName.setVisibility(View.INVISIBLE);
planDate.setVisibility(View.INVISIBLE);
planImage.setVisibility(View.INVISIBLE);
RelativeLayout rl_inflate = (RelativeLayout)view.findViewById(R.id.rl_inflate);
View child = getLayoutInflater().inflate(R.layout.inflate, null);
rl_inflate.addView(child);
}
});
Thanks.
Maybe you need to initialice a boolean variable to check if is clicked or not and refresh all the views. I really recommend you to use a RecyclerView and use 2 viewHolders. If you want information about this check this. If you implement a recycler with 2 viewholder it will be easier than the way that you want to implement it, and you can use notifyDataSetChanged to refresh the recycler. Whatever, you will need anyways a boolean to check if is clicked or not.
I have got a RecyclerView item looking like this
I want to achieve that when I click on item the ImageView will get overlay over it and TextView will become bold. I know how to use adapter and where to handle item clicks. I also know how to make overlay or bold text. I only want to know how to make this item selectable to get the behavior I described above. Because I found only tutorials to change background of item when clicked.
Based on this
I only want to know how to make this item selectable to get the behavior I described above.
So basically you need a way to tell the ViewHolder that the current item is selected, such that in onBindViewHolder() the items are rendered as per need.
I can think of this: Make a model of the item youre adding to the RecyclerView. Add a key as boolean isSelected = false in it.
And inside your onBindViewHolder where youre implementing the onClick()interface. do this:
... new OnClickListener({
... onClick(){
// take the item and set the isSelected flag
list.get(position).setIsSelected(true):
notifyDataSetChanged();
// alternatively you can also toggle this flag.
}
});
and while loading inside onBindViewHolder to this:
if (list.get(position).isSelected()) {
// highlight aka set overlay and bold text to view
} else {
// as per recyclerview doc, reset the views.
}
All you need is having a variable to hold the selected index. Then decorating the selected item in onBindViewHolder() method.
int selectedIndex = 0;
...
public void onBindViewHolder(ViewHolder viewHolder, int position) {
if (selectedIndex == position) {
// Do things you want
}
}
I have a customized list-view and each row of the list-view contains a check box and a text field. Clicking on any check-box in any row of the list-view should disable other check-boxes.How to achieve this.
First of all you should use recycler view, it is way better and practical than listview.
To-do that you need to have a custom adapter class which is extended from BaseAdapter.
You need have a List list for your adapter to iterate over. And in your custommodel you need to add new field called private boolean selected'.
Then you need to define onclick listeners for the checkbox in the getview() method of the adapter.
Then in the onclicklistener.onclick method you should get the position of the clicked item. And iterate over all your data and set 'item.selected = false;'
and only set list[clickPosition].selected = true;
Then call notifyDataSetChanged();
I hope it is clear for you, if not post some more data.
First I'd suggest using a RecyclerView, I'll answer the question for a ListView, but really either way the solutions will be similar.
Assuming you're using the ViewHolder pattern in your BaseAdapter. Inside your getView function set a onCheckedChangedListener and inside that create a function that sets an isListChecked boolean inside the class that implements your adapter's Interface for setListChecked, should look something like:
cbListItem.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
myInterface.setListChecked(isChecked);
notifyDatasetChanged();
}
}
);
Inside getView you will also need to check the isListChecked boolean and set that as the CheckBox state
Inside your custom adapter in getView method :
if(your condition==true){
checkBox.setEnabled(false);
} else {
checkBox.setEnabled(true);
}
declare an array list of checkbox.
Inside your custom adapter in getView method :
put checkbox inside an arraylist.
if(your condition==true) {
for(CheckBox c: lsitOfCheckBox){
c.setEnabled(false);
}
}
I have a list view with multiple items, where i need to select and deselect the list items, and also delete the selected items.
So i have looked into the example in the below link but its for android:minSdkVersion="11"
but i am working on minSdkVersion="10".
Link : http://www.androidbegin.com/tutorial/android-delete-multiple-selected-items-listview-tutorial/
And yes we can do with checked text view, check box and radio button, but the requirement is like that i cannot use that.
Is there any other way that we can acheive this?
Make custom list adapter and get the click of that each view and maintain flag in adapter. If flag is true that means item selected otherwise item deselected, according to that you can change item view like disable that particular item or show some check box.
What I did was created an ArrayList that stores all the position of selected items, and toggle the background colors on clicks.
In my Adapter I define:
public ArrayList<Integer> selectedIds = new ArrayList<Integer>();
With the following method :
public void toggleSelected(Integer position)
{
if(selectedIds.contains(position))
{
selectedIds.remove(position);
}
else
{
selectedIds.add(position);
}
}
which addes\removes items from the ArrayList
In my getView method :
if (selectedIds.contains(position)) {
convertView.setSelected(true);
convertView.setPressed(true);
convertView.setBackgroundColor(Color.parseColor("#FF9912"));
}
else
{
convertView.setSelected(false);
convertView.setPressed(false);
convertView.setBackgroundColor(Color.parseColor("#000000"));
}
This checks if the position is storred in the ArrayList. if it does, paint it as selected. if not, the opposite.
all is left is the OnItemClick listener, i added :
((YourAdapter)list.getAdapter()).toggleSelected(new Integer(position));
When YourAdapter is the adapter of your ListView
Hope this helps anyone, as it's a generic answer :)
Thanks to eric.itzhak here : How to change background color of selected items in ListView?
Hi I am using a Listview in my application and I created separate xml for the layout of each row of that ListView. Each row contains two ImageView and one One TextView. I want to get which ImageView is clicked on that row.
You can set OnClickListener in Adapter class itself inside getView() and get the Click Listener working for both the images. As, you haven't posted any code it not feasible to guess.
put the tag with each imageView in getView function of the adapter....
imageView.setTag(Postion);
and get in onclick with view that you received......
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
system.out.println("position"+position)
}});