I want to add a function in my app that checks if a menu item is disabled or enabled, if disabled I want my app to do tasl a, if enabled I want my app to do task b.
what I have tried so far;
if (menuItem.setEnabled()==false){
//do stuff
} else {
//do stuff
}
if (menuItem.setEnabled(false)){
//do stuff
} else {
//do stuff
}
if (menuItem.setEnabled().equals(false)){
//do stuff
} else {
//do stuff
}
I am not sure how I can do this, as whatever I tried doesn't seem to work.
Try this:
MenuItem mymenu = menu.findItem(R.id.mine_menu);
And:
if(mymenu.isEnabled()){
//do something
}
Related
How do I have an if/else path in my tests
for example
when a certain dialog is present I dismiss it and continue
vs
when it's not present I continue nonetheless
What you need is something like an isVisible(int id), the implementation of which would be something like this:
public boolean isVisible(int elementID) {
try {
onView(withId(elementID)).check(matches(isDisplayed()));
return true;
} catch (Throwable t) {
return false;
}
}
You would then check for the dialog in your test like this:
if(isVisible(R.id.dialogID)) {
onView(withText("OK")).perform(click()); // dismiss the dialog by clicking 'OK' button
// do whatever you want to do after this
}
This should take care of your problem.
According to react-native docs BackAndroid component with simple callback should not allow exit the app on back button press, but it looks like the event listener isn't called at all.
BackAndroid.addEventListener('hardwareBackPress', function() {
return true;
});
What should be changed to allow event listener triggering?
Based on the original github issue, Satyajit Sahoo provided a workable solution:
add the following to MainActivity.java:
#Override
public void onBackPressed() {
if (mReactInstanceManager != null) {
mReactInstanceManager.onBackPressed();
} else {
super.onBackPressed();
}
}
You are missing this.goBack(); before you return true.
BackAndroid.addEventListener('hardwareBackPress', function() {
this.goBack();
return true;
});
I have an application about pmt function. However there are so many conditions that need to be handled. Somehow the app will not work with having more than 12 if-else. I want to use switch case, but i still not really understand how to use switch case(been 1 and half month since my 1st try using eclipse).Any example will be highly appreciated.
here is my example code:
if(String1.toString().equals("condition1")){
//do something
if(String2.toString().equals("condition1.1")&& String3.toString().equals("condition1.2")){
//do something else
}
.
.
.
.
.
if(String2.toString().equals("condition1.##")&& String3.toString().equals("condition1.##")){
//do something else
}
}
else if(String1.toString().equals("condition2")){
//do something
if(String2.toString().equals("condition2.1")&& String3.toString().equals("condition2.2")){
//do something else
}
.
.
.
.
.
if(String2.toString().equals("condition2.##")&& String3.toString().equals("condition2.##")){
//do something else
}
}
if(String1.toString().equals("condition3")){
//do something
if(String2.toString().equals("condition3.1")&& String3.toString().equals("condition3.2")){
//do something else
}
.
.
.
.
.
if(String2.toString().equals("condition3.##")&& String3.toString().equals("condition3.##")){
//do something else
}
}
and still keep going....to handle all possibilities .I am wondering, How to do this in switch case . Or a better implementation if we have 3 times 3 conditions. For example a,b,c(suppose these three conditions can only be used once) and d,e,f and g,h,i then condition 1 is a,d,g ; condition 2 is a,d,h condition 3 is a,d,i ; condition 4 a,e,g........on so on
Note:Suppose that the API version is 8-11 (old android)
thanks
The answer is dependent on your target version of android. From KitKat and upwards (API Level 19+), Java 7's switch (String) is available. I'd also strongly suggest trying to group the subcases (condition n.x) into different methods. It just gets very unwieldly quickly, otherwise:
switch (String1.toString) {
case "condition1":
handleCase1(String2, String3);
break;
case "condition2":
handleCase2(String2, String3);
break;
}
If that still results in too complex code, you can try a lookup table together with a command pattern:
class ConditionKey {
final String String1;
final String String2;
final String String3;
public int hashCode(); // hash strings
public boolean equals(); // compare strings
}
interface ConditionCommand {
// use whatever arguments the operation needs, you can also
// add fields and initialize in the constructor
void perform(final ConditionKey key, /* [...] */);
}
Map<ConditionKey, ConditionCommand> actionMap = new HashMap<>();
actionMap.put(
new ConditionKey("condition1", "condition1.1", "condition1.2"),
new ConditionCommand() {
void perform(final ConditionKey key) {
// perform actions that need to be done
}
}
);
And then instead of the if-else or switch-case:
[...]
ConditionKey key = new ConditionKey(string1, string2, string3);
// get the action from the map
ConditionCommand command = actionMap.get(key);
// perform the command
command.perform(key);
since java 1.7 switch on string is supported.
you could annidate two switch:
switch(String1) {
case "condition1": {
switch(String2) {
case "condition1.1":
break;
// ... other cases
default:
break;
}
}
break;
// ... other cases
default break;
}
In my app I get a bug that makes me unable to load the SharedPreferences. The reason this happens is that when the applications is killed for good(task killer or phone restart) the phone can not load everything again. For now I am using this technique:
if ((sharedPreferences.getString("EXA1", "")) == "Example1"){
//do something
}
else if ((sharedPreferences.getString("EXA1", "")) == "Example2"){
//do something
}
else if ((sharedPreferences.getString("EXA1", "")) == "Example3"){
//do something
}
else{
//do nothing
}
Since I got around 75 else if statements my phone refuses to load them after the app is killed. Are there any more efficient way of loading and then do something?(Note: I got more then one single SharedPreference)
Use strObject.equals("MatchString") method
See:
if ((sharedPreferences.getString("EXA1", "")).equals("Example1")){
^^^^^^^^^^^^^^^^^^^^
//do something
}
else if ((sharedPreferences.getString("EXA1", "")).equals("Example2")){
//do something
}
else if ((sharedPreferences.getString("EXA1", "")).equals("Example3")){
//do something
}
else{
//do nothing
}
You can not compare two String object using == operator, because it is NOT Primitive Data Type.
I am trying to make a donation menu for my app. I have figured out the part that when a user clicks donate, more buttons come up saying how much. Now, I want to be able to have the amount buttons go away if they click the same button again. I want the regular DonateButton to remain. How would I come about doing that?
I have already set it as invisible using purchaseButton.setVisibility(View.GONE);
Here is the code for clicking the button and the other buttons appearing:
public void onClick(View v) {
switch (v.getId()) {
case R.id.DonateButton:
purchaseButton.setVisibility(View.VISIBLE);
purchaseButton2.setVisibility(View.VISIBLE);
purchaseButton3.setVisibility(View.VISIBLE);
case R.id.Donate:
if(BillingHelper.isBillingSupported()){
BillingHelper.requestPurchase(mContext, "donate");
// android.test.purchased or android.test.canceled or android.test.refunded
} else {
Log.i(TAG,"Can't purchase on this device");
}
break;
default:
// nada
Log.i(TAG,"default. ID: "+v.getId());
break;
case R.id.Donatetwo:
if(BillingHelper.isBillingSupported()){
BillingHelper.requestPurchase(mContext, "donate2");
// android.test.purchased or android.test.canceled or android.test.refunded
} else {
Log.i(TAG,"Can't purchase on this device");
}
break;
case R.id.Donatethree:
if(BillingHelper.isBillingSupported()){
BillingHelper.requestPurchase(mContext, "donate3");
// android.test.purchased or android.test.canceled or android.test.refunded
} else {
Log.i(TAG,"Can't purchase on this device");
}
break;
}
}
Screenshot of what I mean:
IMAGE URL (DON'T HAVE 10 REPUTATION YET):
http://i.stack.imgur.com/AMdhS.png
What I am trying to say is.
The app comes up just showing the "Donate!" Button. =>
The user clicks the "Donate!" Button. =>
The buttons "Donate $1", "Donate $3", and "Donate $5" appear. =>
I NEED HELP FROM HERE
A user wants to close the "Donate $1", "Donate $3", and "Donate $5" Buttons. =>
To close them, they click the "Donate!" which was the button they used to open it all. =>
The "Donate $1", "Donate $3", and "Donate $5" go away.
I want it to still allow them to open and close those buttons more than once though.
a simple state variable should do.
put this in your field definition area:
boolean areButtonAmountVisible = false;
and this code as your onClick():
case R.id.DonateButton:
if( areButtonAmountVisible )
{
areButtonAmountVisible = false;
purchaseButton.setVisibility(View.GONE);
purchaseButton2.setVisibility(View.GONE);
purchaseButton3.setVisibility(View.FONE);
}
else
{
areButtonAmountVisible = true;
purchaseButton.setVisibility(View.VISIBLE);
purchaseButton2.setVisibility(View.VISIBLE);
purchaseButton3.setVisibility(View.VISIBLE);
}
Try this.
Use getVisibility() method to know the visible state of button.
int visibility;
visibility = button.getVisibility();
if(visibility == View.VISIBLE) {
button.setVisibility(View.INVISIBLE);
} else {
button.setVisibility(View.VISIBLE);
}
You have to check like this for every button.
I will suggest one more thing.
Take a global variable and use it as notifier, use below code in all button click functions.
boolean again = false;
if (again) {
// make all invisible
again = false;
} else {
// make all visible
again = true;
}