Darksymphony
Darksymphony

Reputation: 2703

Android ImageButton and TextView index position in Framelayout

I have a row of 3 ImageButtons wrapped in LinearLayout.

Then each ImageButton is encapsulated in a FrameLayout:

    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1">

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_margin="5dp"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:scaleType="centerInside"
        android:src="@drawable/btn1"
        android:background="@null"
        android:foreground="?android:attr/selectableItemBackground" />

        <TextView
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:text="1"
            android:textSize="12sp"
            android:textColor="#ffffff"
            android:padding="10dp"
            android:layout_gravity="top|right"
            android:background="@drawable/smallcircle"/>

    </FrameLayout>

On each ImageView I want to have a small number with a circle background on top right. This is working very well, but I am not able to center the text in the circle.

If I have as a text e.g. 15, it seems centered, but when the number is only 1, it is not in the center.

I can't use center in TextView as there is already a layout gravity that moves the number with circle to top right.

Circle background:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

    <size
        android:width="35dp"
        android:height="35dp" />

    <solid android:color="#4CAF50" />

</shape>

Example: enter image description here

Upvotes: 0

Views: 24

Answers (1)

Darksymphony
Darksymphony

Reputation: 2703

Well, the Android Studio did not autosuggested me android:gravity in TextView at all, but when I added it manually, it did the thing. So android:layout_gravity to move to the right position and android:gravity to center the text.

Easy :)

Upvotes: 0

Related Questions