Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make set Wallpaper in Android device.

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 118
    Comment on it

    Here I have created Set Wallpaper in device method. In the below example I have created two activity and two layouts and added some function code in Android Manifest. Below example will described you how to set Wallpaper in device.

    Step(1)-MainActivity-

    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Button button =(Button) findViewById(R.id.button1);
    
            button.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    startActivity(new Intent("com.example.backgroundapp.bakapp"));
                }
            });
        }
    
        @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;
        }
    
    }
    

    Step(2)Create second Activity-

         public class bakapp extends Activity implements OnClickListener {
        ImageView display;
        int tophone;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
    
            // to our activity to cover the whole screen
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    
            setContentView(R.layout.back);
            Button but = (Button) findViewById(R.id.setwallpaper);
            tophone = R.drawable.image4;
            but.setOnClickListener(this);
            display = (ImageView) findViewById(R.id.ivdisplay);
            ImageView image1 = (ImageView) findViewById(R.id.IVimage1);
            ImageView image2 = (ImageView) findViewById(R.id.IVimage2);
            ImageView image3 = (ImageView) findViewById(R.id.IVimage3);
            ImageView image4 = (ImageView) findViewById(R.id.image4);
            ImageView image5 = (ImageView) findViewById(R.id.IVimage5);
    
            image1.setOnClickListener(this);
            image2.setOnClickListener(this);
            image3.setOnClickListener(this);
            image4.setOnClickListener(this);
            image5.setOnClickListener(this);
    
        }
    
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast var;
            switch (v.getId()) {
            case R.id.IVimage1:
                display.setImageResource(R.drawable.image4);
                var = Toast.makeText(bakapp.this, "image changed",
                        Toast.LENGTH_SHORT);
    
                var.show();
    
                tophone = R.drawable.image4;
                break;
            case R.id.IVimage2:
                display.setImageResource(R.drawable.image5);
                var = Toast.makeText(bakapp.this, "image changed",
                        Toast.LENGTH_SHORT);
    
                var.show();
    
                tophone = R.drawable.image5;
                break;
            case R.id.IVimage3:
                display.setImageResource(R.drawable.image6);
                var = Toast.makeText(bakapp.this, "image changed",
                        Toast.LENGTH_SHORT);
    
                var.show();
    
                tophone = R.drawable.image6;
                break;
            case R.id.image4:
                display.setImageResource(R.drawable.image7);
                var = Toast.makeText(bakapp.this, "image changed",
                        Toast.LENGTH_SHORT);
    
                var.show();
    
                tophone = R.drawable.image7;
                break;
            case R.id.IVimage5:
                display.setImageResource(R.drawable.image8);
                var = Toast.makeText(bakapp.this, "image changed",
                        Toast.LENGTH_SHORT);
    
                var.show();
    
                tophone = R.drawable.image8;
                break;
            case R.id.setwallpaper:
                // to set a background we need to use bitmap
                InputStream is = getResources().openRawResource(tophone);
                // to phone is a variable that is updated everytime we click on an
                // ImageView to that imageview resource and by clicking the button
                // we set the phone background to that image.
                Bitmap bm = BitmapFactory.decodeStream(is);
                // decode inputstream is
                try {
                    getApplicationContext().setWallpaper(bm);
                    // to set the wallpaper of the phone background we need to ask
                    // permission from the user so add permission of background from
                    // manifest file
    
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                var = Toast.makeText(bakapp.this, "Wallpaper image changed",
                        Toast.LENGTH_SHORT);
    
                var.show();
    
                break;
            }
        }
    
    }
    

    Step(3)-main.xml layout-

    <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"
        tools:context=".MainActivity" >
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Application to Change background picture of device"
            android:textSize="30dp" />
    
        <Button
            android:id="@+id/button1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="96dp"
            android:text="Click to launch" />
    
    </RelativeLayout>
    

    Step(4)-main2.xml.layout-

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <ImageView
            android:id="@+id/ivdisplay"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:layout_gravity="center"
            android:src="@drawable/image4" />
    "
        <Button
            android:id="@+id/setwallpaper"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="set image background" >
        </Button>
    
        <HorizontalScrollView
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center" >
    
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" 
                android:orientation="horizontal">
    
                <ImageView
                    android:id="@+id/IVimage1"
                    android:layout_width="125dp"
                    android:layout_height="125dp"
                    android:padding="15dp"
                    android:src="@drawable/image4" />
    
                <ImageView
                    android:id="@+id/IVimage2"
                    android:layout_width="125dp"
                    android:layout_height="125dp"
                    android:padding="15dp"
                    android:src="@drawable/image5" />
    
                <ImageView
                    android:id="@+id/IVimage3"
                    android:layout_width="125dp"
                    android:layout_height="125dp"
                    android:padding="15dp"
                    android:src="@drawable/image6" />
    
                <ImageView
                    android:id="@+id/image4"
                    android:layout_width="125dp"
                    android:layout_height="125dp"
                    android:padding="15dp"
                    android:src="@drawable/image7" />
    
                <ImageView
                    android:id="@+id/IVimage5"
                    android:layout_width="125dp"
                    android:layout_height="125dp"
                    android:padding="15dp"
                    android:src="@drawable/image8" />
            </LinearLayout>
        </HorizontalScrollView>
    
    </LinearLayout>
    

    Step(5)Android Manifest.xml-

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.backgroundapp"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="17" />
        <uses-permission android:name="android.permission.SET_WALLPAPER"/>
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.example.backgroundapp.MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity
                android:name="com.example.backgroundapp.bakapp"
                android:label="@string/app_name" 
                android:screenOrientation="portrait">
                <intent-filter>
                    <action android:name="com.example.backgroundapp.bakapp" />
    
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: