Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to create set wallpaper app in android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 139
    Comment on it

    In the below example I have created set wallpaper app. Here I have added two button and ImageView in activity_main.xml layout. In MainActivity I have used setOnClickListener method , displayWallpaper() and Toast function, and I have also used BitMap class for loding Imgae on backgraund screen. You can see below example code it clearly describe you "How to create set wallpaper app in android”.
    Step(1)activity_main.xml -
      

     <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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:padding="16dp"
        android:orientation="vertical"
        tools:context="com.blogspot.android_er.androidwallpaper.MainActivity">
    
        <Button
            android:id="@+id/display"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_gravity="center"
            android:textColor="#2d53e9"
            android:textAlignment="center"
            android:textSize="14dp"
            android:background="#d8a0e4"
            android:text="Show Wallpaper"/>
        <Button
            android:id="@+id/set"
            android:layout_marginTop="8dp"
            android:textColor="#2d53e9"
            android:textSize="14dp"
            android:background="#e4d6a0"
    
        android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Set Wallpaper"/>
        <ImageView
            android:id="@+id/image"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>


    Step(2)-MainActivity-
     

    public class MainActivity extends AppCompatActivity {
    
    
            ImageView imageWallpaper;
            Button btnLoadWallpaper, btnDisplayWallpaper;
    
            private static final int RQS_OPEN_IMAGE = 1;
    
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
    
                imageWallpaper = (ImageView)findViewById(R.id.image);
    
                btnDisplayWallpaper = (Button)findViewById(R.id.display);
                btnDisplayWallpaper.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        displayWallpaper();
                    }
                });
    
                btnLoadWallpaper = (Button)findViewById(R.id.set);
                btnLoadWallpaper.setOnClickListener(btnLoadWallpaperOnClickListener);
                displayWallpaper();
    
            }
    
            View.OnClickListener btnLoadWallpaperOnClickListener = new View.OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
    
                    if (Build.VERSION.SDK_INT >=
                            Build.VERSION_CODES.KITKAT) {
                        intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
                    } else {
                        intent.setAction(Intent.ACTION_GET_CONTENT);
                    }
    
                    intent.addCategory(Intent.CATEGORY_OPENABLE);
    
                    // set MIME type for image
                    intent.setType("image/*");
    
                    startActivityForResult(intent, RQS_OPEN_IMAGE);
                }
            };
    
            private void displayWallpaper(){
                WallpaperManager myWallpaperManager =
                        WallpaperManager.getInstance(getApplicationContext());
                Drawable drawableWallpaper = myWallpaperManager.getDrawable();
                imageWallpaper.setImageDrawable(drawableWallpaper);
    
                Toast.makeText(this,
                        drawableWallpaper.getMinimumWidth() + " x " + drawableWallpaper.getMinimumHeight()
                                + "\n" +
                                drawableWallpaper.getIntrinsicWidth() + " x " + drawableWallpaper.getIntrinsicHeight(),
                        Toast.LENGTH_LONG).show();
            }
    
            @Override
            protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                if (resultCode == Activity.RESULT_OK) {
                    if (requestCode == RQS_OPEN_IMAGE) {
                        Uri dataUri = data.getData();
                        Uri wallpaperUri = dataUri;
                        Toast.makeText(this, wallpaperUri.toString(), Toast.LENGTH_LONG).show();
    
                        Bitmap newOriginalBM= loadBitmap(dataUri);;
                        reloadWallpaper(newOriginalBM);
                    }
                }
            }
    
            private Bitmap loadBitmap(Uri src) {
    
                Bitmap bm = null;
    
                try {
                    bm = BitmapFactory.decodeStream(
                            getBaseContext().getContentResolver().openInputStream(src));
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
    
                return bm;
            }
    
            private void reloadWallpaper(Bitmap bm){
                if(bm != null){
                    WallpaperManager myWallpaperManager =
                            WallpaperManager.getInstance(getApplicationContext());
    
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        if(myWallpaperManager.isWallpaperSupported()){
                            try {
                                myWallpaperManager.setBitmap(bm);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }else{
                            Toast.makeText(MainActivity.this,
                                    "isWallpaperSupported() NOT SUPPORTED",
                                    Toast.LENGTH_LONG).show();
                        }
                    }else{
                        try {
                            myWallpaperManager.setBitmap(bm);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }else{
                    Toast.makeText(MainActivity.this, "bm == null", Toast.LENGTH_LONG).show();
                }
            }
        }
    
    
    

     

 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: