How to use custom font from String.xml file - java

Here is the my string array..here is the question how to apply custom font through java code.
Here string array declare to get resource from array-->
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
Typeface face=Typeface.createFromAsset(getAssets(),"fonts/BRADHITC.TTF");
Please share your knowledge..thank for advance.
<string-array name="nav_drawer_items">
<item >Home</item>
<item >Temples</item>
<item >Resorts</item>
<item >Travels</item>
<item >Colleges</item>
<item >Queries</item>
</string-array>

You cannot apply font to string or string-array . Font need to be applied to a TextView , so that the text style on the view will change depends on the font.
Create your own custom font
Typeface face=Typeface.createFromAsset(getAssets(),"fonts/BRADHITC.TTF");
And, then set it to the TextView like this
textView.setTypeface(face);
If you are setting the string-array items to a listView , then you need to create a Custom Adapter, and when the item view is created , set the font to the item TextView

Related

Adding components programmatically has different style than those in XML layout

Components (text and edit boxes) defined in layout XML looks very different from those i add programatically.
I've tried to apply the same textAppearance style programatically as the ones from my XML has. I tried calling setTheme() after adding the components too. No difference.
TextView tv10 = new TextView(getApplicationContext());
tv10.setText("Back width");
EditText tv11 = new EditText(getApplicationContext());
tv11.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_CLASS_NUMBER);
TextView comes out with small font and gray text, EditText comes out with black background and gray text.
The components must be added programmatically because of options the user choose. Those user choices are defined in the XML and follow the expected color scheme, which is the Android Studio defaults. (Black text on white background)
You shouldn't use Application Context for views. Only Activity context. Also you can pass style as parameter, when create view in code.
As Yamko said, style can be passed in the constuctor
var textView = TextView(context, null, R.style.LoginBodyTextViewStyle)
where the style can be something like
<style name="LoginBodyTextViewStyle" parent="android:Widget.TextView">
<item name="android:textStyle">normal</item>
<item name="android:fontFamily">sans-serif</item>
<item name="android:textSize">#dimen/text_size_default</item>
<item name="android:textColor">#color/grey</item>
</style>

How to change item drawable color inside layer-list in android

I've created a drawable file with the below code:
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/gray_background"/>
<item
android:drawable="#color/light_green"
android:bottom="#dimen/event_button_bottom_color"/>
</layer-list>
Now at run time, I want to change the second item's drawable color (#color/light_green) with some other color programmatically . How can I do that, please help if anyone know how to achieve this.
Thanks a lot in advanced. :)
First add id for item. Find item by id and change color.
<item android:id="#+id/shape_1" android:drawable="#color/gray_background"/>
<item android:id="#+id/shape_2"
android:drawable="#color/light_green"
android:bottom="#dimen/event_button_bottom_color"/>
Modify at runtime:
LayerDrawable layerDrawable = (LayerDrawable) getResources()
.getDrawable(R.drawable.my_drawable);
GradientDrawable gradientDrawable = (GradientDrawable) layerDrawable
.findDrawableByLayerId(R.id.shape_1);
gradientDrawable.setColor(...);

Style applied to an AlertDialog not working correctly

I've been asked to match the look of an Alert Dialog in our app to the one used by the app's theme.
I managed to apply a style to all Alert Dialogs in the app using it as part of the app's theme, but there are situations where the style is not applying correctly.
It happens for example when the Alert Dialog contains a 'Single Choice List' as its' message.
The title looks fine, so is the background and the button bar, but the list itself is problematic.
At first, the radio buttons as well as their textual description were black colored, as if android is using the default color.
I somehow managed to color the radio buttons as I wish, by using these attributes:
<item name="android:colorControlNormal">#color/text_secondary</item>
<item name="android:colorControlActivated">#color/text_secondary</item>
But the text color still remains black, and I've tried EVERY possible text color attribute exposed by android.
It looks like this:
Now this is the full style defined for the Alert Dialogs:
<style name="GenericAlertDialog.Alter" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowMinWidthMajor">#android:dimen/dialog_min_width_major</item>
<item name="android:windowMinWidthMinor">#android:dimen/dialog_min_width_minor</item>
<item name="android:windowTitleStyle">#style/DialogTitle</item>
<item name="android:textColor">#color/text_secondary</item>
<item name="android:textColorPrimary">#color/primary</item>
<item name="android:background">#color/window_background</item>
<item name="android:colorAccent">#color/accent</item>
<item name="android:textColorAlertDialogListItem">#color/text_secondary</item>
<!--<item name="android:textColorSecondary">#color/text_secondary</item>-->
<item name="android:colorControlNormal">#color/text_secondary</item>
<item name="android:colorControlActivated">#color/text_secondary</item>
</style>
This is my Theme definition:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowBackground">#color/window_background</item>
<item name="android:colorPrimary">#color/primary</item>
<item name="android:colorAccent">#color/accent</item>
<item name="android:textColorPrimary">#color/text_primary</item>
<item name="android:textColorSecondary">#color/text_secondary</item>
<item name="android:textColorHint">#color/text_hint</item>
<item name="android:buttonStyle">#style/GenericButton</item>
<item name="android:checkboxStyle">#style/GenericCheckBox</item>
<item name="android:alertDialogTheme">#style/GenericAlertDialog</item>
<item name="alertDialogTheme">#style/GenericAlertDialog</item>
</style>
This is the code I'm using to create a custom Alert Dialog:
AlertDialog.Builder dialogBuilder = null;
try
{
dialogBuilder = new AlertDialog.Builder(i_OwnerActivity, R.style.GenericAlertDialog_Alter);
LayoutInflater layoutInflater = i_OwnerActivity.getLayoutInflater();
// Inflate the dialog's custom title view and set it's text to the matching one to this class
View customTitleView = layoutInflater.inflate(R.layout.dialog_title, null);
TextView customTitleTextView = (TextView) customTitleView.findViewById(R.id.DialogTitleText);
// Set text of customTitleView
dialogBuilder.setCustomTitle(customTitleView);
// Create an event handler for clicking on the negative button
dialogBuilder.setNegativeButton(R.string.action_dialog_negative_cancel, new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface i_Dialog, int i_Which)
{
// Do Something
}
});
} catch (Exception e)
{
LogUtils.logException(AlterDialogUtils.class, e);
}
return dialogBuilder;
And finally, here's the code I'm using to create an Alert Dialog with a 'Single Choice List':
dialogBuilder.setSingleChoiceItems(R.array.squelch_modes, m_InitialState, new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
// Do Something
}
});
What am I doing wrong? How can I change the color of the text?
It is also worth saying that I'm using AppCompat's AlertDialog.
Just found this old post with Google, but since there is no answer I will add what did the trick in my case:
remove the android: prefix from the textColorAlertDialogListItem in your alertdialog style
<item name="textColorAlertDialogListItem">#color/text_secondary</item>
I guess that's because of the parent being an AppCompat theme, but I am not sure about this. I still added both (with and without prefix) in my style...
I know it's probably late... but here's your answer: android.support.v7.app.AlertDialog in AppCompat support library use this layout by default (unless you provide your own adapter) for singleChoiceDialog:
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="?attr/textColorAlertDialogListItem"
android:gravity="center_vertical"
android:paddingLeft="#dimen/abc_select_dialog_padding_start_material"
android:paddingRight="?attr/dialogPreferredPadding"
android:paddingStart="#dimen/abc_select_dialog_padding_start_material"
android:paddingEnd="?attr/dialogPreferredPadding"
android:drawableLeft="?android:attr/listChoiceIndicatorSingle"
android:drawableStart="?android:attr/listChoiceIndicatorSingle"
android:drawablePadding="20dp"
android:ellipsize="marquee" />
The attribute used to set this up in the theme is singleChoiceItemLayout so you can override it with your own layout to get whatever UI you want.
If you just want to change the text color just define the attribute textColorAlertDialogListItem as you can see from the layout it's the one used for android:textColor.
In general when I need something like this i go and look at the source code since it is available. The support libraries source code can be found here, while most of the framework source code can be find here.

Ratingbar style in ListView, strange behavior

I use custom RatingBar style in ListView row element, but it cause some strange problem.
I have got custom style like this:
<style name="RatingBar" parent="#android:style/Widget.RatingBar">
<item name="android:progressDrawable">#drawable/course_rating_bar</item>
<item name="android:minHeight">15dip</item>
<item name="android:maxHeight">15dip</item>
</style>
And I use this in my RatingBar in xml (in layout of listView row):
style="#style/CourseRatingBar"
But then the ListView onItemClickListener just doesn't work. When I click on element,
there is no action.
When I use this as my RatingBar style:
style="?android:attr/ratingBarStyleSmall"
everything (onItemClickListener ;-)) works fine.
The ListView use BaseAdapter with Holder pattern, with RatingBar, TextView and ImageView.
From my expirience- if this don't work, try to customize your rating bar How to create Custom Ratings bar in Android

Android styles and themes

I create simple style.xml file
<style name="You.EditText.Style" parent="#android:style/Widget.EditText">
<item name="android:textColor">#color/colorRed</item>
<item name="android:gravity">center</item>
</style>
But all EditTexts I create dynamically. How I can declare my style to EditText attribute?
Instantiate your EditText view like this:
EditText e = new EditText(context, null, R.style.You_EditText_Style);
You can do this with the EditText#setTextAppearance(context, resid) method.
Example:
myEditText.setTextAppearance(getApplicationContext(), R.style.id);
See EditText reference.

Categories