How to make action bar for android app tutorial




In this lesson, you'll learn to add items to the action bar. Run this compose email app from your Eclipse or android studio Emulator. The action bar content can be defined by an XML menu resource just like the menu. We have added an item Send. Weve added attribute android:showAsAction with value ifRoom|withText to the item. This tells the system to show the item in the action bar instead of the menu if possible. Now we also Add attribute android:icon with value @android:drawable/ic_menu_send to set an icon for the action bar item. To create the action bar we have implemented the onCreateOptionsMenu method. For the system the action bar and the options menu are the same, just the items are shown differently. Inside onCreateOptionsMenu we inflate the action bar from the menu resource XML R.menu.main_menu. Set android:id to @+id/mainMenuSend for the Send item. Now override the method public boolean onOptionsItemSelected(MenuItem item) in the Activity. Hint: Just type onOp inside the class body and use code completion. Use an if or switch statement to check if item.getItemId() is R.id.mainMenuSend and show a toast Sending... with Toast.makeText(...) Add Toast.make text(this, "Sending...", Toast.LENGTH LONG).show(). Congratulations! You've learned to add items to the action bar of your app. Play with the code on your own a bit. For example, add a second button. 

REQUIREMENTS:
Download:
Eclipse

Instruction:
After you download all the files that needed in this tutorial, we need to install and set-up java jdk  path to our environment variables, the step below will show you how.

--Go to Java jdk installation directory in "C:\Program Files\Java\jdk1.7.0\bin" Copy these path
--Go to the environment path by Clicking: Window taskbar--->right click Computer--->Click Advance System setting ---> then go to Advance tab Then select "Environment Variable" then create new Variable name and variable value by clicking the New radio button. With in the "Variable name" input these "JAVA_HOME" then in the "Variable value" point it to your C:\Program Files\Java\jdk1.7.0\bin 
that's it we our on a way of creating our first game and deploy it to a different platform..

After that we will also set-up Android SDK,to it's path by extracting first it to the directory you want for example in "drive D:" or "drive C:"  once we done this, we can now copy the root folder of this SDK which is likely to this format "D:\Android SDK" copy it then add it also to the "Environment path" like what you did before in "Java JDK".



COMPLETE CODE SAMPLE

MAIN ACTIVITY CODE

package com.ninalblog.myapp;

import android.app.*;
import android.os.*;
import android.widget.*;
import android.view.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
{
        super.onCreate(savedInstanceState);
        
        // Set main.xml as user interface layout
        setContentView(R.layout.main);
    }
    
    @Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate main_menu.xml 
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// TODO: Implement this method
switch(item.getItemId()){
case R.id.mainMenuSend:

    Toast.makeText(this,"Sending",Toast.LENGTH_LONG).show();
}
return super.onOptionsItemSelected(item);
}
}

MAIN XML CODE



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="left"
android:padding="20dp"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Compose email:"
android:textSize="20dp"
android:layout_marginBottom="20dp"/>

<EditText
android:inputType="textEmailSubject"
android:hint="Subject"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<EditText
android:hint="Email"
android:inputType="textEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<EditText
android:hint="Message"
android:inputType="textMultiLine"
android:layout_width="match_parent"
android:gravity="top"
android:layout_height="100dp" />
</LinearLayout>

MAIN MENU XML CODE



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical" >

<item
android:icon="@android:drawable/ic_menu_send"
android:id="@+id/mainMenuSend"
        android:title="Send" 
android:showAsAction="ifRoom|withText"/>
</menu>
</LinearLayout>


Post a Comment

0 Comments