Code that is not working
public void fade(View v){
ImageView pic1 = (ImageView) findViewById(R.id.pic1);
ImageView pic2 = (ImageView) findViewById(R.id.pic2);
pic1.animate().alpha(0f).setDuration(2000);
pic2.animate().alpha(1f).setDuration(2000);
}
Not quite sure what you are asking for, but if you want to change the image back and forth onClick, you could try modifying the code I wrote below.
// set a boolean value
private boolean firstImageShown = true;
public void clickFunction(View view) {
ImageView firstImage = (ImageView) findViewById(R.id.firstImage);
if ((firstImage != null) && (firstImageShown)) {
firstImage.setImageResource(R.drawable.sample_image2);
firstImageShown = false;
} else {
if (firstImage != null) firstImage.setImageResource(R.drawable.sample_image1);
firstImageShown = true;
}
}
//Suppose you have two images rose_1 and rose_2 and you want to switch back
//and forth between these on a button press.
//Set a static variable
private static int switcherPressedCount = 2;
//Map the onClick attribute of the button to a function say "switcherPressed".
//"imageView1" is the id attribute of the image which first gets loaded in the
//app, in this example, it is the attribute of rose_1.
public void switcherPressed(View view){
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
if(switcherPressedCount%2 == 0) {
imageView.setImageResource(R.drawable.rose_2);
switcherPressedCount = 3; //to avoid reaching maximum int limit.
}
else {
imageView.setImageResource(R.drawable.rose_1);
switcherPressedCount = 2; //to avoid reaching maximum int limit.
}
}
Related
public class Serchresult extends Activity implements OnClickListener {
ImageView imageView1;
String Status;
String Reason;
TextView status;
TextView reason;
ImageView statusicon;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.act_serchresult);
Intent intent = getIntent();
Status = intent.getExtras().getString("Status");
Reason = intent.getExtras().getString("Reason");
imageView1 = (ImageView) findViewById(R.id.searchstatus_imgBack);
imageView1.setOnClickListener(this);
status = (TextView) findViewById(R.id.status);
reason = (TextView) findViewById(R.id.reason);
statusicon = (ImageView) findViewById(R.id.imageView1);
reason.setText(Reason.replace("null", ""));
if (reason.equals("ACCEPTED")) {
AQuery aq = new AQuery(getApplicationContext());
statusicon.setImageResource(R.drawable.accept_icon);
} else if (reason.equals("REJECTED")) {
AQuery aq = new AQuery(getApplicationContext());
statusicon.setImageResource(R.drawable.reject_icon);
}
else {
// reason.setCompoundDrawables(null, null, null, null);
statusicon.setImageResource(0);
}
status.setText(Status.replace("null", ""));
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.searchstatus_imgBack) {
finish();
}
}
}
here is my code i want display image from drawable folder accept and reject icon .i am getting ACCEPTED and REJECTED from intent on the basis of this response i want to display image in image view i have apply condition but image is not visible please help me where am doing wrong
Your code seems working. But still you can try following code to display image. Make sure your if-else conditions are working
Bitmap defBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.accept_icon);
statusicon.setImageBitmap(defBitmap);
Maybe the Bitmap is too big for the heap, try use BitmapFactory.Options to reduce the size.
BitmapFactory.Options op = new BitmapFactory.Options();
op.inSampleSize = 4;
statusicon.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.accept_icon, op));
I am having weird problems with Android GridView. I create a 3x4 grid and insert buttons into that grid. I want the background of the button to change when the user clicks that button. And this is working just fine for all buttons except the first one (the one with index 0 - top left). OnClick event listener doesn't fire at all for that button no matter what I do.
Here is the code where I create the view:
public View getView(int position, View convertView, ViewGroup parent) {
Button imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
Log.w("NOVO", "narejena nova celica");
imageView = new Button(mContext);
imageView.setPadding(8, 8, 8, 8);
} else {
Log.w("STARO", "stara celica");
imageView = (Button) convertView;
}
imageView.setEnabled(true);
int visina = parent.getHeight();
int sirina = parent.getWidth();
float dip = mContext.getResources().getDisplayMetrics().density;
float margin = 10*dip;
int view_height = (int)(visina - 3*margin)/4;
int view_width = (int)(sirina - 2*margin)/3;
int view_dim = 0;
if (view_height <= view_width)
view_dim = view_height;
else
view_dim = view_width;
imageView.setLayoutParams(new GridView.LayoutParams(view_dim, view_dim));
imageView.setId(position);
imageView.setOnClickListener(celice.get(position));
/*imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Toast toast = Toast.makeText(mContext, v.getId() + "bla", Toast.LENGTH_SHORT);
//toast.show();
celice.get(v.getId()).celicaVisible(4000);
}});*/
celice.get(position).id = position;
celice.get(position).setButton(imageView);
return imageView;
}
If I replace
imageView = (Button) convertView;
with
imageView = new Button(mContext);
then the onClick() gets fired but the background still doesn't change. All the other buttons are working as expected.
And here is the custom class "Celica" that takes care of the actual work - changing the background...
public class Celica implements OnClickListener {
public boolean odkrit;
public boolean najden;
public int id;
public Drawable slikca1, slikca2;
public Celica par;
private Timer tim;
public Button but;
public Context con;
static int buttonsVisible = 0;
Celica(Drawable s1, Drawable s2) {
this.slikca1 = s1;
this.slikca2 = s2;
}
void celicaVisible(int millis) {
if (odkrit)
return;
Log.w("TEST", "prizganih " + buttonsVisible);
if (buttonsVisible >= 2)
return;
odkrit = true;
buttonsVisible++;
tim = new Timer();
tim.schedule(new timerDone(), millis);
((Activity)con).runOnUiThread(new Runnable() {
#Override
public void run() {
but.setBackground(slikca2);
}
});
}
void setButton(Button b) {
but = b;
((Activity)con).runOnUiThread(new Runnable() {
#Override
public void run() {
but.setBackground(slikca1);
}
});
}
class timerDone extends TimerTask {
#Override
public void run() {
if (!najden) {
odkrit = false;
((Activity)con).runOnUiThread(new Runnable() {
#Override
public void run() {
but.setBackground(slikca1);
}
});
}
buttonsVisible--;
tim.cancel();
}
}
#Override
public void onClick(View v) {
celicaVisible(4000);
}
}
In Android, ID of any View must be non zero and non negative number. means View ID should be > 0. and there is problem, when you are setting ID to the Button like
imageView.setId(position)
here ID of a button will be zero when position is zero(means first item). may be due to this, First Button's OnClickListener is not getting fired...try setting a ID that is greater than zero to Button and try once...
you can write like
imageView.setId(position+1) to ensure ID > 0
I actually figured it out. Everything works if I use the view that gets provided by the onClick() method instead of saving the actual button at the creation of the Celica object.
So basically adding:
but = (Button) v;
to the onClick() method solved the problem.
Need some advice again.
I am 99% done with this module I'm working on, although I'm stuck at the last hurdle. I'm dynamically publishing a button on the screen at runtime. This button will take it to a new activity view when pressed. I got that working perfectly.
However, I have another button which randomly changes the view so it effectively needs to reset itself, if that makes sense. What's happening is each time the button is clicked (the dynamic one) it then adds another button to the stack. Effectively I have buttons each time I click running down the screen. Is my logic wrong or is there a way to check and prevent the button to show each time? i.e. Just once...Below is the code.
public void ButtonOnClick(View v) {
Random rnd = new Random();
int randomListIndex = rnd.nextInt(2);
Animation myFadeInAnimation = AnimationUtils.loadAnimation(Page1.this, R.anim.fadein);
int firstRun = 0;
switch (randomListIndex) {
case 0:
//get the image your going to muck with
image = (ImageView) findViewById(R.id.cardImageView);
//set the image with what it should be
image.setImageResource(R.drawable.storm);
//apply the transition effect so it looks correct
image.startAnimation(myFadeInAnimation);
button = (Button)findViewById(R.id.dynamicButton);
button.setText("Need another question?");
Button myButton = new Button(this);
myButton.setText("Press Me");
LinearLayout layout = (LinearLayout) findViewById(R.id.nextPageContainer);
layout.addView(myButton);
final Button myButton1 = myButton;
myButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View arg0) {
String activityName = "Storm";
Intent intent = new Intent(Page1.this, Page2.class);
intent.putExtra(ACTIVITYNAME,activityName);
startActivity(intent);
}
});
break;
}
}
EDIT: How about this? Add a new class field member (a class variable, not a method's local variable) that indicates whether the button was actually added in the layout.
private boolean buttonShown = false; /* Here changed */
public void ButtonOnClick(View v) {
Random rnd = new Random();
int randomListIndex = rnd.nextInt(2);
Animation myFadeInAnimation = AnimationUtils.loadAnimation(Page1.this, R.anim.fadein);
int firstRun = 0;
switch (randomListIndex) {
case 0:
//get the image your going to muck with
image = (ImageView) findViewById(R.id.cardImageView);
//set the image with what it should be
image.setImageResource(R.drawable.storm);
//apply the transition effect so it looks correct
image.startAnimation(myFadeInAnimation);
button = (Button)findViewById(R.id.dynamicButton);
button.setText("Need another question?");
if (buttonShown == false) { /* Here changed */
Button myButton = new Button(this);
myButton.setText("Press Me");
LinearLayout layout = (LinearLayout) findViewById(R.id.nextPageContainer);
layout.addView(myButton);
final Button myButton1 = myButton;
myButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View arg0) {
String activityName = "Storm";
Intent intent = new Intent(Page1.this, Page2.class);
intent.putExtra(ACTIVITYNAME,activityName);
startActivity(intent);
}
});
buttonShown = true; /* Here changed */
} /* Here changed */
break;
}
}
Edited again: Instead of local variable myButton, I used class field member pressMeButton.
private Button pressMeButton;
public void ButtonOnClick(View v) {
Random rnd = new Random();
int randomListIndex = rnd.nextInt(2);
Animation myFadeInAnimation = AnimationUtils.loadAnimation(Page1.this, R.anim.fadein);
int firstRun = 0;
switch (randomListIndex) {
case 0:
//get the image your going to muck with
image = (ImageView) findViewById(R.id.cardImageView);
//set the image with what it should be
image.setImageResource(R.drawable.storm);
//apply the transition effect so it looks correct
image.startAnimation(myFadeInAnimation);
button = (Button)findViewById(R.id.dynamicButton);
button.setText("Need another question?");
if (pressMeButton == null) {
pressMeButton = new Button(this);
pressMeButton.setText("Press Me");
LinearLayout layout = (LinearLayout) findViewById(R.id.nextPageContainer);
layout.addView(pressMeButton);
}
/* If the pressMeButton is already in the layout, all you need to do is just changing the onClickListener. */
pressMeButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View arg0) {
String activityName = "Storm";
Intent intent = new Intent(Page1.this, Page2.class);
intent.putExtra(ACTIVITYNAME,activityName);
startActivity(intent);
}
});
break;
}
}
Your code is fine but the way it is it will always add a button to the screen. Whats the desired behavior your looking for? If its a conditional thing than you need to account for that in the onclick call.
EDIT:
So what you need to do then is to create some kind of flag or id for the button and set it as the button's view.tag like this:
Button b = new Button(context);
b.setTag(flagTag);
Then when you want to check if the button exists you check it like:
if((LinearLayout) findViewById(R.id.nextPageContainer))
.findViewById(tagFlag)==null){
Button b = new Button(context);
b.setTag(flagTag);
}else{
//do nothing or something :p
}
I'll keep this simple. I want to set an ImageView in an AlertDialog to an image in an array of Drawables The image that I would like to set to the ImageView can be retrieved by accessing the mImages[position].
Here is a brief explanation (complete explanation below):
In short- I need a way to pass an image from my main activity to a dialog then on Cancel dismiss the dialog and on confirm set the system wallpaper (to the image passed from the main activity) then finish(); the activity.
Here's a complete explanation:
The user is presented with a Gallery and an ImageView above the Gallery that shows a larger preview of the image that has focus in the Gallery.
The images displayed in the Gallery are setup using:
// setup wallpaper array
private void findWallpapers() {
mThumbs = new ArrayList<Integer>(24);
mImages = new ArrayList<Integer>(24);
final Resources resources = getResources();
final String packageName = getApplication().getPackageName();
addWallpapers(resources, packageName, R.array.wallpapers);
}
// setup array defining all wallpapers & define thumbnails
private void addWallpapers(Resources resources, String packageName, int list) {
final String[] extras = resources.getStringArray(list);
for (String extra : extras) {
int res = resources.getIdentifier(extra, "drawable", packageName);
if (res != 0) {
final int thumbRes = resources.getIdentifier(extra + "_small",
"drawable", packageName);
if (thumbRes != 0) {
mThumbs.add(thumbRes);
mImages.add(res);
}
}
}
}
After "Set Wallpaper" Button is pressed, an AlertDialog should open with another preview of the image that had focus in the Gallery. The AlertDialog will contain a TextView with instructions, the preview of the image we are proposing to set as the wallpaper, an "Okay" Button and a "Cancel" Button. Pressing the "Okay" Button will set the image from the ImageView preview as the system wallpaper via an InputStream.
Thanks again!
private void selectWallpaper(int position) {
Toast.makeText(getBaseContext(), "Select Wallpaper", Toast.LENGTH_SHORT)
.show();
if (mIsWallpaperSet) {
return;
}
mIsWallpaperSet = true;
Context context = this;
// CharSequence text = "Wallpaper Set!";
// int duration = Toast.LENGTH_LONG;
InputStream stream = getResources().openRawResource(
mImages.get(position));
final Dialog accept = new Dialog(context);
accept.setContentView(R.layout.confirm);
accept.setTitle("Please Confirm");
TextView instructions = (TextView) accept.findViewById(R.id.textView1);
instructions.setText("Would you like to set this as your wallpaper?");
ImageView wallpreview = (ImageView) accept
.findViewById(R.id.imageView1);
wallpreview.createFromStream(stream, "test");
// SETUP cancel (no btn) listener
Button cancelbtn = (Button) accept.findViewById(R.id.button2);
cancelbtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
accept.dismiss();
}
});
// SETUP Yes Btn listener
Button okbtn = (Button) accept.findViewById(R.id.button1);
okbtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// dismiss being used as placeholder, actually setting wallpaper
// will be added
accept.dismiss();
}
});
accept.show();
}
{
;
}
You can use the layout inflator to inflate your alert dialog layout and assign it to the View of the alert dialog in your main activity. You can use findViewById() to set the onClickListeners listeners for those buttons. That will ensure that your onClickListener can see mImages.
final ArrayList<Integer> mImages=new ArrayList<Integer>();
final ImageView v=findViewById(R.id.imageView);
AlertDialog d=new AlertDialog.Builder(Main.this).create();
d.setView(getLayoutInflater().inflate(R.layout.dialogView,findViewById(R.id.ROOT_LAYOUT_IN_XML_ID),false));
((ImageView)d.findViewById(R.id.YOUR_VIEW_ID)).setImageResource(mImages.get(?));
((Button)d.findViewById(R.id.cancelButton)).setOnClickListener(new OnClickListener()
{
d.dismiss();
});
((Button)d.findViewById(R.id.acceptButton)).setOnClickListener(new OnClickListener()
{
v.setImageResource(mImages.get(?));
d.dismiss();
});
I am developing application on Images.
In that if I click on same image the image will replace by another one that already into my Drawable
Right now onclick I am able to display only Toast Message; but I want to replace Image. I don't know How to do?
Any Help and suggestion appreciable.
you can take it images name as : img1 ,img2 ,img3.
ImageView imgV = (ImageView) layout.findViewById(R.id.img1);
imgV.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (v.isClickable()) {
i++;
String ii = new Integer(i).toString();
Log.i("Inside", ii);
if (ii.equals("3")) {
Toast.makeText(getApplicationContext(), "Call another Image ",Toast.LENGTH_SHORT).show();}
ImageView imgV = (ImageView) layout.findViewById(R.id.img1);
imgV.setClickable(true);
int counter = 0;
imgV.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
i++;
if(i == 3){
imgV.setImageResource(R.drawable.img2);
//Or you may want to use
//imgV.setBackgroundResource(R.drawable.img2);
i = 0;
}
}