0

I want to align my background image which I declare in the activity_main.xml:

android:background="@drawable/spieler_blau"

The problem is that the whole screen is filled with this picture and I want to have a small white space on the left side of the screen. Like that:

enter image description here

Any solution? Thanks in advance.

Sander
  • 73
  • 2
  • 10

3 Answers3

2

Here is output

You can use android:layout_marginLeft="20dp". You can set margin as per requirement.

Or you can also create one blank View and one ImageView in xml and give it weight.

Example:

1)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/expandable"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000000"
    android:orientation="horizontal" >



    <View  android:layout_weight="1"
         android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        />

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
       android:layout_weight="0.5"
        android:background="@drawable/ic_launcher" />

OR 2)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/expandable"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000000"
    android:orientation="horizontal" >



    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="20dp"
        android:background="@drawable/ic_launcher" />

</LinearLayout>
Hardik Joshi
  • 9,477
  • 12
  • 61
  • 113
  • Thanks. Thats is perfect for me. Again ;). – Sander Sep 05 '13 at 07:15
  • @Sander glad to help you again :) – Hardik Joshi Sep 05 '13 at 07:17
  • At the moment I have a white margin. How can I get it off? I mean on the bottom right and top side. So that the image fills the whole screen. Just the left side has to be white for my other option which I want to fill. – Sander Sep 05 '13 at 07:19
  • Ah, thanks alot I found it. I had android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" in the .xml file and got a margin in it. Anyways - thanks alot Hardy. – Sander Sep 05 '13 at 07:26
1

IF you need space to remain in left side, There possible ways can be,

  1. Set Imageview SRC, not the background , and give padding at left side
  2. Create your background drawable with transparent space on left side
  3. Or you create a custom view
Jaldip Katre
  • 3,216
  • 1
  • 19
  • 14
0

You can do this by adding padding to the left of the image layout with the android:paddingLeft attribute:

<ImageView android:background="@drawable/spieler_blau"
   android:paddingLeft="10dp"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"/>
matthewrdev
  • 11,930
  • 5
  • 52
  • 64