I need to customize my layout which is root_preferences
.
for instance: set gravity for SwitchPreferenceCompat or EditTextPreference.
It is the first time that I use setting fragment and I think both settingFragment
and root_Perfenses
are not like ordinary fragments, I can't give it Id, I can't set gravity.
My settingFragment class:
import android.os.Bundle
import androidx.preference.PreferenceFragmentCompat
import com.example.holyquran.R
class SettingsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.root_preferences, rootKey)
}
}
My layout:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceCategory app:title="@string/messages_header">
<EditTextPreference
app:key="signature"
app:title="نام صندوق"
app:useSimpleSummaryProvider="true" />
<ListPreference
app:defaultValue="reply"
app:entries="@array/reply_entries"
app:entryValues="@array/reply_values"
app:key="reply"
app:title="@string/reply_title"
app:useSimpleSummaryProvider="true" />
</PreferenceCategory>
<PreferenceCategory app:title="عمومی">
<SwitchPreferenceCompat
app:key="sync"
app:title="لرزش" />
<SwitchPreferenceCompat
app:dependency="sync"
app:key="attachment"
app:summaryOff="@string/attachment_summary_off"
app:summaryOn="@string/attachment_summary_on"
app:title="@string/attachment_title" />
</PreferenceCategory>
<PreferenceCategory
app:icon="@drawable/setting"
app:title="settings">
<SeekBarPreference
android:key="volume_notifications"
app:defaultValue="50"
app:showSeekBarValue="true"
app:title="volume" />
<SwitchPreferenceCompat
android:key="notifications"
app:title="Disable notifications"
android:summaryOff="Yow will no longer receive notifications"
android:summaryOn="Yow will receive all notifications"/>
Now I want to set gravity of <PreferenceCategory app:title="@string/messages_header">
to center, I know it is not common to do this.
One more thing how can I use sharePerference
in the Setting fragment?
I want to add a SeekBarPreference and my users can change the size of the application's text by moving it
something like this.
And last but not the least: My app has vibration and there is a toggle in setting which is "لرزش", when it is on vibration is enabled otherwise it is diable so how to say if switch is on it is enabled else it is disabled?
As I said before, I do not have any experience with Setting fragment because I wasn't supposed to do it It was my workaholic job
Here is the link of Project and setting folder is in ui folder
CodePudding user response:
now I want to set gravity of
<PreferenceCategory app:title="@string/messages_header">
to center, I know it is not common to do this.
Do do this you need to create a custom layout that has a TextView
for the title with an id of title
and normally set the gravity with android:gravity
. Also you can use other TextView
attributes that you can't use within the preference xml
For example:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@android:id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" />
To apply that to the PreferenceCategory
use android:layout
attribute:
Assuming the layout you created for the title is preference_message_header_title.xml
:
<PreferenceCategory
android:layout="@layout/preference_message_header_title"
android:title="@string/messages_header">
One more thing how can I use sharePerfenses in the Setting fragment? I want to add a SeekBarPreference and my users can change the size of the application's text by moving it.
and last but not the least: My app has vibration and there is a toggle in setting which is "لرزش", when it is on vibration is enabled otherwise it is diable so how to say if switch is on it is enabled else it is disabled?
These two requests quite abroad, but the general idea is that you attach a preference key in the preference XML using android:key
like you already did with some preferences.
And in the settings fragment class you call findPreference(myPreferenceKey)
to get the preference associated with that key, and setOnPreferenceChangeListener()
on it so that you can listen to the changes done by the user on that preference; then within the listener callback apply that normally by changing the corresponding SharedPreference
value that you used to track the text size or the vibration status.