Set wallpaper button

All your problems with Audio, Video and Images.

Set wallpaper button

Postby Roo » Wed Aug 11, 2010 10:51 am

I've created a custom gallery; however, the "Set wallpaper" button will not set the wallpaper. I can touch the previews of the wallpapers in the gallery to set them, but I'd rather have it the other way around. As in, only setting them by pressing "Set wallpaper." Here's the wallpaper.java I have, any help would be amazing! I'm sure it's something simple I'm over looking.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.totem;
  2.  
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.view.MotionEvent;
  8. import android.view.View;
  9. import android.view.ViewGroup;
  10. import android.view.Window;
  11. import android.widget.AdapterView;
  12. import android.widget.BaseAdapter;
  13. import android.widget.Gallery;
  14. import android.widget.ImageView;
  15. import android.widget.Gallery.LayoutParams;
  16.  
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19.  
  20. public class Wallpaper extends Activity implements
  21.         AdapterView.OnItemSelectedListener, AdapterView.OnItemClickListener {
  22.  
  23.     private static final String LOG_TAG = "Home";
  24.  
  25.     private static final Integer[] THUMB_IDS = {
  26.         R.drawable.andy_small,
  27.        
  28.     };
  29.  
  30.     private static final Integer[] IMAGE_IDS = {
  31.            R.drawable.andy,
  32.  
  33.     };
  34.  
  35.     private Gallery mGallery;
  36.     private boolean mIsWallpaperSet;
  37.  
  38.     public void onCreate(Bundle icicle) {
  39.         super.onCreate(icicle);
  40.         requestWindowFeature(Window.FEATURE_NO_TITLE);
  41.  
  42.         setContentView(R.layout.wallpaper_chooser);
  43.        
  44.         mGallery = (Gallery) findViewById(R.id.gallery);
  45.         mGallery.setAdapter(new ImageAdapter(this));
  46.         mGallery.setOnItemSelectedListener(this);
  47.         mGallery.setOnItemClickListener(this);
  48.     }
  49.  
  50.     @Override
  51.     protected void onResume() {
  52.         super.onResume();
  53.         mIsWallpaperSet = false;
  54.     }
  55.  
  56.     @SuppressWarnings("unchecked")
  57.         public void onItemSelected(AdapterView parent, View v, int position, long id) {
  58.         getWindow().setBackgroundDrawableResource(IMAGE_IDS[position]);
  59.     }
  60.  
  61.     @SuppressWarnings("unchecked")
  62.         public void onItemClick(AdapterView parent, View v, int position, long id) {
  63.         selectWallpaper(position);
  64.     }
  65.  
  66.     private synchronized void selectWallpaper(int position) {
  67.         if (mIsWallpaperSet) {
  68.             return;
  69.         }
  70.         mIsWallpaperSet = true;
  71.         try {
  72.             InputStream stream = getResources().openRawResource(IMAGE_IDS[position]);
  73.             setWallpaper(stream);
  74.             setResult(RESULT_OK);
  75.             finish();
  76.         } catch (IOException e) {
  77.             Log.e(LOG_TAG, "Failed to set wallpaper " + e);
  78.         }
  79.     }
  80.  
  81.     @SuppressWarnings("unchecked")
  82.         public void onNothingSelected(AdapterView parent) {
  83.     }
  84.  
  85.     public boolean onTouchEvent(MotionEvent event, Object Action_Down) {
  86.         getAction(Action_Down);
  87.         selectWallpaper(mGallery.getSelectedItemPosition());
  88.         return true;
  89.     }
  90.  
  91.     private void getAction(Object actionDown) {
  92.                 // TODO Auto-generated method stub
  93.                
  94.         }
  95.  
  96.         public class ImageAdapter extends BaseAdapter {
  97.  
  98.         private Context mContext;
  99.  
  100.         public ImageAdapter(Context c) {
  101.             mContext = c;
  102.         }
  103.  
  104.         public int getCount() {
  105.             return THUMB_IDS.length;
  106.         }
  107.  
  108.         public Object getItem(int position) {
  109.             return position;
  110.         }
  111.  
  112.         public long getItemId(int position) {
  113.             return position;
  114.         }
  115.  
  116.         public View getView(final int position, View convertView, ViewGroup parent) {
  117.             ImageView i = new ImageView(mContext);
  118.  
  119.             i.setImageResource(THUMB_IDS[position]);
  120.             i.setAdjustViewBounds(true);
  121.             i.setLayoutParams(new Gallery.LayoutParams(
  122.                     LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
  123.             i.setBackgroundResource(android.R.drawable.picture_frame);
  124.             return i;
  125.         }
  126.  
  127.     }
  128.  
  129. }
Parsed in 0.044 seconds, using GeSHi 1.0.8.4
Roo
Junior Developer
Junior Developer
 
Posts: 11
Joined: Wed Aug 11, 2010 10:43 am

Top

Re: Set wallpaper button

Postby jonbonazza » Wed Aug 11, 2010 2:27 pm

First, I don't know what you are doing with the onTouchEvent() method. You aren't implementing it properly. Regardless, I don't know why you would even need this. All you need to do is have your class implement OnClickListener
and then set your button to use an onclicklistener like so:

buttonName.setOnClickListener(this);

Then all you have to do is implement the onClick(View v) method.

If you do that, the button will be registered to receive the click and it will call the onClick() method when it is clicked.
jonbonazza
Master Developer
Master Developer
 
Posts: 665
Joined: Thu Jul 15, 2010 2:58 pm
Location: WV USA

Re: Set wallpaper button

Postby Roo » Wed Aug 11, 2010 3:45 pm

jonbonazza wrote:First, I don't know what you are doing with the onTouchEvent() method. You aren't implementing it properly. Regardless, I don't know why you would even need this. All you need to do is have your class implement OnClickListener
and then set your button to use an onclicklistener like so:

buttonName.setOnClickListener(this);

Then all you have to do is implement the onClick(View v) method.

If you do that, the button will be registered to receive the click and it will call the onClick() method when it is clicked.


Thank you, that makes sense.
Roo
Junior Developer
Junior Developer
 
Posts: 11
Joined: Wed Aug 11, 2010 10:43 am

Re: Set wallpaper button

Postby Roo » Thu Aug 12, 2010 12:25 am

I removed the onTouchEvent() method and now I cannot scroll through wallpapers. I implemented OnClickListener
and then set my button to use an onclicklistener and implemented the onClick(View v) method, but it still won't work like it should.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6. import android.view.MotionEvent;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.view.Window;
  10. import android.view.View.OnClickListener;
  11. import android.widget.AdapterView;
  12. import android.widget.BaseAdapter;
  13. import android.widget.Button;
  14. import android.widget.Gallery;
  15. import android.widget.ImageView;
  16. import android.widget.Gallery.LayoutParams;
  17.  
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20.  
  21. public class Wallpaper extends Activity implements
  22.         AdapterView.OnItemSelectedListener, AdapterView.OnItemClickListener {
  23.  
  24.             Button Button;
  25.        
  26.     private static final String LOG_TAG = "Home";
  27.  
  28.     private static final Integer[] THUMB_IDS = {
  29.          R.drawable.andy_small,
  30.          R.drawable.batman_small,
  31.     };
  32.  
  33.     private static final Integer[] IMAGE_IDS = {
  34.          R.drawable.andy,
  35.          R.drawable.batman,
  36.     };
  37.  
  38.     private Gallery mGallery;
  39.     private boolean mIsWallpaperSet;
  40.  
  41.    
  42.     @Override
  43.     public void onCreate(Bundle icicle) {
  44.         super.onCreate(icicle);
  45.         requestWindowFeature(Window.FEATURE_NO_TITLE);
  46.  
  47.         setContentView(R.layout.wallpaper_chooser);
  48.  
  49.         mGallery = (Gallery) findViewById(R.id.gallery);
  50.         mGallery.setAdapter(new ImageAdapter(this));
  51.         mGallery.setOnItemSelectedListener(this);
  52.         mGallery.setOnItemClickListener(this);
  53.        
  54.         Button = (Button)findViewById(R.id.set);
  55.        
  56.         Button.setOnClickListener(new OnClickListener(){
  57.  
  58.                         public void onClick(View v) {
  59.                
  60.                                
  61.                         }
  62.     });
  63.     }
  64.  
  65.     @Override
  66.     protected void onResume() {
  67.         super.onResume();
  68.         mIsWallpaperSet = false;
  69.     }
  70.  
  71.     public void onItemSelected(AdapterView parent, View v, int position, long id) {
  72.         getWindow().setBackgroundDrawableResource(IMAGE_IDS[position]);
  73.     }
  74.  
  75.     public void onItemClick(AdapterView parent, View v, int position, long id) {
  76.         selectWallpaper(position);
  77.     }
  78.  
  79.     private synchronized void selectWallpaper(int position) {
  80.         if (mIsWallpaperSet) {
  81.             return;
  82.         }
  83.         mIsWallpaperSet = true;
  84.         try {
  85.             InputStream stream = getResources().openRawResource(IMAGE_IDS[position]);
  86.             setWallpaper(stream);
  87.             setResult(RESULT_OK);
  88.             finish();
  89.         } catch (IOException e) {
  90.             Log.e(LOG_TAG, "Failed to set wallpaper " + e);
  91.         }
  92.     }
  93.  
  94.     public void onNothingSelected(AdapterView parent) {
  95.     }
  96.  
  97.     @Override
  98.     public boolean onTouchEvent(MotionEvent event) {
  99.         selectWallpaper(mGallery.getSelectedItemPosition());
  100.         return true;
  101.     }
  102.  
  103.     public class ImageAdapter extends BaseAdapter {
  104.  
  105.         private Context mContext;
  106.  
  107.         public ImageAdapter(Context c) {
  108.             mContext = c;
  109.         }
  110.  
  111.         public int getCount() {
  112.             return THUMB_IDS.length;
  113.         }
  114.  
  115.         public Object getItem(int position) {
  116.             return position;
  117.         }
  118.  
  119.         public long getItemId(int position) {
  120.             return position;
  121.         }
  122.  
  123.         public View getView(final int position, View convertView, ViewGroup parent) {
  124.             ImageView i = new ImageView(mContext);
  125.  
  126.             i.setImageResource(THUMB_IDS[position]);
  127.             i.setAdjustViewBounds(true);
  128.             i.setLayoutParams(new Gallery.LayoutParams(
  129.                     LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
  130.             i.setBackgroundResource(android.R.drawable.picture_frame);
  131.             return i;
  132.         }
  133.  
  134.     }
  135.  
  136. }
Parsed in 0.046 seconds, using GeSHi 1.0.8.4


Here's my layout XML-wallpaper_chooser
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"
  3.  xmlns:android="http://schemas.android.com/apk/res/android">
  4.     <ImageView android:id="@id/wallpaper" android:layout_width="fill_parent" android:layout_height="0.0dip" android:scaleType="fitCenter" android:layout_weight="1.0" />
  5.     <Gallery android:id="@id/gallery" android:layout_width="fill_parent" android:layout_height="wrap_content" />
  6.     <Button android:layout_gravity="center_horizontal" android:id="@id/set" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Set wallpaper" />
  7. </LinearLayout>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4
Roo
Junior Developer
Junior Developer
 
Posts: 11
Joined: Wed Aug 11, 2010 10:43 am

Re: Set wallpaper button

Postby jonbonazza » Thu Aug 12, 2010 12:35 am

Hmm.. That's odd... Well, you could try fixing your onTouchEvent() method. It should look something like this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2. public boolean onTouchEvent(MotionEvent event) {
  3.         int e = event.getAction();
  4.         if(e == MotionEvent.ACTION_DOWN)
  5.         {
  6.                    selectWallpaper(mGallery.getSelectedItemPosition());
  7.                    return true;
  8.         }
  9. }
  10.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Try that. Also, post your code after trying my first method. The code you posted in your last post is identical to the code in the OP.
jonbonazza
Master Developer
Master Developer
 
Posts: 665
Joined: Thu Jul 15, 2010 2:58 pm
Location: WV USA

Re: Set wallpaper button

Postby Roo » Thu Aug 12, 2010 1:08 am

Okay, so, added the onTouchEvent() method fixes the scrolling problem, now I just the button to work.

The second code I posted did have changes, this is what I added to it.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.             Button Button;
  3.        
  4.    
  5.  
  6.         setContentView(R.layout.wallpaper_chooser);
  7.  
  8.         mGallery = (Gallery) findViewById(R.id.gallery);
  9.         mGallery.setAdapter(new ImageAdapter(this));
  10.         mGallery.setOnItemSelectedListener(this);
  11.         mGallery.setOnItemClickListener(this);
  12.        
  13.         Button = (Button)findViewById(R.id.set);
  14.        
  15.         Button.setOnClickListener(new OnClickListener(){
  16.  
  17.                         public void onClick(View v) {
  18.                
  19.                                
  20.                         }
  21.     });
  22.     }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


More specifically just the button code.
Roo
Junior Developer
Junior Developer
 
Posts: 11
Joined: Wed Aug 11, 2010 10:43 am

Top

Re: Set wallpaper button

Postby Roo » Thu Aug 12, 2010 2:45 am

I fixed it! Boy that's a relief. Thanks again for the help jonbonazza!
Roo
Junior Developer
Junior Developer
 
Posts: 11
Joined: Wed Aug 11, 2010 10:43 am

Re: Set wallpaper button

Postby jonbonazza » Thu Aug 12, 2010 1:57 pm

What was the problem if you don't mind me asking?
jonbonazza
Master Developer
Master Developer
 
Posts: 665
Joined: Thu Jul 15, 2010 2:58 pm
Location: WV USA

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: Exabot [Bot] and 14 guests