I'am using several CheckBoxPreference in my android app to add a settings menu. But is there a way to say that one and only one CheckBoxPreference MUST be checked at the time?
Here's one way to do it, but it could certainly be improved!
public class Settings extends PreferenceActivity{
CheckBoxPreference cb1;
CheckBoxPreference cb2;
CheckBoxPreference cb3;
#Override
public void onCreate(Bundle b) {
super.onCreate(b);
addPreferencesFromResource(R.xml.preferences);
cb1 = (CheckBoxPreference) findPreference("CheckBox1");
cb2 = (CheckBoxPreference) findPreference("CheckBox2");
cb3 = (CheckBoxPreference) findPreference("CheckBox3");
cb1.setOnPreferenceChangeListener(new Listener());
cb2.setOnPreferenceChangeListener(new Listener());
cb3.setOnPreferenceChangeListener(new Listener());
}
private class Listener implements OnPreferenceChangeListener {
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if((Boolean) newValue)
removeOthers(preference.getKey());
return true;
}
}
private void removeOthers(String key) {
if(key.equals("CheckBox1")) {
cb2.setChecked(false);
cb3.setChecked(false);
}
else if(key.equals("CheckBox2")) {
cb1.setChecked(false);
cb3.setChecked(false);
}
else if(key.equals("CheckBox3")) {
cb1.setChecked(false);
cb2.setChecked(false);
}
}
}
This is pretty basic logic:
int numOfCheck = 0;
//Check each checkbox
if(checkbox1.checked){
numOfCheck+=1;
}
//... Add other checkboxes
if(numOfCheck>1||numOfCheck<1){
//... Alert the user
}
It's simple: RadioGroup.
Related
I want to set something like this on button click:
boolean isLightOn = false;
if(!isLightOn)
{
FlashTask.flash.on();
isLightOn=true;
return;
} else {
FlashTask.flash.off();
isLightOn=false;
return;
}
But it's not working...
Thanks advance.
Hard to know without seeing all your code. But you could declare a variable as field member, then change it on every button click.
That's the simpliest way:
public class MyActivity extends Activity {
//boolean field member initialized as false by default
private boolean isLightOn;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_layout_id);
final Button button = findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(isLightOn){
FlashTask.flash.off();
} else{
FlashTask.flash.on();
}
isLightOn = !isLightOn;
}
});
}
}
Every time the button is clicked, boolean isLightOn = false; is called and isLightOn will always be false when the button is clicked. You are not preserving the previous state.
To preserve the state, make isLightOn as a class variable and remove boolean isLightOn = false; from your listener method.
You need somthing like this.
private boolean isLightOn = false; //defined as class variable;
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!isLightOn){
FlashTask.flash.on();
isLightOn=true;
}else {
FlashTask.flash.off();
isLightOn=false;
}
}
});
But make sure isLightOn is defined outside the listener scope.
i create preference screens dynamically according to my dynamic array. this is done in a for loop. to every screen objects, i add onclick listeners. but first one is only working. what is the problem?
here my code.
MenuActivity.java
public static ArrayList<Details> device_list;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preference);
createPreference();
}
private void createPreference() {
devicePref = (PreferenceScreen)getPreferenceScreen().findPreference("Manage Devices");
devicePref.removeAll();
if (MainActivity.device_list.size()!=0) {
for (int i = 0; i < MainActivity.device_list.size(); i++) {
PreferenceScreen screen= getPreferenceManager().createPreferenceScreen(this);
screen.setTitle(device_list.get(i).getName());
screen.setKey(device_list.get(i).getCust_id());
screen.setSummary(.device_list.get(i).getNic()+" "+device_list.get(i).getNumber());
screen.setOnPreferenceClickListener(new DevicePreferenceListner());
devicePref.addPreference(screen);
}
}else {
Toast.makeText(MenuActivity.this, "There is No any users", Toast.LENGTH_LONG).show();
}
}
private class DevicePreferenceListner implements OnPreferenceClickListener{
#Override
public boolean onPreferenceClick(Preference preference) {
//do some stuff....
return true;
}
}
when i run this it creates preference screens according to device_list array elements. but onclick listener work only for first screen. what i did wrong?
thank you.
In your case, only one object of PreferenceScreen exists. You should use a list of PreferenceScreen:
List <PreferenceScreen> prefScreens = new ArrayList<PreferenceScreen> ();
for (int i = 0; i < MainActivity.device_list.size(); i++)
{
prefScreens.add(new PreferenceScreen(getPreferenceManager().createPreferenceScreen(this)));
prefScreens.get(i).setOnPreferenceClickListener(new DevicePreferenceListner());
}
I'm trying to add some options to the context menu in a JavaFX WebView when the user right clicks a link, however I can't figure out how to do it.
I found I could add my own context menu using:
final ContextMenu contextMenu = new ContextMenu();
view.addEventHandler(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent mouseEvent) {
System.out.println(mouseEvent.getEventType());
if(mouseEvent.isSecondaryButtonDown()) {
System.out.println("Secondary");
contextMenu.show(view, mouseEvent.getSceneX(), mouseEvent.getSceneY());
} else if (mouseEvent.isPrimaryButtonDown()) {
System.out.println("Primary");
} else if (mouseEvent.isMiddleButtonDown()) {
System.out.println("Middle");
}
}
});
javafx.scene.control.MenuItem menuItem1 = new javafx.scene.control.MenuItem("View Source");
menuItem1.setOnAction(new EventHandler<javafx.event.ActionEvent>() {
#Override
public void handle(javafx.event.ActionEvent actionEvent) {
System.out.println("Link: " + rightClickURL);
System.out.println("You clicked view source");
}
});
contextMenu.getItems().add(menuItem1);
Unfortunately, when I do that both menus appear:
If I use view.setContextMenuEnabled(false); then the default menu disappears. Unfortunately doing that also prevents me from detecting which link was right clicked. Here is the code I'm using for that:
engine.getLoadWorker().stateProperty().addListener(new ChangeListener<Worker.State>() {
#Override
public void changed(ObservableValue ov, Worker.State oldState, Worker.State newState) {
if (newState == Worker.State.SUCCEEDED) {
EventListener listener = new EventListener() {
#Override
public void handleEvent(org.w3c.dom.events.Event evt) {
String domEventType = evt.getType();
if (domEventType.equals(EVENT_TYPE_CLICK)) {
String href = ((Element)evt.getTarget()).getAttribute("href");
} else if (domEventType.equals(EVENT_TYPE_RIGHT_CLICK)) {
String href = ((Element)evt.getTarget()).getAttribute("href");
rightClickURL = href;
System.out.println(href);
}
}
};
Document doc = engine.getDocument();
NodeList nodeList = doc.getElementsByTagName("a");
for (int i = 0; i < nodeList.getLength(); i++) {
((EventTarget) nodeList.item(i)).addEventListener(EVENT_TYPE_CLICK, listener, false);
((EventTarget) nodeList.item(i)).addEventListener(EVENT_TYPE_RIGHT_CLICK, listener, false);
}
}
}
});
So my question is this: how can I access the default ContextMenu so I can customize it? I've scoured the docs but cannot find any method that allows you to access the default ContextMenu. It seems like there must be a way to do this but I'm stumped as to how.
If customizing the default ContextMenu is not possible, does anyone know how I can show a custom context menu when right clicking a link and also capture which link was clicked? I've been trying to achieve this for days with absolutely no luck...
Currently that's not possible. There is an open issue on JavaFX for that:
https://javafx-jira.kenai.com/browse/RT-20306
If you just want to add a different context menu, then you could do that
webView.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent mouse) {
if (mouse.getButton() == MouseButton.SECONDARY) {
menu = new ContextMenu();
//add some menu items here
menu.show(this, mouse.getScreenX(), mouse.getScreenY());
} else {
if (menu != null) {
menu.hide();
}
}
}
});
I'm writing an app for the Sony Smartwatch, using their SDK. Here's part of the main activity:
class SmartTickerActivity extends ControlExtension {
private Handler mHandler;
SmartTickerActivity(final String hostAppPackageName, final Context context, Handler handler) {
super(context, hostAppPackageName);
if (handler == null) {
throw new IllegalArgumentException("handler == null");
}
}
#Override
public void onStart() {
//do some stuff
PreferenceManager.setDefaultValues(mContext, R.xml.preference, false);
}
The problem is that the saved preferences aren't being applied on the Smartwatch when the application launches. Nor are the default preference values from XML. However, if I click on any of the app's preferences on the phone, the saved preference values are immediately applied to the Smartwatch.
Note that the main class has no onCreate() method, and that's throwing me for a loop.
Here's part of the Preference activity:
public class MyPreferenceActivity extends PreferenceActivity {
private OnSharedPreferenceChangeListener mListener = new OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Preference pref = findPreference(key);
if (pref instanceof ListPreference) {
ListPreference listPref = (ListPreference) pref;
pref.setSummary(listPref.getEntry().toString());
}
if (pref instanceof EditTextPreference) {
EditTextPreference editTextPref = (EditTextPreference) pref;
pref.setSummary(editTextPref.getText().toString());
}
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preference);
setSummaries();
setTypeface(SmartTickerActivity.mainLayout);
if (previewLayout != null) setTypeface(previewLayout);
// Handle read me
Preference readMe = findPreference(getText(R.string.preference_key_read_me));
readMe.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference readMe) {
showDialog(DIALOG_READ_ME);
return true;
}
});
// Handle about
Preference about = findPreference(getText(R.string.preference_key_about));
about.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference about) {
showDialog(DIALOG_ABOUT);
return true;
}
});
// Handle preview
Preference preview = findPreference(getText(R.string.preference_key_preview_dialog));
preview.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preview) {
showDialog(DIALOG_PREVIEW);
return true;
}
});
}
I'm rather inexperienced at Android development, so the problem might very well have nothing to do whatsoever with the Sony SDK. Can anyone help?
You are correct, the preferences of the official sample extensions are not loaded until the PreferenceActivity is shown for the first time. If you use correct default values when accessing the preferences, this should not be a problem.
If you would like for the preferences to be loaded when the extension is initiated the first time, you could extend the android.app.Application class, and the onCreate method.
For example:
public class MySmartWatchApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
PreferenceManager.setDefaultValues(this, R.xml.app_preferences, false);
}
}
I have a couple of CheckBoxPreferences set up, my preference class extends PreferenceActivity and implements OnSharedPreferenceChangeListener
This is what I'm using to respond to people checking/unchecking the CheckBoxPreferences:
public void onSharedPreferenceChanged(SharedPreferences P, String K) {
if (K.equals(CheckBoxPref_KEY_HERE)) {
MyClass.BooleanVariable = P.getBoolean("CheckBoxPref_KEY_HERE", true);
}
}
As far as I can tell, the onSharedPreferenceChanged method above is never even getting called?
Here is the soln if you want to do something on all the preferences:
Create a class member:
SharedPreferences settings;
in your onCreate method:
settings = getSharedPreferences(<your_pref_name>, 0);
settings.registerOnSharedPreferenceChangeListener(new SharedPreferences.OnSharedPreferenceChangeListener() {
#Override
public void onSharedPreferenceChanged (SharedPreferences sharedPreferences, String key) {
// Do whatever
}
});
checkBoxPreference = (CheckBoxPreference) this.findPreference("CheckBoxPref_KEY_HERE");
checkBoxPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
// do your work here
return true;
}
});