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();
}
}
});
Try this..
Like_btn.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
if(fun)
{
Like_btn.setImageResource(R.drawable.unlike);
fun=false;
}
else
{
fun=true;
Like_btn.setImageResource(R.drawable.like);
Toast.makeText(getApplicationContext(), "Changed", Toast.LENGTH_LONG).show();
}
}
});
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>
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();
}
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.