Tuesday, May 26, 2015

ToolBar Android Example

                    ToolBar Example Output :

ToolBar Output
Project Structure : 

Project Structure

Step 1 :
Add dependencies in "build.gradle" file in Android Studio

dependencies {
    compile fileTree(dir'libs'include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.1.1'    

    compile 'com.android.support:cardview-v7:21.0.3'}

Note : here i can import new module => "cardslib-core-2.0.1 aar" file from :
 http://mvnrepository.com/artifact/com.github.gabrielemariotti.cards/cardslib-core/2.0.1

Step 2 :
activity_main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#FFFFFF">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/ColorPrimary"
        android:elevation="5dp"
        android:layout_gravity="top"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="10sp"
                  android:textAppearance="@android:style/TextAppearance.DeviceDefault.Large"
            android:text="Toolbar Example"
            android:textColor="#000000"/>
</LinearLayout>

Step 3 :
menu_main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/androi"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity">
    <item
        android:id="@+id/action_search"
        android:icon="@drawable/abc_ic_search_api_mtrl_alpha"
        android:orderInCategory="200"
        android:title="Search"
        app:showAsAction="ifRoom" />
    <item
        android:id="@+id/action_user"
        android:icon="@drawable/ic_action_my_cart"
        android:orderInCategory="300"
        android:title="User"
        app:showAsAction="ifRoom" />
</menu>

Step 4 :
styles.xml

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
</resources>

Step 5 :
Note : if color.xml is not exist then create "color.xml" in "values" folder
color.xml


<?xml version="1.0" encoding="utf-8"?>

<resources>

    <color name="ColorPrimary">#ff7caaff</color>

    <color name="ColorPrimaryDark">#ff6ce2ff</color>


</resources>

Step 6 :
MainActivity.java

package com.pratik.toolbarexample;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
private Toolbar toolBar;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);   
setContentView(R.layout.activity_main);
toolBar = (Toolbar) findViewById(R.id.toolBar);
setSupportActionBar(toolBar);

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {

//Inflate the menu; this adds items to the action bar if it is present. 
getMenuInflater().inflate(R.menu.menu_main, menu);   
return true;
@Override
public boolean
onOptionsItemSelected(MenuItem item) {
// Handle actionbar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml. 
int id = item.getItemId();
//noinspection SimplifiableIfStatement 
    if(id == R.id.action_bar) {
    return true;
    }
return super.onOptionsItemSelected(item);
}
}

Step 7 :

Run this app and Enjoy........
download this project from below link : 

https://drive.google.com/file/d/0By1zg6cvLTQ9UkJ0V1FoSmxKOGc/view?usp=sharing

No comments:

Post a Comment