Android create custom overflow menu item

后端 未结 8 1141
小鲜肉
小鲜肉 2020-12-16 16:54

I want to create a custom overflow menu item in my ActionBar in addition at the Setting item like described in the image below:

8条回答
  •  春和景丽
    2020-12-16 17:34

    This will help you. I had coded this for overflow menu

    In menu/main.xml file

    
        
            
    
            
    
            
        
    
    

    And here is your java code:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    
        getMenuInflater().inflate(R.menu.main, menu);
    
        return true;
    
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    
        // Handle action bar actions click
    
        super.onOptionsItemSelected(item);
        if(item.getItemId() == R.id.facebook){
            Toast.makeText(Getstarted.this, "Option pressed= facebook",Toast.LENGTH_LONG).show();
        }
        else if(item.getItemId() == R.id.Youtube){
            Toast.makeText(Getstarted.this, "Option pressed= youtube",Toast.LENGTH_LONG).show();
        }
        else if(item.getItemId() == R.id.Twitter){
            Toast.makeText(Getstarted.this, "Option pressed= twitter",Toast.LENGTH_LONG).show();
        }
        return true;    
    
    }
    

提交回复
热议问题