Custom Font in View Pager in android - android

I am developing demo application in which I am using view pager. Now I want to know that can we change the text style of Title displayed in view pager.
Please give your suggestions on it.
Thanking you in advance.

Working demo
TextView txt = (TextView) v.findViewById(R.id.custom_font);
and then change your font
Something like this:
switch (position) {
case 0:
v = (LinearLayout) LayoutInflater.from(cxt).inflate(R.layout.lcmeter, null);
TextView txt = (TextView) v.findViewById(R.id.custom_font);
Typeface font = Typeface.createFromAsset(getAssets(), "Chantelli_Antiqua.ttf");
txt.setTypeface(font);
break;
}

Related

How to Change font of Text in MaterialTabHost in Android?

I am using MaterialTabHost for displaying tabs. I wanted to change the font of the MaterialTabHost when onTabSelected. I tried to get the textview of the same. but cant figured it out. I referred https://github.com/neokree/MaterialTabs.
any help would be appriciated.
Here is my XML .
<!-- for Text Tabs -->
<it.neokree.materialtabs.MaterialTabHost
android:id="#+id/materialTabHost_MainActivity"
android:layout_width="match_parent"
android:layout_height="48dp"
android:animateLayoutChanges="false"
app:accentColor="#color/white"
app:primaryColor="#color/main_blue_light"
app:textColor="#color/white"
/>
so how do we change the font ?
This is how I change font of text into tab when it is create:
Typeface fontRobotoRegular = Typeface.createFromAsset(getAssets(),"font/RobotoCondensed-Regular.ttf");
MaterialTab tab = new MaterialTab(getApplicationContext(), false);
View tabView = tab.getView();
TextView text = (TextView) tabView.findViewById(R.id.text);
tab.setText(pagerAdapter.getPageTitle(i));
text.setTypeface(fontRobotoRegular);
tab.setTabListener(this);
tabHost.addTab(tab);
So if you want to change font when the tab is selected you can do something like this on onPageSelected of your ViewPager:
public void onPageSelected(int position) {
...
Typeface fontBold = Typeface.createFromAsset(getAssets(), "font/RobotoCondensed-Bold.ttf");
TextView text = (TextView) tabs.getCurrentTab().getView().findViewById(R.id.text);
text.setTypeface(fontBold);
...
}

How to set custom font to android toolbar's subtitle?

i want to get the subtitle´s textview in an android's toolbar to change it's font. Actually i'm doing it with the title, getting it on this way:
Field f = toolbar.getClass().getDeclaredField("mTitleTextView");
f.setAccessible(true);
titleTextView = (TextView) f.get(toolbar);
I've tried with the same code but trying to get "mSubtitleTextView" but that's not the solution.
Thanks!!
You can get the subtitle TextView like this:
View subTitleView = toolbar.getChildAt(1);
If you don't add any views to the toolbar, his default structure is:
[0] - (TextView) title
[1] - (TextView) subtitle
[2] - (ActionMenuView) menu
Hope it helps!
Definitely not the best way, but in a pinch this will work. You will have to figure out a way to implement myTypefaceSpan though; I'm using Calligraphy so it ties together decently for me.
CalligraphyTypefaceSpan myTypefaceSpan = new CalligraphyTypefaceSpan(
TypefaceUtils.load(this.getAssets(), "fonts/custom_font.ttf"));
public static void setToolbarSubtitle(String subtitle, Context context) {
SpannableStringBuilder sBuilder = new SpannableStringBuilder();
sBuilder.append(subtitle);
sBuilder.setSpan(MainActivity.myTypefaceSpan, 0, sBuilder.length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
((MainActivity)context).getSupportActionBar().setSubtitle(sBuilder);
}

Change the text of textview based on its ID

I am dynamically adding a textView into a layout and giving it ID's. Based on some conditions I want to change the text of the TextView based on its ID assigned.
Something like this: personOne.setText("abcd"); where personOne.id = buttonID;
personOne = new TextView(this);
int buttonID = 2000 + personCount;
personOne.setTextAppearance(this, R.style.button_text);
personOne.setTextColor(getResources().getColor(R.color.red));
personOne.setTextSize(15);
personOne.setText(personAdded);
personOne.setId(buttonID);
Help / Suggestions much appreciated.
Thank you
Use something like this:
TextView tv = (TextView) findViewById(yourId);
tv.setText("abcd");
Assuming the TextView is a child of the Activity's content view.
You can use the buttonID to find and manipulate TextView dynamically as follows:
TextView dynamicTextView = (TextView) findViewById(buttonID);
dynamicTextView.setText("The text you want to set");

Multiple fonts for multiple languages

I have recently come across a situation while developing an app where i have to display different languages in text view . currently I am displaying few using fonts/typeface like this :
Typeface tf = Typeface.createFromAsset(this.getAssets(),
"DroidHindi.ttf");
TextView textView1 = (TextView) findViewById(R.id.textView2);
textView1.setTypeface(tf);
textView1.setText("कचजड, कचजड");
Typeface tf1 = Typeface.createFromAsset(this.getAssets(),
"asunaskh.ttf");
TextView textView = (TextView) findViewById(R.id.textView1);
textView.setTypeface(tf1);
textView.setText("یہ انگریزی نہیں");
Typeface tf2 = Typeface.createFromAsset(this.getAssets(),
"Banglafont.ttf");
TextView textView2 = (TextView) findViewById(R.id.textView3);
textView2.setTypeface(tf2);// এই ইংরেজি নয়
textView2.setText("এই ইংরেজি নয়");
its fine my question is that i have to support for some 20 different languages then things will become very tedious when i apply this in different activities . Any alternative way to achieve.
You do is to create a class and a function with access public that will return you TextView Object with the Font that you want to suppose.
TextView public SetLanguage(TextView tv,string type)
{
TextView newtv = tv;
Typeface tf;
switch(type)
{
case "urdu":
tf = Typeface.createFromAsset(this.getAssets(),"urdu.ttf");
break;
case "hindi":
tf = Typeface.createFromAsset(this.getAssets(),"hindi.ttf");
break;
// up so on
}
newtv.setTypeface(tf);
return newtv;
}
// and call it any where..
TextView textView1 = (TextView) findViewById(R.id.textView2);
textView1 = classobj.SetLanguage(textView1,"urdu");
//assign string of text to it
Initialize your typefaces when the app starts up, and make a method which takes any view and sets the typeface based on the language.
Just like we do with other resource files, we can also define a font directory for any locale or country code we want to. The downside to this approach is that both the fonts have to be named the same although they are different but otherwise a clean solution.

How to apply Font type for a whole application in Android?

I want to apply Font type without using XML. By code, I want to change Font type and it should be able to apply for a whole application, when I click Arial else Times New Roman etc.
try this code to change the font of app
TypeFace typeFace = Typeface.createFromAsset(getAssets(), "font.ttf");
TextView view= (TextView) findViewById(R.id.view);
view.setTypeface(typeFace)
I would reccomend having a list preference, in a preference screen. You can then set the fonts from that. i.e:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
String font = sp.getString("font","-1")
if(font.equals("timesroman")) {
TypeFace typeFace = Typeface.createFromAsset(getAssets(), "timesnewroman.ttf");
}
else if(font.equals("arial")) {
TypeFace typeFace = Typeface.createFromAsset(getAssets(), "arial.ttf");
}
TextView view= (TextView) findViewById(R.id.view);
view.setTypeface(typeFace)

Categories

Resources