rejo
rejo

Reputation: 3350

How to give Fixed width and height for Button in Android

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.

Upvotes: 3

Views: 38437

Answers (5)

AAnkit
AAnkit

Reputation: 27549

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.

Upvotes: 1

user1213202
user1213202

Reputation: 1305

You need To give Like this

<Button android:layout_Width="150dp"
    android:layout_Height="50dp"  />

Upvotes: 0

abbas.aniefa
abbas.aniefa

Reputation: 2905

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

Upvotes: 13

Lucifer
Lucifer

Reputation: 29632

You need to give Numeric Values as following,

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

Upvotes: 1

vipin
vipin

Reputation: 3001

try it like this

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

where dip is density indepedent pixel

Upvotes: 1

Related Questions