Zhen Liu
Zhen Liu

Reputation: 7932

Pass touch events from parent view to a specific subview

I have a android view layout structure as follows

<RelativeLayout>
 <TextView>
 <EditText>
</RelativeLayout>

The EditText, due to my limited layout skills, is sized as wrap_content and is significantly smaller than the parent RelativeLayout.

But when user touches on RelativeLayout, I would like the UI to effectively behave as if user just focused on the EditText instead.

It doesn't matter to me if cursor starts in the front, middle, or end. (Preferably at the end).

Is this something that i can achieve in the code or on the layout?

Thanks!

Upvotes: 1

Views: 194

Answers (1)

leonardkraemer
leonardkraemer

Reputation: 6793

What you can do is to call the editText's onClick in the onClick method of the relative layout. This way all clicks on the relativeLayout go to the editText.

This should work see answer:

relativeLayout.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
        //needed for some older devices.
        InputMethodManager inputMethodManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
    }
});

I would do this programatically (in code), because it does not handle how the views look, but how they behave. Therefore, say in MVC, it belongs in the controller.

Upvotes: 1

Related Questions