Android Menus and Action Bar - Hallo sahabat Google Android Developer Tutorial, Pada Artikel yang anda baca kali ini dengan judul Android Menus and Action Bar, kami telah mempersiapkan artikel ini dengan baik untuk anda baca dan ambil informasi didalamnya. mudah-mudahan isi postingan
Artikel Android menus and action bar tutorial, yang kami tulis ini dapat anda pahami. baiklah, selamat membaca.
Judul : Android Menus and Action Bar
link : Android Menus and Action Bar
Figure 3 - Adding images to the
3.Copy the code in MainActivity.java. Activity.
Anda sekarang membaca artikel Android Menus and Action Bar dengan alamat link https://googleandroiddevelopertutorial.blogspot.com/2012/09/android-menus-and-action-bar.html
Judul : Android Menus and Action Bar
link : Android Menus and Action Bar
Android Menus and Action Bar
Android Menus and Action Bar
The Tutorial focuses on following views. The views discussed include:
- Context Menu
- Options Menu
Menus are useful for displaying additional options that are not directly visible on the main UI of an application. There are two main types of menus in Android:
- Context Menu – displays information related to a particular view on an activity. In Android, to activate a context menu you press and hold on to it.
- Options Menu – displays information related to the current activity. In Android, you activate the options menu by pressing the MENU key.
Figure 1 shows an example of an options menu in the Browser application. The option menu is displayed whenever the user presses the MENU button. The menu items displayed is dependent on the current activity that is running.
Figure 2 shows a context menu that is displayed when the user presses and holds on a hyperlink displayed on the page. The menu items displayed are dependent on the component or view currently selected. To activate the context menu, the user selects an item on the screen and either presses and holds it or simply presses the center button on the directional keypad.
If you're running your code on Android 3.0+, the icons in the menu are not shown by design. This is a design decision by Google.You are recommended to use Action Bar.
Action Bar
Android action bar was introduced to maintain a consistent navigation across the application. It has the powerful capabilities like adapting to screen configurations (landscape & portrait), prioritizing important actions, adding widgets to action bar (search, sharing etc.), providing navigation between screens (drop-down & tabbed navigation) and much more.
Action bar is introduced in android 3.0 (API level 11), but if you want your app to support action bar in older versions too, then use Support Library to make it compatible with older versions (Android 2.1 and above)
Here the important xml attributes should be known are
android:icon – Defines the icon of the action item.
android:title – Title for the icon.
android:showAsAction – Defines the visibility of the action item. It accepts following values.
android:title – Title for the icon.
android:showAsAction – Defines the visibility of the action item. It accepts following values.
ifRoom | Displays the icon if there is space available on the screen |
never | Never places this icon on the action bar |
always | Forces to display the icon always irrespective of space available. This way is not suggested. |
withText | Displays a text along with the icon. Normally the text value defined by android:title will be displayed |
collapseActionView | Defines the action layout associated with it. This action view defined usingandroid:actionLayout or android:actionViewClass |
2.Copy the code to the file activity_main.xml in res/layout folder
<?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:orientation="vertical" >
<Button
android:id="@+id/btn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="LongClick" />
</LinearLayout>
2.Copy the code to the file main.xml in res/menu folder
2.Copy the code to the file main.xml in res/menu folder
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_srm"
android:icon="@drawable/srm"
android:showAsAction="never"
android:title="About SRM"/>
<item
android:id="@+id/menu_campus"
android:icon="@drawable/campus"
android:showAsAction="never"
android:title="Campus"/>
<item
android:id="@+id/menu_courses"
android:icon="@drawable/courses"
android:showAsAction="never"
android:title="Courses"/>
<item
android:id="@+id/menu_location"
android:icon="@drawable/location"
android:showAsAction="never"
android:title="Location"/>
</menu>
Figure 3 - Adding images to the
res/drawable
folder3.Copy the code in MainActivity.java. Activity.
packagein.ac.srmuniv.actionbar_menututorial;
importandroid.app.Activity;
import android.os.Bundle;
importandroid.view.ContextMenu;
importandroid.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
importandroid.view.MenuInflater;
importandroid.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
importandroid.widget.Button;
importandroid.widget.Toast;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btn1);
btn.setOnCreateContextMenuListener(this);
}
@Override
public booleanonCreateOptionsMenu(Menu menu) {
CreateMenu(menu);
return super.onCreateOptionsMenu(menu);
}
private void CreateMenu(Menu menu) {
/*
* SubMenu sub = menu.addSubMenu("More");
* sub.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
* sub.getItem().setIcon(R.drawable.actionbar);
*/
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main, menu);
}
@Override
public booleanonOptionsItemSelected(MenuItem item) {
MenuChoice(item);
return super.onOptionsItemSelected(item);
}
@Override
public booleanonContextItemSelected(MenuItem item) {
MenuChoice(item);
return super.onContextItemSelected(item);
}
public voidonCreateContextMenu(ContextMenu menu, View view,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, view, menuInfo);
CreateMenu(menu);
}
private boolean MenuChoice(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_srm:
Toast.makeText(this, "You clicked on About SRM", Toast.LENGTH_LONG)
.show();
return true;
case R.id.menu_campus:
Toast.makeText(this, "You clicked on Campus", Toast.LENGTH_LONG)
.show();
return true;
case R.id.menu_courses:
Toast.makeText(this, "You clicked on Courses", Toast.LENGTH_LONG)
.show();
return true;
case R.id.menu_location:
Toast.makeText(this, "You clicked on Location", Toast.LENGTH_LONG)
.show();
return true;
}
return false;
}
}
4.No need to modify Androidmanifest.xml
<?xml version="1.0"
encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="in.ac.srmuniv.actionbar_menututorial"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="in.ac.srmuniv.actionbar_menututorial.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>
5.Run the application in the emulator.
Figure 4 - Displaying a options menu in your application
Figure 5 - Displaying context menu in your application
CODE EXPLANATION
The main.xml in res/menu folder contains entries of menu item attributes such as icons,menu title etc.The attribute android:showAsAction="never" makes the optionMenu available through menu button click.
The
The
CreateMenu()
method creates a list of menu items available for the activity. MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main, menu); The
MenuChoice()
method displays the selected menu item using the Toast
class. Button btn = (Button) findViewById(R.id.btn1);
btn.setOnCreateContextMenuListener(this);
To display and use the context menu for your activity, you need to override two methods 1.
onCreateContextMenu()
2.onContextItemSelected()
.Like the Options menu, you need to override theonCreateContextMenu()
andonContextItemSelected()
methods. Press the Button view and hold for a few seconds.You will now see the context menu (see Figuri).
If the options menu has more than six items, there will be aMore menu item to represent the additional menu
items.
Action Bar
Change the menu attribute android:showAsAction="always" makes the optionMenu available through Action Bar.
6.Copy the code to the file main.xml in res/menu folder
<menu xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
android:id="@+id/menu_srm"
android:icon="@drawable/srm"
android:showAsAction="always"
android:title="About SRM"/>
<item
android:id="@+id/menu_campus"
android:icon="@drawable/campus"
android:showAsAction="always"
android:title="Campus"/>
<item
android:id="@+id/menu_courses"
android:icon="@drawable/courses"
android:showAsAction="always"
android:title="Courses"/>
<item
android:id="@+id/menu_location"
android:icon="@drawable/location"
android:showAsAction="always"
android:title="Location"/>
</menu>
Demikianlah Artikel Android Menus and Action Bar
Sekianlah artikel Android Menus and Action Bar kali ini, mudah-mudahan bisa memberi manfaat untuk anda semua. baiklah, sampai jumpa di postingan artikel lainnya.
Anda sekarang membaca artikel Android Menus and Action Bar dengan alamat link https://googleandroiddevelopertutorial.blogspot.com/2012/09/android-menus-and-action-bar.html
Android Menus and Action Bar
4/
5
Oleh
Unknown