How to create preferences in Android? - android

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.

Related

Android not showing menu

I'm a new one in Android. I have some problems with showing menu. I don't see three dots in right corner in my screen. Please, help me to understand my mistake. THANK YOU A LOT!
Activity:
public class MainActivity extends AppCompatActivity {
private EditText numb1;
private EditText numb2;
private Button btn_sum;
private Button btn_extr;
private Button btn_mult;
private Button btn_div;
private TextView result;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*some code*/
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.reset:
numb1.setText("");
numb2.setText("");
break;
case R.id.exit:
fileList();
break;
}
return super.onOptionsItemSelected(item);
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/reset"
android:title="#string/reset"
app:showAsAction="never"/>
<item android:id="#+id/exit"
android:title="#string/exit"
app:showAsAction="never"/>
</menu>
Menu Items are not showing on Action Bar
Check this answer
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="ifRoom|withText"
android:title="#string/action_option1"/>
<item
android:id="#+id/action_settings34"
android:orderInCategory="100"
android:showAsAction="ifRoom|withText"
android:title="#string/action_option2"/>
<item
android:id="#+id/action_settings3"
android:orderInCategory="100"
android:showAsAction="ifRoom|withText"
android:title="#string/action_option3"/>
</menu>
Use this code in your activity, but you should have action bar in it.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.your_menu, menu);
return true;
}
if you run your application on oldest version of samsung or other the three DOTS is not appear on ActionBar
so try to click on Option Key on mobile
The solution to appear Three DOTS
call this method in your application class' onCreate method
private void makeActionOverflowMenuShown() {
//devices with hardware menu button (e.g. Samsung Note) don't show action overflow menu
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if (menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) {
Log.d(TAG, e.getLocalizedMessage());
}
}
i am also new to android i guess u wrote onCreateOptionsMenu(Menu menu) inside on create
try this
public class MainActivity extends AppCompatActivity {
private EditText numb1;
private EditText numb2;
private Button btn_sum;
private Button btn_extr;
private Button btn_mult;
private Button btn_div;
private TextView result;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*some code*/
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.reset:
numb1.setText("");
numb2.setText("");
break;
case R.id.exit:
fileList();
break;
}
return super.onOptionsItemSelected(item);
}
}
You have done the minor mistake while creating the option menu .you should call the onCreateOptionsMenu() and onOptionsItemSelected() method outside the onCreate(Bundle savedInstanceState) method. you may check the following example :
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.reset:
break;
case R.id.exit:
fileList();
break;
}
return super.onOptionsItemSelected(item);
} }
Help me to understand my mistake - Sure
The Main culprit is in your menu XML file app:showAsAction="never" this line replace this line with app:showAsAction="ifRoom"
here showAsAction is set to never means you tell that don't show my menu in action bar if you replace with "ifRoom" means you said show my all menu in action bar and if there is space for all my menus

Set android:onClick to a Menu Item in XML - Where to put method?

So I have a menu with 2 items set on it in res/menu/menu_item.xml. I want to add on onClick method to the Menu Item but where do I put the method? I have the menu_item set on 3 different activities but I want one universal method which is called by the onClick method in the menu_item.xml file.
res/menu/menu_item.xml code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="Household"
app:showAsAction="never"
android:onClick="myHousehold"/>
<item
android:id="#+id/profile"
android:orderInCategory="100"
android:title="About"
app:showAsAction="never"
android:onClick="aboutApp"/>
</menu>
This is the method which uses this XML to set the menu on each activity.
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
You actually don't need the onClick and should add a method like this to enter menu item activities:
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.action_settings:
{
// Here you can set your intent and start the activity
Intent intent1 = new Intent(this, myActivity.class);
this.startActivityForResult(intent1, MY_ACTIVITY_REQUEST);
return super.onOptionsItemSelected(item);
}
case R.id.profile:
{
// Here you can set your intent and start the activity
return super.onOptionsItemSelected(item);
}
default:
return super.onOptionsItemSelected(item);
}
}
You have to overrideonOptionsItemSelected method from your Activity class and check selected item and do your operation as per your needs.
Try this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_action_settings: {
// Do something
return true;
}
case R.id.profile: {
// Do something
return true;
}
}
return super.onOptionsItemSelected(item);
}
Create a common class like below:
//Utils.java
public class Utils{
Context mContext;
// constructor
public Utils(Context context){
this.mContext = context;
}
public String getUserName(){
return "test";
}
public void doSomething(){
// Do something
}
}
Use it from all activities:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_action_settings: {
Utils utils = new Utils(getApplicationContext());
String username = utils.getUserName();
return true;
}
case R.id.profile: {
// Do something
Utils utils = new Utils(getApplicationContext());
utils.doSomething();
return true;
}
}
return super.onOptionsItemSelected(item);
}
Hope this will help~

Checkbox item state on menu android

How can I set initial value of checkbox item part of a menu? When I start an Activity I want to set a boolean value saved in Shared Preferences.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".LoginActivity">
<item
android:id="#+id/checkbox"
android:title="#string/checkbox"
android:orderInCategory="10"
android:showAsAction="never"
android:visible="true"
android:checkable="true"
android:checked="true"/>
</menu>
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.login, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.checkbox){
if(item.isChecked()){
item.setChecked(false);
}else{
item.setChecked(true);
}
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean(PrefsConsts.CHECKBOX,item.isChecked());
editor.commit();
}
return true;
}
I can always know what was the value, but I donĀ“t know how to set the new one.
First of all, get rid of the checked value in the layout
<item
android:id="#+id/action_check"
android:title="#string/action_check"
android:orderInCategory="200"
app:showAsAction="never"
android:visible="true"
android:checkable="true"/>
Now you can set it in the code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
SharedPreferences settings = getSharedPreferences("settings", 0);
boolean isChecked = settings.getBoolean("checkbox", false);
MenuItem item = menu.findItem(R.id.action_check);
item.setChecked(isChecked);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_check) {
item.setChecked(!item.isChecked());
SharedPreferences settings = getSharedPreferences("settings", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("checkbox", item.isChecked());
editor.commit();
return true;
}
return super.onOptionsItemSelected(item);
}

customize options menu in android

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).

Android create drop down menu

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);
}
}

Categories

Resources