SearchView custom font in android - android

I have been trying to set custom font to the android.support.v7.widget.SearchView query hint and the text entered in the View.I did try setting the font dynamically from the assests to the searchView by creating a TypeFace object, but the problem occurs that "SearchView doesn't contain a setTypeface(Typeface tf) method." I did try for a custom SearchView class but couldn't find one.
private void setFont(String font1, String font2, String font3)
{
Typeface tf = null;
tf = UiUtil.changeFont(MainActivity.this, font1);
btn1.setTypeface(tf);
tf = UiUtil.changeFont(MainActivity.this, font2);
btn2.setTypeface(tf);
tf = UiUtil.changeFont(MainActivity.this, font3);
btn3.setTypeface(tf);
tf = UiUtil.changeFont(MainActivity.this, font3);
// no set typeface method..
}

The workaround here is to first get the searchView's textView and then change the typeface for the textView instead:
TextView searchText = (TextView)
searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
Typeface myCustomFont = Typeface.createFromAsset(getAssets(),"fonts/myFont.ttf");
searchText.setTypeface(myCustomFont);
Or, if you're not using appcompatv7:
int id = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
TextView searchText = (TextView) searchView.findViewById(id);
then set the typeface as usual.

To change the font family in searchview programmatically from xml font folder:
Typeface tf = ResourcesCompat.getFont(dis,R.font.montserrat_regular);
TextView searchText = (TextView)search_view.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchText.setTypeface(tf);

For Kotlin and Androidx
val face: Typeface = Typeface.createFromAsset(context?.assets, "font.otf")
val searchText = searchView.findViewById<View>(androidx.appcompat.R.id.search_src_text) as TextView
searchText.typeface = face

Thanks to all the previous answers, I found my best solution witch I hope that you find it the cleanest answer, too.
This is working for SearchView class inside the package android.widget
First, create an extension like below:
import android.graphics.Typeface
import android.widget.SearchView
import android.widget.TextView
fun SearchView.setTypeFace(typeface: Typeface?) {
val id = context.resources.getIdentifier("android:id/search_src_text", null, null)
val searchText = searchView.findViewById(id) as TextView
searchText.typeface = typeface
}
Then use this extension wherever you like:
val typeface = ResourcesCompat.getFont(requireContext(), R.font.sans)
searchView.setTypeFace(typeface)
You may want to get typeface from assets or other ways. but the rest is the same.

If anyone looking to implement the same in xml
Try the solution mentioned this answer able to customise
{textSize, fontFamily, textColor, hideBottomLine} in SearchView - https://stackoverflow.com/a/73997430/3020859

Related

Assigning Custom Font to TextView [duplicate]

This question already has answers here:
Android - Using Custom Font
(21 answers)
Closed 5 years ago.
I'm new to programming and I'm looking to add a custom font I download to Android Studio. I was able to follow instructions how to add the font but when I run the app I can only get one TextView of my two two TextViews to use this font. This is my code, can someone please tell me what I'm doing wrong here. Thank you!
public class MainActivity extends AppCompatActivity {
TextView text, text2;
Typeface tfc1, tfc2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.top_text);
text2 = (TextView) findViewById(R.id.bottom_text);
tfc1 = Typeface.createFromAsset(getAssets(),"fonts/StarWars.ttf");
text.setTypeface(tfc1);
tfc1 = Typeface.createFromAsset(getAssets(),"fonts/StarWars.ttf");
text2.setTypeface(tfc2);
}
}
I assume there's a typo. You wrote tfc1 but you set tfc2. From your code:
tfc1 = Typeface.createFromAsset(getAssets(),"fonts/StarWars.ttf");
text2.setTypeface(tfc2);
As both of the tfc1 and tfc2 fonts are same. You can use one typeface to both of the textviews as #Sagar Patel showed.
STEP 1/
Start by creating a folder named assests then inside that folder create another one named folder and import your *.ttf files to that folder
STEP 2/
Now import this before start writing the code given below:
import android.graphics.Typeface;
Now implement the following code to your class:
// Font path
String fontPath = "fonts/Face Your Fears.ttf";
// text view label
TextView txtGhost = (TextView) findViewById(R.id.ghost);
// Loading Font Face
Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
// Applying font
txtGhost.setTypeface(tf);
I suggest you follow this tutorial right here it will guide step-by-step through using external fonts in Android Studio
This tutorial link helps to understand on configuring the custom font in android.
You can use android:fontFamily attribute in xml file.
public class MainActivity extends AppCompatActivity {
TextView text, text2;
Typeface tfc1, tfc2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.top_text);
text2 = (TextView) findViewById(R.id.bottom_text);
tfc1 = Typeface.createFromAsset(getAssets(),"fonts/StarWars.ttf");
text.setTypeface(tfc1);
// tfc1 = Typeface.createFromAsset(getAssets(),"fonts/StarWars.ttf");
//you get error in this line text2.setTypeface(tfc2);
text2.setTypeface(tfc1);
}
}

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);
}

I got stuck in getAssests() in the below code

public class XYZ extends LinearLayout{
TextView text = (TextView) findViewById(R.id.kid_name);
Typeface font = Typeface.createFromAsset(getAssets(), "eng111.ttf");
text.setTypeface(font);
}
I just try to use the other format for the text and the problem is occurred "create the getAssets() method "
Where do I mistake ? please sort out this problem
You need a Context, since you are not in an Activity you need to call:
getContext().getAssets();

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