I am trying to fire a event when a user click the delete icon in the list , But I could not able to do it.I am using a simple cursor adapter with setviewbinder to display items in the list.In the setviewBinder method I set the textview layout clicklistener to be null that's why, view is not clicked when a user click in the textview.When a user is swipe in the list immediately delete icon is appeared.I need to fire a click event in the deleteIcon.Currently, when a user is clicked the delete icon, the whole view is clickable.I need to achieve when a user is clicked the delete icon, need to fired the event, only in the delete icon.
public boolean setViewValue(final View view, final Cursor data, int columnIndex) {
LinearLayout layoutTaskView=(LinearLayout)view.findViewById(R.id.layoutTaskView),
layoutTaskDelete = (LinearLayout) view.findViewById(R.id.LayoutDeleteTask);
boolean crossed = Boolean.valueOf(data.getString(columnIndex));
if(crossed){
icon.setImageState(new int[] { android.R.attr.state_checked }, true);
text1.setPaintFlags(text1.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
text2.setPaintFlags(text2.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
layoutTaskView.setOnClickListener(null);
}
}
Layout Xml File
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/layoutTaskView"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:padding="15dp" >
<TextView
android:id="#+id/taskTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/taskName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:paddingLeft="10dp"
android:text="TextView" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/LayoutDeleteTask"
android:clickable="false">
<ImageView
android:id="#android:id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:padding="15dp"
android:src="#drawable/checkmark" />
</LinearLayout>
</LinearLayout>
ListView xml
<com.myexample.CrossView
android:id="#+id/crossview"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</com.myexample.CrossView>
Adapter Code
String[] from = new String []
{
DatabaseHelper.COLUMN_CATEGORY,DatabaseHelper.COLUMN_TASKNAME,
DatabaseHelper.COLUMN_CROSSED};
int[] to = new int[] {R.id.taskTime,R.id.taskName, android.R.id.content};
adapter = new SimpleCursorAdapter(context, R.layout.layout_list_item, data, from, to, 0);
adapter.setViewBinder(new CrossBinder());
lv.setAdapter(adapter);
public void onItemClick(AdapterView<?> parent, View v, final int position, final long id) {
ImageView icon = (ImageView)v.findViewById(android.R.id.icon);
icon.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(v.getId() == android.R.id.icon){
}
}
}
You need to use android:descendantFocusability = "afterDescendants" parameter to root layout of list item. In your case, the root LinearLayout.
For more information check this link.
EDIT : remove android:clickable="false" from your LinearLayout containing ImageView. Put android:clickable="true" in ImageView. Moreover, you can use ImageButton instead ImageView.
P.S. I have suggested to use android:descendantFocusability in main parent LinearLayout.
Create custom adapter like that answer Custom adapter for a list of items that have multiple child items?
This answer give tutorial link to create custom dapters for listview.
Related
I'm having trouble with a ListView.
Basically, i have this hidden ListView to be shown on a TextView click.
This works ok.
When I click on a Item of the ListView, i would like to show another Layout(containing a TextView & a FloatingButton).
The problem is that when I show the LinearLayout, I can't click on the ListItem anymore. Any suggestions?
Here's some code:
Layout.xml
<ListView
android:id="#+id/meal_insertion_meals_portions_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/meal_insertion_location"
android:layout_above="#id/meal_insertion_add_container"/>
<LinearLayout
android:id="#id/meal_insertion_add_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="8dp">
<android.support.design.widget.FloatingActionButton
android:id="#+id/meal_insertion_add_meal_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="#drawable/ic_add"
android:tint="#android:color/white"
app:backgroundTint="#color/colorPrimary"/>
<TextView
android:id="#+id/meal_insertion_add_meal_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:gravity="center_vertical|center_horizontal"
android:text="#string/diary.meal.insertion.aliment.add"
android:textColor="#color/colorPrimary"/>
</LinearLayout>
Fragment.java
mealSelector.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (selectorList.getVisibility() == View.VISIBLE) {
selectorList.setVisibility(View.GONE);
mealLocation.setVisibility(View.VISIBLE);
} else {
selectorList.setVisibility(View.VISIBLE);
mealLocation.setVisibility(View.GONE);
}
}
});
selectorList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView t1 = (TextView) view.findViewById(android.R.id.text1);
mealSelector.setText(t1.getText());
selectorList.setVisibility(View.GONE);
mealLocation.setVisibility(View.VISIBLE);
addLayoutContainer.setVisibility(View.VISIBLE);
}
}
);
THIS IS NOT THE RIGHT SOLUTION - Just a workaround:
I just hide the ContainerLayout again when I show the ListView.
This let me clicks on Items again.
AGAIN: Still looking for a better solution.
Try below solutions,
change listview, android:layout_height="wrap_content" to android:layout_height="match_parent", in addition try with removing android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false" from Floating button.
As the subject suggests, setOnItemClickListener doesn't work when setOnLongClickListener is being used, the layout I'm using in the base adapter is below,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:descendantFocusability="blocksDescendants"
android:paddingBottom="20dp"
android:paddingTop="5dp">
<com.mikhaellopez.circularimageview.CircularImageView
android:id="#+id/image1"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="2dp"
android:focusable="false"
app:border_color="#EEEEEE"
app:border_width="4dp"/>
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/contacts_grid_image"
android:layout_centerHorizontal="true"
android:focusable="false"
android:text="Name"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="11dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/name"
android:layout_centerHorizontal="true"
android:focusable="false"
android:text="Mobile number"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="9dp"/>
</RelativeLayout>
and I'm using this,
image.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
ClipData.Item item = new ClipData.Item((CharSequence) v.getTag());
String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
ClipData dragData = new ClipData("test", mimeTypes, item);
MyDragShadowBuilder myShadow = new MyDragShadowBuilder(holder.image);
if (groups.size() > holder.position) {
v.startDrag(dragData, myShadow, null, 0);
return true;
}
});
and this,
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Log.i(TAG, "clicked");
}
});
Now when I tap on the image the item click doesn't work, but when i click on the text views it works, what am i doing wrong?
Also note that when I remove the image.setOnLongClickListener() the whole gridview item becomes clickable again.
This should be the expected behaviour for GridView, because the child view is clickable, even though it is only handling the OnLongClick only.
As a workaround, you can
setOnClickListener for ImageView to perform the same action as you would in setOnItemClickListener
consider using setOnItemLongClickListener, but this would affect the whole RelativeLayout
use onTouchListener, but this would requires more works
I have a GridView in my app with the following item layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#android:drawable/list_selector_background" >
<TextView
android:id="#+id/notizenTitel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Titel"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
<TextView
android:id="#+id/notizenBeschreibung"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Beschreibung" />
</LinearLayout>
Now I want to select an item if the user clicks on it. I tried with this code:
gv.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE);
gv.setOnItemClickListener(this);
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
GridView gv = (GridView) getView().findViewById(R.id.notizenGrid);
gv.setSelection(position);
adapter.notifyDataSetChanged();
}
But now if I click on an item the background is changed to yellow shortly and dissapead than. So the selector is working while I click on an item but not for the selection. Do you have any ideas why?
Add a property in linear layout
android:clickable = "true"
It will help.
If you add andrid:clickable="true", your gridView will not preform click event, it will block all click events.
i tryied to custom my ListView , so i use this item layout 「 list_item.xml」
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/title"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="10dp"
android:textSize="16dip"
android:textColor="#000000"
android:gravity="center_vertical"
android:background="#FFFFFF"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_weight="1">
</TextView>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0"
android:background="#FFFFFF"
android:padding="10dip"
android:gravity="center_vertical">
<ImageView
android:id="#+id/pause"
android:src="#drawable/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="pauseBtn_onClick"
/>
<ImageView
android:id="#+id/play"
android:src="#drawable/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="playBtn_onClick"/>
</RelativeLayout>
</LinearLayout>
just as what you saw, there are 2 images in this layout, what i wanna do is,
when user click any item , the play_icon(the second image in the listview item) , will
disappear.
so i write code as the following :
mp3_listView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View _view, int index , long arg3)
{
String _url = musicList.get(index).get("link").toString();
LinearLayout _container = (LinearLayout)mp3_listView.getChildAt(index);
RelativeLayout _container2 = (RelativeLayout) _container.getChildAt(1);
ImageView img = (ImageView)_container2.getChildAt(1);
img.setVisibility(View.INVISIBLE);
Toast.makeText(tabDigTest.this , "GG : "+_container.findViewById(R.id.play) , Toast.LENGTH_SHORT).show();
}
}
but something strange occurred .... more than one item's img be hidden because of the:
img.setVisibility(View.INVISIBLE);
i don't why because i only set one img to INVISIBLE ......i think i need help O_o
To retrieve the user click, I guess you set up an OnItemClickListener.
ListView lv = (fecth your list vieW)
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//you might want to use 'view' here
}
});
In the onItemClick method, the view parameter is the view you returned in your ListAdapter's getView method. So if say, you have returned a LinearLayout containing three childrens with id ("textview", "imageview1", "imageview2"), you would simply call view.findViewById(R.id.imageview1) to retrieve the first ImageView.
Is it possible to use a OnItemClickListener on a ListView when the Items layout has a clickable/editable widget (RadioButton,EditText, or CheckBox)?
You might want to take a look at this issue. Having a focusable item in a row of a ListView causes the OnItemClickListener NOT to be invoked. However, that does not mean you cannot have focusable/clickable items in a row, there are some workarounds like this one.
Also, you can take a look at the Call Logs screen. It has a ListView with clickable item(the call icon on the right).
See Source code here
Quoting comment #31 in the link mentioned by Samuh (which solved the problem for me):
In fact you can add it to the layout XML (if inflated by one): android:descendantFocusability="blocksDescendants".
Adding here JIC that webpage is down in the future.
If any row item of list contains focusable or clickable view then OnItemClickListener won't work.
row item must be having param like android:descendantFocusability="blocksDescendants"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:descendantFocusability="blocksDescendants"
android:gravity="center_vertical" >
// your other widgets here
</LinearLayout>
Tried many complex solutions, but this was the simplest one that worked:
Just use android:focusable="false" as in:
<CheckBox
android:id="#+id/fav_check_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false" />
Two best solution
Add android:descendantFocusability="beforeDescendants" to listView
in xml OR
Set given two attributes to false
like
android:focusable="false"
android:focusableInTouchMode="false"
Then it will handle the listView row item child(Button,EditText etc) events instead of listView.setOnItemClick .
I fixed my problem different , in my item I have more than one LinearLayout
so if you give id to your linearayout and setOnclickListener in adapter class it will work, only original effect of touching will dissapear.
but this link Making a LinearLayout act like an Button is usefull to make linearlaout act like button on click
item
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp">
<TextView
android:id="#+id/txt_item_followers_name"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="center|start"
android:paddingLeft="15dp"
android:text="Ali"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="#+id/imageView"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentStart="true"
android:layout_below="#+id/txt_item_followers_name"
android:layout_marginLeft="10dp"
android:src="#drawable/puan_icon" />
<TextView
android:id="#+id/txt_item_followers_mark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView"
android:layout_toEndOf="#+id/imageView"
android:background="#color/red_400"
android:paddingLeft="10dp"
android:text="25.5"
android:textAppearance="?android:attr/textAppearanceSmall" />
<LinearLayout
android:id="#+id/linear_one"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/txt_item_followers_name"
android:background="#color/red_400"
android:orientation="vertical">
<ImageView
android:id="#+id/btn_item_followers_2b_follow"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_marginLeft="10dp"
android:src="#drawable/follow_buton" />
</LinearLayout>
</RelativeLayout>
inside getView method
#Override
public View getView(final int position, View convertView,
ViewGroup parent) {
View view = convertView;
if (convertView == null)
view = inflater.inflate(R.layout.deneme, null);
final Followers2 myObj = myList.get(position);
LinearLayout linear_one = (LinearLayout) view.findViewById(R.id.linear_one); // HERE WE DOMUNİCATE IT
linear_one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(parentActivity, "One Two", Toast.LENGTH_SHORT).show();
}
});
TextView name = (TextView) view.findViewById(R.id.txt_item_followers_name);
TextView mark = (TextView) view.findViewById(R.id.txt_item_followers_mark);
final ImageView btn_follow = (ImageView) view.findViewById(R.id.btn_item_followers_2b_follow);
name.setText(myObj.getName());
mark.setText(myObj.getScore());
/* if (myObj.isFollow() == true) {
btn_follow.setImageResource(R.drawable.following_buton);
} else {
btn_follow.setImageResource(R.drawable.follow_buton);
}
btn_follow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Followers2 myObj = myList.get(position);
if (myObj.isFollow() == true) {
btn_follow.setImageResource(R.drawable.following_buton);
} else {
btn_follow.setImageResource(R.drawable.follow_buton);
}
}
});*/
return view;
}