Android layout as button with selectable functionality

I’m trying to create a button-like component, with a left-aligned ImageView and then 2 TextViews to the right of the ImageView, stacked one above the other and formatted differently, like the following example:.

 __________________________
|                          |
| |-----|  Bold Main Text  | 
| |Image|                  |
| |-----|  Small Sub Text  |
|__________________________|

I also want the ImageView to change depending on the click state of the outer container, much like a standard button would do with a selectable resource associated with it. So that when I click anywhere in the outer box the image selectable state is changed.

  • Clear firebase persistence after logout
  • Invoke native date picker from web-app on iOS/Android
  • Unit tests with Android Studio and Gradle?
  • Nested directory creator: Phonegap
  • Testing an Android library with Robolectric
  • Android Market multiple APK… How about different CPU architectures?
  • I know I can use a Button, setting the ‘drawableLeft’ property to create a single line of text associated with an Image as a button, but it seems I can only have a single item of text using this strategy.

    Has anyone implemented any UI components like this in the past?

    Thanks!

    Related posts:

    Sending pause to dialer
    getting Error: spawn EACCES while ionic build android in ubuntu 14.04
    ActionBar Tabs with fragments on rotate
    Android Crop Center of Bitmap
    When a Fragment is replaced and put in the back stack (or removed) does it stay in memory?
    java.lang.RuntimeException: Unable to instantiate service .GCMIntentService
  • Android NumberPicker not saving EditText changes
  • Creating a Fragment: constructor vs newInstance()
  • Where are all the standard Android icon resources?
  • How to show exact number of items in RecyclerView?
  • How to programmatically initiate a Google Now voice search?
  • AlertDialog inside onClickListener
  • One Solution collect form web for “Android layout as button with selectable functionality”

    You can add android:duplicateParentState="true" to the ImageView widget. Also you need to make the ImageView‘s parent clickable and focusable.

    The RelativeLayout in the following code will act as a Button:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout:height="match_parent"
        android:orientation="vertical">
    
        <RelativeLayout
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:focusable="true">
    
            <ImageView
                android:id="@+id/image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:duplicateParentState="true"
                android:src="@drawable/icon" />
            <TextView
                android:id="@+id/text1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/image"
                android:layout_alignTop="@+id/image"
                android:layout_alignWithParentIfMissing="true"
                android:duplicateParentState="true" />
            <TextView
                android:id="@+id/text2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"            
                android:layout_toRightOf="@+id/image"
                android:layout_below="@+id/text1"
                android:layout_alignWithParentIfMissing="true"
                android:duplicateParentState="true" />
        </RelativeLayout>
    </LinearLayout>
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.