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:orientation="vertical" tools:context="in.kantapp.example110.MainActivity"> <ImageView android:id="@+id/img" android:layout_width="fill_parent" android:layout_height="300dp" android:paddingTop="10dp" android:layout_gravity="center_horizontal" android:src="@drawable/aa" /> <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="SetWallpaper" /> <HorizontalScrollView android:layout_width="match_parent" android:layout_height="fill_parent"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="5dp" android:paddingRight="5dp" android:orientation="horizontal"> <ImageView android:id="@+id/img1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="fitXY" android:src="@drawable/a" /> <ImageView android:id="@+id/img2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="5dp" android:scaleType="fitXY" android:src="@drawable/b" /> <ImageView android:id="@+id/img3" android:layout_width="160dp" android:layout_height="160dp" android:paddingLeft="5dp" android:scaleType="fitXY" android:src="@drawable/c" /> <ImageView android:id="@+id/img4" android:layout_width="160dp" android:layout_height="160dp" android:paddingLeft="5dp" android:scaleType="fitXY" android:src="@drawable/d" /> <ImageView android:id="@+id/img5" android:layout_width="160dp" android:layout_height="160dp" android:paddingLeft="5dp" android:scaleType="fitXY" android:src="@drawable/e" /> </LinearLayout> </HorizontalScrollView> </LinearLayout>
MainActivity.java
package in.kantapp.example110; import java.io.IOException; import java.io.InputStream; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; public class MainActivity 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.activity_main); Button but = (Button) findViewById(R.id.button); tophone = R.drawable.aa; but.setOnClickListener(this); display = (ImageView) findViewById(R.id.img); ImageView image1 = (ImageView) findViewById(R.id.img1); ImageView image2 = (ImageView) findViewById(R.id.img2); ImageView image3 = (ImageView) findViewById(R.id.img3); ImageView image4 = (ImageView) findViewById(R.id.img4); ImageView image5 = (ImageView) findViewById(R.id.img5); 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.img1: display.setImageResource(R.drawable.aa); var = Toast.makeText(MainActivity.this, "image changed", Toast.LENGTH_SHORT); var.show(); tophone = R.drawable.aa; break; case R.id.img2: display.setImageResource(R.drawable.bb); var = Toast.makeText(MainActivity.this, "image changed", Toast.LENGTH_SHORT); var.show(); tophone = R.drawable.bb; break; case R.id.img3: display.setImageResource(R.drawable.cc); var = Toast.makeText(MainActivity.this, "image changed", Toast.LENGTH_SHORT); var.show(); tophone = R.drawable.cc; break; case R.id.img4: display.setImageResource(R.drawable.dd); var = Toast.makeText(MainActivity.this, "image changed", Toast.LENGTH_SHORT); var.show(); tophone = R.drawable.dd; break; case R.id.img5: display.setImageResource(R.drawable.ee); var = Toast.makeText(MainActivity.this, "image changed", Toast.LENGTH_SHORT); var.show(); tophone = R.drawable.ee; break; case R.id.button: // 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(MainActivity.this, "Wallpaper image changed", Toast.LENGTH_SHORT); var.show(); break; } } }
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="in.kantapp.example110"> <uses-permission android:name="android.permission.SET_WALLPAPER"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" 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>
No comments:
Post a Comment