0

I have imagebutton and I want to write setOnClickListener method with toggle click (like toggleButton). I know how working togglebutton but I do not need to use it. it is a possible to write toggleclick method in imagebutton. I wrote some code but not working such as togglebutton

strada_chart.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            if(arg0.isClickable()==true)
            {
                Toast.makeText(getApplicationContext(), "1", Toast.LENGTH_SHORT).show();
            }
            else
            {
                Toast.makeText(getApplicationContext(), "2", Toast.LENGTH_SHORT).show();
            }
        }
    });
Cœur
  • 37,241
  • 25
  • 195
  • 267
user3775061
  • 145
  • 2
  • 11

3 Answers3

1

Try this..

likeBtn.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        if(fun) {
            likeBtn.setImageResource(R.drawable.unlike);
            fun=false;
        } else {
            fun=true;       
            likeBtn.setImageResource(R.drawable.like);
            Toast.makeText(
                    getApplicationContext(),
                    "Changed",
                    Toast.LENGTH_LONG
            ).show();
        }
    }
});
Bob
  • 61
  • 1
  • 6
Giridharan
  • 4,402
  • 5
  • 27
  • 30
0
public void onToggleClicked(View view) {
        // Is the toggle on?
        switch (view.getId()) {
        case R.id.xyz:
            boolean on = ((ToggleButton) view).isChecked();

            if (on) {


                Toast.makeText(mContext, "A", Toast.LENGTH_SHORT).show();
            } else {


                Toast.makeText(mContext, "B", Toast.LENGTH_SHORT).show();
            }
        }
    }

Use property of Toggle button

In XML:

<ToggleButton
            android:id="@+id/xyz"
            style="@style/toggleButton"
            android:layout_width="190dp"
            android:layout_height="50dp"
            android:layout_gravity="center_vertical|center_horizontal"
            android:layout_margin="8dp"
            android:background="@drawable/ic_toggle_sv"
            android:onClick="onToggleClicked" />

In ic_toggle_sv

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+android:id/background"
        android:drawable="@android:color/transparent"/>
    <item
        android:id="@+android:id/toggle"
        android:drawable="@drawable/ic_toggle1"/>

</layer-list>

In ic_toggle1 pass both images of Toggle Button

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/ic_toggle_stills" android:state_checked="false"/>
    <item android:drawable="@drawable/ic_toggle_videos" android:state_checked="true"/>

</selector>
Rohit
  • 810
  • 2
  • 9
  • 34
0

Try this....

private static boolean isClicked = true;
strada_chart.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        if(isClicked)
        {
            Toast.makeText(getApplicationContext(), "1", Toast.LENGTH_SHORT).show();
            isClicked = false;
        }
        else
        {
            Toast.makeText(getApplicationContext(), "2", Toast.LENGTH_SHORT).show();
        }

    }
});
user3782810
  • 113
  • 1
  • 6