Note that there are some explanatory texts on larger screens.

plurals
  1. POwallpaper is stretching in background for android
    primarykey
    data
    text
    <p>I am creating an application using which I can set the image which is inside my application as a background of a phone. I am new in this field.I have already search everywhere and try to modify my code too but its not working.</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" &gt; &lt;LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" &gt; &lt;Button android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="25" android:onClick="gotoPreviousImage" android:padding="10dip" android:text="&amp;lt;" /&gt; &lt;Button android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="50" android:onClick="setAsWallpaper" android:padding="10dip" android:text="Set as Background" /&gt; &lt;Button android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="25" android:onClick="gotoNextImage" android:padding="10dip" android:text="&gt;" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>My MainActivity.java file</p> <pre><code>package com.blundell.tutorial.ui.phone; import static com.blundell.tutorial.util.HeavyLifter.FAIL; import static com.blundell.tutorial.util.HeavyLifter.SUCCESS; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.View; import android.widget.ImageView; import android.widget.Toast; import com.blundell.tutorial.R; import com.blundell.tutorial.util.HeavyLifter; public class MainActivity extends Activity { private static final List&lt;Integer&gt; backgrounds = new ArrayList&lt;Integer&gt;(); /** The total number of backgrounds in the list */ private static final int TOTAL_IMAGES; static { backgrounds.add(R.drawable.background1); backgrounds.add(R.drawable.background2); backgrounds.add(R.drawable.background3); TOTAL_IMAGES = (backgrounds.size() - 1); } /** the state of what wallpaper is currently being previewed */ private int currentPosition = 0; /** our image wallpaper preview */ private ImageView backgroundPreview; private HeavyLifter chuckNorris; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); backgroundPreview = (ImageView) findViewById(R.id.backgroundPreview); // Set the default image to be shown to start with changePreviewImage(currentPosition); chuckNorris = new HeavyLifter(this, chuckFinishedHandler); } public void gotoPreviousImage(View v) { int positionToMoveTo = currentPosition; positionToMoveTo--; if(positionToMoveTo &lt; 0){ positionToMoveTo = TOTAL_IMAGES; } changePreviewImage(positionToMoveTo); } /** * Called from XML when the set wallpaper button is pressed * Thie retrieves the id of the current image from our list * It then asks chuck to set it as a wallpaper! * The chuckHandler will be called when this operation is complete * @param v */ public void setAsWallpaper(View v) { int resourceId = backgrounds.get(currentPosition); chuckNorris.setResourceAsWallpaper(resourceId); } /** * Called from XML when the next button is pressed * Increment the current state position * @param v */ public void gotoNextImage(View v) { int positionToMoveTo = currentPosition; positionToMoveTo++; if(currentPosition == TOTAL_IMAGES){ positionToMoveTo = 0; } changePreviewImage(positionToMoveTo); } /** * Change the currently showing image on the screen * This is quite an expensive operation as each time the system */ public void changePreviewImage(int pos) { currentPosition = pos; backgroundPreview.setImageResource(backgrounds.get(pos)); Log.d("Main", "Current position: "+pos); } /** */ private Handler chuckFinishedHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch(msg.what){ case SUCCESS: Toast.makeText(MainActivity.this, "Wallpaper set", Toast.LENGTH_SHORT).show(); break; case FAIL: Toast.makeText(MainActivity.this, "Uh oh, can't do that right now", Toast.LENGTH_SHORT).show(); break; default: super.handleMessage(msg); } } }; </code></pre> <p>}</p> <pre><code>My another Heavylifter.java file package com.blundell.tutorial.util; import java.io.IOException; import android.app.WallpaperManager; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Handler; import android.util.Log; </code></pre> <p>/** * <b>This class uses Threads</b> * An alternative to this class would be to use an ASyncTask * @author blundell * */ public class HeavyLifter {</p> <pre><code>public static final int SUCCESS = 0; public static final int FAIL = 1; private final Context context; private final Handler callback; private WallpaperManager manager; /** * Setup the HeavyLifter * @param context the context we are running in - typically an activity * @param callback the handler you want to be notified when we finish doing an operation */ public HeavyLifter(Context context, Handler callback) { this.context = context; this.callback = callback; this.manager = (WallpaperManager) context.getSystemService(Context.WALLPAPER_SERVICE); } /** * Takes a resource id of a file within our /res/drawable/ folder&lt;br/&gt; * It then spawns a new thread to do its work on&lt;br/&gt; * The reource is decoded and converted to a byte array&lt;br/&gt; * This array is passed to the system which can use it to set the phones wallpaper&lt;br/&gt; * Upon completion the callback handler is sent a message with eith {@link HeavyLifter#SUCCESS} or {@link HeavyLifter#FAIL} * * @param resourceId id of a file within our /res/drawable/ folder */ public void setResourceAsWallpaper(final int resourceId) { new Thread() { @Override public void run() { try { manager.setBitmap(getImage(resourceId)); callback.sendEmptyMessage(SUCCESS); } catch (IOException e) { Log.e("Main", "Cant set wallpaper"); callback.sendEmptyMessage(FAIL); } } }.start(); } /** * Decodes a resource into a bitmap, here it uses the convenience method 'BitmapFactory.decodeResource', but you can decode * using alternatives these will give you more control over the size and quality of the resource. * You may need certain size resources within each of your /hdpi/ /mdpi/ /ldpi/ folders in order * to have the quality and look you want on each different phone. */ private Bitmap getImage(int resourceId) { Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId, null); Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, manager.getDesiredMinimumWidth(), manager.getDesiredMinimumHeight(), true); bitmap.recycle(); bitmap = null; return scaledBitmap; } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload