Android: How to Propagate Click Event to Linearlayout Childs and Change Their Drawable

Android: How to propagate click event to LinearLayout childs and change their drawable

Put

android:duplicateParentState="true"

in your ImageView and TextView..then the views get its drawable state (focused, pressed, etc.) from its direct parent rather than from itself.

onClick not triggered on LinearLayout with child

for every child

android:duplicateParentState="true"

Complext button (LinearLayout with child views) with background drawable - on children click doesn't reflect the state_pressed

not sure if it works for your specific implementation, but a very easy way of achieving this "complex" button is by making a normal button and using android:drawableLeft or android:drawableRight or android:drawableTop or android:drawableBottom and android:drawablePaddig to achieve the same visual result in just one view.

for example:

<LinearLayout orientation=vertical>
<ImageView/>
<TextView/>
</LinearLayout>

is pretty much the same as

<Button
drawableLeft="@drawable/..."
text="..."
/>

that way your whole layout is simpler and the pressed states works out-of-the-box.

How to let click event pass to container in android?

You could also set android:clickable="false" to the TextView/ImageView layouts and android:clickable="true" on the background, that way the background view always catches the clicks.

Additionally, the following answer might be helpful for people considering this issue:

SO: Android: How to propagate click event to LinearLayout childs and change their drawable

Android : detect click event at empty space of gridview inside linearlayout

Since the parent LinearLayout can be assigned its own OnClickListener, the issue is only how to detect a click within the GridView that occurs outside of its child Views. You can subclass GridView and override the dispatchTouchEvent() method to accomplish this. Using the pointToPosition() method, we can determine if a touch event occurs outside of the child Views, and use an interface to notify a listener if it is. In the following example, the OnNoItemClickListener interface provides that functionality.

public class TouchyGridView extends GridView
{
// Depending on how you're creating this View,
// you might need to specify additional constructors.
public TouchyGridView(Context context, AttributeSet attrs)
{
super(context, attrs);
}

private OnNoItemClickListener listener;
public interface OnNoItemClickListener
{
public void onNoItemClick();
}

public void setOnNoItemClickListener(OnNoItemClickListener listener)
{
this.listener = listener;
}

@Override
public boolean dispatchTouchEvent(MotionEvent event)
{
// The pointToPosition() method returns -1 if the touch event
// occurs outside of a child View.
// Change the MotionEvent action as needed. Here we use ACTION_DOWN
// as a simple, naive indication of a click.
if (pointToPosition((int) event.getX(), (int) event.getY()) == -1
&& event.getAction() == MotionEvent.ACTION_DOWN)
{
if (listener != null)
{
listener.onNoItemClick();
}
}
return super.dispatchTouchEvent(event);
}
}

Add View programmatically and after adding on button click change view background

when you are adding imageView into linear layout, at that time you are setImageBitmap to imageView.

if you want to reset Image to Imageview, you should use img1.setImageResource

    btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

//use anyone as your requirement
img1.setBackgroundResource(); // for set background resource from drawable folder(for src)
img1.setBackground();// for set background(Drawable)(for background)
img1.setBackgroundColor(); //for set background color(for background)

}
});


Related Topics



Leave a reply



Submit