Reputation: 1551
I am writing an android program. The first thing I want to make is an menu icon on the top. Here are my codes: menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/add"
android:icon="@drawable/plus"
android:title="Adding"
android:orderInCategory="100"
android:showAsAction="always"
/>
</menu>
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</LinearLayout>
and myActivity.java
package com.example.myapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuInflater;
public class MyActivity extends Activity {
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public boolean onCreateMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.add:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
The problem is the icon is in the middle but I want to put it in the right top and I dont know how...
also I have seen android:actionlayout and android:action-layout... What's the diffrence between these?!
I would be so happy if anyone can answer my question :)
Upvotes: 0
Views: 40
Reputation: 3191
Try using:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.firstactivity_menu, menu);
return true;
}
Where R.menu.firstactivity_menu should be your menu xml !
Upvotes: 1
Reputation: 599
The correct method to override is onCreateOptionsMenu(Menu menu)
. You inflate the menu in this method and returns true. It should display on the right side of the action bar.
Upvotes: 1