Auto refresh button? - android

I have an app that gets the longitude and latitude on a push of a button. I want to know how to get it so I don't have to push the button every time that I want to refresh to see the location. I know that I can take out the button function and it will automatically update but I need it to only show when the button is selected to on. Is there a way that I can refresh the button without having to keep clicking it?

Rather than having the button call a refresh function, have it set a boolean value "isButtonPressed" to true. And then when the button is pressed again (turned off), set it back to false. Then somewhere in your application that gets called continuously (do you have a game loop? update function? something like that?) do this:
void Update() {
if (isButtonPressed) {
Refresh();
}
}
Make sure you initialize the boolean value to false, set it to true when the button is pressed, and then set it to false when the button is pressed again. You can do that like this:
void ButtonPressedCallback() {
isButtonPressed = !isButtonPressed;
}
This is all making the assumption that you are running a game and would have a game loop, or that you have some sort of update function that's called infinitely in your application until it exits. If not, consider looking at Android activities in order to do something like that.

private Handler mHandler = new Handler() {
#Override public void handleMessage(Message msg) {
update(); // The refresh stuffs
mHandler.sendMessageDelayed(Message.obtain(mHandler, -1), 1L); // wait
}
}
With this you wont need a button at all. In my opinion, I don't know why you have a button if you don't want to click it....

Related

Only make it clickable if....?

I have an application, where you can click on the background, and it's changes from the drawables to another background, but I only want to make it clickable when the user click on a button that i call, "I want to click it".
So, how to write a code like:
If user click on button1 2 times, make layout clickable
else
not make layout clickable
So, I want to store somehow the click, and force my app to remember to it, and I also want to count the clicks.
What chapter of Android are helping me understanding this? Thanks for help, and sorry for the noobish question:)
You can have counter that increments on click and then just disable component when counter reach value that you want
int count = 0;
Button button = (Button) findViewById(R.id.i_want_to_click_it);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (++count == 2) {
// make stuff clickable here on 2nd click
}
// if you also want to make things unclickable if there
// are more than 2 clicks, add the else{} condition
else {
// make stuff unclickable here
}
}
});
You can detect when user clicks a button by registering a onClickListener on it. Inside that callback you can count how many times it has been clicked and store that information inside a variable. If your app can change from portrait to landscape mode, dont forget to store the variable in onSavedInstanceState and then retrieve it in onCreate, because changing layout mode will destroy the activity and rebuild it which will reset your variable. I highlighted keywords to search for.

Android Device Backbutton should not work

First of all i am learning android, I am testing my Andorid app in my Samsung Galaxy S1.
My app Function is: while i am pressing RandomNumber button, it will generate Random numbers and displaying in the screen in TextArea.
But i am facing the below issues.
The Device back button is allow user to go back. How i can avoid that? ( I have buttons defined in the program dynamically, only that Back button should work )
While shaking the phone or change the position of the phone, then the Random numbers are automatically generating. How to avoid that?. Please advise.
Button Creation Dynamic
final Button buttonToAdd = new Button(this);
buttonToAdd.setText("RandomNumber");
Listener:
buttonToAdd.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Strvalue = (String) buttonToAdd.getText();
if (Strvalue.equals("RandomNumber"))
{
Randomnumbergeneration();
}
}
});
You can overwrite the Activities onBackPressed() method to handle the back-button click event.
#Override
public void onBackPressed() {
// put some code here or just do nothing
// don't call super.onBackPressed() if you want to disable the back function
}
But if you want to publish your application you should follow the official design guidelines and do not disable this behaviour because every android user is used to it and will find it unlikely if the back button does not work anymore.

"Login" activity: ProgressDialog alternative

I have got an activity where the user can enter host name, user name and password and then click on a "Verify credentials" button. Then the credentials will be checked, which will take some time. In the meantime the user should neither be able to change the credentials nor to click on "Verify" again. So, a modal dialog like the ProgressDialog seems perfect for this.
Unfortunately, ProgressDialog has the well-know limitations regarding orientation changes etc. The guide (UI/Dialogs) tells to avoid ProgressDialog at all and use a ProgressBar in the layout instead (like in Progress & Activity). What does this mean? Shall I create another activity with just one progress bar? Or disable all input fields and put a progress bar on top of it? Sounds quite weird to me... whats your preferred solution?
Best thing which I use is:
Put a ProgressBar just beside the Login Button.
I have put a progressbar beside it(Whose visibility is set to View.GONE) in the OnCreate method.
When the user clicks on the Login/Submit button, I set the visibility of the button to View.GONE and visibility of ProgressBar to View.VISIBLE.
It looks good and the user cannot click on the button until the work is done, If an error occurs, toggle the visibility to let the user try again
Like #micro.pravi mentioned in his answer, you can implement the ProgressBar inside your layout. To keep the state after an orientation change you have to use onSaveInstanceState and onRestoreInstanceState to save and restore important values, i.e. private variables, like the private boolean isChecking
public class MyActivity extends Activity {
public boolean isProcessing;
#Override
public void onCreate(Bundle stateBundle) {
super.onCreate(stateBundle);
// Set Layout
setContentView(R.layout.main);
if(stateBundle!=null) {
// read your data here from the bundle
isProcessing = stateBundle.getBoolean("isProcessing");
}
setUiState(isChecking);
}
#Override
protected void onRestoreInstanceState(Bundle stateBundle) {
// Second value of getBoolean is the default value
isProcessing = stateBundle.getBoolean("isProcessing", false);
super.onRestoreInstanceState(stateBundle);
}
#Override
protected void onSaveInstanceState(Bundle stateBundle) {
// Save the critical data
stateBundle.putString("isProcessing", isProcessing);
super.onSaveInstanceState(stateBundle);
}
#Override
protected onResume() {
setUiState(isProcessing);
}
private setUiState(boolean processing) {
textView.setEnabled(!processing);
button.setEnabled(!processing);
progressbar.setVisibility(processing?View.VISIBLE:View.GONE);
}
}
This should be used to saved any critical data on orientation change or when the App is being killed and later restored by the OS. You don't have to save your TextView data, as the defautl View elements already handle this by themselves. Also do not store Image data this way. Instead store the Uri or path to the Url and load it on restore
For temporarily solving your problem, you can continue using the Progress Dialog and put this line in your Login Activity's tag in Manifest.xml file :
android:configChanges="orientation|keyboardHidden|screenSize"
Using this line of code will not affect the Progress Dialog on orientation changes. But it is considered a wrong practice according to Android Development's Documentation.
In the long run, i recommend you to Preserve the states for orientation changes.

Android, how to tell if user clicks a linkify link, or presses home button

I think I need to put some code within my onStop method. It pertains to a service that should be running only when the activity is finished()
but when the user follows some linkify'd text to the web browser, or when the user presses the homescreen, both call onStop() but these do not end the activity, I don't want to end the activity when a user follows a link, so I can't put finish() within onStop() unless I can detect and differentiate when this happens within onStop()
is there a way I can override Linkify() so that I can add a flag within it, or maybe make it run startActivityforResult() so that I can information back in a result?
similarly, is there a way I can set the activity to finish() when the user presses the home button?
Thanks
Is it possible for you to check isFinishing() in your onStop() to decide whether you need to run the service-related code or not?
#Override
protected void onStop() {
super.onStop();
if (isFinishing()) {
// Your service-related code here that should only run if finish()
// was called.
}
}
UPDATE: (after understanding the problem better)
Very similar to my suggested approach on another question, you can probably override startActivity() to intercept when the link is launching and set your flag if that's the case.
#Override
public void startActivity(Intent intent) {
final String action = intent.getAction();
if (action.equals(Intent.ACTION_VIEW)) {
// maybe also check if getScheme() is 'http' then set our flag
persist.saveToPrefs("linkifyClick", true);
}
// proceed with normal handling by the framework
super.startActivity(intent);
}
That other answer also show how you can call startActivityForResult() too instead if you want.
SOLUTION (with still a remaining problem)
I put an onClick: attribute on my textView in the xml
<TextView
android:id="#+id/body"
android:paddingTop="10dp"
android:onClick="onTextViewClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
and then saved a flag within my sharedpreferences within that new method
public void onTextViewClick(View v){
//write to sharedpreferences
persist.saveToPrefs("linkifyClick", true);
}
and then in my onStop I can detect whether that flag is set or not!
#Override
public void onStop(){
super.onStop();
if(persist.getFromPrefs("linkifyClick", false) == false)
{
finish();
}
else
persist.saveToPrefs("linkifyClick", false); //if it was true, then set to false
}
PROBLEM
It is possible to click on the linkified text without touching the actual link portion. This sets the flag to true and still mixes up the lifecycle I am going for. So I need to be able to detect when the home button is pressed
UPDATE: this helped clarify the problem for the other poster (with the accepted answer), that person updated the answer and I accepted it after I put it in my code. It works.

Make Button in Android to repeat process inside activity until counter reaches certain number

I currently have Button in my main view which checks if users answer is correct.
Button CheckButton= (Button) this.findViewById(R.id.CheckButton);
CheckButton.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
// some action, setting text
}
});
If button is pressed once it checks if answer is correct, and if button is pressed second time I want it to repeat activity e.g. present user with another question.
OnClickListener is inside onCreate method and question is generated using switch and unique id (for game difficulty).
What would be the best way to set this repeat activity until it's been repeated 4 times. Thanks
switch (difficulty_level)
{
case DIFFICULTY_HARD:
// do this
case DIFFICULTY_EASY:
// do this
}
To me, this doesn't sound like logic belonging in the OnClickListener at all. The listener should simply register the click and then call a function in your activity "handleButtonClicked" that have access to fields that keep track of the number of clicks for the question, if the answer is correct and what the appropiate action is.
The fact that the OnClickListener is set in the onCreate only says that it is ready to be used after OnCreate. It doesn't require the OnCreate to be run again.
Instead of an anonymous inner implementation of OnClickListener, define it as a private class.
That way you can pass the parameter of how many iterations you want in its constructor / setter
Each time the application is launched, increase a counter; when such counter is bigger or equal than the said parameters, ignore the following clicks
You need a global state variable that you set when the click has occured twice, or you could just count and every even number you change the question by loading another activity.

Categories

Resources