-2

i want to create full width button in android using xml. here is my code

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@drawable/menu"
    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=".MainActivity" >


    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="180dp"
        android:layout_height="180dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:src="@drawable/manulogo" />
    <Button
    android:id="@+id/button1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:drawableLeft="@android:drawable/btn_star_big_on"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="220dp"
    android:text="Drawable left" 
    />
</RelativeLayout>
Giru Bhai
  • 14,370
  • 5
  • 46
  • 74
Guru
  • 11
  • 1
  • 3

2 Answers2

5

If you want to get rid of 16 dp margin on left and right, you will need to remove the padding properties from main relative layout.

Also, you can't use property orientation with relative layout. That's for Linear layout.

And you should use match_parent instead of fill_parent for better practice:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/menu"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="180dp"
        android:layout_height="180dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:src="@drawable/ic_launcher" />

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="220dp"
        android:drawableLeft="@android:drawable/btn_star_big_on"
        android:text="Drawable left" />

</RelativeLayout>

This modified code works fine.

Output:

enter image description here

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
0

you have do like this

android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"

parent layout have left and right padding so remove it and Button will be full screen

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:background="@drawable/menu"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity" >

     <ImageView
        android:id="@+id/imageView2"
        android:layout_width="180dp"
        android:layout_height="180dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:src="@drawable/manulogo" />
      <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:drawableLeft="@android:drawable/btn_star_big_on"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="220dp"
        android:text="Drawable left" 
      />
</RelativeLayout>
MilapTank
  • 9,988
  • 7
  • 38
  • 53
  • First i have ANSWER this question and user has also accept my answer for what reason he has unaccepted my answer i don't know and now some buddy has down what can you explain why ??? down voted my answer ? – MilapTank Jul 04 '14 at 13:44