Multine line label on settings - android

I would like to crate a settings Activity with items with Title and subtitle.
I've checked a lot in documentation and even other posts here and people say it's not possible... android default settings page has a lot of it:
sorry for the picture in portuguese, but what is written there doesn't matter, the point is the settings page built on android has plenty of titile/subtitle items (and it happens in many other screens)
I would like to achieve it using android default Preference api. is that possible?

Yes it is possible. One fast solution is the following (that might be improvied and tuned according your needs):
public class SettingsActivity extends Activity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.settings_fragment, new PrefsFragment()).commit();
}
}
public static class PrefsFragment extends PreferenceFragment {
public PrefsFragment(){}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.settings);
}
}
}
activity_settings.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="#+id/settings_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.vb.myapplication.SettingsActivity$PrefsFragment" />
</LinearLayout>
settings.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:key="keyCategory"
android:title="titleCategory">
<CheckBoxPreference
android:key="keyCheckPreference"
android:title="titlePreference"
android:summary="summaryPreference"
android:defaultValue="false"/>
</PreferenceCategory>
</PreferenceScreen>

Related

Set the location of a PreferenceScreen

I am using PreferenceFragment and a PreferenceScreen to show several settings. This is pretty OK in portrait mode. My app is used with VR goggles. In this landscape mode, the settings cannot be read because they are too far at the edge of the screen. I'd like to show the PrefernceScreen in a RelativeLayout with fixed width and android:layout_gravity="center_horizontal". How could I achiev this?
My code:
public class SettingsActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content , new MyPreferenceFragment()).commit();
}
public static class MyPreferenceFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
...
My PreferenceScreen layout:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<ListPreference
android:defaultValue="0"
android:entries="#array/connections"
android:entryValues="#array/connectionvalues"
android:key="Connection"
android:title="PC connection" />
...

Problems with UnregisterOnSharedPreferenceChangeListener in android

I am creating a basic settings page and my code works fine till now. But when i add UnregisterOnSharedPreferenceChangeListener at Onpause it starts giving errors. Insight is required that what i am doing wrong and where should i insert the unregister. All the codes are presented below. I would be happy if you guys could test it out in Android Studio. Any help would be appreciated. I am trying to avoid using deprecated code but if no alternatives i will use it as last resort.
Main Activity.java
public class MainActivity extends AppCompatActivity {
TextView settingsValue;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button startsettings = (Button)findViewById(R.id.startsettings);
settingsValue=(TextView)findViewById(R.id.textView);
startsettings.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,SettingsActivity.class));MainActivity.this.finish();}
});
}}
SettingsActivity.java
public class SettingsActivity extends PreferenceActivity {
static SharedPreferences sharedPreferences12;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sharedPreferences12 = PreferenceManager.getDefaultSharedPreferences(this);
getFragmentManager().beginTransaction().replace(android.R.id.content,new SettingsFragment()).commit();
}
public static class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener
{
Context context;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.custompreferences);
context=getActivity();
Preference prefalarm = findPreference("key_ringtone_alarm");
Ringtone ringtone = RingtoneManager.getRingtone(getActivity(),Uri.parse(sharedPreferences12.getString("key_ringtone_alarm","")));
prefalarm.setSummary(ringtone.getTitle(getActivity()));
Preference prefnoti = findPreference("key_ringtone_notification");
Ringtone ringtonenoti = RingtoneManager.getRingtone(getActivity(),Uri.parse(sharedPreferences12.getString("key_ringtone_notification","")));
prefnoti.setSummary(ringtonenoti.getTitle(getActivity()));
PreferenceManager.getDefaultSharedPreferences(context).registerOnSharedPreferenceChangeListener(this);
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Preference pref = findPreference(key);
if(pref instanceof RingtonePreference)
{
Ringtone ringtone = RingtoneManager.getRingtone(context,Uri.parse(sharedPreferences.getString(key,"")));
pref.setSummary(ringtone.getTitle(context));
}
}
}
#Override
public void onBackPressed() {
super.onBackPressed();
startActivity(new Intent(SettingsActivity.this,MainActivity.class));SettingsActivity.this.finish();
}
#Override
protected void onPause() {
super.onPause();
}
#Override
protected void onResume() {
super.onResume();
}
}
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.atulk.createcustomsettings.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Settings"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:id="#+id/startsettings"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintVertical_bias="0.095" />
</android.support.constraint.ConstraintLayout>
custompreferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="User Details"
android:key="key_user_details">
<Preference
android:key="key_user_name"
android:title="User First Name"
android:summary="John Smith"/>
<Preference
android:key="key_user_emailid"
android:title="User Email ID"
android:summary="asd#asd.com"/>
</PreferenceCategory>
<PreferenceCategory
android:title="Sounds & Notifications"
android:key="key_sounds_settings">
<RingtonePreference
android:key="key_ringtone_alarm"
android:title="Alarm Ringtone"
android:ringtoneType="alarm"/>
<RingtonePreference
android:key="key_ringtone_notification"
android:title="Notification"
android:ringtoneType="notification"/>
<SwitchPreference
android:title="Vibrate"
android:summary="Virate during Alarm or Notification"
android:key="key_vibrate_onoff"/>
</PreferenceCategory>
<PreferenceCategory
android:title="Message Delivery"
android:key="key_message_delivery">
<SwitchPreference
android:title="Shorten SMS"
android:summary="Reduce SMS length by using slangs"
android:key="key_shorten_sms_length"/>
</PreferenceCategory>
</PreferenceScreen>
Just create 2 different java files with preferenceactivity and preferencefragment
and call PF from PA with getFragmentManager routine. Rest all will work fine. Also use UnregisterOnPreferenceChangeListener in OnDestroy instead of OnPause.

Access SharedPreferences that are edited in a PreferenceFragment under another Activity

I have one main activity with multiple Fragments. From the menu, the user can go to a new activity which contains the PreferenceFragment:
Preference Activity and Fragment:
public class SettingsActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.options_screen);
}
/**
* This fragment shows the preferences for the first header.
*/
public static class Prefs1Fragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.pref_options);
}
}
}
Preferences xml file:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="#string/pref_cat_display">
<ListPreference
android:key="pref_key_display_units"
android:title="#string/pref_title_units"
android:summary="#string/pref_summary_units"
android:entries="#array/entries_list_units"
android:entryValues="#array/entryvalues_list_units"
android:dialogTitle="#string/pref_dialog_title_list_units"
android:defaultValue="0"/>
</PreferenceCategory>
</PreferenceScreen>
Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/settings_content"
android:paddingTop="?android:attr/actionBarSize">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.cs.biodyapp.usl.activity.SettingsActivity$Prefs1Fragment"
android:id="#+id/fragment" />
</LinearLayout>
Once the option has been initialized, I try to access to this preference from the original Activity but I always get an empty Map in the SharedPreferences object.
I tried many ways that supposely let you access preferences form a different activity or directly from the file but no chance:
//SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getActivity().getBaseContext());
SharedPreferences sharedPref = getActivity().getApplication().getSharedPreferences("pref_options", Context.MODE_PRIVATE);
int units = sharedPref.getInt("pref_key_display_units", 0);
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
Is the correct line to use ( getting the application context and not the base context ).
I also detected a problem in using ListPreference with integer arrays in a PreferenceFragment. Finally I had to use string arrays and sharedPrefs.getString() to then cast it to an integer.

Unexpected interruption of application

Why, in this very simple example, the last statement (setBackgroundColor)
produces a crash of the application ?
public class Macumba extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView vista = (ImageView)findViewById(R.id.vista);
vista.setBackgroundColor(Color.YELLOW);
}
}
main.xml is very simple, too:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="#+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<ImageView
android:id="#+id/vista"
android:layout_width="300px"
android:layout_height="330px"
android:layout_x="9px"
android:layout_y="8px"
/>
</AbsoluteLayout>
What kind of exception do you get? NullPointerException? If so, it's most likely because vista is null, which is because R.id.vista is not in your layout.
Are you sure it's the right main.xml file? And not one from eg. another project?

Set custom title bar in PreferenceAcivity

I am using PreferenceActivity, how can I set a custom title bar? Not only text but background color, size - the whole layout.
PreferenceActivity extends ListActivity, and when you inflate the preferences from xml with addPreferencesFromResource(), it puts the stuff into the standard ListView that ListActivity uses.
So basically, you can use setContentView() to specify a layout, but you need to have a ListView in it with the id "#+android:id/list".
So using kleini's example code:
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.login_settings);
setContentView(R.layout.login_settings_layout);
}
You would need a ListView in login_settings_layout.xml that looks something like:
<ListView
android:id="#+android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
This is the only thing that worked for me. Rest of the above stuff did not fetch desired results on my 4.3 Nexus Tablet.
I could not really recreate a proper actionbar like widget, but was able to put a large 'Settings' title on top of the PreferenceActivity, with below steps.
I got the hint from another stackoverflow answer and am just making it more elaborate.
In PreferenceActivity class (remove the existing titlebar)
public class Settings extends PreferenceActivity· {
...
#Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE); // This goes first
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
In res/xml/settings.xml, I would like to draw attention to the first PreferenceCategory
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:layout="#layout/settings_titlebar" />
<PreferenceCategory android:title="Notifications">
<Preference .....
In res/layout/settings_titlebar.xml
Navals-MacBook-Pro:ver_ui_0.2 Naval$ vi res/layout/settings_titlebar.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/header_background"
android:enabled="false">
<TextView android:src="#drawable/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="#android:color/transparent"
android:padding="4dip"
android:text="Settings"
android:textSize="32sp"
/>
</RelativeLayout>
public class Preferences extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
super.onCreate(savedInstanceState);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar);
}
}
Ben's method worked out well for me!!. Here is the code
public class PreferenceCustomTitleActivity extends PreferenceActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preference);
/** Customize your background, textsize, cachetint, dividers
for your list view in the xml **/
setContentView(R.layout.layout_with_simple_listview_only);
ListView list = (ListView) findViewById(android.R.id.list);
View preferenceHeader = getLayoutInflater().inflate(
R.layout.preference_header, null);
list.addHeaderView(preferenceHeader);
}
}
Awesome, worked fine for "NO_TITLE":
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.login_settings);
setContentView(R.layout.login_settings_layout);
}
Or like this:
#Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
super.onCreate(savedInstanceState);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.customtitlebar);
addPreferencesFromResource(R.xml.preferences);
}
With a customtitlebar.xml like this:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/customTitleBar"
android:layout_width="match_parent"
android:layout_height="match_parent"
style="#style/CustomWindowTitle">
</TextView>

Categories

Resources