How to Set Layout Background in Android UI

I'm new to Android programming. I have a UI with some TextView and Button controls. How do I set a background behind those components? Lets call it background.png.

Answers


in your parent layout element, e.g. linearlayout or whatever, simply add android:background="@drawable/background" This will set the background of your layout, assuming you have the image in a /drawable folder.


First you have to place your background.png image in your res/drawable/ folder.Later you have to set a parent layout for your TextView and Button widgets.I will consider a LinearLayout as a parent layout for you and the code goes like this:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:background="@drawable/background.png" 
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">
 <TextView    android:layout_width="fill_parent"
              android:layout_height="wrap_content" 
              android:text="@string/hello" />
 <Button      android:text="Button" 
              android:id="@+id/button1"
              android:layout_width="wrap_content" 
              android:layout_height="wrap_content"></Button>
</LinearLayout>

*its very simple past a image in res/drawable-ldpi/ folder whatever you want to diaplay and in your xml write this.

< LinearLayout 

    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myview"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="top"
    android:background="@drawable/icon">
< Button/>

< TextView   />


< / LinearLayout>

You can set background color/drawable in xml file or from java file.

  1. Changing background in XML file

    android:background is responsible for setting up background in View. So add this if you want to set background to any View. Eg. in top LinearLayout, RelativeLayout or TextView or Button etc. Depends on your requirements.

    android:background="@drawable/background"

  2. Changing background from Java file

    use setBackgroundResource to set background to any View.

    or simply if you want to change the color of the View you have to use setBackgroundColor


Have a look at : android:background

This is a sample xml of what you might need to build on ,

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:id="@+id/my_view"
    xmlns:android="http://schemas.android.com/apk/res/android"    
    android:layout_width="fill_parent"    
    android:layout_height="fill_parent"    
    android:background="@drawable/background">        
</FrameLayout> 

android:background="@drawable/yourBG" in XML.

yourLayout.setBackgroundResource(resid);

or yourLayout.setBackgroundDrawable(drawble);


first right click on "Drawable" folder in your Project Tab. select new-> image asset and then select your image.

In your layout coding of xml file, set android:background="@Drawable/background.png"


first copy high resolution image that you want to set in background and then past it(Doing right click on Drawable folder).

then set property of layout as below android:background="@drawable/Yourimagefilename"


You can also add the background image programmatically this way:

getWindow().setBackgroundDrawableResource(R.drawable.your_image);

Need Your Help

What does => (equals greater than) mean in Fortran?

pointers fortran variable-assignment fortran90

I'm looking through some old Fortran 90 code and have come across the =&gt; symbol:

exif image rotation issue using carrierwave and rmagick to upload to s3

ruby-on-rails amazon-s3 carrierwave exif rmagick

I've got a photo upload feature in my rails app. The app uploads direct to s3 through carrierwave via rmagick and fog. The issue I am having is when a photo is uploaded via mobile through the "take a