Android - ImageButton Values / SharedPreference - android

If you have time , please can you take some time for me ? I really need some help.
Let me explain;
Im working on an android app.
There is a layout and it has 5 ImageButton and a webview. When the users click on a imagebutton, without problem it calls a website below..but more or less i have 20 web site.i want to add an option for users..for example the user will choose a web site from Prefs. screen then automatically one of imagebuttons values (i mean icon AND its loadurl function) will change.
I created Pref Screen and i can see my website in this which i wrote in array.xml
but totally im not able to set them to Imagebuttons..
im beggin u.its our last curve..then it ll finish.
Im tried to use this code :
Data = getSharedPreferences(filename, 0);
SharedPreferences.Editor e = data.edit();
e.putString("website", websiteVariable);
e.commit();
but i couldnt.
Please explain me step by step clearly.
also i dont want that only for me,on internet there is no source for this issue.im searching and trying everything what i can think more than 6 days but nothing.
Thank you so much

SharedPreference Data = getSharedPreferences(filename, 0);
SharedPreferences.Editor e = data.edit();
e.putString("website", websiteVariable);
e.commit();
Basically what this does is allows you to store user information such as scores for a game, stats, and other variables.
The first line gets reference or creates the file to write the data to.
The second line allows you to edit the file to write new information to it..
e.putString()
Takes two parameters the first one being the Key to pull the value you out later, and the second being the value you want to put into the file.
The last line commits the data so that it is saved to the file.
You can get more info from the docs here
Also if you want to pull the data out just do
SharedPreference Data = getSharedPreferences(filename, 0);
String value = Data.getString("website"); // use the key here to pull the data out
EDIT:
So for example if the user selects a certain image you could use a key to refer to each image and get the value later to decided which icon the user picked before.

Related

How to direct user from a splash activity to the main activity

I am asked to develop an Android application whose description is this:
Let’s assume if you like Football or Cricket so much and you can find
some unknown having the same interests.
You can send Hello to him/her and can chat as well. Fantastic. Let’s
Jump into the Project Ideas
You can Select the Interest from the menu
Find the person with the common interests and send him Hello request
You can get so much information about your interests.
Chat with Friends
You can build this App using Android Studio.
So I have made a splash screen, register, login page and a user setup activity till now.
I have also designed a dashboard type interface which would display various kinds of interest for the user to choose from. So I would have to make a different main activity for different interests, like if the user chooses sports as interest, the main activity would be different than for the user who choses singing as his/her interest.
What do I do if I want to direct the user from the splash screen to the main activity screen (sports, singing, etc.) of his/her choice? (Example code would be appreciated.)
You can use Shared Preference for this.
Just save your interest in preferences with some tag when you select it like below:
SharedPreferences sharedPreferences = getSharedPreferences("MySharedPref",MODE_PRIVATE);
SharedPreferences.Editor myEdit = sharedPreferences.edit();
myEdit.putString("name", name.getText().toString());
myEdit.putInt("age", Integer.parseInt(age.getText().toString()));
myEdit.apply();
myEdit.commit();
Then you can get it on Splash screen from shared preference like:
// Make sure to use the same name while getting data that you use while saving it.
SharedPreferences sh = getSharedPreferences("MySharedPref", MODE_APPEND);
String S1 = sh.getString("name", "");
int A = sh.getInt("age", 0);
You can now use S1 and A variables for whatever you want.
In your case, you can use if/else or switch.

Trying to import a SharedPreference of values?

I used SharedPreferences in one activity to basically store a bunch of data in a HashMap like method. In particular, app names (String) and their respective sorting as either a Category 1, 2, or 3 app. Stuff like:
editor.putString("Cut the Rope", "Category 1");
In another activity, I want to use this SharedPreference to create an ArrayList of only the Category 1 apps. Here is how I thought this could be done:
ArrayList<String> cat1Apps = new ArrayList<String>();
SharedPreferences stored = getSharedPreferences("Sorted Apps", 0);
SharedPreferences.Editor editor = stored.edit();
Map<String, ?> mappedPreferences = stored.getAll();
for(Map.Entry<String, ?> entry: mappedPreferences.entrySet()){
if(entry.getValue().toString().equals("Category 1")){
cat1Apps.add(entry.getKey());
}
}
For some reason though, this isn't working. I'm taking these app names and then later there's some code that creates a GridView display of Category 1 apps. If I just define cat1Apps.add() a bunch of time, this works fine, but if I try to import through the SharedPreferences as above, it doesn't create the GridView.
The problem here is that editor.commit() was never inserted after after adding the Strings to SharedPreferences so they were never saved. Otherwise, this code to retrieve the SharedPreferences was correct.
Some debug links as asked for by the OP
This one isn't necessarily about debugging but has some good information
Eclipse Debugging
YouTube video onDebugging- Note I just scrolled through some of this but looks like it could be helpful
Also not that when they say, "Right click and click toggle breakpoint", you can also double-click in the margin and you should see a green dot which means there's a breakpoint there. Double-click the green dot to remove the breakpoint.

Is there simple way to create user defaults values for TextView in Android?

I'm looking for simple way to create user defaults values for TextView in Android: I need the standart solution that user is seeing the previous value, that was entered by him to the TextView. Sure, there is way to store value of TextView at somewhere and then read it again while starting activity, but, may be, there is any simple tool to get what I wish ?
You can use SharedPreferences to store previous values and then get previous values from shared preferences to set as default values
Just to get your question correctly:
You want to present the values the user selected/set in his last visit?
You can save those values in the SharedPreferences - That's what they're made for.
You can give the google prediction api a try.

Android daily update text app

I'm currently trying to think of the best way to program an app which simply has text and images that update every day to different content. Would the best way to do this be to store all of the items in an array and then call upon each according to the phone's clock? Or is there a better or simpler way of doing this?
If you need to know I'm using Eclipse to program the app.
You could have the app check for updates each day or you could make a mobile website and use a webview to display the site ,and just update the site daily
Since you are using text and images, I would recommend storing them in your app's SharedPreferences. These keep data stored that your app can easily access to view or change. You can store text and bitmaps easily. For text:
SharedPreferences prefs = getSharedPreferences("MySharedPreferences", Activity.MODE_PRIVATE);
SharedPreferences.Editor preferences = prefs.edit();
String firstString = prefs.getString("MyFirstString");
//check if firstString has changed on the server
//for example, set a new String that you retrieve from the server to firstStringMaybe
if (firstString != firstStringMaybe) { //meaning you need to update firstString
prefs.putString("MyFirstString", firstStringMaybe);
prefs.commit();
}
Bitmaps storage is much more complicated, because you need to first Serialize the bitmap, then store it as a String. This probably means creating a new class that contains the bitmap object and implements Serializable. There are many examples available for how to do this:
http://www.johnwordsworth.com/2011/06/serializing-and-de-serializing-android-graphics-bitmap/
android how to save a bitmap - buggy code

Android SharedPreferences

I have 3 webviews in my search engine app.When the user enters his query, by default he gets Google results, followed by yahoo and Ask buttons at the bottom, on clicking either of them, he gets results for that query from those sites. Now I want to give the user the privilege to change the default result site. I have created 3 radiobuttons. Upon confirmation, say he chooses Yahoo, how can i set it as Yahoo till the next time he changes it to some other site,
Accessing data from SharedPreferences:
SharedPreferences sharedPref = getSharedPreferences("FileName",MODE_PRIVATE);
String webViewChoice = sharedPref.getString("userChoice","null");
if no choice was saved (in the case when the application is running for the first time), you'll get "null" in webViewChoice.
use this condition as you wish
Saving data in SharedPreferences:
SharedPreferences sharedPref = getSharedPreferences("FileName",MODE_PRIVATE);
SharedPreferences.Editor prefEditor = sharedPref.edit();
prefEditor.putString("userChoice",usersChoice);
prefEditor.commit();
I hope it helps.
Save the user's preference as default-engine=google by default in a shared preferences file.
On app loading, read the file and set the default engine during the app runtime. When user chooses a different engine as default, then update the preferences file.
Hope this helps.

Categories

Resources