3

Im beginner in android development. I ve just created a button in main.xml file

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >



  <Button 
    android:text="Click"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

I just give integer value for android: but I got error error: Error: Integer types not allowed

How can give fixed width and height for Button here? and What is the main difference between android:layout_width and android:Width?

Thanks.

rejo
  • 3,352
  • 5
  • 27
  • 34

5 Answers5

13

To create a button with fixed height and width, you can give values in both px or in dp.

giving values in dp is more convenient , bec android automatically scale the height and width in ldpi,mdpi and hdpi devices.

<Button 
    android:text="Click"
    android:layout_width="100dp"
    android:layout_height="50dp"
    />

What is the difference between android:layout_width and android:width

Community
  • 1
  • 1
abbas.aniefa
  • 2,855
  • 21
  • 30
1

try it like this

<Button 
    android:text="Click"
    android:layout_width="200dip"
    android:layout_height="wrap_content"
    />

where dip is density indepedent pixel

vipin
  • 2,851
  • 4
  • 18
  • 32
  • for more on it please refer this http://stackoverflow.com/questions/2025282/difference-of-px-dp-dip-and-sp-in-android – vipin Feb 23 '12 at 06:32
1

You need to give Numeric Values as following,

 <Button 
    android:text="Click"
    android:layout_width="250dp"
    android:layout_height="50dp" />
Lucifer
  • 29,392
  • 25
  • 90
  • 143
1

use integer(25, 40 ...) + type(DP,DIP,PX) :like

android:layout_width="25dp"

Why you want to do it . better use wrap content, or use weight tag, so it will support and will look good on all sizes devices.

AAnkit
  • 27,299
  • 12
  • 60
  • 71
0

You need To give Like this

<Button android:layout_Width="150dp"
    android:layout_Height="50dp"  />
user1213202
  • 1,305
  • 11
  • 23