In this tutorial exercise, we will show you how to use the Choice Menu in the Android app. For Android applications, you can use three in-menu menus, Options menu, alternate menu, and PopPup menu. At the point when the client presses the menu button on their Android device. Then a Options menu appears. This is a common feature in all apps, so we have to use the Option Menu in our App.Why the Options Menu is where a variety of alternatives are used to provide additional data about the app, and it connects the setup options to it in case you need to make a selection menu in your app. Follow the code given below.
Step 1.First start creating a project in your Android Studio After the project is done, you will see that two files are visible, first the function_main .xml and the second MainActivity.java
activity_main.xml <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/Rate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Step 2. To create option menu, first of all you have to create Menu folder. To create Menu folder, first you have to right-click on the res folder then apear new menu (Android resouce directory) and after clicking on Android resource directory, you will create menu folder by clicking on Android resource directory.
Step 3. Now you will see a menu folder, by right-clicking on that menu folder, you will have to create a new xml layout in which you can give the name of your choice.
main_menu.xml <?xml version="1.0" encoding="utf-8"?>
<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:title="shareApp"
android:icon="@drawable/ic_share"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/more"
android:title="More"/>
<item
android:id="@+id/help"
android:title="Help"/>
<item
android:id="@+id/contact"
android:title="Contact Us"/>
</menu>
When you do a project, you will get an activity_main.xml, so you should use the same xml, it will be like this
Now after this you have to paste this code in the MainActivity.java class
MainActivity.java
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.viewpager.widget.ViewPager;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import com.google.android.material.tabs.TabLayout;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu,menu); // here main_menu is folder name
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.share:
Toast.makeText(getApplicationContext(),"click on share",Toast.LENGTH_LONG).show();
return true;
case R.id.more:
Toast.makeText(getApplicationContext(),"click on smore",Toast.LENGTH_LONG).show();
return true;
case R.id.help:
Toast.makeText(getApplicationContext(),"click on help",Toast.LENGTH_LONG).show();
return true;
case R.id.contact:
Toast.makeText(getApplicationContext(),"click on contact ",Toast.LENGTH_LONG).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
This is also useful for you
0 Comments
Please do not enter any spam link in the comment box
Emoji