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;
}
Related
I am developing an app for Quick settings Tile. I want to open a layout or a custom dialog when user click on the tile, whichever is a good option. Assume this as a custom dialog. I tried many examples, but I got no luck!
Thank you in advance.
You just have to #Override method of Tile service.
#Override
public void onClick() {
super.onClick();
Tile tile = getQsTile();
switch (tile.getState()) {
case Tile.STATE_INACTIVE:
// write code for start service or open activity according to your prefrrance
StaticUtils.closeNotificationTopPanel(this);
tile.setLabel(getString(R.string.service_running));
updateTileState(Tile.STATE_ACTIVE);
break;
case Tile.STATE_ACTIVE:
updateTileState(Tile.STATE_INACTIVE);
break;
default:
updateTileState(Tile.STATE_INACTIVE);
break;
}
}
when you Click of tile service button you have to close the notification panel below code will help for that.
public static void closeNotificationTopPanel(Context context) {
Intent closeIntent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
context.sendBroadcast(closeIntent);
}
below method will help you to change the tile state and name of a button according to state.
private void updateTileState(int state) {
Tile tile = getQsTile();
if (tile != null) {
tile.setState(state);
Icon icon = tile.getIcon();
switch (state) {
case Tile.STATE_ACTIVE:
icon.setTint(Color.WHITE);
break;
case Tile.STATE_INACTIVE:
case Tile.STATE_UNAVAILABLE:
default:
icon.setTint(Color.GRAY);
break;
}
tile.updateTile();
}
}
Try to use this link: http://wintechtutorials.com/blog/android-customize-quick-setting-tiles-7-0-nougat/
Here in this link they use default Alert dialog. Try to create custom dialog as per need. Hope this will work for you.
With no problem, connected my app to firebase and I can test realtime database and other features, but when it comes to Phone Authentication, I am having problems. The problem is when Edittext is left empty and Button is clicked the app gets crashed. Don't know which code should be responsible for this problem.
Please help me to define it.
If you have carefully read the documentation of phone auth in firebase you will have below code:
private boolean validatePhoneNumber() {
String phoneNumber = mPhoneNumberField.getText().toString();
if (TextUtils.isEmpty(phoneNumber)) {
mPhoneNumberField.setError("Invalid phone number.");
return false;
}
return true;
Then either with if-else or switch-case
You should call this method
NOTE: onCLick method comes after btn.setOnClickListener depending upon your button id.
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button_start_verification:
if (!validatePhoneNumber()) {
return;
}
startPhoneNumberVerification(mPhoneNumberField.getText().toString());
break;
case R.id.button_verify_phone:
String code = mVerificationField.getText().toString();
if (TextUtils.isEmpty(code)) {
mVerificationField.setError("Cannot be empty.");
return;
}
verifyPhoneNumberWithCode(mVerificationId, code);
break;
case R.id.button_resend:
resendVerificationCode(mPhoneNumberField.getText().toString(), mResendToken);
break;
case R.id.sign_out_button:
signOut();
break;
}
}
Due to the codes which were not provided by you, I have written some references based on my own app. Please consider changing those and probably you wont get further crashes.
I'm planning to make a digital magazine with adobe flash cc 2014. I have different scenes for different page and I use Gesture_SWIPE to navigate the magazine.
The problem is, I only write the script in 1 scene but it worked for all the scenes. So I can do the swipe in all scenes.
While I want each scenes have a different action such as only page 1 that can't be swiped to the left, only page 2 can be swiped up and down and etc
I did use the if else in each case, it worked for the swiped left, but not working for the swipe right.
Can you please help me? How can I make this scripts work only for 1 scene?
This is my gesture swipe code:
import flash.events.TransformGestureEvent;
Multitouch.inputMode = MultitouchInputMode.GESTURE;
stage.addEventListener(TransformGestureEvent.GESTURE_SWIPE, SwipeHandler);
function SwipeHandler(event:TransformGestureEvent):void
{
switch(event.offsetX)
{
case 1: //swiped right
{
prevScene();
break;
if(this.currentScene.name == "Scene 1") //THIS ONE DIDN'T WORK
{
stop();
}
}
case -1: //swiped left
{
if(this.currentScene.name == "Scene 12") //THIS ONE WORK
{
stop();
}
else
{
nextScene();
break;
}
}
}
}
I had a little trouble understanding your problem and requirements but a few things to continue with:
your first case should probably work in the same way as the 2nd
your breaks won't be always fired so will sometimes flow onto the next case the way your statement were setup.
Multitouch.inputMode = MultitouchInputMode.GESTURE;
stage.addEventListener(TransformGestureEvent.GESTURE_SWIPE, SwipeHandler);
function SwipeHandler(event:TransformGestureEvent):void {
switch(event.offsetX) {
case 1:
if(this.currentScene.name == "Scene 1") {
stop();
} else {
prevScene();
}
break;
case -1:
if(this.currentScene.name == "Scene 12") {
stop();
} else{
nextScene();
}
break;
default:
//something else happened
}
}
Previously i am using if else condition for checking the edit text field its working but i need to change into switch case.I am not getting to implement switch case inside my code.please tell me how to implement that in switch case.
btnSave.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
/**
* Validation
*/
if(tvStartLocation.getText().toString().equalsIgnoreCase(""))
{
Toast.makeText(getActivity(),"Please enter start location", Toast.LENGTH_SHORT).show();
}
else if(tvEndLocation.getText().toString().equalsIgnoreCase(""))
{
Toast.makeText(getActivity(),"Please enter end location", Toast.LENGTH_SHORT).show();
}
else if(etStartOdometer.getText().toString().equalsIgnoreCase(""))
{
Toast.makeText(getActivity(),"Please enter Trip Start Odometer reading", Toast.LENGTH_SHORT).show();
}
else
{
gotonextfraggment();
}
You cannot use a switch-case construct in your case. In a switch-case, only one argument is matched with the case labels and if there is a match, that label is executed.
switch(arg) {
case "label1":
case "label2":
.
.
.
default:
}
arg is tested with label1, label2 and so on.
In your case, in every else-if, you are trying to test the equality of the text in different EditTexts with "". So your arg changes in every else-if. Even if you try to implement switch-case, the arg of your switch-case will change continuously and you'll not be able to go any further. You cannot even do this:
switch("") {
case edittext1.getText():
case edittext2.getText();
.
.
.
default:
}
Because the case labels must be literals not variable values.
So it is impossible to implement switch-case for the problem that you are facing
In fact what you've done right now is the perfect way to do it.
I'm working on a music player for android.
In it I have an activity which contains ListView that displays songs/artists/albums etc.
I keep track of the lists the user views (For example all album --> artist x --> album y)
In order to provide a way for the user to go back in the lists, I override onBackPressed() so it will pick the previous list the user viewed (handled by a Stack)
#Override
public void onBackPressed()
{
if (ListActivity.onScreenList == ListActivity.ALL_SONGS && viewStack.empty())
super.onBackPressed();
else if (viewStack.empty())
navListView.performItemClick(navListView.getChildAt(0), 0, navListView.getChildAt(0).getId());
else
{
TypeAndName tan = viewStack.pop();
if (tan.getName() == null)
{
int num = tan.getType();
navListView.performItemClick(navListView.getChildAt(num), num, navListView.getChildAt(num).getId());
}
else
{
switch (tan.getType())
{
case ARTISTS:
break;
case ALBUMS:
{
Album alb = albumListAdapter.getAlbumAtPosition(position);
c = db.rawQuery(DatabaseHandler.qryGetSongsByAlbum, new String[] { alb.getAlbumName() });
songListAdapter.setPlaylist(DatabaseHandler.convertCursorToSongsArray(c));
lv.setAdapter(songListAdapter);
ListActivity.onScreenList = ListActivity.ALL_SONGS;
}
break;
case GENRES:
break;
case PLAYLISTS:
break;
case YEAR:
break;
// TODO add other options here
}
}
}
}
the weird part is, that the back sound (the one that is played whenever you press the back button) is played twice.
However, the onBackPressed() method is not called twice and the code works as expected.