user3352926
user3352926

Reputation: 1047

Round buttons with icon in center and text under icon

I'm trying to do this:

enter image description here

I've tried to make customized square button:

    public class SquareButton extends Button {

    public SquareButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public SquareButton(Context context) {
        super(context);
    }

    @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int height = getMeasuredHeight();

        setMeasuredDimension(height, height);      
    }

}

and use it with icon and text under image, but without success. Button's background is a shape with gray stroke and white background color. Also there is an icon and string-text.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f5f5f5"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainMenuActivity" >

    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" >

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

            <views.SquareButton
                android:id="@+id/SquareButton01"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="@drawable/main_menu_button"
                android:drawableTop="@drawable/statistics_icon"
                android:gravity="center_horizontal"
                android:text="Button"
                android:textAlignment="center" />

            <views.SquareButton
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="@drawable/main_menu_button"
                android:drawableTop="@drawable/statistics_icon"
                android:gravity="center_horizontal"
                android:text="Button"
                android:textAlignment="center" />

        </TableRow>

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

            <views.SquareButton
                android:id="@+id/SquareButton03"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="@drawable/main_menu_button"
                android:drawableTop="@drawable/statistics_icon"
                android:gravity="center_horizontal"
                android:text="Button"
                android:textAlignment="center" />

            <views.SquareButton
                android:id="@+id/SquareButton02"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="@drawable/main_menu_button"
                android:drawableTop="@drawable/statistics_icon"
                android:gravity="center_horizontal"
                android:text="Button"
                android:textAlignment="center" />

        </TableRow>

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

            <views.SquareButton
                android:id="@+id/SquareButton05"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="@drawable/main_menu_button"
                android:drawableTop="@drawable/statistics_icon"
                android:gravity="center_horizontal"
                android:text="Button"
                android:textAlignment="center" />

            <views.SquareButton
                android:id="@+id/SquareButton04"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="@drawable/main_menu_button"
                android:drawableTop="@drawable/statistics_icon"
                android:gravity="center_horizontal"
                android:text="Button"
                android:textAlignment="center" />

        </TableRow>
    </TableLayout>    
</LinearLayout>

Please, help. I've "googled" a lot of topics to find a solution of such problem, but not found yet. (((

Upvotes: 0

Views: 4566

Answers (4)

C.F.G
C.F.G

Reputation: 1463

I use TextView inside of constraintLayout and that constraintLayout together ImageView inside of a FrameLayout to unify all these stuffs. Using this method you can control text position using app:layout_constraintVertical_bias="0.9"

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/RelativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical">


    <TableLayout
        android:id="@+id/tableLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:weightSum="2">
    
        <!-- without Text-->

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

            <ImageButton
                android:id="@+id/imageButton1"
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:scaleType="fitCenter"
                android:src="@drawable/ic_gift_box_giving" />

            <ImageButton
                android:id="@+id/imageButton2"
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:scaleType="fitCenter"
                android:src="@drawable/ic_money_bag_riyal" />
        </TableRow>


        <!-- with Text-->

        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:weightSum="2">

        <FrameLayout
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_gravity="center"
            android:layout_margin="4dp"
            android:layout_weight="1">

            <androidx.appcompat.widget.AppCompatImageButton
                android:id="@+id/imageButton6"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingHorizontal="30dp"
                android:scaleType="fitCenter"
                android:src="@drawable/ic_password_lock" />

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <androidx.appcompat.widget.AppCompatTextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_horizontal|bottom"
                    android:text="LOCKED!"
                    android:textSize="20sp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintVertical_bias="0.9" />
            </androidx.constraintlayout.widget.ConstraintLayout>
        </FrameLayout>

        <FrameLayout
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_gravity="center"
            android:layout_margin="4dp"
            android:layout_weight="1">

            <androidx.appcompat.widget.AppCompatImageButton
                android:id="@+id/imageButton6"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingHorizontal="30dp"
                android:scaleType="fitCenter"
                android:src="@drawable/ic_password_lock" />

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <androidx.appcompat.widget.AppCompatTextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_horizontal|bottom"
                    android:text="LOCKED!"
                    android:textSize="20sp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintVertical_bias="0.9" />
            </androidx.constraintlayout.widget.ConstraintLayout>
        </FrameLayout>

        </TableRow>

    </TableLayout>

</RelativeLayout>

Upvotes: 0

Hareshkumar Chhelana
Hareshkumar Chhelana

Reputation: 24848

// try this way 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="5dp"
        android:background="@android:color/holo_blue_dark">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/white"
            android:text="Main menu"/>
        </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="50dp"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="10dp"
        android:gravity="center">
    <Button
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:drawableTop="@drawable/ic_launcher"
        android:drawablePadding="5dp"
        android:text="Start learning"
        android:textColor="@android:color/holo_blue_dark"
        android:padding="5dp"
        android:gravity="center"
        android:background="@android:color/white"/>

    <Button
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:drawableTop="@drawable/ic_launcher"
        android:drawablePadding="5dp"
        android:text="Add card"
        android:layout_marginLeft="10dp"
        android:textColor="@android:color/holo_blue_dark"
        android:padding="5dp"
        android:gravity="center"
        android:background="@android:color/white"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="50dp"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="10dp"
        android:gravity="center">
        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:drawableTop="@drawable/ic_launcher"
            android:drawablePadding="5dp"
            android:text="Import words"
            android:textColor="@android:color/holo_blue_dark"
            android:padding="5dp"
            android:gravity="center"
            android:background="@android:color/white"/>

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:drawableTop="@drawable/ic_launcher"
            android:drawablePadding="5dp"
            android:text="Statistics"
            android:layout_marginLeft="10dp"
            android:textColor="@android:color/holo_blue_dark"
            android:padding="5dp"
            android:gravity="center"
            android:background="@android:color/white"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="50dp"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="10dp"
        android:gravity="center">
        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:drawableTop="@drawable/ic_launcher"
            android:drawablePadding="5dp"
            android:text="Settings"
            android:textColor="@android:color/holo_blue_dark"
            android:padding="5dp"
            android:gravity="center"
            android:background="@android:color/white"/>

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:drawableTop="@drawable/ic_launcher"
            android:drawablePadding="5dp"
            android:text="Change User"
            android:layout_marginLeft="10dp"
            android:textColor="@android:color/holo_blue_dark"
            android:padding="5dp"
            android:gravity="center"
            android:background="@android:color/white"/>
    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        android:layout_marginTop="20dp"
        android:text="Hello username!"/>
</LinearLayout>

Upvotes: 1

Machinarius
Machinarius

Reputation: 3731

Android allows you to replace the background for any control, just declare a shape with code around the likes of

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid
        android:color="#FFFFFF"/>

    <stroke
        android:width="3px"
        android:color="#EDEDED"/>
</shape>

Set the button's background to that drawable and you are set for the white-with-gray-border button. You might want to wrap that drawable into a selector though, so another background is in place when the user touches the button.

And use the code and explanations in this question to achieve the text-below-image effect you want https://stackoverflow.com/a/4457063/528131

More info on shapes and drawables: http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape

Upvotes: 0

Timothy Miko
Timothy Miko

Reputation: 130

I created a view that is nearly exact to yours on a project I was working on...the solution I came up with was to have all of the images at the same, fixed dimensions. Then just wrapped the images in a TableLayout to achieve the desired result you show in your mockup above.

Upvotes: 0

Related Questions