public class MyActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_buscar:
Toast.makeText(getApplicationContext(), "BUSCAR", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_cortar:
Toast.makeText(getApplicationContext(), "CORTAR", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_copiar:
Toast.makeText(getApplicationContext(), "COPIAR", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_eliminar:
Toast.makeText(getApplicationContext(), "ELIMINAR", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_compartir:
Toast.makeText(getApplicationContext(), "COMPARTIR", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
activity_my.xml
<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" tools:context=".MyActivity"> <TextView android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context=".MyActivity" > <item android:id="@+id/menu_buscar" android:title="buscar" android:icon="@drawable/ic_action_search" app:showAsAction="ifRoom|withText"/> <item android:id="@+id/menu_cortar" android:title="cortar" android:icon="@drawable/ic_action_cut" app:showAsAction="ifRoom|withText"/> <item android:id="@+id/menu_copiar" android:title="copiar" android:icon="@drawable/ic_action_copy" app:showAsAction="ifRoom|withText"/> <item android:id="@+id/menu_eliminar" android:title="eliminar" android:icon="@drawable/ic_action_delete" app:showAsAction="ifRoom|withText"/> <item android:id="@+id/menu_compartir" android:title="compartir" android:icon="@drawable/ic_action_share" app:showAsAction="ifRoom|withText"/> </menu>
AndroidManifest.xml
Debemos agregar esto
Debemos agregar esto
android:uiOptions="splitActionBarWhenNarrow"
Nos debería quedar asi
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.tatoado.actionbardividir" > <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MyActivity" android:label="@string/app_name" android:uiOptions="splitActionBarWhenNarrow"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>