kavie
kavie

Reputation: 2244

Find imageview is Clicked or not in Android

Requirement

I want image-view should be clicked after changing orientation,How Can I find Image view is Clicked or not before changing orientation?

Upvotes: 0

Views: 1716

Answers (3)

azertiti
azertiti

Reputation: 3150

You can define a local variable in activity that will be saved when you rotate the phone and restored later. Something like this:

private static final String CLICKED_KEY = "clicked_key";
private boolean clicked = false;

protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);

   if (savedInstanceState != null) {
      clicked = savedInstanceState.optBoolean(CLICKED_KEY, false);
   }
   if (clicked) {
        // do something
   } 

   imgView.setOnClickListener(new OnClickListener() {
     public void onClick(View v) {
         clicked = true;
     }
   });

}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putBoolean(CLICKED_KEY, clicked);
}

Upvotes: 1

user2894435
user2894435

Reputation:

use shared preference to hold your click event or use global variable for toggle operation here i use shared preference :

 SharedPreferences pref = getApplicationContext().getSharedPreferences(
                                   "any_prefname", MODE_PRIVATE);

        ImageView imgFavorite = (ImageView) findViewById(R.id.favorite_icon);
        imgFavorite.setClickable(true);
        imgFavorite.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {

                           Editor editor = pref.edit();
                           editor.putBoolean("key_name", true);
                           // Storing boolean - true/false
                           editor.commit();

                      }

                });

and in onOrientationChange check get boolean using

pref.getBoolean("key_name", null);

and again reset value into

                   Editor editor = pref.edit();
                   editor.putBoolean("key_name", false);
                   // Storing boolean - true/false
                   editor.commit();

Upvotes: 0

MysticMagicϡ
MysticMagicϡ

Reputation: 28823

You can try setting a boolean flag in onClick

boolean clicked = false;

imgView.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
       clicked = true;
    }
});

Check this flag in onOrientationChange. Hope it helps.

Upvotes: 0

Related Questions