android popup menu in android studio example
Hello Guys. Welcome to our new tutorial of android popup menu example using android studio. basically android pop up menu shows a list of items or menu in a model popup window anchored to a View. pop up menu like a simple menu.
please follow the code given below to display Analog clock and Digital clock in android.
- First Create a new project in Android Studio
- File ⇒ New Android ⇒ Application Project
- Then Open src -> package -> MainActivity.java and then add following code :
JAVA (MainActivity.java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.PopupMenu; import android.widget.Toast; public class PopupMenuu extends AppCompatActivity implements PopupMenu.OnMenuItemClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_popup_menu); Button btn = (Button) findViewById(R.id.btnShow); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { PopupMenu popup = new PopupMenu(PopupMenuu.this, v); popup.setOnMenuItemClickListener(PopupMenuu.this); popup.inflate(R.menu.popup_menu); popup.show(); } }); } @Override public boolean onMenuItemClick(MenuItem item) { android.widget.Toast.makeText(this, "Selected Item: " +item.getTitle(), Toast.LENGTH_SHORT).show(); switch (item.getItemId()) { case R.id.search_item: // do your code return true; case R.id.copy_item: // do your code return true; case R.id.print_item: // do your code return true; default: return false; } } } |
- Now Open res -> layout -> activity_main.xml and then add following code :
XML (activity_main.xml)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/btnShow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show Popup Menu" app:layout_constraintBottom_toTopOf="@+id/adView" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout> |