I am trying to add a page in "Settings" menu option on top of the android app. The app by default had the 3 dots on top right corner and when I click on it, it doesn't go anywhere and now I am trying to implement settings to my app. What I have done so far is following,
Created xml file in /res/xml folder called preferences.xml with following content;
<?xml version="1.0" encoding="utf-8"?>
<preferencescreen xmlns:android="http://schemas.android.com/apk/res/android">
<preference android:key="sample"
android:summary="can open other activity"
android:title="click here">
</preference>
<edittextpreference android:key="Name"
android:summary="Add user Name."
android:title="Your Name">
</edittextpreference>
</preferencescreen>
Then I have created a new java class called Preferences in /src/main/java/package/ with following content;
package com.domain.mobile.tipcalculator.app;
import android.preference.PreferenceFragment;
import android.os.Bundle;
public class Preferences extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
Then to my understanding this is all I need. I m not getting any errors on compilation time but when I click on the 3 dots on top right and click on "Settings" I still don't see the page come up. Any ideas? Thank you.
P.S. I am using PreferenceFragment because my min sdk requirement is 11.
To use menus like that, you have to create a menu xml file then inflate it and then switch between the menu item ids so that you can handle click events like the following:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.preferences:
startActivity(new Intent(this, PreferenceActivity.class));
return true;
case R.id.settings:
startActivity(new Intent(this, SettingsActivity.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
You should be able to achieve what you need this way and I hope this helped.
Related
I started my project with the template "Tabbed Activity" and am now trying to add an options menu. I have done this before with a blank activity and it worked fine. As far as I can tell I have done all the same steps, even as outlined by the documentation: https://developer.android.com/guide/topics/ui/menus#options-menu
When I run the app, nothing shows up. I would expect to see the 3 dots icon. I also tried adding icons to see if they would show up as actions. No luck. Here's what I've got in my MainActivity.java file:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
ViewPager viewPager = findViewById(R.id.view_pager);
viewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabs = findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu_layout, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
switch (item.getItemId())
{
case R.id.settingsMenuItem:
ShowSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void ShowSettings() {
// TODO show settings
}
}
And this is the options menu xml file which is "options_menu_layout.xml" located in the res/menu directory:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/settingsMenuItem"
android:icon="#drawable/baseline_settings_black_48"
android:title="#string/menu_option_settings"/>
</menu>
This seems to work on a blank activity, but not in this tabbed activity. Is there something I'm missing that is required to make these compatible?
show the xml, style and manifest files.
According to the documentation of onCreateOptionsMenu(),
"You must return true for the panel to be displayed; if you return false it will not be shown."
In your code sample, you have return super.onCreateOptionsMenu(menu);.
Is there a reason for calling the parent's class method as well? (The android developer guide code sample also does a return true;)
So I would first try to change onCreateOptionsMenu() to return true; to see if the options menu shows up and as a next step investigate the need to call the parent class' super.onCreateOptionsMenu(menu).
Turns out when using the template "Tabbed Activity" the manifest file uses a "NoActionBar" version of the theme.
I deleted the last part of this line:
android:theme="#style/Theme.ExampleApp.NoActionBar">
So it is this instead:
android:theme="#style/Theme.ExampleApp">
When using the Android Eclipse IDE, how do I add a menu that is triggered by the phone menu button such that it has to have an "About" choice that displays an "About my app" dialog and a "Quit" choice that exits the app?
To create a content menu with about a quit items, please add all of the following to your project after first removing any other attempts at an about menu that might potentially conflict with this code. Let Eclipse help you add the required imports or peruse the android documentation as you try to build this, and after everything's in place it should just work :-)
In /res create a folder menu containing a file main.xml with the following xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
<item
android:id="#+id/action_quit"
android:orderInCategory="101"
android:showAsAction="never"
android:title="#string/action_quit"/>
</menu>
In MainActivity.java add the following after your onCreate is closed:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
AboutDialog about = new AboutDialog(this);
about.setTitle(“About “my app);
about.show();
return true;
case R.id.action_quit:
System.exit(0);
return true;
}
}
Add a new java file but extend Dialog instead of Activity, like shown:
public class AboutDialog extends Dialog {
public AboutDialog(Context context) {
super(context);
}
#Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.dialog_about);
}
}
Finally, add a new layout dialog_about.xml like so:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ScrollView>
Of course you’ll need to add another layout and content inside the ScrollView, and you may want other features in your menu and your quit item could use a confirmation (possibly). But this should get you started.
I have the problem that my application doesn't show the options menu when I hit the menu button. Debugging shows that the onCreateOptionsMenu(Menu menu) method is not called after hitting the menu button. I have another application with the same code for the menu and there it works. So now my code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.app_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.options:
Intent intent = new Intent(this, OptionsActivity.class);
startActivityForResult(intent, 1);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
In res -> menu -> app_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/options" android:title="#string/options" />
</menu>
I have no idea why the onCreateOptionsMenu is not called after hitting the menu button. I hope you guys can help me.
Edit: I'm not using Fragments and the onCreateOptionsMenu is really never called. Not at the start of the app and not when i'm hitting the menu button on my device.
Not sure from your post that you're using Fragments. If so, you must set menu options on by
setHasOptionMenu(true);
call this method from Fragment's onCreate() and the options menu will then be shown.
Try to add these item attributes in your app_menu.xml file:
<item
android:id="#+id/options"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/options"/>
You must declare a string for options in Strings.xml
It will call when app starts for the first time not after menu item selected.
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
if (item.getItemId() == R.id.menuitem_id) {
}
return super.onMenuItemSelected(featureId, item);
}
this method will be called after selection
In Manifest file just set target versions as below:
android:minSdkVersion="8" android:targetSdkVersion="10"
When I run my application there is no menu button to go to the settings activity.
I have a settings activity which uses shared preferences. I call in the Main Activity like this:
private SharedPreferences settings;
private OnSharedPreferenceChangeListener listener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settings = PreferenceManager.getDefaultSharedPreferences(this);
listener = new OnSharedPreferenceChangeListener() {
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
}
};
settings.registerOnSharedPreferenceChangeListener(listener);
And this is my settings activity:
public class SettingsActivity extends PreferenceActivity {
#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
And my settings.xml is in XML folder and its very simple at the moment. I just want to be able to open settings in my application. The XML is like this:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceCategory android:title="General settings" >
<EditTextPreference
android:key="pref_username"
android:title="User name" />
<CheckBoxPreference
android:key="pref_viewimages"
android:summary="Determines whether lists are shown with thumbnail photo"
android:title="View images"/>
</PreferenceCategory>
I've also added the settings activity to the Manifest file:
<activity
android:name=".SettingsActivity"></activity>
so far it eclipse doesn't show me any errors. But when I run my application there is no menu to go to the settings page! I was under the impression that when I create a settings activity, Android will automatically create a settings menu. But there is no menu button or anything. How can I fix this?
The menu and sharedpreferences are not directly linked. Shared preferences store values inside storage. Menu would be the graphical layout bringing you to PreferenceActivity that allow you to used shared preferences to store the settings.
You have to first inflate the menu like this in your activity :
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
where menu contains the menu details like these (res/menu/main.xml):
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<<item android:id="#+id/item1" android:title="Your setting name"></item>
</menu>
This would add "Your setting name" as an option when you click the menu button. Now to make the menu button do something use this (put it in your mainactivity) :
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle item selection
switch (item.getItemId())
{
case R.id.item1:
//Do something here like in my case launch intent to my new settings menu
Intent options1 = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(options1);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Now the menu button called "Your setting name" will launch your settings activity.
I don't think it's created by default. I had to do it myself. Anyway, check the menu file for your activity and check if this menu is used in your activity.
I need to create a menu which opens a webview page or a simple page with images slide.
I'm more proficient in web programming, not so much in programming for Android. So if it is easier to do it in a WebView I will use it. I will use the images slide otherwise.
So for now, I need 2 items when I press the menu button of the phone. Upon clicking they should open 2 different links in the app (not in the browser).
I've done only menu.xml for now:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_howto" android:title="#string/howto"
android:icon="#android:drawable/ic_menu_howto" />
<item android:id="#+id/menu_about" android:title="#string/about"
android:icon="#android:drawable/ic_menu_info" />
</menu>
in your main class you must #override the menu like:-
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
and then to make the menu buttons clickable you have to add this after
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.exit:
finish();
return true;
case R.id.loadanotherwebpage:
loadUrl("www.google.com");
// inside the menu button you don't need to call Super.loadUrl, LoadUrl its enough
return true;
case R.id.share:
share(); // here i'm calling the share method !
default:
return super.onOptionsItemSelected(item);
}
}
edit : if you want to load the html file from assets file then do this
super.loadUrl("file:///android_asset/index.html");
hope that what you asking for :)