Wednesday, 1 March 2017

Android CheckBox In Option Menu

     Here, one linear layout inside set three child linear layout whenever user make it check or uncheck check box form the option menu then accordingly child linear layout visibility will be changed.
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context="com.androidsimplifycodes.example.MainActivity">

    <LinearLayout
        android:id="@+id/llPhone"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:visibility="gone">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Phone"
            android:textAppearance="?android:textAppearanceLarge"
            android:textColor="@color/colorAccent" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/llMail"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:visibility="gone">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Mail"
            android:textAppearance="?android:textAppearanceLarge"
            android:textColor="@color/colorAccent" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/llCalender"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Calender"
            android:textAppearance="?android:textAppearanceLarge"
            android:textColor="@color/colorAccent" />

    </LinearLayout>

</LinearLayout>
     In menu.xml file have one xml attribute like android:checkable="true | false" if true then check box will be display into your option menu.
menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_phone"
        android:checkable="true"
        android:title="Phone"
        app:showAsAction="never" />
    <item
        android:id="@+id/action_mail"
        android:checkable="true"
        android:title="Mail"
        app:showAsAction="never" />
    <item
        android:id="@+id/action_calender"
        android:checkable="true"
        android:title="Calender"
        app:showAsAction="never" />
</menu>
MainActivity.java

package com.androidsimplifycodes.example;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {

    protected LinearLayout llPhone;
    protected LinearLayout llMail;
    protected LinearLayout llCalender;
    protected SharedPreferences sharedPreferences;
    protected SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_main);
        initView();

        // sharedPreferences used for the storing the state of the check box.
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        editor = sharedPreferences.edit();
        // by default phone item if you want to make checked then make it true
        editor.putBoolean("phone", false);
        // by default mail item if you want to make checked then make it true
        editor.putBoolean("mail", false);
        // by default calender item if you want to make checked then make it true
        editor.putBoolean("calender", true);
        // commit the changes
        editor.commit();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);

        // when menu create then using stored state of the item
        // make checkable or uncheckable item.
        if (sharedPreferences.getBoolean("phone", false)) {
            // findItem id function return the id of the menu
            menu.findItem(R.id.action_phone).setChecked(true);
        } else if (sharedPreferences.getBoolean("mail", false)) {
            menu.findItem(R.id.action_mail).setChecked(true);
        } else if (sharedPreferences.getBoolean("calender", false)) {
            menu.findItem(R.id.action_calender).setChecked(true);
        }

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // previously committed editor object never use
        // so we can create new editor reference
        editor = sharedPreferences.edit();
        switch (item.getItemId()) {
            case R.id.action_phone:
                if (item.isChecked()) {
                    llPhone.setVisibility(View.GONE);
                    // make it false because user unchecked item.
                    item.setChecked(false);
                    // checked item now unchecked
                    // so make it false into sharedPreferences
                    editor.putBoolean("phone", false);
                } else {
                    item.setChecked(true);
                    llPhone.setVisibility(View.VISIBLE);
                    editor.putBoolean("phone", true);
                }
                break;
            case R.id.action_mail:
                if (item.isChecked()) {
                    item.setChecked(false);
                    llMail.setVisibility(View.GONE);
                    editor.putBoolean("mail", false);
                } else {
                    item.setChecked(true);
                    llMail.setVisibility(View.VISIBLE);
                    editor.putBoolean("mail", true);
                }
                break;
            case R.id.action_calender:
                if (item.isChecked()) {
                    item.setChecked(false);
                    editor.putBoolean("calender", false);
                    llCalender.setVisibility(View.GONE);
                } else {
                    item.setChecked(true);
                    editor.putBoolean("calender", true);
                    llCalender.setVisibility(View.VISIBLE);
                }
                break;
        }

        // required to commit the changes
        editor.commit();
        return super.onOptionsItemSelected(item);
    }

    private void initView() {
        llPhone = (LinearLayout) findViewById(R.id.llPhone);
        llMail = (LinearLayout) findViewById(R.id.llMail);
        llCalender = (LinearLayout) findViewById(R.id.llCalender);
    }
}
Output :