I created a main class and an adapter for my ListView. In this adapter I created three different types of rows. One of them is a row with EditText. In the adapter class I created a Listener for "Enter" click. After an user clicks "Enter" button, the information entered in EditText saves in a variable.
What I need: I want my app (my main class) wait until this variable changes (from null to something) and after continue doing some actions.
I tried to create a loop, but if ended up with error.
Try to use onEditorDoneListener on the edittext(example is here) when the user presses done button on the keyboard, you can verify the value of the editText and proceed accordingly.
After an user clicks "Enter" button, the information entered in EditText saves in a variable.
It seems that this variable (presumably a String) is only ever changed when the "Enter" button is pressed. So whatever action you want when that String is changed, put them into the onClick method in your Java:
private void enterButtonOnClick(View v) {
// Store the text from EditText into the String variable here
// ...
// Your string variable just changed, so do the desired action...
// Stuff
// and
// things
}
And then in your XML, add this in the button attribute :
android:onClick="enterButtonOnClick"
Related
I have a list. I shoot data using firebase database. I have a favorite button image on my list. When I click the button, favorites are added to the favorite page. When I click the button, the favorite button image changes. But when I scroll up or down on the page or switch to another page, this changed image returns to its original state. What can I do.
The state of the button is not preserved when moving through activities, because the activity object is re-instantiated each time, so in order to fix it, you just make it a static variable.
private static boolean isCandyPressed = false;
Then in your onclick() of button, set it to true. And thus, handle your logic throughout by cheking this variable.
Hope this solves your problem
I am writing some code in Xamarin but I assume my issue would be similar in Java.
In my main activity I have few spinners and I want to trigger some action when an item is changed.
Suppose the spinner holds a list of names and the action to perform when an item is selected is a toast showing the name I selected in the spinner.
When I first launch the app, the spinner is populated with some names and I put the following line to trigger the toast when an item is selected
spinNames.ItemSelected += SpinNames_ItemSelected;
and the method
private void SpinNams_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
{
// toast message with the selected name
}
the problem is that this event is fired multiple times cause actually there is always an item selected.
How can I make sure that the event is only fired the very first time (when the activity is launched, so the spinner is populated and shown the first time) and then only when there is a new item selected in the spinner.
So that the event is only fired once when I actuallymake a new selection in the spinner.
I thought I could use some global variable to store the current selection so that the firing of the event can be conditioned to the item selected in the spinner to differ from what the global variable stores.
But seems too complex since I might have many new global variables to handle when I have multiple spinners. Any smarter way to achieve my goal?
I don't know if that possible in android or what
but how to type word by just click on button
example
Button 1 = "Hello"
but i want it typed on keyboard
it's like hot key
ii have tried Key Event but i didn't recognized how does it work like if i want Button To Type Hello automatically on keyboard even keyboard is hidden
dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_BACK));
If I understand you correctly, you want to type a word in an EditText when you press a button? If so you can just append your text to the EditText in your onClick method:
public void onClickHello(View view) {
mEditText.append(mHelloButton.getText().toString());
}
I am trying to create a button form component that tests the connection without using the next button because it isn't required before continuing to the next screen. However all the variables that are set in the ui dialog are unavailable till I hit the next button. Anyone have experience of how to either access those variables or force them to be available while on the same dialog without pressing next button.
Variable name: smtp.host
When I read the variable by pressing a button it is empty until I click next -> then back and then I click the button it is assigned and it functions properly.
Run this before you use any of the variables and it will work perfectly.
//Save the variables from the form before continuing
if(!formEnvironment.saveFormComponents())
{
Util.logError(context, "Unable to save form components");
}
I'm creating an app to track a score. The score is displayed in a TextView and when the user clicks the TextView it increments. This works perfectly! However, I would like to setup an onLongClickListener() to edit the text. So, when the user does a LongClick then I would like a digits only editor to pop up on the screen and when the user clicks OK or Done it will update the TextView value to the user inputted amount.
Can someone tell me how I can accomplish this please? This is my first real app so I'm a little puzzled as to how to accomplish this and all the searching on Google/Stackoverflow isn't helping.
Since the TextView is read only, what I've done in the past is have an EditText and a TextView in the same place. Done this way, you'll have to have code to show and hide the two views when you want to enter and exit "edit mode". So, in your onLongClick event, you'll hide the TextView and show the EditText. And on the Enter key (use a key listener on the EditText) and when the EditText loses focus (there's a focus listener as well), you'll do the opposite.
The other piece is how to limit the input to numeric input. Check this out:
https://stackoverflow.com/a/7300516/53501
As far as what to do when the EditText loses focus, I would prompt the user to confirm or cancel the change. I'd personally use an AlertDialog for this (via AlertDialog.Builder).
Having read the comment that this is your first app, please comment if you'd like clarification on any of these things.
Firstly create the view, which you would like to display (see here how to do that). Construct the dialog and inside the onLongClickListener - show the dialog.
Also - assign an onClickListener for a button inside your dialog, which will apply the input to your TextView.
you can do by this way...
text1.setOnLongClickListener(new OnLongClickListener()
{
#Override
public boolean onLongClick(View arg0)
{
text.setText("Your Text");
return false;
}
});