Android Device Backbutton should not work - android

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.

Related

How to prevent an activity from being initialized multiple times

I have an Android app, when a user taps a button multiple times quickly, same activity is initialized multiple times.
To prevent this, I added android:launchMode="singleInstance" in Manifest file. But now, when an activity calls itself, it doesn't work.
I also tried
Intent myintent = getIntent();
myintent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
But this didn't work either.
How can I prevent having multiple activities when user clicks a button multiple times quickly, and how can I have same activity call itself correctly. Thanks.
Yeah, this happens if you are "trigger happy". You can also in many situations use multi-touch to activate a bunch of options at the same time. If you really need to solve this, you can look at disabling elements like J Whitfield suggested (element.setEnabled(false) or element.setClickable(false)) or intercepting onTouch.
You could try disabling the button after the first click has been detected.
Button button = theView.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(v.isEnabled()){
v.setEnabled(false);
}
//Call your new activity here
...activity stuff...
}
});

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.

How to refresh the screen when back button is pressed

I have scenario with two screens.
Screen 1 shows data from from API in list format
There is a "+" button in menu bar
Clicking this button takes user to screen 2
User can enter some info on screen 2 and press the "save" button on top of this screen. This does a POST to my API and saves the data.
After saving, I would like to put the user back to screen 1. I've done that with this:
#Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
if (menuItem.getTitle().toString().equalsIgnoreCase("save")) {
new CreateSomethingTask(this,enteredName.getText().toString(), id).execute();
Intent listscreen = new Intent(getApplicationContext(), ShowListActivity.class);
startActivity(listscreen);
return true;
}
return true;
}
However, the added item is not shown. If I close my app and open it again then the item shows up.
Is there a good way to handle this? I like how the Github Android app handles this when creating a new Gist. But I'm not sure how to implement that.
You should start your screen2 with startActivityForResult(). That way you could send a result back and a code and proceed to refresh your screen1. See example : How to manage `startActivityForResult` on Android?
Below function maybe help you, didn't tried.
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
callFunctionToRefreshList();
//or redraw data from api
//setContentView(R.layout.activity_book);
}

Do Button events in Android really have to be explicitly disabled?

So I'm new to Android and have this wee app that has a variety of Buttons. The buttons do a variety of things, but of particular interest are the buttons that intent to another activity.
Because as I'm happily programming and testing along, I discover that I can double- and sometimes triple-tap these buttons.
I look for methods on the Button object that will allow me to specify the number of clicks that the button is allowed or whether the button should be (even briefly) disabled after a click. I find nothing of the sort.
Incredulous, I begin googling for a high-level discussion of this strange behavior. I find no interesting discussions, just suggestions about how to handle the issue on every single button in my app.
With a heavy sigh, I surrender to the time demands of my project, and add private variables to my activities (no static locals in Java. crap.), which the click-handling method uses to tell whether it's already busy handling a button click.
But still I wonder. Do Button events in Android really have to be explicitly disabled?
Edit: I'm looking for an answer of the form: "Yes (or no), and I know they have to be explicitly disabled because X".
The platform can't assume you only want to allow the button to be clicked once, or how frequently you should be able to click it. Just add logic to disable the button once you've clicked it, e.g.:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
v.setEnabled(false);
//Do other stuf
}
});
You can use droidQuery to set a button or a set of buttons to handle the first click event, then not any future click events. For example:
$.with(button).one("click", new Function() {
#Override
public void invoke($ d, Object... params) {
//Do stuff - this will only happen on the first click event
}
});

Context menu for a button using android?

I have a button that when pressed, will call the company. Now, I was doing some research and found that there is a way to include a context menu. I really like the context menu because it gives you so many options.
Do you think it would be a waste of code to set a context menu for a click of the button that when pressed will open up the options to add contact, call contact, sms contact, etc.? Is it necessary?
I did come across these:
Android opening context menu after button click
http://developer.android.com/guide/practices/ui_guidelines/menu_design.html#tour_of_the_menus
I think it would be a good feature to include. Thats what context menu is there for, to give more options. I think it would be good to give the user more options when the button is clicked. Well it makes more since anyway.
Heres how you would get the long click
Button downSelected = (Button) findViewById(R.id.downSelected);
downSelected.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
return true;
}
});
EDIT:
If you just want one click on the button just register its click listener like this..
downSelected.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
}
It would be nice to provide a big main button to call the number, and some additional mechanism, let's say a smaller + button to do more stuff with as you sugggested. Also a long click could be considered a right user interaction to provide with more features.
Just a user feeling...

Categories

Resources