save an editText - android

I have an editText (R.id.editText1).
But when i exit the activity and restart it, the text is gone.
So i thought that i make an option menu.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, MENU_SAVE, 0, "Save");
menu.add(1, MENU_GET, 1, "Get");
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_SAVE:
Here i must save the editText1
return true;
case MENU_GET:
Here i must get the editText1 that i have saved
return true;
}
return false;
But i don't now how to save an editText and how to load it.
I hope you can help me, because i'm newbe to android.
Gaauwe
Edit:
I found the answer on my own.
If you have also problems you can go to:
http://www.edumobile.org/android/android-beginner-tutorials/state-persistence/
Gaauwe

Try using a SharedPrefrences to save the value in an edittext.

Depends on how long you want to persist the data and how big the text is. Checkout this page on Android.com
http://developer.android.com/guide/topics/data/data-storage.html

SharedPrefrences is an easy way to do this.

Related

How can I tell when text is being typed into my Android SearchView on the ActionBar

I want to disable certain features of my app while the user is entering text for a search. The xml for the relevant item in my ActionBar is
<item android:id="#+id/actionbar_search"
android:orderInCategory="1"
android:showAsAction="always|withText|collapseActionView"
android:actionViewClass="android.widget.SearchView"
android:icon="#drawable/earth_2508858_search_en"
android:inputType="textPostalAddress" />
and in the corresponding code that I have at present to cater for the search is
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_menu, menu);
MenuItem DestinationTxt = menu.findItem(R.id.actionbar_search);
final SearchView mySearchView = (SearchView)DestinationTxt.getActionView();
mySearchView.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String newText) { return false; }
#Override
public boolean onQueryTextSubmit(String query) {
//Hide the Keyboard
imm.hideSoftInputFromWindow(mySearchView.getWindowToken(), 0);
// CODE TO DO THE SEARCH
return true;
}
});
}
I've browsed the methods on SearchView, but I didn't see anything that would tell me whether it's active or not. I'm also worried about putting in a boolean state variable to indicate when the text is being typed into the SearchView, in case some behaviour that I haven't catered for occurs (e.g. back button pressed, activity gets suspended), and somehow the state variable gets stale so that the disabled features stay disabled. So I'm looking for a robust way of doing this, all help appreciated :-).
Update. An answer below suggests using the interface OnFocusChangeListener which is implemented by the mySearchView object, and/or the mySearchView.isFocussed() method. Both sounded promising, however I've now tested and neither seem to work. Perhaps their failure has got something to do with the fact that this SearchView is in the ActionBar? In any case, I'm still after a robust solution.
It's right there.
mySearchView.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String newText) { return false; }
That's where you'll get updates to text changes in the SearchView.
The return value should be as such (documentation):
Returns
false if the SearchView should perform the default action of showing any suggestions if available, true if the action was handled by the listener.
If you want to know if the SearchView has been activated or deactivated, use View.setOnFocusChangeListener(View.OnFocusChangeListener);
public interface OnFocusChangeListener{
public void onFocusChange (View v, boolean hasFocus);
// The boolean will tell you if it's focused or not.
}
Since monitoring the focus didn't work, I looked at the SearchView documentation again. It's a bit convoluted, but it seems like the intended solution to this problem.
If your SearchView is inflated from a menu XML in onCreateOptionsMenu(), then you can add this line:
menu.findItem(/* your SearchView's ID here */).setOnActionExpandListener(
new OnActionExpandListener(){
#Override
public boolean onMenuItemActionCollapse (MenuItem item){
enableInteraction();
return true; // Allow the SearchView to collapse.
}
#Override
public boolean onMenuItemActionExpand(MenuItem item){
disableInteraction();
return true; // Allow the SearchView to expand.
}
}
);
Then enable and disable your Activity's views in enableInteraction() and disableInteraction(), respectively. You should retain the MenuItem in your Activity so you can query it in onResume() like so:
#Override
public void onResume(){
super.onResume();
searchViewMenuItem.isActionViewExpanded() ?
disableInteraction() : enableInteraction();
}
This part might not be needed. The SearchView might automatically get collapsed when the Activity is hidden and stay that way, so you can simply call enableInteraction() in onResume() so your user isn't locked out.
If you just need to reference the state of the SearchView, use
searchViewMenuItem.isActionViewExpanded();

onCreateOptionsMenu not working properly

I have this recurring issue with the onCreateOptionsMenu method. I have it set up so it enables or disables options depending on the value of some SharedPreferences, but for some reason the first time you open the menu it doesn't work as it should, the options that should be disabled are enabled and the other way around. If I close it and reopen it, it works fine.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
menuConfig(menu);
return true;
}
#Override
public boolean onPrepareOptionsMenu (Menu menu) {
menuConfig(menu);
return true;
}
public void menuConfig(Menu menu){
menu.getItem(getResources().getInteger(R.integer.MENU_ABOUT)).setEnabled(true);
if (preferences.getBoolean(getString(R.string.PREFS_STARTED), false)){
menu.getItem(getResources().getInteger(R.integer.MENU_START)).setEnabled(false);
menu.getItem(getResources().getInteger(R.integer.MENU_STOP)).setEnabled(true);
}else{
menu.getItem(getResources().getInteger(R.integer.MENU_START)).setEnabled(true);
menu.getItem(getResources().getInteger(R.integer.MENU_STOP)).setEnabled(false);
}
if(!preferences.getBoolean(getString(R.string.PREFS_STARTED),false) && preferences.getBoolean(getString(R.string.PREFS_FILES_CREATED),false)){
menu.getItem(getResources().getInteger(R.integer.MENU_DELETE)).setEnabled(true);
menu.getItem(getResources().getInteger(R.integer.MENU_SET_ID)).setEnabled(true);
}
else{
menu.getItem(getResources().getInteger(R.integer.MENU_DELETE)).setEnabled(false);
menu.getItem(getResources().getInteger(R.integer.MENU_SET_ID)).setEnabled(false);
}
}
These two pics are taken subsequently with a few seconds between each menu key press.
The first one is wrong, it should look like the second one.
I fixed it by changing the default value returned by the queries to the SharedPreferences object but I have no idea of what is actually happening.

Android - checkable buttons on options menu

Can anyone point in the direction of any tutorials that show how to create an options menu with clicakble checks like in the picture below:
alt text http://img291.imageshack.us/img291/1221/deviceit.png
I have tried as follows:
/** Menu creation and setup **/
/* Creates the menu items */
public boolean onCreateOptionsMenu(Menu menu) {
boolean result = super.onCreateOptionsMenu(menu);
menu.add(0, 1, 0, "Speaker").setCheckable(true);
menu.add(0, 2, 0, "Mute").setCheckable(true);
return result;
}
/* Handles item selections */
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1:
if(audioManager.isSpeakerphoneOn()==false){
audioManager.setSpeakerphoneOn(true);
audioManager.setRouting(AudioManager.MODE_IN_CALL,
AudioManager.ROUTE_SPEAKER, AudioManager.ROUTE_ALL);
}else{
audioManager.setSpeakerphoneOn(false);
audioManager.setRouting(AudioManager.MODE_IN_CALL,
AudioManager.ROUTE_EARPIECE, AudioManager.ROUTE_ALL);
}
return true;
case 2:
if(audioManager.isMicrophoneMute())
audioManager.setMicrophoneMute(false);
else
audioManager.setMicrophoneMute(true);
return true;
}
return false;
}
But this doesn't work it only gives me text on the buttons on the options menu
EDIT: I have added the following onPrepareOptionsMenu method:
public boolean onPrepareOptionsMenu(Menu menu){
boolean result = super.onPrepareOptionsMenu(menu);
if(audioManager.isSpeakerphoneOn())
menu.findItem(1).setChecked(true);
else
menu.findItem(1).setChecked(false);
if(audioManager.isMicrophoneMute())
menu.findItem(2).setChecked(true);
else
menu.findItem(2).setChecked(false);
return result;
}
However I get the same outcome just text and no check light as in the picture above
If you want to change dynamically the state of your Option Menu, you need to use onPrepareMenu(). In this method, you can do dynamic checks and update anything you want.
Good luck!!
documentation
After some digging, this look like a custom view. I think your picture comes from this code.
This is an old question, but I had the same problem and searched a lot to find such an optionsMenu shown above. I found a tutorial on http://www.codeproject.com and modified it a little bit. Maybe it is not a profi-programmers-code, but it works for me. See my modifications at my (poor arranged) web page on google sites (I got an little tutorial too on this site):
https://sites.google.com/site/opiatefuchs/android-code-examples
This code is original from wjfrancis on the Code Project web page (many props):
http://www.codeproject.com/Articles/173121/Android-Menus-My-Way
I just modified it and would be glad if somebody got any ideas to improve this code. But for now, it works.

option item selection in Dialog menu

I have been running into some troubles recently and I think I need your help :).
I am currently trying to show a menu on top of a dialog, I know that it could be far easier to launch a new activity yet doing so would compell me to store/pass a lot of data.
I managed to show an optionmenu by writing a custom dialog and rewriting the oncreateOptionMenu method.
My problem is I can't get any listener to these button, I tried to rewrite the onoptionitemselectedmethod but nothing happens.
Ps: my dialog is nearly full screen so i can't see the activity dialog (i didn't find any put on top method)
I would be glad to try any solution you could provide.
Thanks a lot
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(Menu.NONE,0,Menu.NONE,c.getString(R.string.home));
menu.add(Menu.NONE,4,Menu.NONE,c.getString(R.string.report));
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
if(item.getItemId()==0){
getOwnerActivity().startActivity(new Intent(c,Home.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
}
else
if(l>1)
getOwnerActivity().startActivity(new Intent(c,report.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
else
dismiss();
return true;
}
Maybe this little snippet out of my app helps you:
private static final int REFRESH_ID = Menu.FIRST + 1;
public boolean onCreateOptionsMenu(Menu menu) {
boolean result = super.onCreateOptionsMenu(menu);
menu.add(0, REFRESH_ID, 0, R.string.menu_refresh).setIcon(R.drawable.and_refresh);
return result;
}
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case REFRESH_ID: {
// Do whatever you want here!
return true;
}
}
return super.onMenuItemSelected(featureId, item);
}
As you can see, I've got constants for my menu-items. Those items get the Menu.First + n number as integer. For every item, I count it up. Easier, then change it everytime ;)
And in the onMenuItemSelected you can switch those constants easily.
Hope that helps!

android: any tutorials on creating menus for apps?

I need a very simple menu which probably contains only one or two items: settings/options, where pressing one of them should show some customer defined parameters (is it called dialog), e.g., number of results shown. Is there any good tutorial on creating such kind of menus? I've looked at the "notepad" example in android, it doesn't really help.
Depending on what you're asking for, these are either "Options Menus" or "Context Menus", and creating them is very easy. Here's a link to the page on the Developers' Website explaining how to do menus.
Here's a basic example of code for options menus, adapted from my game:
public boolean onCreateOptionsMenu(Menu menu){
// Define your menu, giving each button a unique identifier numbers
// (MENU_PAUSE, etc)
// This is called only once, the first time the menu button is clicked
menu.add(0, MENU_PAUSE, 0, "Pause").setIcon(android.R.drawable.ic_media_pause);
menu.add(0, MENU_RESUME, 0, "Resume").setIcon(android.R.drawable.ic_media_play);
return true;
}
public boolean onPrepareOptionsMenu(Menu menu){
// This is called every time the menu button is pressed. In my game, I
// use this to show or hide the pause/resume buttons depending on the
// current state
}
public boolean onOptionsItemSelected(MenuItem item){
// and this is self explanatory
boolean handled = false;
switch (item.getItemId()){
case MENU_PAUSE:
pauseGame();
handled = true;
break;
case MENU_RESUME:
resumeGame();
handled = true;
break;
}
return handled;
}
Edit: See the comments for some details on AlertDialogs

Categories

Resources