Menus - Option Menu (Menu and App Bar)
It is the basic structure of menu item for an activity.
To create a menu for an activity, we need to create a folder in res with name menu and create and menu as
main_menu.xml
|
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/share"
android:icon="@android:drawable/ic_menu_share"
android:title="Share"
app:showAsAction="always" />
<item
android:id="@+id/wishlist"
android:icon="@android:drawable/star_on"
android:title="Wishlist"
app:showAsAction="ifRoom" />
<item
android:id="@+id/view"
android:icon="@android:drawable/ic_menu_view"
android:title="View"
app:showAsAction="withText" />
<item
android:id="@+id/save"
android:icon="@android:drawable/ic_menu_save"
android:title="Save"
app:showAsAction="withText" />
</menu>
|
activity_main.xml
|
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textColor="#000000"
android:text="@string/app_name" />
</LinearLayout>
|
MainActivity.java
|
public class MainActivity extends AppCompatActivity {
TextView message;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
message=findViewById(R.id.message);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.share:
message.setText("Share it.");
break;
case R.id.wishlist:
message.setText("Add to Wishlist.");
Toast.makeText(MainActivity.this,"Added",Toast.LENGTH_LONG).show();
break;
case R.id.view:
message.setText("View it");
break;
case R.id.save:
message.setText("Save");
break;
}
return super.onOptionsItemSelected(item);
}
}
|
ReplyDeleteReally, it's a great blog.
Thank you for sharing useful information with us.
Morpheus tv apK is the best application which streams our movies and tv shows with high-quality videos just of no cost it’s free.
I think it may useful for each one. If you want to know more, then check
Morpheus TV For PC
Morpheus TV For APK
Your Affiliate Money Printing Machine is waiting -
ReplyDeleteAnd making money with it is as simple as 1--2--3!
Here's how it works...
STEP 1. Tell the system which affiliate products the system will advertise
STEP 2. Add some PUSH BUTTON traffic (this LITERALLY takes 2 minutes)
STEP 3. See how the system grow your list and sell your affiliate products all for you!
Do you want to start making money??
Your MONEY MAKING affiliate solution is RIGHT HERE