Friday, January 29, 2016

Android SubMenu Example

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="0dp"
    tools:context=".MainActivity"
    android:background="#fdedf6"
    >
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        />
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Change This Text Style\nUsing Toolbar Menu"
        android:textSize="35dp"
        android:gravity="center"
        android:textStyle="bold"
        android:fontFamily="sans-serif-condensed"
        />
</RelativeLayout>
res/menu/toolbar_options_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/text_color"
        android:title="Text Color"
        app:showAsAction="always|withText"
        android:icon="@drawable/ic_palette_black_36dp"
        android:orderInCategory="1"
        >
        <!-- Starting the text color sub menu which have three items-->
        <menu>
            <item
                android:id="@+id/red"
                android:title="Red"
                android:icon="@drawable/ic_palette_red_36dp"
                />
            <item
                android:id="@+id/green"
                android:title="Green"
                android:icon="@drawable/ic_palette_green_36dp"
                />
            <item
                android:id="@+id/blue"
                android:title="Blue"
                android:icon="@drawable/ic_palette_blue_36dp"
                />
        </menu>
    </item>
    <item
        android:id="@+id/large_text"
        android:title="Large"
        app:showAsAction="ifRoom|withText"
        android:orderInCategory="2"
        android:icon="@drawable/ic_text_fields_black_36dp"
        />
    <item
        android:id="@+id/medium_text"
        android:title="Medium"
        app:showAsAction="never|withText"
        android:orderInCategory="3"
        android:icon="@drawable/ic_text_fields_black_36dp"
        />
    <item
        android:id="@+id/small_text"
        android:title="Small Text"
        app:showAsAction="never|withText"
        android:orderInCategory="4"
        android:icon="@drawable/ic_text_fields_black_36dp"
        />
</menu>
res/values/styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.support.v7.widget.Toolbar;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private Activity mActivity;

    private RelativeLayout mRelativeLayout;
    private TextView mTextView;
    private Toolbar mToolbar;


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

        // Get the application context
        mContext = getApplicationContext();

        // Get the activity
        mActivity = MainActivity.this;

        // Get the widgets reference from XML layout
        mRelativeLayout = (RelativeLayout) findViewById(R.id.rl);
        mTextView = (TextView) findViewById(R.id.tv);
        mToolbar = (Toolbar) findViewById(R.id.toolbar);

        // Set a title for toolbar
        mToolbar.setTitle("Android SubMenu Example");
        //mToolbar.setTitleTextColor(Color.WHITE);

        // Set support actionbar with toolbar
        setSupportActionBar(mToolbar);

        // Change the toolbar background color
        mToolbar.setBackgroundColor(Color.parseColor("#FF8BCEF2"));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.toolbar_options_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch(item.getItemId()){
            case R.id.red:
                // Set the text color to red
                mTextView.setTextColor(Color.RED);
                return true;
            case R.id.green:
                // Set the text color to green
                mTextView.setTextColor(Color.GREEN);
                return true;
            case R.id.blue:
                // Set the text color to blue
                mTextView.setTextColor(Color.BLUE);
                return true;
            case R.id.large_text:
                // Set the text size to large
                mTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 50);
                return true;
            case R.id.medium_text:
                // Set the text size to medium
                mTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,35);
                return true;
            case R.id.small_text:
                // Set the text size to small
                mTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 25);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}
build.gradle [dependencies]

compile 'com.android.support:appcompat-v7:23.1.1'