Getting value from edittext preference in preference screen - android

I'm new to android development. I just heard about preference screen in android. So I just made one. This is just to enter name in a dialog.
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<EditTextPreference
android:title="Name"
android:summary="To enter name click on Name"
android:dialogTitle="Name Dialog"
android:dialogMessage="Enter Your Name"
android:key="name"/>
This is my created preference screen. I just need to know how can I access the entered name 'tony' in my mainactivity. I don't know the way to access the name from the dialog. Do I need to add any seperate activity for accessing the name in the name dialog. Can someone please explain it to me.

Try this:
PreferenceManager.getDefaultSharedPreferences(this).getString("name", null);
Where this is a Context like an Activity.
name is the key defined in your xml file.

Related

Android EditTextPreference default value not shown

I have a XML PreferenceScreen that holds an EditTextPreference like below
<EditTextPreference
app:key="preference_key"
app:title="test preference"
app:useSimpleSummaryProvider="true"/>
If the user has not set anything "Not set" is shown as expected. However I want this to have a default value which is also shown, but the value is dynamic based on some other settings. I already have the code to get said setting, but I just don't know how to set it in the EditTextPrefrence (preference_key) programmatically?
If i try with Preference.setDefaultValue it still showns "Not set"

SharedPreference values form xml

I'm writing preferences page (preferences.xml), and trying to retrieve values from resources file. This code is working;
<PreferenceScreen ...
<ListPreference
android:entries="#array/tips"
android:entryValues="#array/tips"
android:title="Tip rate"
/>
But this is not;
<EditTextPreference
android:defaultValue="#integer/invoiceNumberNext" // this doesn't work
android:key="invoice_number_next"
android:title="Next Invoice #"
android:inputType="numberDecimal"
/>
I have this in my defaults.xml page which is under res/values
<resources>
<integer name="invoiceNumberNext">1001</integer>
</resources>
Any idea why I'm not able to fetch some values?
The problem is that you misplaced your value. If you reference a value with #integer it must be in a xml file called (what a mystery...) integer.xml. This file must be in the folder structure
res/values/integer.xml
So right click on values folder and click "New-->Values resource file" and then enter integer.xml Here, now add your resources like you have done....tataaaa....you can use it.
EDIT
As I hadn´t understand the original question correctly, I have to elaborate this answer. Of course it is possible to create a file defaults.xml and put an <integer> item inside, and this is still available by #integer. So, at the beginning of this question, it seems that you misplaced the file into the wrong directory. But that isn´t the case so your problem must be anywhere else. If the default.xml is in res/values, it should work.

Using SharedPreferences for application settings. Android

I'd like to know what is the best way to use shared preferences for application settings, namely to change text size and text colour. The tutorials I am finding are all confusing and most of them are using deprecated methods. What is the best way to proceed for API 17?
Create a preferences screen using this so your users have a place to change values. In your code, check the value of the keys you used in this preference screen and do whatever you need to do.
By the way, the example in the Android doc I linked shows hard-coded key string literals. The best practice way to do this is to create string keys in strings.xml resource file and reference the string key in your preference screen xml file and in your java code.
For example, in strings.xml:
<string name="wifiEnabled">wifi enabled</string>
In you preference screen xml file:
<CheckBoxPreference
android:key="#string/wifiEnabled"
android:title="WiFi" />
In your java code:
String wifiEnabledStringKey = getString(R.string.wifiEnabled);
//this will give you just 'wifi enabled'; you can then use this to retrieve the value of this key from SharedPreferences.

New activity from default preferences

I have setup my default android Preferences with the necessary options. The prime reason of the Preferences are to allow users to manage their resources. In the context of my app, they are "Contacts", "Types of projects" and "Currency".
The "Currency" section works fine. The display just yet, not the programming. But what I really want to do is, when either of the "Contacts" or "Type of projects" is clicked, they should open another custom activity to let the user manage his contacts. These are by the way, not the android contacts. It is an activity connected to a database table allowing users to manage his contacts and his type of projects.
Any advise on this please? Perhaps, I shouldn't be using the default android preferences at all?
This is my XML code for the Preferences:
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:title="Manage your resources" >
<ListPreference
android:key="contacts"
android:title="Manage your Contacts"
android:summary="Click here to ADD, EDIT and DELETE Contacts" >
</ListPreference>
<ListPreference
android:key="projects"
android:title="Types of Projects"
android:summary="Click here to maintain a list of Types of Projects that match your profession" >
</ListPreference>
<ListPreference
android:key="currency"
android:title="Select currency"
android:summary="Set a default currency that you wish to use" android:entryValues="#array/entryvalues_list_preference" android:entries="#array/entries_list_preference">
</ListPreference>
</PreferenceCategory>
Thanks in advance....
Perhaps, I shouldn't be using the default android preferences at all?
IMHO, you "shouldn't be using the default android preferences" for "Contacts" and "Types of Projects", if you will be rendering a non-preference UI for them.

How to rename an Android app programmatically?

Some users want to rename my Android app to something else.
So I'd like to provide a text field for them to put in a new name inside my app.
Then how can I programmatically rename my app name to the new name?
You want to rename your app or change the title when application is running?
To change the title you have setTtle() funcion from activity.
If you want to change application name in string.xml you should have a value called app_name.
You have to rename it to the new value.
To do so you have to unzip the apk and modify the xml file and then repack it.
But making this way I think application have to be reinstalled.

Categories

Resources