How to make wallpaper app in android studio 2019
Hey Guys Welcome to my blogger in this article we will discuss about , how to make wallpaper app. A lots of people requested that to make wallpaper app. In this article i will explain each and every thing about wallpaper app so do not miss any step otherwise you will faces a lots of problems.
You can watch a lots of videos on YouTube channels but this article are very unique. So kindly visit my website everyday.
Procedure
- First step is to add few dependencies which are very important after adding dependencies click on syn button.
- Create one layout for showing images on RecyclerView and also create one java class for adapter. In adapter we can access layout and then show images on that layout.
- Create on resource layout for text box background, In this layout we will set corners and also set solid color.
- In main activity we can set recyclerview and also set image view.
- In last step just copy paste the code in java classes and in layouts.
Note!! Classes name must be same otherwise you will faces a lots of problems.
Code
Build.gradle
implementation 'com.android.support:recyclerview-v7:28.0.0'
AndroidManifest
<uses-permission android:name="android.permission.SET_WALLPAPER" > </uses-permission>
CustomAdapter.java
import android.content.Context; import android.content.Intent; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.LinearLayout; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.CustomHolder> { int [] images; Context context; public CustomAdapter(int[] images, Context context) { this.images = images; this.context = context; } @NonNull @Override public CustomHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { LayoutInflater layoutInflater=LayoutInflater.from(context); View view=layoutInflater.inflate(R.layout.images_list,parent,false); return new CustomHolder(view); } @Override public void onBindViewHolder(@NonNull CustomHolder holder, int position) { final int image= images[position]; holder.imageView.setImageResource(image); holder.linearLayout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent=new Intent(context,MainActivity.class); intent.putExtra("image",image); context.startActivity(intent); } }); } @Override public int getItemCount() { return images.length; } public class CustomHolder extends RecyclerView.ViewHolder { ImageView imageView; LinearLayout linearLayout; public CustomHolder(@NonNull View itemView) { super(itemView); imageView=(ImageView) itemView.findViewById(R.id.images); linearLayout=(LinearLayout) itemView.findViewById(R.id.linear); } } }
images_list.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:layout_margin="10dp" android:layout_gravity="bottom" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <LinearLayout android:id="@+id/linear" android:background="@drawable/frame" android:layout_width="140dp" android:gravity="center" android:layout_height="140dp"> <ImageView android:scaleType="fitXY" android:layout_gravity="center" android:src="@drawable/ic_launcher_background" android:id="@+id/images" android:layout_width="100dp" android:layout_height="100dp" /> </LinearLayout> </LinearLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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"> <ImageView android:id="@+id/imageMain" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/image1" android:scaleType="fitXY" /> <LinearLayout android:id="@+id/setLinear" android:visibility="invisible" android:layout_alignParentRight="true" android:layout_marginBottom="10dp" android:layout_above="@+id/SET1" android:gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:padding="4dp" android:background="@drawable/back" android:text="Set as Wallpaper" android:textSize="18dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/white" /> <Button android:layout_width="80dp" android:layout_height="80dp" android:background="@drawable/button" /> </LinearLayout> <LinearLayout android:id="@+id/SET1" android:layout_alignParentRight="true" android:layout_marginBottom="10dp" android:layout_above="@+id/recyclerview" android:gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:visibility="invisible" android:padding="4dp" android:background="@drawable/back" android:text="Click again to close" android:textSize="18dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/white" android:id="@+id/textSet" /> <Button android:background="@drawable/button" android:layout_width="80dp" android:layout_height="80dp" android:onClick="SetButton" /> </LinearLayout> <androidx.recyclerview.widget.RecyclerView android:layout_alignParentBottom="true" android:id="@+id/recyclerview" android:layout_width="wrap_content" android:layout_height="wrap_content"> </androidx.recyclerview.widget.RecyclerView> </RelativeLayout>
MainActivity.java
import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import android.app.WallpaperManager; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; import java.io.IOException; public class MainActivity extends AppCompatActivity { LinearLayout linearLayout; TextView textView; ImageView imageView; RecyclerView recyclerView; CustomAdapter customAdapter; int imagePosition; int on=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); linearLayout=(LinearLayout) findViewById(R.id.setLinear); imageView=(findViewById(R.id.imageMain)); recyclerView=(RecyclerView) findViewById(R.id.recyclerview); textView=(TextView) findViewById(R.id.textSet); Intent intent=getIntent(); imagePosition = intent.getIntExtra("image",00); imageView.setImageResource(imagePosition); int [] image= new int[] { R.drawable.image1, R.drawable.image2 , R.drawable.image3 , R.drawable.image4 , R.drawable.image5 }; recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayout.HORIZONTAL,false)); customAdapter=new CustomAdapter(image,this); recyclerView.setAdapter(customAdapter); linearLayout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { setAsWallpaper(); } }); } public void SetButton(View view) { if(on==0) { on++; textView.setVisibility(View.VISIBLE); linearLayout.setVisibility(View.VISIBLE); } else if (on!=0) { --on; textView.setVisibility(View.INVISIBLE); linearLayout.setVisibility(View.INVISIBLE); } } public void setAsWallpaper() { WallpaperManager wallpaperManager = WallpaperManager.getInstance(MainActivity.this); if (imagePosition != 0) { try { Toast.makeText(MainActivity.this, "WallPaper Set", Toast.LENGTH_SHORT).show(); wallpaperManager.setResource(imagePosition); } catch (IOException e) { e.printStackTrace(); } } else if (imagePosition == 0) { { int tempImage= R.raw.image1; try { Toast.makeText(MainActivity.this, "WallPaper Set", Toast.LENGTH_SHORT).show(); wallpaperManager.setResource(tempImage); } catch (IOException e) { e.printStackTrace(); } } } }}
Drawable (back.xml)
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="@android:color/black"> </solid> <corners android:radius="30dp"> </corners> </shape>
YouTube Videos
Part 1, How to show images in recyclerview
Part 2, How to apply click listener on recyclerview in android studio
Part 3, How to set wallpaper in android studio