Nick
Nick

Reputation: 31

Add background image while remain RelativeLayout

This is my *activity_login.xml*

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:useDefaultMargins="true"
tools:ignore="ExtraText" >

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/userid"
    android:layout_centerHorizontal="true"
    android:text="@string/userid"
    android:textSize="20sp" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/password"
    android:layout_centerHorizontal="true"
    android:text="@string/password"
    android:textSize="20sp" />

<EditText
    android:id="@+id/userid"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView3"
    android:layout_alignRight="@+id/password"
    android:ems="10"
    android:inputType="textPersonName" />

<Button
    android:id="@+id/login"
    android:layout_width="100dp"
    android:layout_height="50dp"
    android:layout_above="@+id/newuser"
    android:layout_alignRight="@+id/textView3"
    android:layout_marginBottom="27dp"
    android:gravity="center"
    android:text="@string/button_login" />

<EditText
    android:id="@+id/password"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/login"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="25dp"
    android:ems="10"
    android:inputType="textPassword" />

<Button
    android:id="@+id/register"
    android:layout_width="100dp"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="42dp"
    android:gravity="center"
    android:text="@string/button_register" />

<TextView
    android:id="@+id/newuser"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/register"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="14dp"
    android:text="@string/newuser" />

</RelativeLayout>

I want pos_malaysia.png to be set as my login page background on the top center on the screen.. I saw some of them say use <bitmap> .

The thing is when I replace RelativeLayout with bitmap all the android:layout_above="@+id/userid" and android:layout_centerHorizontal="true" cannot be used since its only applies for RelativeLayout. what should I do to maintain the layout with added background?

Upvotes: 0

Views: 246

Answers (5)

Unii
Unii

Reputation: 1617

if you would like to add background use

android:background="@drawable/image_name"

(provided that the image you are planning to load is in the Project resources.)

in widget or RelativeLayout if you would like to maintain its size create syle or just create imageView

Upvotes: 0

sijeesh
sijeesh

Reputation: 298

try this:

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/RelativeLayout1"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:useDefaultMargins="true"
 tools:ignore="ExtraText" >

<ImageView
android:id="@+id/imgv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/*****YOUR IMAGE NAME****"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>

</RelativeLayout>

Upvotes: 0

Klawikowski
Klawikowski

Reputation: 615

There are 2 possible solutions for it.

1st add android:background="@drawable/[PIC]" to your Relative Layout.

2nd add ImageView as 1st layer (on bottom) of Relative Layout.

Depends on what kind of background (logo, image) you should consider using 9patch, so it will nicely scale.

Upvotes: 1

AndroidHacker
AndroidHacker

Reputation: 3596

Try this. You can same in linear layout as well

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:useDefaultMargins="true"
tools:ignore="ExtraText"
android:background="@drwable/your_bg_image"
 >

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/userid"
    android:layout_centerHorizontal="true"
    android:text="@string/userid"
    android:textSize="20sp" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/password"
    android:layout_centerHorizontal="true"
    android:text="@string/password"
    android:textSize="20sp" />

<EditText
    android:id="@+id/userid"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView3"
    android:layout_alignRight="@+id/password"
    android:ems="10"
    android:inputType="textPersonName" />

<Button
    android:id="@+id/login"
    android:layout_width="100dp"
    android:layout_height="50dp"
    android:layout_above="@+id/newuser"
    android:layout_alignRight="@+id/textView3"
    android:layout_marginBottom="27dp"
    android:gravity="center"
    android:text="@string/button_login" />

<EditText
    android:id="@+id/password"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/login"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="25dp"
    android:ems="10"
    android:inputType="textPassword" />

<Button
    android:id="@+id/register"
    android:layout_width="100dp"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="42dp"
    android:gravity="center"
    android:text="@string/button_register" />

<TextView
    android:id="@+id/newuser"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/register"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="14dp"
    android:text="@string/newuser" />

</RelativeLayout>

Upvotes: 0

Moberg
Moberg

Reputation: 5469

Try

<RelativeLayout>
    <ImageView
        android:layout_centerHorizontal="true"
        android:layout_alignParentTop="true">
    </ImageView>

</RelativeLayout>

Upvotes: 0

Related Questions