1

There are 3 imageview in my Android project. It should be changed to icon when button clicked. When release click, it should be default icon. In my code it change icon when click button, but it change icon permanently till click back button from this Activity. It should only replace mainpage_acil_icon_p with mainpage_acil_icon when click button.

How can I solve this problem?

activity_mainpage.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/Linear_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#000000"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="10dp"
            android:alpha=".85"
            android:background="@drawable/mainpage_background"
            android:orientation="vertical"
            android:weightSum="60">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dip"
                android:layout_weight="40"
                android:orientation="vertical">
                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="EMAS"
                    android:textAlignment="center"
                    android:textColor="@color/colorWhite"
                    android:textSize="18sp"
                    android:textStyle="bold" />
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dip"
                android:layout_weight="20"
                android:orientation="vertical">
                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="#c7c7c8"/>

                <include layout="@layout/mainpage_inner_part" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout> 

MainPageActivity.java

public class MainPageActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mainpage);

      ListView list;
    final String[] web = {
            getString(R.string.mainpage_acil),
            getString(R.string.mainpage_bildirim),
            getString(R.string.mainpage_isguveligi),
    };
    Integer[] imageId = {
            R.drawable.mainpage_acil_icon_p,
            R.drawable.mainpage_bildirim_icon_p,
            R.drawable.mainpage_isguvenligi_icon_p,
    };
    CustomListMainPage adapter = new
            CustomListMainPage(MainPageActivity.this, web, imageId);
    list = (ListView) findViewById(R.id.list);
    list.setAdapter(adapter);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            Log.v("Pozisyon", String.valueOf(position));
            Intent intentPos;
            ImageView imageView= (ImageView) view.findViewById(R.id.img);
            boolean flag=false;
            switch (position) {
                case 0 :
                    //Toast.makeText(MainPageActivity.this, "You Clicked at " + web[+position], Toast.LENGTH_SHORT).show();
                    imageView.setImageResource(R.drawable.mainpage_acil_icon);
                    intentPos=new Intent(MainPageActivity.this,EmergencyActivity.class);
                    startActivity(intentPos);
                    break;

                case 1 :
                    imageView.setImageResource(R.drawable.mainpage_bildirim_icon);
                    intentPos=new Intent(MainPageActivity.this,NotificationPage.class);
                    startActivity(intentPos);                     break;

                case 2 :
                    imageView.setImageResource(R.drawable.mainpage_isguvenligi_icon);
                    intentPos=new Intent(MainPageActivity.this,JobSecurityActivity.class);
                    startActivity(intentPos);
                    break;

                default :
                    System.out.println("Hatali secim! 1, 2 ya da 3'e basiniz.");
                    break;
            }
        }
    });
}

}
Ahmet Yılmaz
  • 57
  • 1
  • 11

1 Answers1

2

I think the easiest way for this is using selector.

In your activity layout file replace ImageView with Button and set selector as background.

In your activity layout:

<Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Button"
  android:background="@drawable/btn_selector"
  android:id="@+id/button"/>

And here is the btn_selector.xml file

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_checked="true" android:state_pressed="true" android:drawable="@drawable/mainpage_acil_icon" />
  <item android:state_pressed="true" android:drawable="@drawable/mainpage_acil_icon"/>
  <item android:state_checked="true" android:drawable="@drawable/mainpage_acil_icon"/>
  <item android:drawable="@drawable/mainpage_acil_icon_p"/>
</selector>

put this selector file in your res/drawable folder. I think this will help you.

Md Jubayer
  • 272
  • 3
  • 13
  • Ok. Thank you. I didn't write this code. The developer before me, he write these codes. So, I want to integrate these codes. – Ahmet Yılmaz May 09 '18 at 12:03
  • I solved this problem. I make xml for every imageview like `btn_selector.xml` that you write. And change this code `R.drawable.mainpage_acil_icon_p` with `R.drawable.btn_selector`. And also delete `imageView.setImageResource(R.drawable.mainpage_acil_icon);` in `case`. It solved. Thank you for your helping. – Ahmet Yılmaz May 10 '18 at 06:17
  • Glad to know that my answer help you. – Md Jubayer May 13 '18 at 05:26