Breaking News

How to create a Wallpaper App in Android Studio



1 - Copy Paste this code inside MainActivity.class:

package com.android.wallpaperbackgrapp;

import android.app.Activity;
import android.app.WallpaperManager;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends Activity implements View.OnClickListener {

    private int[] backgrounds;

    private ImageView background1;
    private ImageView background2;
    private ImageView background3;
    private ImageView background4;

    private WallpaperManager wallpaperManager;

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

        wallpaperManager = WallpaperManager.getInstance(this);

        background1 = findViewById(R.id.background1);
        background1.setOnClickListener(this);
        background2 = findViewById(R.id.background2);
        background2.setOnClickListener(this);
        background3 = findViewById(R.id.background3);
        background3.setOnClickListener(this);
        background4 = findViewById(R.id.background4);
        background4.setOnClickListener(this);

        loadBackgrounds();
    }

    void loadBackgrounds(){
        backgrounds = new int[4];
        backgrounds[0] = R.drawable.background1;
        backgrounds[1] = R.drawable.background2;
        backgrounds[2] = R.drawable.background3;
        backgrounds[3] = R.drawable.background4;
    }

    void updateBackground(int background){
        try{
            wallpaperManager.setResource(background);
        }catch (Exception e){
            System.err.println(e.getMessage());
        }
    }

    @Override
    public void onClick(View view) {
        if(view == background1){
            updateBackground(backgrounds[0]);
            return;
        }

        if(view == background2){
            updateBackground(backgrounds[1]);
            return;
        }

        if(view == background3){
            updateBackground(backgrounds[2]);
            return;
        }

        if(view == background4){
            updateBackground(backgrounds[3]);
            return;
        }
    }
}

2 - Copy Paste this code inside activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

     <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:orientation="vertical"
         android:layout_margin="10dp"
         android:padding="5dp">

         <LinearLayout
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:orientation="horizontal"
             android:weightSum="2"
             android:layout_weight="1">

             <ImageView
                 android:id="@+id/background1"
                 android:layout_width="0dp"
                 android:layout_weight="1"
                 android:layout_height="match_parent"
                 android:src="@drawable/background1"
                 android:layout_margin="5dp"/>

             <ImageView
                 android:id="@+id/background2"
                 android:layout_width="0dp"
                 android:layout_weight="1"
                 android:layout_height="match_parent"
                 android:src="@drawable/background2"
                 android:layout_margin="5dp"/>


         </LinearLayout>

         <LinearLayout
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:orientation="horizontal"
             android:layout_weight="1">

             <ImageView
                 android:id="@+id/background3"
                 android:layout_width="0dp"
                 android:layout_weight="1"
                 android:layout_height="match_parent"
                 android:src="@drawable/background3"
                 android:layout_margin="5dp"/>

             <ImageView
                 android:id="@+id/background4"
                 android:layout_width="0dp"
                 android:layout_weight="1"
                 android:layout_height="match_parent"
                 android:src="@drawable/background4"
                 android:layout_margin="5dp"/>

         </LinearLayout>

     </LinearLayout>

</LinearLayout>

3 - Copy Paste this code inside AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.wallpaperbackgrapp">

    <uses-permission android:name="android.permission.SET_WALLPAPER"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

4 - Download Images and put inside "drawable" folder:





5 - Click run and Enjoy.

Full Video:

13 comments:

  1. Great Post and very helpful for me Thanks by Hani Studio

    ReplyDelete
  2. Thanks for taking the time to discuss this, I feel strongly that love and read more on this topic. If possible, such as gain knowledge, would you mind updating your blog with additional information? It is very useful for me. XE88

    ReplyDelete
  3. They additionally need to actualize a complete computerized advertising technique to advance the application by focusing on the most pertinent clients. Bulk SMS Service Provider in Sharjah UAE

    ReplyDelete
  4. if i copy and paste this into android studio is there the code for the users to submit a photo?

    ReplyDelete
  5. Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. download prince of persia psp games

    ReplyDelete
  6. I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work. cyberflix

    ReplyDelete
  7. I admire this article for the well-researched content and excellent wording. I got so involved in this material that I couldn’t stop reading. I am impressed with your work and skill. Thank you so much. best android app creator

    ReplyDelete
  8. On the off chance that you are going to video a show, ensure that you are in a physical situation to make your subject look great on this Download Social Media

    ReplyDelete
  9. I like viewing web sites which comprehend the price of delivering the excellent useful resource free of charge. I truly adored reading your posting. Thank you! yabo

    ReplyDelete
  10. That is the reason; it gets basic for engineers fabricate portable applications by focusing on a wide scope of gadgets controlled by various variants of Android. bluestacks download

    ReplyDelete