swati saoji swati saoji - 4 months ago 20
Android Question

Custom Buttons in ToolBar not responding to actions

I have a very basic application , and I am trying to put a checkbox in toolbar .

This is how my xml looks :

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="xxxxxxxx.xx.xx.HomeActivity">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:padding="5dp"
app:popupTheme="@style/AppTheme.PopupOverlay">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText
android:id="@+id/searchET"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="8"
android:background="@android:color/white"/>

<CheckBox
android:id="@+id/favorite_toggle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
/>
</LinearLayout>
</android.support.v7.widget.Toolbar>


</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_home" />

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>


In my MainActivity I set a simple listener :

favoriteBtn = (CheckBox) findViewById(R.id.favorite_toggle);

favoriteBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if(isChecked){
Toast.makeText(HomeActivity.this, "changed", Toast.LENGTH_SHORT);
addFavorite(url);
}else{
Toast.makeText(HomeActivity.this, "again changed", Toast.LENGTH_SHORT);
removeFavorite(url);
}
}
});


When I click the checkbox, nothing happens , the checkbox toggles but I dont see the Toast . I see following messages in log

D/ViewRootImpl: ViewPostImeInputStage processPointer 1

Can someone give me pointers on what could be wrong .

Update: I think its just the toasts not showing up .

Answer

To show toast you must use method show. So in Your code :

Toast.makeText(HomeActivity.this, "changed", Toast.LENGTH_SHORT).show();