Table of Contents
hide
Example : How to create an Options Menu (Three Vertical Dot) in an Android App using XML and Android Java.
NB:
First of all, right click on res folder - New - Android resource directory - select menu from resource type - ok - right click on menu folder - New - Menu resource file - name option_menubars - now option_menubars.xml file created.
activity_main.xml(optional)
-----------------------
option_menubars.xml
-----------------------
MainActivity.java
---------------------
package com.rkm.menubardemo;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu opmenu)
{
getMenuInflater().inflate(R.menu.option_menubars,opmenu);
return true;
//MenuInflater inflater = getMenuInflater();
//inflater.inflate(R.menu.option_menubars, menu);
//return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item)
{
int item_id=item.getItemId();
switch (item_id)
{
case R.id.option_one:
Toast.makeText(this, "Option one Clicked", Toast.LENGTH_SHORT).show();
case R.id.option_two:
Toast.makeText(this, "Option two Clicked", Toast.LENGTH_SHORT).show();
case R.id.option_three:
Toast.makeText(this, "Option three Clicked", Toast.LENGTH_SHORT).show();
case R.id.option_four:
Toast.makeText(this, "Option four Clicked", Toast.LENGTH_SHORT).show();
default:
return super.onOptionsItemSelected(item);
}
/*------------------- OR -------------------
if (item.getItemId() == R.id.option_one)
{
Toast.makeText(getApplicationContext(), item.getTitle() + " is selected", Toast.LENGTH_SHORT).show();
}
else if (item.getItemId() == R.id.option_two)
{
Toast.makeText(getApplicationContext(), item.getTitle() + " is selected", Toast.LENGTH_SHORT).show();
}
else if (item.getItemId() == R.id.option_three)
{
Toast.makeText(getApplicationContext(), item.getTitle() + " is selected", Toast.LENGTH_SHORT).show();
}
else if (item.getItemId() == R.id.option_four)
{
Toast.makeText(getApplicationContext(), item.getTitle() + " is selected", Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);*/
}
}
Example : How to create a Context Menu in an Android App using XML and Android Java.
NB:
(i) First of all, right click on 'res' folder - New - Android resource directory - select menu from resource type - ok - right click on 'menu' folder - New - Menu resource file - put the name 'context_menu' - now 'context_menu.xml' file created.
(ii) If switch case does not work properly then, add this statement - android.nonFinalResIds=false in 'Gradle Properties' file of 'Gradle Scripts' in left pane/side.
(iii) Context Menu is a menu appear from the list of items when long press over on an item (not clicking).
activity_main_activity2_context_menu.xml
---------------------------------------------
context_menu.xml
---------------------
MainActivity2ContextMenu.java
---------------------------------
package com.rkm.myapplication;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity2ContextMenu extends AppCompatActivity
{
ListView cList;
String[] Contacts={"User1","User2","User3","User4"};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2_context_menu);
cList=(ListView) findViewById(R.id.contacts_list);
ArrayAdapter adapter= new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,Contacts);
cList.setAdapter(adapter);
registerForContextMenu(cList);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
{
getMenuInflater().inflate(R.menu.context_menu,menu);
//super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(@NonNull MenuItem item)
{
int id= item.getItemId();
switch (id)
{
case R.id.call:
Toast.makeText(this, "User wants to Call", Toast.LENGTH_SHORT).show();
return true;
case R.id.share:
Toast.makeText(this, "User wants to Share", Toast.LENGTH_SHORT).show();
return true;
case R.id.sms:
Toast.makeText(this, "User wants to send SMS", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onContextItemSelected(item);
}
}
}
Example : How to create a Popup Menu in an Android App using XML and Android Java.
NB:
(i) First of all, right click on 'res' folder - New - Android resource directory - select menu from resource type - ok - right click on 'menu' folder - New - Menu resource file - put the name 'pop_up_menu' - now 'pop_up_menu.xml' file created.
(ii) If switch case does not work properly then, add this statement - android.nonFinalResIds=false in 'Gradle Properties' file of 'Gradle Scripts' in left pane/side.
(iii) Must use implements PopupMenu.OnMenuItemClickListener in public class MainActivity extends AppCompatActivity as
public class MainActivity extends AppCompatActivity implements PopupMenu.OnMenuItemClickListener
(iv) Popup Menu is a menu appear when clicking on a Button or TextView etc.
activity_main.xml
-----------------------
pop_up_menu.xml
-----------------------
MainActivity.java
---------------------
package com.rkm.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements PopupMenu.OnMenuItemClickListener
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void showPopup(View v)
{
PopupMenu popup= new PopupMenu(this, v);
popup.setOnMenuItemClickListener(this);
popup.inflate(R.menu.pop_up_menu);
popup.show();
}
@Override
public boolean onMenuItemClick(MenuItem item)
{
switch (item.getItemId())
{
case R.id.option1:
Toast.makeText(this,"Option1 Clicked",Toast.LENGTH_SHORT).show();
return true;
case R.id.option2:
Toast.makeText(this,"Option2 Clicked",Toast.LENGTH_SHORT).show();
return true;
case R.id.option3:
Toast.makeText(this,"Option3 Clicked",Toast.LENGTH_SHORT).show();
return true;
default:
return false;
}
}
}
0 Comments