-1

Is it possible to add multiple TextView inside one ImageButton with colour background ?

The core need is to have a button with the action text on it, and a subtext nearby explaining the action or giving other information related to the action. This subtext can vary from time to time.

Considering this requirement, one solution is to have a normal button and a subtext below, not clickable. But I find it messy. A better approach which I like is, on iOS for instance, to have a clickable UIView containing the action as bold text and the explanation as light text. See the image bellow containing 4 buttons : iOS illustration

How to achieve the same on Android with Java ? The closest I can have is to have an ImageButton bellow a TextView, and it does not sound right.

Is possible to nest TextViews inside an ImageButton ? If not, what is the best alternative ?

NoonanRosenblum
  • 503
  • 6
  • 19
  • Another option on iOS would be to use a grouped table view where you have 1 section (with one row) for each action (i.e. the rows replace your current buttons). Then you can just use the table row selection event. – ghostatron Feb 07 '18 at 21:26
  • why not using a `Viewgroup` like a `LinearLayout` – Bishoy Kamel Feb 07 '18 at 21:37
  • @BishoyAbd indeed, a ```LinearLayout``` with ```clickable=true``` is exactly what I need, thank you. – NoonanRosenblum Feb 07 '18 at 21:41

2 Answers2

1

I hope this may be useful it explains how to position a textView within and in front of a imageView in the XML.

TextView inside of ImageButton/ImageView XML - Android Dev

Obviously make sure each view has a unique id/name which you can assign as shown here on this link

Sorry I cannot explain specifically myself but it has been a while since developing in Java for Android.

Ollie Beumkes
  • 301
  • 2
  • 17
0

I dont know why you want this behaviour but you can make a container for your views and add a click listener to the whole view. you can also use it anywhere.

an example of this would be.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent" 
android:background="@drawable/container_background"
android:layout_height="wrap_content">

<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:layout_weight="0.33"
  />

<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:layout_weight="0.33"
    />

<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:layout_weight="0.33"
  />

add a selector background

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="@color/text_pressed" />
<item android:color="@color/normal" />
</selector>

and the listener

 findViewById(R.id.container).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
Bishoy Kamel
  • 2,327
  • 2
  • 17
  • 29