i'm trying to display an arabic text (Right to Left) in my app, but i get words displayed left to right in lines.
I'm using an arabic supporting font and arabic reshaper lib. The text is loaded from strings file.
The app support Android 2.1.
Here a screen shot for the result:
For more details:
public class MainActivity extends Activity {
private TextView tvtest = null;
private static Typeface typeface;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvtest = (TextView) findViewById(R.id.tvtest);
tvtest.setText(ArabicUtilities.reshape(getString(R.string.first_part)));
Typeface tf;
tf = GetFont(getApplicationContext());
tvtest.setTypeface(tf);
}
public static final Typeface GetFont(Context context) {
if (typeface == null) {
typeface = Typeface.createFromAsset(context.getAssets(),
// "DroidSansFallback.ttf");
"MSHQW.TTF");
}
return typeface;
}
}
Try to set gravity of your TextView.
in code:
`textView.setGravity(...);
or in XML:
android:gravity="..."
Related
I take a hindi text value(using gboard hindi keyboard) from editText and set it on TextView using a button click and then have 3 buttons which changes the font of the textview.
Hindi font is taken from the assets.
It is not working when I'm using hindi keyboard but it is working when I'm typing text using english keyboard.
Here is the code:
EditText et;
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
et = (EditText) findViewById(R.id.et);
tv = (TextView) findViewById(R.id.tv);
}
public void setclick(View v){
tv.setText(et.getText());
}
public void click1(View v){
tv.setTypeface(Typeface.createFromAsset(getAssets(), "hindi2.ttf"));
}
public void click2(View v){
tv.setTypeface(Typeface.createFromAsset(getAssets(), "hindi3.ttf"));
}
public void click3(View v){
tv.setTypeface(Typeface.createFromAsset(getAssets(), "hindi4.ttf"));
}
So my question is how can I change the font of hindi text which is acquired by user input.
This question already has answers here:
TextView with different fonts and styles?
(3 answers)
Closed 5 years ago.
Assume my title is "Nearby - Friends" I want to show that as "Nearby-
Friends"
How can I achieve it.
I tired a lot but I can't achieve that.
HERE is code that I use, I use Typeface
TextView text = (TextView) tab.getCustomView();
String subTitle = text.getText().toString().trim();
text.setTextColor(getResources().getColor(R.color.white));
Typeface face = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Bold.ttf");
text.setTypeface(face);
TextView title = new TextView(getApplicationContext());
title.setText("Nearby - ");
title.setTypeface(face);
if (subTitle.equals("NEAR BY")) {
subTitle = "People";
} else if (subTitle.equals("FRIENDS")) {
subTitle = "Friends";
} else if (subTitle.equals("FAMILY")) {
subTitle = "Family";
}
toolbar.setTitle(title.getText().toString() + subTitle);
Try This
Let say myToolBar is your toolbar layout in xml.
Toolbar toolBar = (Toolbar) findViewById(R.id.myToolbar);
setSupportActionBar(toolBar);
Then Add Below code
String title = "<b>Near by-</b>Friends"
getSupportActionBar().setTitle(Html.fromHtml(title));
You can use SpannableString to set different font style for single string sentence in TextView or other text enabled component
String strMessage="Nearby- Friends";
Spannable spannable = new SpannableString(strMessage);
//"Nearby-" bold font
spannable.setSpan(getTextAppearanceSpan(ContextCompat.getColor(mContext, android.R.color.BLACK),Typeface.BOLD), 0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannable);
//"Friends" normal font
spannable.setSpan(getTextAppearanceSpan(ContextCompat.getColor(mContext, android.R.color.BLACK),Typeface.NORMAL), 8, strMessage.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannable);
TextAppearanceSpan method to customized text with color
/*
*get customized text with color, style
*/
private TextAppearanceSpan getTextAppearanceSpan(int color, int fontStyle) {
ColorStateList blueColor1 = new ColorStateList(new int[][]{new int[]{}}, new int[]{color});
return new TextAppearanceSpan(null, fontStyle, -1, blueColor1, null);
}
If you want use two different fonts in TextView (e.g. toolbar title) you should use Spannable to apply font to each part of text.
The following solution will apply two custom fonts from your resources.
String title = "Nearby- ";
String subtitle = "Friends";
Typeface typefaceBold = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Bold.ttf");
Typeface typefaceNormal = Typeface.createFromAsset(getAssets(), "fonts/Roboto.ttf");
SpannableString spannableTitle = new SpannableString(title + subtitle);
spannableTitle.setSpan (new BetterTypefaceSpan(typefaceBold), 0, title.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
spannableTitle.setSpan (new BetterTypefaceSpan(typefaceNormal), title.length(), spannableTitle.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
toolbar.setText(spannableTitle);
BetterTypefaceSpan.java
// Copy of CalligraphyTypefaceSpan
// https://github.com/chrisjenx/Calligraphy/blob/df1c07f82926831a5c47443bc49f9ff718a4c700/calligraphy/src/main/java/uk/co/chrisjenx/calligraphy/CalligraphyTypefaceSpan.java
import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.text.style.MetricAffectingSpan;
public class BetterTypefaceSpan extends MetricAffectingSpan {
private final Typeface typeface;
public BetterTypefaceSpan(final Typeface typeface) {
if (typeface == null) {
throw new IllegalArgumentException("typeface is null");
}
this.typeface = typeface;
}
#Override
public void updateDrawState(final TextPaint drawState) {
apply(drawState);
}
#Override
public void updateMeasureState(final TextPaint paint) {
apply(paint);
}
private void apply(final Paint paint) {
final Typeface oldTypeface = paint.getTypeface();
final int oldStyle = oldTypeface != null ? oldTypeface.getStyle() : 0;
final int fakeStyle = oldStyle & ~typeface.getStyle();
if ((fakeStyle & Typeface.BOLD) != 0) {
paint.setFakeBoldText(true);
}
if ((fakeStyle & Typeface.ITALIC) != 0) {
paint.setTextSkewX(-0.25f);
}
paint.setTypeface(typeface);
}
}
I'm trying to have one TextView switch from one sentence to another alongside my ImageSwitcher. Here is a sample code for my activity
private Integer images[]={R.drawable.image1,R.drawable.image2,R.drawable.image3,R.drawable.image4....};
private int currImage=0;
private Integer text[]={R.string.text0,R.string.text1,R.string.text2,R.string.text3,R.string.text4....};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeImageSwitcher();
setInitialImage();
setImageRotateListener();
setInitialText();
}
private void initializeImageSwitcher() {
final ImageSwitcher imageSwitcher = (ImageSwitcher) findViewById(R.id.imageswitcher);
imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
#Override
public View makeView() {
ImageView imageView = new ImageView(MainActivity.this);
return imageView;
}
});
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.right_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.left_out));
}
private void setImageRotateListener() {
final ImageButton rightarrow = (ImageButton) findViewById(R.id.next);
rightarrow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
currImage++;
if (currImage == 29) {
currImage = 0;
}
setCurrentImage();
setCurrentText();
}
});
}
private void setInitialImage() {
setCurrentImage();
}
private void setInitialText(){
setCurrentText();
}
private void setCurrentImage() {
final ImageSwitcher imageSwitcher = (ImageSwitcher) findViewById(R.id.imageswitcher);
imageSwitcher.setImageResource(images[currImage]);
}
private void setCurrentText() {
final TextView textSwitcher = (TextView) findViewById(R.id.textView);
textSwitcher.setText(Integer.toString(text[currImage]));
}
The it compiles and run just fine. Issue lies in the text output. Instead of the sentences defined in the string, the TextView displays a series of numbers.
For example:
<string name="text1">Sentence one</string>
<string name="text2">Sentence two</string>
<string name="text3">Sentence three</string>
<string name="text4">Sentence four</string>
The output for "text1" would be 8513856, "text2" would be 841363, "text3" would be 18413587, and so on.
If anyone one has an idea of how to solve this issue, your help would be greatly appreciated.
You are displaying the resource id's of the strings, not the strings themselves.
Change to:
private void setCurrentText() {
final TextView textSwitcher = (TextView) findViewById(R.id.textView);
textSwitcher.setText(getResources().getString(text[currImage]));
}
I am trying to change the font of android.support.v7.app.AlertDialogtitle text.
METHOD 1 :
TextView title = (TextView) dialog.findViewById(android.R.id.title); //returns null
METHOD 2 :
final int titleId = context.getResources().getIdentifier("alertTitle", "id", "android");
TextView title = (TextView) dialog.findViewById(titleId); //Also returns null.
Is there any other way to get the title TextView?
Please note I do not want to use a custom layout.
Thanks.
I got it to work using this solution :
final AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
Typeface tf = //get the typeface.
CustomTFSpan tfSpan = new CustomTFSpan(tf);
SpannableString spannableString = new SpannableString(title);
spannableString.setSpan(tfSpan, 0, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
alertBuilder.setTitle(spannableString);
AlertDialog dialog = alertBuilder.create();
dialog.show();
CustomTFSpan
public class CustomTFSpan extends TypefaceSpan {
private Typeface typeface;
public CustomTFSpan(Typeface typeface) {
super("");
this.typeface = typeface;
}
#Override
public void updateDrawState(TextPaint ds) {
applyTypeFace(ds, typeface);
}
#Override
public void updateMeasureState(TextPaint paint) {
applyTypeFace(paint, typeface);
}
private static void applyTypeFace(Paint paint, Typeface tf) {
paint.setTypeface(tf);
}
}
Use this one
TextView title = (TextView) dialog.findViewById(R.id.alertTitle);
Without any custom title :)
Your question has already answer here : Change Title Font Of Alert Dialog Box Android
You can simply use a textview and set it as custom title like this : builder.setCustomTitle(tv2);
Create a simple TextView
TextView tv;
And replace
builder.setTitle("My Title");
with
builder.setCustomTitle(tv);
I have a large table with all kinds of strings and numbers, and a number of them have to be editable. The table is loaded from an object with getters and setters of course.
I want to create a general input screen with an edit text and an ok button; and use that to edit the fields. However, i can't get this to work, this is a snippet of my code:
public class InputViewController extends Activity {
private String inputType;
EditText mEdit;
private String inputString;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.inputscreen);
Bundle extras=getIntent().getExtras();
{
//get the type of input from the screen that sent us here, contained in a putextra
inputType = extras.getString("inputType");
TextView textViewInput = (TextView) findViewById(R.id.textViewInput);
textViewInput.setText(inputType);
mEdit = (EditText)findViewById(R.id.editText1);
}
//save input and return to previous screen
final Button button = (Button) findViewById(R.id.button3);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//save input based on inputType
if(inputType.equals("Oplossing")){
inputString = mEdit.getText().toString();
ActivityViewController.activityObject.setSolution(inputString);
System.out.println(ActivityViewController.activityObject.getSolution());
}
//return to previous screen
finish();
}
});