ActionBar:
2.Simple Copy and Past the below code: actvitiy_main.xml
ActionBar are added on the top of the Title bar in an Activity screen.
Action Bar in Android is used to provide navigation and to
perform some actions.
In Android ActionBar Break up into 4 parts like Application ICON, View Control,
Action Buttons, OverFlow.
In the above figure its clearly seen that on Number 1 is an App Icon,
2 View Control
3 Action Buttons
4 Overflow
OUT PUT Of The Tutorial:
OUT PUT Of The Tutorial:
Create Android Application:
- File >> New >> Android Application
- Enter Project name: ActionBarApp
- Package: com.ambilpursunil.newapp
- Keep other default selections, click Next until you reach Finish

1.Simply Copy and Past the Code which is display below in the MainActivity.java
package com.ambilpursunil.newapp;
import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar
actionBar = getActionBar();
//actionBar.setTitle("Hello");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the
menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.phone:
Toast.makeText(getApplicationContext(),
"You
Selected Phone", Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
}
<RelativeLayout
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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.addingactionbuttons.MainActivity"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
3.Change the main.xml file which is located res>>main>>main.xml Copy and Past the below code:
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.addingactionbuttons.MainActivity"
>
<item
android:id="@+id/phone"
android:title="@string/phone"
android:showAsAction="ifRoom|withText"
android:icon="@drawable/phone"/>
<item
android:id="@+id/computer"
android:title="@string/computer"
android:showAsAction="always"
android:icon="@drawable/computer"/>
<item
android:id="@+id/gamepad"
android:title="@string/gamepad"
android:showAsAction="always"
android:icon="@drawable/gamepad"/>
<item
android:id="@+id/video"
android:title="@string/video"
android:showAsAction="ifRoom"
android:icon="@drawable/video"/>
<item
android:id="@+id/email"
android:title="@string/email"
android:showAsAction="ifRoom"
android:icon="@drawable/email"/>
<item
android:id="@+id/camera"
android:title="@string/camera"
android:showAsAction="always"
android:icon="@drawable/camera"/>
</menu>
4.Copy and past the code for manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ambilpursunil.newapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="18"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
When our application launch Just check.....it
Please Send Your Comments To ambilpura.sunil@gmail.com
Stay Tune For Next Tutorial... Custom Toast In Android:
No comments:
Post a Comment