Does anyone know how I can create some sort of drop down or pop up on a page so that i can then go to any page from the page that i am on ?
For example i have a menu page but when i select an option i want to be able to navigate to another page instead of going back to the menu
public class PopUpMenu extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.popupmenu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.location:
Intent intent1 = new Intent(getApplicationContext(), MyLocation.class);
startActivity(intent1);
return true;
case R.id.search:
Intent intent2 = new Intent(getApplicationContext(), Search.class);
startActivity(intent2);
return true;
case R.id.add:
Intent intent3 = new Intent(getApplicationContext(), AddSite.class);
startActivity(intent3);
return true;
// case R.id.help:
// Intent intent4 = new Intent(getApplicationContext(), Help.class);
// startActivity(intent4);
// return true;
case R.id.exit:
Intent intent5 = new Intent(Intent.ACTION_MAIN);
intent5.addCategory(Intent.CATEGORY_HOME);
intent5.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent5);
default:
return super.onOptionsItemSelected(item);
}
}
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/location" android:title="My Location" />
<item android:id="#+id/search" android:title="Search" />
<item android:id="#+id/add" android:title="Add Site" />
<item android:id="#+id/help" android:title="Help" />
<item android:id="#+id/exit" android:title="Exit" />
</menu>
First of all, here is a good tutorial for you: Activity Testing
You have a couple of samples on the developers.android site.
The most comprehensive is the API Demos which includes a wide specter of tools and techniques.
Then, for your purpose, the widget is named Spinner, and a demo of it is Spinner Test.
The menu (in XML):
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/food" android:title="Food" />
<item android:id="#+id/other" android:title="Other" />
</menu>
To set the menu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mymenu, menu);
return true;
}
To handle switching activities:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.food:
Intent intent1 = new Intent(getContext(), Food.class);
startActivity(intent1);
return true;
case R.id.other:
Intent intent2 = new Intent(getContext(), Other.class);
startActivity(intent2);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Related
I'm having a popup menu on click of action bar button. When i click on the action bar button I'm getting my popup window. But i want to open another activities on clicking the popup menu items. How could i do that?
Following are my code snippets.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
#SuppressLint("NewApi") #Override
public boolean onOptionsItemSelected(MenuItem item) {
View menuItemView = findViewById(R.id.action_button);
PopupMenu popupMenu = new PopupMenu(this, menuItemView);
popupMenu.inflate(R.menu.popup);
popupMenu.show();
return true;
}
and my popup menu is as follows,
<menu xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<item
android:id="#+id/one"
android:title="About"
android:visible="true"
android:showAsAction="ifRoom|withText"/>
<item
android:id="#+id/two"
android:title="Contact Us"
android:visible="true"
android:showAsAction="ifRoom|withText"/>
</menu>
What i want to do is, when i click on these menu items another activities has to be opened. How could i do that?
Can someone help me please. Thanks in advance.
Use the id to launch the activity using switch statement with menu itemId
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.one:
Intent intent1 =new Intent(this,ActivityOne.class);//firstActivity
startActivity(intent1);
return true;
case R.id.two:
Intent intent2 =new Intent(this,ActivityTwo.class);//second Activity
startActivity(intent2);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Try this
popupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(getApplicationContext(),
item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
});
To open activity on popup menu click:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.menu_item1:
Intent intent = new Intent(this, ActivityForItemOne.class);
this.startActivity(intent);
break;
case R.id.menu_item2:
// another startActivity, this is for item with id "menu_item2"
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
I want to add multiple activities under one `onCreateOptionsMenu(Menu menu) in my android app, I have already added two activities and they are working fine but third activity isn't working, following is my code
onCreateOptionsMenu(Menu menu)
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public final boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_share:
shareURL();
}
if(item.getItemId() == R.id.menu_item_refresh){
mWebView.reload();
return true;
}
if(item.getItemId() == R.id.share_this_app)
mShareActionProvider.setShareIntent(getDefaultShareIntent());
return super.onOptionsItemSelected(item);
}
private void shareURL() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, mWebView.getUrl());
startActivity(Intent.createChooser(shareIntent, "Share This Website!"));
shareIntent.setPackage("com.whatsapp");
}
/** Returns a share intent */
private Intent getDefaultShareIntent(){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "download the app");
intent.putExtra(Intent.EXTRA_TEXT," play.google.com ");
return intent;
}
menu_main.xml
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item
android:title="#string/share"
android:id="#+id/menu_item_share"
android:showAsAction="always"
android:icon="#drawable/share"
/>
<item
android:id="#id/menu_item_refresh"
android:title="Refresh"
android:showAsAction="never"
android:icon="#drawable/refresh"
/>
<item
android:id="#+id/share_this_app"
android:title="Share this app"
android:showAsAction="never"
android:actionProviderClass="android.widget.ShareActionProvider"/>
From above, menu_item_share and menu_item_refresh is working, but Share this app isn't working.
It's probably your are missing a return true statement below this line mShareActionProvider.setShareIntent(getDefaultShareIntent());. You can simply follow a clean structure to achieve your task.
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_share: {
shareURL();
break; //or, return true;
}
case R.id.menu_item_refresh: {
mWebView.reload();
break; //or, return true;
}
case R.id.share_this_app: {
mShareActionProvider.setShareIntent(getDefaultShareIntent());
break; //or, return true;
}
return super.onOptionsItemSelected(item);
}
Hey I think this would help...
You have written
android:showAsAction="never".
So it would generate an error while putting an icon inside the menu icon(three dots at toolbar).
Hence put the value "always" or "ifRoom" for this attribute.
Hope this would work :-)
I have a problem with android settings. I want to create settings for changing the background color of an activity. What do I have to do?
I have layout:
public class MyApp extends PreferenceActivity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void display(View view)
{
Intent intent = new Intent(this, Display.class);
startActivity(intent);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu, menu);
return true;
}
public boolean onOptionsItemSelected( MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_about:
Intent menu_about = new Intent(this, About.class);
startActivity(menu_about);
return true;
case R.id.menu_copyright:
Intent menu_copyright = new Intent(this, Copyright.class);
startActivity(menu_copyright);
return true;
case R.id.menu_settings:
// ACTIVITY OF SETTINGS
return true;
case R.id.menu_exit:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
};
}
I want preference like this:
http://i.stack.imgur.com/k2qA5.png
Now, did You understend me?
Create SharedPreferences this way
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("PrefName", VALUE);
editor.commit();
Get its values this way
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
String s = settings.getString("PrefName", ""));
The last statement means you are looking for the value of "PrefName" and setting "" if nothing is found.
Hope it helps
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/settings"
android:title="Settings"
>
<menu>
<item android:id="#+id/red"
android:title="Red" />
<item android:id="#+id/Blue"
android:title="Blue" />
</menu>
</item>
</menu>
AndroidMenusActivity.java
public class AndroidMenusActivity extends Activity {
LinearLayout li;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
li= findViewById(R.id.layoutid);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.red:
li.setBackgroundColor("#ff0000");
break;
case R.id.blue:
li.setBackgroundColor("#0000ff");
break;
}
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutid"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</LinearLayout>
Did you mean Change the activity background over XML file?
android:background="#android:color/xxxxx"
Pick one of listed color.
I'd like to customize options menu in my app. After some googling and making a lot of effort I am still unable to do it. Here is what I want:
menu.xml file:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_first"
android:icon="#drawable/phone_selector"
android:title="#string/first"/>
<item
android:id="#+id/menu_second"
android:icon="#drawable/butterfly_selector"
android:title="#string/second"/>
<item
android:id="#+id/menu_third"
android:icon="#drawable/umbrella_selector"
android:title="#string/third"/>
</menu>
OptionsMenuActivity.java:
public class OptionsMenuActivity extends Activity {
private Menu currentMenu;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
this.currentMenu = menu;
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
setMenuItemIconState(menu, MyApplication.getSelectedMenuItemDrawIcon(), false);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_phone:
setMenuItemIconState(currentMenu, MyApplication.getSelectedMenuItemDrawIcon(), true);
MyApplication.setSelectedMenuItemDrawIcon(R.drawable.phone_active);
Intent intent = new Intent(OptionsMenuActivity.this, FirstActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
case R.id.menu_butterfly:
setMenuItemIconState(currentMenu, MyApplication.getSelectedMenuItemDrawIcon(), true);
MyApplication.setSelectedMenuItemDrawIcon(R.drawable.butterfly_active);
intent = new Intent(OptionsMenuActivity.this, SecondActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
case R.id.menu_umbrella:
setMenuItemIconState(currentMenu, MyApplication.getSelectedMenuItemDrawIcon(), true);
MyApplication.setSelectedMenuItemDrawIcon(R.drawable.umbrella_active);
intent = new Intent(OptionsMenuActivity.this, ThirdActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void setMenuItemIconState(Menu menu, int menuItemIconRes, boolean switchLastActiveMenuItemIcon) {
int menuItemId = 0;
int lastDeActiveDrawIcon = 0;
switch (menuItemIconRes) {
case R.drawable.phone_active:
menuItemId = R.id.menu_first;
lastDeActiveDrawIcon = R.drawable.phone;
break;
case R.drawable.butterfly_active:
menuItemId = R.id.menu_second;
lastDeActiveDrawIcon = R.drawable.butterfly;
break;
case R.drawable.umbrella_active:
menuItemId = R.id.menu_third;
lastDeActiveDrawIcon = R.drawable.umbrella;
break;
}
if (switchLastActiveMenuItemIcon) {
MenuItem menuItem = menu.findItem(menuItemId);
menuItem.setIcon(lastDeActiveDrawIcon);
return;
}
MenuItem menuItem = menu.findItem(menuItemId);
menuItem.setIcon(MyApplication.getSelectedMenuItemDrawIcon());
}
}
In MyApplication.java I have only a getter and a setter of selectedMenuItemDrawIcon.I managed to set the icons properly, but failed with the others.
Is it possible to customize options menu like that? If not, is there any other approaches for creating an xml file and make it appear and disappear smoothly just like basic menu of android.
Any help would be appreciated. Thanks in advance, (please provide with code example).
When my menu is clicked two times onoptionitemselected is called. how to stop it
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.docmenu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.upload:
Log.e("testing", "called");
return true;
case R.id.back:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
my menu xml is
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/upload"
android:icon="#drawable/menu_upload"
android:title="#string/upload" />
<item android:id="#+id/back"
android:icon="#drawable/menu_back"
android:title="#string/back" />
</menu>
when upload icon is selected. In log testing called is printed two times.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.Aboutus:
final Dialog d1 = new Dialog(Welcome.this);
d1.setContentView(R.layout.aboutus);
d1.show();
break;
And make sure that you have created folder under res named menu. and make new menu.xml file
and put code like this in menu.xml file as follows:
<?xml version="1.0" encoding="utf-8"?>
<item android:id="#+id/Aboutus"
android:title="About Us" android:icon="#drawable/ic_menu_about_us" />
<item android:id="#+id/Settings"
android:title="Settings" android:icon="#drawable/ic_menu_settings"/>
<item android:id="#+id/help"
android:title="Help" android:icon="#drawable/ic_menu_help" />
onOptionsItemSelected return true is ok
Try this code...
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_settings:
Toast.makeText(getApplicationContext(), "Settings", Toast.LENGTH_LONG).show();
break;
case R.id.my_settings:
Toast.makeText(getApplicationContext(), "Home Page", Toast.LENGTH_LONG).show();
break;
default:
Toast.makeText(getApplicationContext(), "Exit", Toast.LENGTH_LONG).show();
}
return super.onOptionsItemSelected(item);
}
And create a new xml in menu folder and apply this code.
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_settings"
android:title="#string/menu_settings"
android:orderInCategory="100"
android:showAsAction="never" />
<item android:id="#+id/my_settings"
android:title="#string/my_settings"
android:orderInCategory="100"
android:showAsAction="never" />
</menu>