Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a LinearLayout act like an Button

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?

like image 462
Jason Prado Avatar asked Apr 05 '11 02:04

Jason Prado


People also ask

How do you make a clickable layout?

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.

How do you set a button at the bottom of a linear layout?

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...

How do you align text in LinearLayout?

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".


1 Answers

If you want add the Android default background behavior to make a Layout acts like a "clikable" View, set on the targeted Layout:

API 11+ (Pure Android):

android:background="?android:attr/selectableItemBackground" 

API 7+ (Android + AppCompat Support Library):

android:background="?attr/selectableItemBackground" 

Any API:

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).

like image 139
FabiF Avatar answered Sep 18 '22 09:09

FabiF