2

Is it possible to have a tick togglebutton ?

The layout is below

enter image description here

 <ImageView
            android:id="@+id/donePic"
            android:src="@mipmap/done"
            android:tint="@color/red"
            android:paddingLeft="320dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

Currently the tick button is ImageView. How to make the ImageView become toggleButton ? Is it possible ?

John
  • 684
  • 11
  • 35

3 Answers3

2

Make a Tic toggle button with selector

<ToggleButton 
                android:id="@+id/toggle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/check"
                android:focusable="false"
                android:focusableInTouchMode="false"
                android:textOff=""
                android:textOn="" />

Selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- When selected, use tic -->
    <item android:drawable="@drawable/tic_img"
          android:state_checked="true" />
    <!-- When not selected, use un tic-->
    <item android:drawable="@drawable/untic_img"
        android:state_checked="false"/>

 </selector>
Mohit Trivedi
  • 699
  • 3
  • 13
0

Set up onClickListenr for your imageView and when the user click the image, change the drawable to done or to undone.

You can keep a boolean value to see whether the user has clicked boolean ifClick = false or not!

pacholik
  • 8,607
  • 9
  • 43
  • 55
O_o
  • 1,103
  • 11
  • 36
0

You can use two images,one for toggle off and another for toggle on and set onClickListner on imageView.

boolean isToggle=false;

yourImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(!isToggle)
                {
                    //Do whatever you want to do

                    imageView.setImageResource(R.drawable.toggleOffImage);//set Toggle Off image
                    isToggle =true;
                }
                else
                {
                    //Do whatever you want to do
                    imageView.setImageResource(R.drawable.toggleOnImage); //set Toggle Off image
                    isToggle =false;
                }
            }
        });
AbhayBohra
  • 2,047
  • 24
  • 36