Thursday, March 6, 2014

Background image in Android


In android activity we can give the background image. using the android:background="@drawable/imagename" we can give the image of particular activity.


Steps :-
step1:- crete the new android project.
step2:- copy the image.
step3:- paste it into res/drawable-hdpi folder.
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD step4:- open the main_activity.xml file and write the code        android:background="@drawable/yourimagename".
   Note:-give the image name without extension.


following some screen shots helps you .

Before copy of the image in you project your project structure is:



Copy image in your drive
paste your image into drawable-hdpi folder
 
After pase image file in drawable-hdpi folder your project structure shoud be like this


 in main_activity.xml file coding image

Coding:-
MainActivity.java

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}


2)activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@drawable/background"
    tools:context=".MainActivity" >

</RelativeLayout>




OUTPUT