1

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!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Zhen Liu
  • 7,550
  • 14
  • 53
  • 96

1 Answers1

1

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.

leonardkraemer
  • 6,573
  • 1
  • 31
  • 54
  • Sounds good to me! How do I invoke onClick of children? Do you mind writing a simple example couple lines of code and i will accept as answer! – Zhen Liu Nov 21 '17 at 15:43
  • There you go, turns out EditText is a bit more complicated, but nothing too special. – leonardkraemer Nov 21 '17 at 16:04