When an Android item list item is clicked, the child item items (like a button) get focus
I have a list. Every item in the listview is customized. Each element has
text view1
text view2 button1
text view1
text view2 button1
text view1
text view2 button1
when the button is pressed, the items are focused. I dont want to get the button focus even though I gave the following button attributes in every viewlist layout android: focusable = "false" android: focusableInTouchMode = "false" Anyone please help me this
source to share
public class DontPressWithParentButton extends Button {
public DontPressWithParentButton(Context context) {
super(context);
}
public DontPressWithParentButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DontPressWithParentButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void setPressed(boolean pressed) {
if (pressed && getParent() instanceof View && ((View) getParent()).isPressed()) {
return;
}
super.setPressed(pressed);
}
}
Add the following item to your layout file
<DontPressWithParentButton />
Hope this works great without any downsides.
source to share
Until some days I have the same problem. But now I have solved it.
Please do the following.
Make one class called DontPressWithParentButton that extends the button as shown below:
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
public class DontPressWithParentButton extends Button {
public DontPressWithParentButton(Context context) {
super(context);
}
public DontPressWithParentButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DontPressWithParentButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void setPressed(boolean pressed) {
if (pressed && getParent() instanceof View && ((View) getParent()).isPressed()) {
return;
}
super.setPressed(pressed);
}
}
Now, in your xml line, define the Button as shown below (add your package instead of YOUR_APP_PACKAGE)
<YOUR_APP_PACKAGE.DontPressWithParentButton android:id="@+id/deleteBtn" android:layout_width="100dp"
android:layout_height="wrap_content" android:layout_alignParentRight="true"
android:textColor="#FFFFFF"
android:text="Approve"
android:focusable="false"
android:focusableInTouchMode="false"
/>
Now provide the id in your java file as shown below:
DontPressWithParentButton deleteGameBtn=(DontPressWithParentButton) findViewById(R.id.deleteGameBtn);
Now run the project. You will get what you want.
please comment if you have any request.
source to share
Add android:descendantFocusability="blocksDescendants"
so the child view (row) can be locked.
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants" />
And if you want to vise wersa than add focusable and focusableInTouchMode to false
in imageButton/Button
to item.xml
source to share