2

This is XML file code piece:

    <ImageView
        android:id="@+id/ivh4c5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:clickable="true"
        android:src="@drawable/logoutmenu" />

This is code piece:

    logOut.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
            final SharedPreferences prefs = getApplicationContext().getSharedPreferences("ProfileName", MODE_PRIVATE);
            Editor editor = prefs.edit();
            editor.clear();
            editor.commit();

            finish();
        }});

Result:

On clicking logout image application must logout (only on first time).

Problem:

Clicking on logout image do not respond on first click , I have to click it again in order to logout from application.

Please provide corrected code.

  • I can not use image button, i have to use image only. –  Sep 27 '13 at 09:46
  • Then look for other posts http://stackoverflow.com/questions/4617898/how-can-i-give-imageview-click-effect-like-a-button-in-android this question is redoundant – An-droid Sep 27 '13 at 09:49
  • Link you provide is not the answer of my question, I dont want to change image, i just want to respond it on first click. –  Sep 27 '13 at 10:15
  • It should respond on first click so i invited you to look to other post to see other methods to make an image clickable. When you can't make your method work sometimes it is a good idea to look at other's – An-droid Sep 27 '13 at 10:21

3 Answers3

2

use this only

<ImageView
        android:id="@+id/ivh4c5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/logoutmenu" />
Ali Ahmad
  • 1,351
  • 9
  • 11
2

With your xml code you are focusing the ImageView in the first click and calling onClick with the second click.

You should put android:focusable="false" android:focusableInTouchMode="false" (or remove the lines) to respond on first click.

If you want the image could be focusable and respond to first click you can check: onFocusChangeListener

Josu Garcia de Albizu
  • 1,128
  • 2
  • 12
  • 22
0

by clicking a focusable ImageView, OnFocusChangeListener() gets called. you can get the event like this:

logOut.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
    if(hasFocus){
            final SharedPreferences prefs = getApplicationContext().getSharedPreferences("ProfileName", MODE_PRIVATE);
            Editor editor = prefs.edit();
            editor.clear();
            editor.commit();

            finish();
    }
    }
   }
});
arianoo
  • 657
  • 14
  • 22