So I'm doing a test app more to study myself, I'm a beginner on android do not know much, I'm trying to get it in the application when I click on an image that it goes into this method:
p1.setOnClickListener(new ImageView.OnClickListener() {
@SuppressWarnings("ResourceType")
public void onClick(View arg0) {
posicao =1;
Toast.makeText(getContext(), "Imagem 1 selecionada!", Toast.LENGTH_SHORT).show();
}});
I'm wondering why when it enters into this method, it does not assign value to the position variable, I've tried other ways I declare another variable there and made the position receive what was inside more nor does it work.
This is the image that the above code is from the first image, the action when you click on the image and says it was selected.
The variable position would be the variable that would define the value of the if below, so that when I declare for example position = 1, that is, position gets one out of function it works without problems, plus inside method p1 that is the left image is as if the position = 1 was not even typed there.
At the end of the post has my complete code ...
publicclassFragment_mainextendsFragment{publicFragment_main(){}privateintposicao=0;@OverridepublicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,BundlesavedInstanceState){finalViewAcess=inflater.inflate(R.layout.fragment_main,container,false);ImageViewp1=(ImageView)Acess.findViewById(R.id.p1);ImageViewp2=(ImageView)Acess.findViewById(R.id.p2);p1.setOnClickListener(newImageView.OnClickListener(){@SuppressWarnings("ResourceType")
public void onClick(View arg0) {
posicao =1;
Toast.makeText(getContext(), "Imagem 1 selecionada!", Toast.LENGTH_SHORT).show();
}});
p2.setOnClickListener(new ImageView.OnClickListener() {
@SuppressWarnings("ResourceType")
public void onClick(View arg0) {
Toast.makeText(getContext(), "Imagem 2 selecionada!", Toast.LENGTH_SHORT).show();
posicao =2;
}});
// position =1;
// pegando o valor que recebeu lá ....
if (posicao==1) {
Button caso1button = (Button) Acess.findViewById(R.id.bts); // botão
//ImageView imagePreview = (ImageView)acessR.findViewById(R.id.viewimg); // idimg
//imagePreview.setImageResource(R.drawable.colorpass); // local da img original
caso1button.setOnClickListener(new Button.OnClickListener() {
@SuppressWarnings("ResourceType")
@Override
public void onClick(View arg0) {
WallpaperManager myWallpaperManager
= WallpaperManager.getInstance(getContext());
try {
myWallpaperManager.setResource(R.drawable.gpx17); // wallpaper
Toast.makeText(getContext(), "Wallpaper aplicado com Sucesso!", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}
});
posicao = 0;
}else if (posicao==2) {
Button caso2button = (Button) Acess.findViewById(R.id.bts); // botão
//ImageView imagePreview = (ImageView)acessR.findViewById(R.id.viewimg); // idimg
//imagePreview.setImageResource(R.drawable.colorpass); // local da img original
caso2button.setOnClickListener(new Button.OnClickListener() {
@SuppressWarnings("ResourceType")
@Override
public void onClick(View arg0) {
WallpaperManager myWallpaperManager
= WallpaperManager.getInstance(getContext());
try {
myWallpaperManager.setResource(R.drawable.gpx15); // wallpaper
Toast.makeText(getContext(), "Wallpaper aplicado com Sucesso!", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}
});
posicao = 0;
}
return Acess;
}
}