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:
2 Answers 2
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++;
}
}
}
}
1 Comment
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...