This video will tell how to set menu items in android activity and how to perform action on that item. We will learn about onCreateOptionsMenu() and …

Nguồn:https://u-os.org/

Xem Thêm Bài Viết Khác:https://u-os.org/cong-nghe

Author

11 Comments

  1. Prince Nain Reply

    Sir, please make a video on making a app browser please sir.

  2. Oliver Ilmjarv Reply

    how do i do this with fragments ? stackoverflow answers did not help.. just cant get them to work

  3. darkman237 Reply

    Is this the only way to create a menu? I was looking for a more standard group of buttons to switch between activities.

  4. dhwani shah Reply

    package com.example.user.alertview;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    import android.widget.Toast;

    public class Menu extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu);
    }

    @Override
    public boolean onCreateOptionsMenu(android.view.Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.activity_menu, menu);

    return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.Settings:
    Toast.makeText(this, "now you configure", Toast.LENGTH_SHORT).show();
    return true;
    case R.id.about:
    Toast.makeText(this, "Dhwnia", Toast.LENGTH_SHORT).show();
    return true;
    case R.id.contact:
    Toast.makeText(this, "Sah", Toast.LENGTH_SHORT).show();
    return true;

    }
    }
    }

    I got error to get intent
    Solution?

  5. Sanjeev Singh Reply

    There are two buttons 1. Patient and 2. Doctor……. on clicking each of these popup menu appears….. then i added this onOptionsItemselected but it doesn't work at all…… nothing happens when i click the buttons under popup menu

    here the java code…

    public void onClickingPatient(View view){
    PopupMenu popup = new PopupMenu(this,view);
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.main_menu,popup.getMenu());
    popup.show();
    }

    public void onClickingDoctor(View view){
    PopupMenu popup = new PopupMenu(this,view);
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.doctor_menu,popup.getMenu());
    popup.show();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
    switch(item.getItemId()) {
    case R.id.loginAsPatient:
    View viewCurrent = LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_login_details, null);
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setMessage("Login Here");
    builder.setView(viewCurrent);
    break;
    case R.id.loginAsDoctor:
    break;
    }
    return false;
    }

Write A Comment