I have a LinearLayout
that I've styled to look like a button
, and it contains a few text/ImageView elements. I would like to make the whole LinearLayout
act like a button
, in particular to give it states that are defined in a so it has a different background when it is pressed.
Is there a better way than making an ImageButton
the size of the whole Layout and positioning absolutely?
To make a View clickable so that users can tap (or click) it, add the android:onClick attribute in the XML layout and specify the click handler. For example, you can make an ImageView act like a simple Button by adding android:onClick to the ImageView . In this task you make the images in your layout clickable.
Just add layout_weight="1" to in your linearLayout which having Buttons. adding anroid:layout_weight="1" does not move the linear layout (and buttons) down to the bottom...
If in linearlayout your orientation vertical, you can put the textview in the "horizontal center" by android:layout_gravity="center" . For centering textview vertically you need to set layout_height of textview to match_parent and set android:gravity to "center".
If you want add the Android default background behavior to make a Layout
acts like a "clikable" View
, set on the targeted Layout
:
android:background="?android:attr/selectableItemBackground"
android:background="?attr/selectableItemBackground"
android:background="@android:drawable/list_selector_background"
Answers above still true but didn't help me for just add the default pressed and released UI state (like in a ListView
for instance).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With