0

I create an app name of android wallpaper but the problem is that when i clicked on left and right button the next and previous wallpaper does not comes. I tried it lots but failed.

public class Wallpapers extends AppCompatActivity {
 WallpaperManager managerr;
 Button B;
 Button b1;
 Button b2;
 ImageView m;
 int a[]={R.drawable.w1,R.drawable.w2,R.drawable.w3};
 int aa;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_wallpapers);
 click();
 }
 public void click()
 {
 m = (ImageView) findViewById(R.id.imageView);
 B=(Button)findViewById(R.id.button3);
 b1=(Button)findViewById(R.id.button);
 b2=(Button)findViewById(R.id.button2);
 managerr = WallpaperManager.getInstance(getApplicationContext());
 m.setImageResource(R.drawable.w1);
 b1.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View view) {
 if()
 {
 m.setImageResource(R.drawable.w2);
 }
 else if(m.getDrawable()==getResources().getDrawable(R.drawable.m2).getCurrent())
 {
 m.setImageResource(R.drawable.w3);
 }
 }
 });
 }
}

That's the code.

That's the image of app:

enter image description here

Amit Upadhyay
7,4214 gold badges46 silver badges61 bronze badges
asked Feb 12, 2017 at 13:42

2 Answers 2

1

you have set onClickListener only for b1.

change your code follows

int currentPosition = 0;
public void click(){
 m = (ImageView) findViewById(R.id.imageView);
 B= (Button) findViewById(R.id.button3);
 left = (Button) findViewById(R.id.button);
 right = (Button) findViewById(R.id.button2);
 managerr = WallpaperManager.getInstance(getApplicationContext());
 m.setImageResource(R.drawable.m1);
 left.setOnClickListener(new OnClickListener(){
 @Override
 public void onClick(View view) {
 if(currentPosition > 0){
 m.setImageResource(previuosDrawable);
 currentPosition--;
 }
 }
 }
 right.setOnClickListener(new OnClickListener(){
 @Override
 public void onClick(View view) {
 if(currentPosition < pictures.length){
 m.setImageResource(nextDrawable);
 currentPosition++;
 }
 }
 }
}
answered Feb 12, 2017 at 13:51
Sign up to request clarification or add additional context in comments.

1 Comment

i have edited my example. Now you only needs a list of drawables, to set previous or next drawable
1

First of all, if() has no conditions so it can't works add first if(m.getDrawable()==getResources().getDrawable(R.drawable.m2).getCurrent()) and then add all the else if you need and finally the else including m.setImageResource(R.drawable.w2);

Second you need is to setOnClickListener to both Button objects because you only set the listener to b1 and not to b2

For what's B?

Why you have an array called a with the drawables if you don't access to it fields? You can access to drawables using a[index] and increasing or reducing the index number from 0 to 3 or from 3 to 0 when you click b1 or b2. It will be better...

answered Feb 12, 2017 at 14:00

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.