Sachin Solanki Sachin Solanki - 2 months ago 12
Android Question

ANDROID: How to select only one ImageView at a time?

I set four

ImageView
as a menu.
in four menu item i want to select only one at a time.

And i set the 2 Images for one Menu Item like 1 from selected and another 3 for Deselected.

here i posted the code:

footer.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#f1eeee"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/fHome"
android:src="@drawable/home" /> <!-- your image here -->

<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/fAttendence"
android:src="@drawable/home" />

<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/home"
android:src="@drawable/target" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/home"
android:src="@drawable/report" />




main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.sachin.colorchanged.MainActivity">
<include
layout="@layout/footer"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:id="@+id/include2" />
</RelativeLayout>


Mainactivity

public class MainActivity extends AppCompatActivity {
ImageView mHomeButton,mattendence,mTarget,mReport;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mHomeButton = (ImageView) findViewById(R.id.fHome);
mattendence = (ImageView) findViewById(R.id.fAttendence);
mTarget= (ImageView) findViewById(R.id.fTarget);
mReport = (ImageView) findViewById(R.id.fReport);

mHomeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Set the button's appearance
mHomeButton.setSelected(!mHomeButton.isSelected());
if (mHomeButton.isSelected()) {
//Handle selected state change
mHomeButton.setImageResource(R.drawable.blue);
} else {
//Handle de-select state change
mHomeButton.setImageResource(R.drawable.home);
}

}
});
mattendence.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mattendence.setSelected(!mattendence.isSelected());
if (mattendence.isSelected()) {
//Handle selected state change
mattendence.setImageResource(R.drawable.blue);
} else {
//Handle de-select state change
mattendence.setImageResource(R.drawable.home);
}
}
});
mTarget.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mTarget.setSelected(!mTarget.isSelected());
if (mTarget.isSelected()) {
//Handle selected state change
mTarget.setImageResource(R.drawable.blue);
} else {
//Handle de-select state change
mTarget.setImageResource(R.drawable.home);
}
}
});
mReport.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mReport.setSelected(!mReport.isSelected());
if (mReport.isSelected()) {
//Handle selected state change
mReport.setImageResource(R.drawable.blue);
} else {
//Handle de-select state change
mReport.setImageResource(R.drawable.home);
}
}
});

}
}


[home.png for deselected item]

1[blue.png for selected item]2

please help me out of this and suggest me with the example..

Answer

First of all make your class implement View.OnClickListener as:

public class MainActivity extends AppCompatActivity implements View.OnClickListener 

then implement the onClick method of the interface:

@Override
public void OnClick(View v)

Now in you OnClick method first load all the default images of the images like:

  mHomeButton.setImageResource(R.drawable.home);
  mattendence.setImageResource(R.drawable.home); // and other imageViews

then define a switch case in onClick method as:

switch (v.getId()){
  case R.id.fHome:
     mHomeButton.setImageResource(R.drawable.blue);
     break;
  // define same as all other views
}

The final code will be:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    ImageView mHomeButton,mattendence,mTarget,mReport;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mHomeButton = (ImageView) findViewById(R.id.fHome);
        mattendence = (ImageView) findViewById(R.id.fAttendence);
        mTarget= (ImageView) findViewById(R.id.fTarget);
        mReport = (ImageView) findViewById(R.id.fReport);

       mHomeButton.setOnClickListener(this);
       mattendence.setOnClickListener(this);
       mTarget.setOnClickListener(this);
       mReport.setOnClickListener(this);
    }

    @Override
    public void OnClick(View v){
        mHomeButton.setImageResource(R.drawable.home);
        mattendence.setImageResource(R.drawable.home);
        mTarget.setImageResource(R.drawable.home);
        mReport.setImageResource(R.drawable.home);

        switch(v.getId()){

        case R.id.fHome:
            mHomeButton.setImageResource(R.drawable.blue);
            break;

        case R.id.mTarget:
            mTarget.setImageResource(R.drawable.blue);
            break;

        case R.id.fAttendence:
            mattendence.setImageResource(R.drawable.blue);
            break;


        case R.id.mReport:
            mReport.setImageResource(R.drawable.blue);
            break;


      }   
}
Comments