0

I would like to create buttons with round corners, an image and a text but I have issues in centering text and icon.

This is what I tried so far:

<FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <GridLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:columnCount="2"
        android:rowCount="2"
        android:layout_gravity="center"
        **android:layout_marginLeft="15dp"**>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:id="@+id/btnResult"
            android:layout_gravity="center"
            android:src="@drawable/result"
            android:background="@layout/round_corners"
            android:layout_row="0"
            android:layout_column="0"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="@string/result"
            android:id="@+id/tvAnalysis"
            android:layout_gravity="center_horizontal"
            android:layout_row="0"
            android:layout_column="0"
            **android:layout_marginTop="180dp"**/>

</...

I need to specify the android:layout_marginLeft of the GridLayout to have it centered on my Nexus 5. On all other devices this is not feasable.

Any idea what I need to change to center the imageview/text combination?

Soham
  • 4,397
  • 11
  • 43
  • 71
AntonSack
  • 1,021
  • 2
  • 25
  • 47

2 Answers2

0

For creating round corners for button ,

please create a xml lets called myshape.sml in drawable folder and paste below code

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

    <corners android:radius="5dp" />
    <solid android:color="@color/header"/>

</shape>

If you want to add image to button and text then

  <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
             android:drawableLeft="@drawable/abc_ab_share_pack_mtrl_alpha"                               
            android:text="Test"
            android:textColor="@color/white"
             android:background="@drawable/button_bg"
            android:textSize="14sp />

Please note here android:drawableLeft is property in which you place icons to buttons

Amarjit
  • 4,327
  • 2
  • 34
  • 51
0

Can You Try TextView with DrawableLeft(or as per your need, Right,top,bottom) Instade of Button & Imageview ?

Divyang Panchal
  • 1,889
  • 1
  • 19
  • 27