I'm trying to get my Preference category header to be consistent with the theme of my app, so after searching around both on SO and some blogs I follow..I found the best(and easiest) way was just to create a custom Preference Category class that overrides onCreateView() or onBindView() and programmatically set the color of the category TextView and it's background. Sounds easy enough.
My only two caveats is that I'm using a SherlockPreferenceActivity, and therefore I'm not able to simply apply a custom theme to my Activity since I have to use a Sherlock theme or a derivative. Also, my other thing(and I think this may be the cause) is that I'm trying to set the TextView's background to a Shape Drawable, which is a gradient with a stroke in xml. The background of the preference category changes just fine, but I don't see any TextView at all, which leads to me to think that maybe the background is being drawn on top of it's textView? I'm not sure
Here's the code for my CustomPreferenceCategory class. As you can see I tried it both using onBindView() and onCreateView().
public class CustomPreferenceCategory extends PreferenceCategory {
public CustomPreferenceCategory(Context context) {
super(context);
}
public CustomPreferenceCategory(Context context, AttributeSet attrs) {
super(context);
}
public CustomPreferenceCategory(Context context, AttributeSet attrs,
int defStyle) {
super(context);
}
#Override
protected void onBindView(View view) {
super.onBindView(view);
TextView titleView = (TextView) view.findViewById(android.R.id.title);
titleView.setBackgroundResource(R.drawable.header_non_rounded);
titleView.setTextColor(Color.WHITE);
}
//#Override
/*protected View onCreateView(ViewGroup parent) {
// And it's just a TextView!
TextView categoryTitle = (TextView) super.onCreateView(parent);
categoryTitle.setBackgroundResource(R.drawable.header_non_rounded);
categoryTitle.setTextColor(Color.WHITE);
return categoryTitle;
}*/
}
Here's how I'm setting it via my preferences.xml file
<com.brightr.weathermate.views.CustomPreferenceCategory android:title="Weather" >
<CheckBoxPreference
android:defaultValue="true"
android:key="degreesC"
android:summary="Display the weather degrees in Celsius units"
android:title="Show Celsius" />
<CheckBoxPreference
android:defaultValue="true"
android:key="degreesF"
android:summary="Display the weather in Farenheit units"
android:title="Show Farenheit" />
<ListPreference
android:entries="#array/textColors"
android:entryValues="#array/textColor_values"
android:summary="Change the color of the temerature text"
android:title="Temperature Text Color" />
</com.brightr.weathermate.views.CustomPreferenceCategory>
Any thoughts as to why only the background is being changed but the TextView is not visible? Also, even when I use setBackgroundResource() instead of setBackgroundDrawable(), it still doesn't set the text. Any help would be greatly appreciate guys.
Got it. Silly me, I forgot to pass in the other two params in each of the constructors. I feel like kicking myself.
public CustomPreferenceCategory(Context context) {
super(context);
}
public CustomPreferenceCategory(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomPreferenceCategory(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
Related
I have a custom font file, say myfont.ttf in assets/fonts/
I have created a custom View like this
public class IconFontView extends AppCompatTextView {
public IconFontView(Context context) {
super(context);
applyIconFonts(context);
}
public IconFontView(Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
applyIconFonts(context);
}
public IconFontView(Context context, #Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
applyIconFonts(context);
}
private void applyIconFonts(final Context context) {
final Typeface iconFont = Typeface.createFromAsset(context.getAssets(), "fonts/myfont.ttf");
setTypeface(iconFont);
}
}
in XML:
<com.smule.singandroid.customviews.IconFontView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="400dp"
android:text="#string/icontext"/> <!-- my unicode for the specific icon -->
strings.xml:
<string name="icontext"></string>
This way, I can see the preview perfectly fine.
But, if I enter this fragment, I see nothing but an empty view. (using "Show layout bounds" to tell you that this view exists there, just not drawing)
HOWEVER, if I add this font to my styles.xml like this
<style name="IconFont">
<item name="fontPath">fonts/myfont.ttf</item>
</style>
and apply it in layout xml like this
<com.smule.singandroid.customviews.IconFontView
style="#style/IconFont"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="400dp"
android:text="#string/icontext"/> <!-- my unicode for the specific icon -->
This not only displays correct preview, but also shows the icon perfectly fine.
This has same effect as doing something like
mIconFontview.setText(R.string.icontext);
mIconFontView.setTypeFace(...);
Why would this work this way?
Then there would be no reason to create IconFontView at all. Might as well just use TextView.
I have a lot of views using one and the same color as a background. I want to change the color of all views when I receive a call from the server programmatically. I don't want to call for every view
view.setBackgroundColor(new color);
Is there a way to change a color code that is in colors.xml.
Short answer: No, you can't. The resources are defined at compile time.
See this question for a similar case: How can I programmatically change the value of a color in colors.xml?
You can't replace the value of the color in the xml file. But you
can create different themes which are used in your application and
change the theme dynamically
See this tutorial:
http://www.developer.com/ws/android/changing-your-android-apps-theme-dynamically.html
What I end up doing is create a custom class that sets the color form preference. And use this class everywhere I want to change the color. And next time the view is drawn it gets the new color. Something like this:
public class ColoredToolbar extends android.support.v7.widget.Toolbar {
public ColoredToolbar(Context context) {
super(context);
setBackgroundColor(context);
}
public ColoredToolbar(Context context, AttributeSet attrs) {
super(context, attrs);
setBackgroundColor(context);
}
public ColoredToolbar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setBackgroundColor(context);
}
private void setBackgroundColor(Context context) {
int color = PreferenceHelper.getToolBarColor(context, Preferences.PREF_TITLE_BAR_COLOR_KEY);
this.setBackgroundColor(color);
}
}
i have Helvetica Neue.ttf in asset Folder , How to set the Helvetica Neue textStyle on My Entire Applcation.
There is currently no way to do this with the Views that come with the Android SDK. You can set your View to use any of the Roboto fonts as per this answer, but you cannot set a custom font.
The way I typically tackle this problem is to create my own TextView that uses my font, like so:
public class MyFontTextView extends TextView {
public static final String FONT_PATH = "fonts/MyFont.ttf";
public MyFontTextView(Context context) {
super(context);
initFont();
}
public MyFontTextView(Context context, AttributeSet attrs) {
super(context, attrs);
initFont();
}
public MyFontTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initFont();
}
/**
* Set up the font.
*/
private void initFont() {
if (!isInEditMode()) {
Typeface font = Typeface.createFromAsset(getContext().getAssets(), FONT_PATH);
setTypeface(font);
}
}
}
You replace all of your TextViews with this TextView, and then you will have your font. Note that other UI elements (e.g. Buttons) will still use Roboto unless you also customize those.
If you have a View that you only use once in your application, you could call setTypeFace() on that View instead of creating a custom View. The custom View method works well for Views that you use a lot in an application such as TextViews.
i want use all the component in android which having the same font type face, for that i am creating a individual custom class for each component like CustomTextView, CustomEditText, etc,..
So instead of creating a individual class for each component can i create a view CustomView class that will automatically apply style for all the components in android
Just declare your own TextView and use it in your XML, it should appear in the custom Views
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setType(context);
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setType(context);
}
public MyTextView(Context context) {
super(context);
setType(context);
}
private void setType(Context context){
this.setTypeface(Typeface.createFromAsset(context.getAssets(), "chalk_board.ttf"));
}
Oh dam u want it globally for all views, so this is the wrong approach.... Sorry for that
You have at least 2 ways:
create your own TextView class and set fontFace in constructor
you can use custom LayoutInflater. And every time view gets inflated check that it is textView (or other view not extending textView but having font settings) and set correct fontFace settings.
I have developed a very huge application and now i have a requirement of having custom font for all controls in the application. so I want to know the better way to change the font in one shot. The application has more than a hundred XML layout. and i cant change all controls to a custom component with custom font. Please provide a solution to Change the font without altering all the controls in XML.
Do something like this
pacage com.prac;
class MyFontedTextView extends TextView {
public FontedTextView(Context context) {
super(context);
init();
}
public FontedTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public FontedTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
String otfName = "MyCustomOtfFileWhichIPutInAssetsFolder.otf";
Typeface font = Typeface.createFromAsset(context.getAssets(), otfName);
this.setTypeface(font);
}
}
Now replace this all over in xml file from your TextViews
<com.prac.MyFontedTextView .... instead of <TextView
This change you have to do all over for it to apply
also for the case of button text . Button is also subclass of TextView
So the same can work for button's too
Hope this help or can lead you to the solution you are looking