0

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. (((

donfuxx
  • 11,059
  • 6
  • 43
  • 74
user3352926
  • 897
  • 1
  • 9
  • 15
  • "without success" can you explain what happended? Your approach does not look so wrong after all. – Henry Mar 03 '14 at 18:13
  • http://i016.radikal.ru/1403/cf/8219e3653fca.jpg - that is the result. As you see - textView is "hiding" on small dimension screens. And, how can I change the size of icon? – user3352926 Mar 03 '14 at 18:59

3 Answers3

1
// 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>
Haresh Chhelana
  • 24,394
  • 5
  • 55
  • 67
0

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.

Timothy Miko
  • 130
  • 1
  • 10
0

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

Community
  • 1
  • 1
Machinarius
  • 3,467
  • 1
  • 27
  • 50