How to Change Font Size in Preferencescreen

How can I change font size in PreferenceScreen

Finally I was able to solve my problem simply by passing one layout. Maybe this will be helpful to someone.
This is my modified preference.xml see below code. We can apply our own properties to pref title and summary.

preference.xml:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="First Category"
android:textSize="20px"
android:layout="@layout/mylayout">

<CheckBoxPreference
android:title="Checkbox Preference"
android:defaultValue="false"
android:summary="This preference can be true or false"
android:key="checkboxPref"
android:layout="@layout/mylayout"/>

</PreferenceCategory>
</PreferenceScreen>

mylayout.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TextView android:id="@android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"/>

<TextView android:id="@android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="26sp"/>

</LinearLayout>

Please ask me if any queries....Keep Smiling. If this is useful give me one comment.

How to change the font size of a preference?

I have one solution for you.you can change font size for specific preference by managing layout file.

Here is your edited pref_headers.xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<Preference
android:key="general"
android:title="General"
android:icon="@drawable/ic_person_black_24dp"
android:layout="@layout/font_layout"/>

<Preference
android:key="categories"
android:title="Categories"
android:icon="@drawable/ic_list_black_24dp"
android:layout="@layout/font_layout"/>

<Preference
android:key="productivity"
android:title="Productivity"
android:icon="@drawable/ic_timeline_black_24dp"
android:layout="@layout/font_layout"/>

<Preference
android:key="delete_this_accnt"
android:title="Delete this Account"
android:layout="@layout/font_layout"/>
</PreferenceScreen>

Here is the layout(font_layout.xml) file from which you can change font size or any attribute which you want to change like color etc.:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="8dp"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:id="@android:id/icon"
android:layout_width="40dp"
android:paddingStart="4dp"
android:paddingEnd="4dp"
android:layout_height="40dp" />

<TextView
android:id="@android:id/title"
android:layout_weight="1"
android:layout_width="0dp"
android:textColor="#201f1f"
android:layout_height="wrap_content"
android:textSize="18sp" />
</LinearLayout>

Hope this helps!

Text size within an PreferenceScreen

You can make a TextView layout xml that looks something like this:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
style="?android:attr/listSeparatorTextViewStyle"
android:textColor="@android:color/white"
android:id="@+android:id/title"
/>

and set the layout of your preference category in your preferences.xml like this:

<PreferenceCategory
android:title="Category Title"
android:layout="@layout/pref_category"
/>

As long as the TextView has the id @+android:id/title you can make the layout look however you want. There is also a way to do this with styles that I haven't quite figured out.

Font Size in Preference

I found this little snip that fixed it

<LinearLayout
android:id="@+android:id/widget_frame"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:minWidth="48dp"
android:orientation="vertical" />

How to set the font size of the item in dialog when I click ListPreference in android?

import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences.Editor;
import android.graphics.Typeface;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckedTextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class FontPreference extends DialogPreference implements DialogInterface.OnClickListener {

// Keeps the font file paths and names in separate arrays
private List<String> m_fontPaths;
private List<String> m_fontNames;

public FontPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
protected void onPrepareDialogBuilder(Builder builder) {
super.onPrepareDialogBuilder(builder);
// Get the fonts on the device
HashMap<String, String> fonts = FontManager.enumerateFonts();
m_fontPaths = new ArrayList<String>();
m_fontNames = new ArrayList<String>();
// Get the current value to find the checked item
String selectedFontPath = getSharedPreferences().getString(getKey(), "");
int idx = 0, checked_item = 0;
for (String path : fonts.keySet()) {
if (path.equals(selectedFontPath))
checked_item = idx;
m_fontPaths.add(path);
m_fontNames.add(fonts.get(path));
idx++;
}
// Create out adapter
// If you're building for API 11 and up, you can pass builder.getContext
// instead of current context
FontAdapter adapter = new FontAdapter();
builder.setSingleChoiceItems(adapter, checked_item, this);
// The typical interaction for list-based dialogs is to have click-on-an-item dismiss the dialog
builder.setPositiveButton(null, null);
}

public void onClick(DialogInterface dialog, int which) {
if (which >= 0 && which < m_fontPaths.size()) {
String selectedFontPath = m_fontPaths.get(which);
Editor editor = getSharedPreferences().edit();
editor.putString(getKey(), selectedFontPath);
editor.commit();
dialog.dismiss();
}
}

// Font adaptor responsible for redrawing the item TextView with the appropriate font.
// We use BaseAdapter since we need both arrays, and the effort is quite small.
public class FontAdapter extends BaseAdapter {
@Override
public int getCount() {
return m_fontNames.size();
}

@Override
public Object getItem(int position) {
return m_fontNames.get(position);
}

@Override
public long getItemId(int position) {
// We use the position as ID
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
// This function may be called in two cases: a new view needs to be created,
// or an existing view needs to be reused
if (view == null) {
// Since we're using the system list for the layout, use the system inflater
final LayoutInflater inflater = (LayoutInflater)
getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// And inflate the view android.R.layout.select_dialog_singlechoice
// Why? See com.android.internal.app.AlertController method createListView()
view = inflater.inflate(android.R.layout.select_dialog_singlechoice, parent, false);
}
if (view != null) {
// Find the text view from our interface
CheckedTextView tv = (CheckedTextView) view.findViewById(android.R.id.text1);
// Replace the string with the current font name using our typeface
Typeface tface = Typeface.createFromFile(m_fontPaths.get(position));
tv.setTypeface(tface);
// If you want to make the selected item having different foreground or background color,
// be aware of themes. In some of them your foreground color may be the background color.
// So we don't mess with anything here and just add the extra stars to have the selected
// font to stand out.
tv.setText(m_fontNames.get(position));
}
return view;
}
}
}

end see this answer

set custom font for text in PreferenceScreen

public class CustomPreference extends Preference 
implements PreferenceStyle {

private int style;

public CustomPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);

}

public CustomPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}

public CustomPreference(Context context) {
super(context);
}

@Override
protected void onBindView(View view) {
super.onBindView(view);
switch (style) {
case STYLE_ALARMED:
setStyleAlarmed(view);
break;
case STYLE_NORMAL:
setStyleNormal(view);
break;
case STYLE_WARNING:
setStyleWarning(view);
break;
case STYLE_SUMMARY_ALARM:
setStyleSummaryAlarm(view);
break;
case STYLE_SUMMARY_WARNING:
setStyleSummaryWarning(view);
break;
case STYLE_DISABLED:
setStyleDisabled(view);
break;
default:
break;
}

}

private void setStyleWarning(View view) {
TextView titleView = (TextView) view.findViewById(android.R.id.title);
titleView.setTextColor(Color.YELLOW);
// add your FONT here
}

private void setStyleAlarmed(View view) {
int alarmREDColor = view.getContext().getResources().getColor(R.color.alarmed_red);
TextView titleView = (TextView) view.findViewById(android.R.id.title);
titleView.setTextColor(alarmREDColor);
}

public void setStyle(int style) {
switch (style) {
case STYLE_ALARMED:
this.style = STYLE_ALARMED;
break;
case STYLE_NORMAL:
this.style = STYLE_NORMAL;
break;
default:
this.style = STYLE_NORMAL;
}
}

public interface PreferenceStyle {

public static final int STYLE_DISABLED = 0;
public static final int STYLE_NORMAL = 10;
public static final int STYLE_ALARMED = 20;
public static final int STYLE_WARNING = 30;

public static final int STYLE_SUMMARY_ALARM = 40;
public static final int STYLE_SUMMARY_WARNING = 50;

public void setStyle(int style);
public Context getContext();
public void setSummary(CharSequence summary);

}

public class YourActivity extends PreferenceActivity {

private CustomPreference pref1;
private CustomPreference pref2;
private CustomPreference pref3;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefence_xml);
pref1 = findPreference();
pref1.setOnPreferenceClickListener(this);
pref1.setStyle(PreferenceStyle.STYLE_NORMAL);
}
}

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

<PreferenceCategory
android:title="category1">

<com.project.main.preference.CustomPreference
android:key="key"
android:title="title"
/>

<com.project.main.preference.CustomPreference
android:key="key"
android:title="title"/>

</PreferenceCategory>

</PreferenceScreen>

How to stop my preference screen giving a different font size to a custom made preference

If you inflate or create your own (Text)Views, you should make sure they have the same styling as default implementations.

From this source layout it seems like they use textAppearanceMedium for titles and textAppearanceSmall for summaries.

So, in your custom layout for a title you should use

<!-- for title -->
android:textAppearance="?android:attr/textAppearanceMedium"
<!-- for summary or description -->
android:textAppearance="?android:attr/textAppearanceSmall"

If you create Views only pramatically (which you probably shouldn't in this case), you can use

// for title
titleView.setTextAppearance(android.R.style.TextAppearance_DeviceDefault_Medium);
// for summary
titleView.setTextAppearance(android.R.style.TextAppearance_DeviceDefault_Small);


Related Topics



Leave a reply



Submit