I'm a Android-Beginner and want to make a if-question in my settings/preferences-Activity.
Here is my Preferences.java:
package net.dominik.genpush;
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class Preferences extends PreferenceActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
}
}
...and my Prefs.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:key="pushCheckBox"
android:title="#string/pushnachr_headline"
android:summary="#string/pushnachr_explain"
android:defaultValue="true">
</CheckBoxPreference>
</PreferenceScreen>
Now I wanted to make something like
public void onCheckboxPush(View b)
{
if (pushCheckBox.isChecked())
{
//CODE
}
else...
but don't know how I can start the methode when the checkbox changes.
In a other way I could use
android:onClick="MethodeNameHere" in my activity.xml-File. This don't work in my PreferenceActivity class so no message.
Is there a (easy) way to do something like the "onClick" in a similar manner?
You can set listener for checkbox.
CheckBoxPreference checkBox = (CheckBoxPreference) findPreference("pushCheckBox");
checkBox.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object value) {
//your code here
return true;
}
});
According to documentation return true will update the state of the Preference with the new value.
And simply check inside of listener if checkBox is checked.
If you are simply trying to make one attribute depend on another, you can use the android:dependency attribute. Will that do what you want? E.g.,
<CheckBoxPreference
android:key="pushCheckBox"
android:title="#string/pushnachr_headline"
android:summary="#string/pushnachr_explain"
android:defaultValue="true">
<!-- This setting will be disabled if pref_one is not checked -->
<CheckBoxPreference
android:defaultValue="false"
android:key="preference_two"
android:dependency="pushCheckBox"
android:title="Pref Two" />
But, if you really just want to get the click event to do something else, put something like this in the onResume method.
Preference pushCheckBox = findPreference("pushCheckBox");
pushCheckBox.setOnPreferenceClickListener(new OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
// do something here
return false;
}
});
Related
I'm trying to enable/disable a seekbar when a checkbox is checked(or not).
I'm using this to create and refer:
CheckBox checkBoxProva = (CheckBox) findViewById(R.id.checkbox_prova);
boolean varCheckBoxProva = checkBoxProva.isChecked();
And this simple if to disable/enable the view:
if (!varCheckBoxProva) {
seekBarProva.setEnabled(false);
}
All this is inside the onCreate.
When the app starts the seekbar is disabled (so the if works), but if I check the CheckBox it doesn't change to enabled.
EDIT: I've succed thank to the reply of #rohan bhatia.
You are doing all of this in your onCreate method that only gets called when the activity is first created. You need to setup a listener that will be fired when the checkbox is clicked. See: https://developer.android.com/guide/topics/ui/controls/checkbox.html
You need a Listener that notifies when a checkbox is selected or deselected..
checkBoxProva.setOnCheckedChangeListener(new new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
seekBarProva.setEnabled(isChecked);
}
}
);
Accordingly, whenever you click the checkbox the method onCheckedChanged() will be executed.. You will still need to specify this in onCreate -
seekBarProva.setEnabled(checkBoxProva.isChecked());
As onCheckedChanged() only listens for changes in checked state, so to specify initial stage you will need that above line too.
Simply insert an else block
if (!varCheckBoxProva) {
seekBarProva.setEnabled(false);
}else{
seekBarProva.setEnabled(true);
}
Alternative solution:
Personally, I'd recommend adding a listener to the checkbox inside your onCreate method.
checkBoxProva.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(varCheckBoxProva){
seekBarProva.setEnabled(true);
}else{
seekBarProva.setEnabled(false);
}
}
});
I am working on simple settings activity. I am using preference-headers, PreferenceFragment, multiple preference-screen files and PreferenceActivity. I want it to look like and work like android system Settings.
Everything works fine on tablet. Settings activity display two fragments, one with headers and one with preference-screen.
When i open app on the phone, it displays just one fragment with headers, which is exactly what i want, but when i click on something, app crashes. I want to make it working exactly like android system settings, which means that when i click on something on phone, it replaces fragment with preference-screen.
Thank you for your help.
Here is my code:
headers.xml
<preference-headers
xmlns:android="http://schemas.android.com/apk/res/android">
<header
android:fragment="settingsApps.com.bendit.ILY.mysetts.PrefsF"
android:title="#string/settings_update"
android:summary="#string/settings_updateSummary" >
</header>
<header
android:fragment="settingsApps.com.bendit.ILY.mysetts.PrefsF"
android:title="#string/settings_display"
android:summary="#string/settings_displaySummary">
</header>
<header
android:fragment="settingsApps.com.bendit.ILY.mysetts.PrefsF"
android:title="#string/settings_notify">
</header>
</preference-headers>
PreferenceActivity(Prefs.java)
public class Prefs extends PreferenceActivity {
public void onBuildHeaders(List<Header> target) {
loadHeadersFromResource(R.xml.headers, target);
}
}
PreferenceFragment(PrefsF.java)
public class PrefsF extends PreferenceFragment{
#Override
public void onCreate(Bundle savedInstanceState) {
int preferenceFile_toLoad=-1;
String settings = getArguments().getString("settings");
if (Constants.SETTING_UPDATE.equals(settings)) {
// Load the preferences from an XML resource
preferenceFile_toLoad= R.xml.preference_update;
}else if (Constants.SETTING_DISPLAY.equals(settings)) {
// Load the preferences from an XML resource
preferenceFile_toLoad=R.xml.preference_display;
}else if (Constants.SETTING_NOTIFY.equals(settings)) {
// Load the preferences from an XML resource
preferenceFile_toLoad=R.xml.preference_notify;
}
addPreferencesFromResource(preferenceFile_toLoad);
}
}
You get that error because your Preference Activity is wrong set for the preference headers type, the correct way for the Activity is this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public void onBuildHeaders(List<Header> target) {
loadHeadersFromResource(R.xml.preference_headers, target);
setContentView(R.layout.preference);
}
#Override
protected boolean isValidFragment(String fragmentName) {
return (Prefs1.class.getName().equals(fragmentName)
|| Prefs2.class.getName().equals(fragmentName)
|| Prefs3.class.getName().equals(fragmentName));
}
Here Prefs"number" is the headers of the preferences like your PrefsF.
I want to disable(not show) the Dialog, when i click the EditTextPreference in a Preference Activity.
When i click the EditTextPreference, a pop up dialog show. Now i want to disable this. I try set an onclicklistener, but the dialog show same.
public class Settings extends PreferenceActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
EditTextPreference userName=(EditTextPreference) findPreference("prefUsername");
userName.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
return false;
}
}); ...
I try add this to the EditTextPreference: android:clickable="false", but nothing.
Disable it.
Use
android:enabled="false"
in XML. Or to disable in runtime,
userName.setEnabled(false);
Actually, if you do not want a dialog, you probably should use simple Preference instead of EditTextPreference.
try to replace
EditTextPreference
with
Preference
so your code will be like this
<Preference
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="Version "
android:key="edit_text_preference_1"
android:summary="1.0"
android:enabled="true" />
hope this help you
Ok so every post i find i cannot get this to work, i am trying to inside my list preference
Settingsbasic.xml
<ListPreference
android:title="themes"
android:summary="Select the option of the list"
android:key="listPref"
android:entries="#array/Themes"
android:entryValues="#array/list"
android:defaultValue="default" />
Now about as you can see above this is my listpreference inside my settingsbasic.xml file. Now what i need to know how to do is i have 2 java files, my main activity. and my preferences java file. I need to know how i can when the user clicks one of the the entries it does something, likes opens something or changes the ui, just something i think i can take it from there. I just need to know how and where the code would go. inside the main acitivty or preference activity.
here is my preference java file
public class Prefs extends PreferenceActivity {
ListPreference listPref;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settingsbasic);
}
protected void onResume() {
super.onResume();
// Registers a callback to be invoked whenever a user changes a preference.
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
protected void onPause() {
super.onPause();
// Unregisters the listener set in onResume().
// It's best practice to unregister listeners when your app isn't using them to cut down on
// unnecessary system overhead. You do this in onPause().
getPreferenceScreen()
.getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
// Sets refreshDisplay to true so that when the user returns to the main
// activity, the display refreshes to reflect the new settings.
WebViewClientDemoActivity.????? = true;
}
}
Any example code would help, or adding on to my code above. I just need someone whom can shed some light on this code. i've tried so many different things and none of it works.
Ok so using the method of the sample app that was recommended below here is some more code i have .
Main Activity
public class WebViewClientDemoActivity extends Activity {
public static String sPref = null;
public void onStart() {
super.onStart();
// Gets the user's network preference settings
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
// Retrieves a string value for the preferences. The second parameter
// is the default value to use if a preference value is not found.
sPref = sharedPrefs.getString("listPref", "Default");
}
This is the way I used PreferenceActivity:
public class EditPrefs extends PreferenceActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.screen_prefs);
}
}
Then in /res/xml folder I have the XML file:
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:key="txtColor"
android:title="textView Color"
android:summary="select color for textViews"
android:entries="#array/txtColor"
android:entryValues="#array/txtColorValues" />
</PreferenceScreen>
And in /res/values I have this XML file includes items and their values:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="txtColor">
<item>blue</item>
<item>brown</item>
<item>gray</item>
<item>violet</item>
</string-array>
<string-array name="txtColorValues">
<item>#ff000099</item>
<item>#5F1E02</item>
<item>#333333</item>
<item>#421C52</item>
</string-array>
</resources>
Then I easily call this class from another android class for instance when user clicks on a menu item:
startActivity(new Intent(this, EditPrefs.class));
You can call the preferences in onCreate and onResume like:
#Override
protected void onResume() {
super.onResume();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String txtColor = prefs.getString("txtColor", DEFAULT COLOR); // for instanse : #ff000099
textView.setBackgroundColor(Color.parseColor(txtColor));
}
I have resolved my question by this.
Prefs
public static String theme = "Theme1";
#Override
public void onBackPressed() {
ListPreference listPreference = (ListPreference) findPreference("listPref");
String currValue = listPreference.getValue();
theme = currValue;
super.onBackPressed();
}
Main Activity
if (Prefs.theme.equals("Theme1"))
setContentView(R.layout.main);
else
setContentView(R.layout.main2);
Preference XML
<ListPreference
android:title="themes"
android:summary="Select the option of the list"
android:key="listPref"
android:entries="#array/listReturnValue"
android:entryValues="#array/listDisplayWord" />
Here is my problem. I setup the buttons exactly the way they are setup in the Android documentation, but I am getting a warning, and the button will not do anything.
Here is my Java code:
package com.variDice;
import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
public class VariDiceActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//die1Clicked();
}
private void die1Clicked() {
ImageButton die1button = (ImageButton)findViewById(R.id.die1button);
die1button.setImageResource(R.drawable.icon);
}
}
...and the XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1" android:layout_gravity="center_horizontal">
<ImageView
android:id="#+id/varidice_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="#drawable/icon"></ImageView>
<ImageButton
android:id="#+id/die1button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#null"></ImageButton>
</LinearLayout>
...and the warning:
The method die1Clicked from the type VariDiceActivity is never used locally.
I must say that I am completely new to Android development. I made my app for the iPhone, and I am now trying to make a version for the android. The iPhone version was sooo much easier, because of the better interface builder (so I can just make an action and connect it to the button that way), so this is almost impossibly hard for me to understand. In other words, I do not understand how you connect an action to the button. Could somebody please tell me what I am doing wrong?
Try this in your xml:
<ImageButton
android:id="#+id/die1button"
android:onClick="die1Clicked"
...></ImageButton>
And in your code, change the method signature to:
public void die1Clicked(android.view.View v) {
ImageButton die1button = (ImageButton)findViewById(R.id.die1button);
die1button.setImageResource(R.drawable.icon);
}
Here is the Android Button tutorial.
To bind some behavior to an UI button, you need to register a listener that receives notifications of a certain event type. In your case, you register a OnClickListener (for the click event); just like in the following snippet:
// create the implementation of OnClickListener
private OnClickListener mDie1Listener = new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
}
};
protected void onCreate(Bundle savedValues) {
...
// get the button from layout
Button button = (Button)findViewById(R.id.die1button);
// register the onClick listener with the implementation above
button.setOnClickListener(mDie1Listener);
...
}
You need to add a click listener to your button. Put this in your onCreate():
ImageButton die1button = (ImageButton)findViewById(R.id.die1button);
die1button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// What to do when the button is clicked
});
Most answers on SO tend to use 'setOnClickListener' instead of using xml properties.
I personally prefer using xml for making items clickable in android.
The mistake you have made is setting your function as private. The function which gets called after clicking the item should be public.
There are 3 things you should keep in mind:
Define the 2 properties in xml.
android:clickable="true"
android:onClick="functionName"
Define that function in the Activity file. Make sure to keep the function public.
public void functionName(View v) {
// TODO Auto-generated method stub
}
Make sure to pass 'View v' as an argument for that function.