I'm creating a compose screen for my app. I have a ScrollView
which contains a RelativeView
which in turn contains two things: the EditText
where the user types a message, and an ImageView
whose visibility is toggled on and off depending on whether an image is attached to the status or not. Here's that part of my layout XML.
<!-- @dimen/bigGap = 8dp -->
<ScrollView android:id="@+id/parentScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:layout_marginTop="@dimen/bigGap"
android:layout_marginRight="@dimen/bigGap"
android:layout_marginLeft="@dimen/bigGap"
android:layout_marginBottom="@dimen/bigGap"
android:layout_above="@+id/footer"
android:background="#006400"
> <!-- green background color -->
<RelativeLayout android:id="@+id/parentLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFD700"> <!-- yellow background color -->
<EditText android:id="@+id/postText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#dddddd"
android:inputType="textMultiLine"
android:gravity="top|left"
/> <!-- gray background color -->
<ImageView android:id="@+id/postImage"
android:layout_width="@dimen/thumbnailSize"
android:layout_height="@dimen/thumbnailSize"
android:visibility="gone"
android:layout_below="@id/postText"
/>
</RelativeLayout>
</ScrollView>
Because my EditText
's height is wrap_content
, the whole thing starts off with a single line of gray background (the EditText
) on top of a yellow background (the RelativeLayout
, which fully covers the green background of the ScrollView
). However, I'll later change all the views' backgrounds to white (to make them look like a single component) and it will be counter-intuitive for the user to be able to tap only that single line of EditText
to make the keyboard pop up.
What I want to do is to redirect the click and long click actions of the RelativeLayout
to the click and long click actions of the EditText
, but my code below doesn't work. Help?
final EditText editText = (EditText) findViewById(R.id.postText);
RelativeLayout rl = (RelativeLayout) findViewById(R.id.parentLinearLayout);
rl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Logger.d("onClick invoked!");
editText.performClick();
}
});
rl.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Logger.d("onLongClick invoked!");
return editText.performLongClick();
}
});
My intention here is so that when the RelativeLayout
is clicked, the keyboard pops up (as it does when done to an EditText
) and when long-pressed, display the cut/copy/paste/text selection options (same behavior with EditText
).