1

I have setup a TableLayout and added a row to include a button and an ImageView. The ImageView is just a color dot. I have used the style in the past with RelativeLayout and position the ImageView above button image. The new app I am working on uses a TableLayout but when I try to setup button with ImageView it will not allow positioning. Is this option not available for TableLayout?

<TableLayout
    android:id="@+id/TB1"
    android:layout_width="250dp"
    android:layout_height="match_parent"
    android:layout_weight="1" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

        <Button
            android:id="@+id/Scene1"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            android:background="@drawable/custom_scene_buttons"
            android:text="Scene 1"
            android:textColor="@drawable/white"
            android:textSize="20sp" />

        <ImageView
            android:id="@+id/colordot1"
            android:layout_width="21dp"
            android:layout_height="21dp"
            android:background="@drawable/circle" />
    </TableRow>

This is the code to change color dot.

                Resources res = getResources();
        final Drawable drawable = res.getDrawable(R.drawable.circle);
        drawable.setColorFilter(Color.rgb(i, j, k), Mode.SRC_ATOP);
        ImageView img = (ImageView) findViewById(R.id.colordot1);
        img.setBackgroundDrawable(drawable);

Here is the linear layout option I tried

                <LinearLayout android:orientation="vertical" >

            <Button
                android:id="@+id/Scene1"
                android:layout_width="100dp"
                android:layout_height="match_parent"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:layout_weight="1"
                android:background="@drawable/custom_scene_buttons"
                android:drawableRight="@drawable/circle"
                android:text="Scene 1"
                android:textColor="@drawable/white"
                android:textSize="20sp" />

            <ImageView
                android:id="@+id/colordot1"
                android:layout_width="21dp"
                android:layout_height="21dp"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/circle" />
        </LinearLayout>

enter image description here

Bobby
  • 659
  • 2
  • 13
  • 26

1 Answers1

0

EDIT: Including entire TableLayout. I've gotten this to work using drawableRight. You can change the circle programmatically, by grabbing the Button and changing its drawable. Here's a link describing this. Below is a screenshot of the TableLayout, which I've tested.

TableLayout

<TableLayout
    android:id="@+id/TB1"
    android:layout_width="250dp"
    android:layout_height="match_parent"
    android:layout_weight="1" >

<TableRow
    android:id="@+id/tableRow1" >

    <Button
        android:id="@+id/Scene1"
        android:layout_width="50dp"
        android:layout_height="100dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_weight="1"
        android:background="@color/blue"
        android:drawableRight="@drawable/circle"
        android:text="Scene 1"
        android:textColor="@color/white"
        android:textSize="20sp" />

    <Button
        android:id="@+id/Scene2"
        android:layout_width="50dp"
        android:layout_height="100dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_weight="1"
        android:background="@color/blue"
        android:drawableRight="@drawable/circle"
        android:text="Scene 2"
        android:textColor="@color/white"
        android:textSize="20sp" />

</TableRow>

<TableRow
    android:id="@+id/tableRow2" >

    <Button
        android:id="@+id/Scene3"
        android:layout_width="50dp"
        android:layout_height="100dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_weight="1"
        android:background="@color/blue"
        android:drawableRight="@drawable/circle"
        android:text="Scene 3"
        android:textColor="@color/white"
        android:textSize="20sp" />

    <Button
        android:id="@+id/Scene4"
        android:layout_width="50dp"
        android:layout_height="100dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_weight="1"
        android:background="@color/blue"
        android:drawableRight="@drawable/circle"
        android:text="Scene 4"
        android:textColor="@color/white"
        android:textSize="20sp" />

</TableRow>

</TableLayout>

The issue is when using a TableLayout, each TableRow's child is a column. So, your button and ImageView belong to different columns. Do you have other rows in this TableLayout? The width of a column is defined by the view with the greatest width in that column. So, if you have another TableRow and its second child view has a width of 100dp, that will affect the width of your second column, ie your circle ImageView.

Why not use drawableRight attribute instead. Modify the Button as follows and remove your ImageView. You can also modify your drawable padding.

<Button
        android:id="@+id/Scene1"
        android:layout_width="100dp"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_weight="1"
        android:background="@drawable/custom_scene_buttons"
        android:text="Scene 1"
        android:textColor="@drawable/white"
        android:drawableRight="@drawable/circle"
        android:textSize="20sp" />
Community
  • 1
  • 1
Mike Ortiz
  • 4,031
  • 4
  • 27
  • 54
  • Color dot not showing after I adding android:drawableRight="@drawable/circle" I also posted code that I use to change color dot. I have 10 of these on the layout can I cast the drawable to change? – Bobby Jan 04 '14 at 22:17
  • Odd. I think you can alternatively wrap the Button and ImageView into a LinearLayout. Then, that LinearLayout will be a single child, and column, of the TableRow. – Mike Ortiz Jan 04 '14 at 22:22
  • Linear layout would not give me the option to have the dot on top of button only next to it. Thank you for the suggestion. – Bobby Jan 04 '14 at 22:47
  • That's not true. You could set the LinearLayout's orientation to "vertical". Then, place the circle before the button. – Mike Ortiz Jan 04 '14 at 23:00
  • I posted your suggestion but dont see how to get the dot on top (inside button) or to right side of text. – Bobby Jan 04 '14 at 23:08
  • Can you post your entire TableLayout and describe exactly how you want the final layout to look like? Also, can you better describe where you want the circle in relation to the button? On top, below, inside, etc? – Mike Ortiz Jan 04 '14 at 23:27
  • I posted the screen shot. See button F1. The dot was photo shopped. – Bobby Jan 04 '14 at 23:44
  • I could not get the drawright to work. I can use an image and it shows on the button but I cant get it to work by using the shape. I need to use a image that I can control the color RGB. – Bobby Jan 12 '14 at 20:25