How to change the font in my app based on Locale? - java

public class MainActivity extends AppCompatActivity {
TextView hi;
Typeface ty;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hi = (TextView) findViewById(R.id.hi);
ty = Typeface.createFromAsset( getAssets() , "fonts/Spirax-Regular.ttf");
hi.setTypeface(ty);
}
}
this code worked very well for me.
My question is if my application support two languages which are (Arabic and English), is there any way to add two types of fonts to the same text? one for the Arabic Text and the other for the English one?

Try this
if(Locale.getDefault().getISO3Language().equals("ar")){
//LANGUAGE IS ARABIC
text.setTypeface(arabicTypeface);
}
else{
//LANGUAGE IS ENGLISH
text.setTypeface(englishTypeface);
}

This is not the proper way but this will solve your problem
textView.setText(Html.fromHtml(text));
This type of example is use to simply change the text font.

Solution :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
hi = (TextView) findViewById(R.id.tvTime);
setContentView(R.layout.activity_main);
if(getResources().getConfiguration().locale.getLanguage().equals("ar"))
hi.setTypeface(Typeface.createFromAsset( getAssets() , "YOUR ARABIC .TTF FILE HERE"));
else if(getResources().getConfiguration().locale.getLanguage().equals("en"))
hi.setTypeface(Typeface.createFromAsset( getAssets() , "YOUR ENGLISH .TTF FILE HERE"));
}

Related

Adding a raw .txt file to a layout

I'm trying to add a text file for a help page on my app but with the code I have it's just crashing the app when I select help from the options menu. I know it's to do with this code as when I comment it out it opens the help page fine.
The code is in my HelpActivity class:
public class HelpActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.help_page);
}
InputStream iFile = getResources().openRawResource(R.raw.gamehelp);
private String inputStreamToString(InputStream iFile) {
TextView helpText = (TextView) findViewById(R.id.tvHelpText);
String strFile = inputStreamToString(iFile);
helpText.setText(strFile);
return strFile;
}
}
Can anyone see any problem with how I'm trying to do this?
Thanks
Since you didn't include any error logs, I'm judging just from your code:
InputStream iFile = getResources().openRawResource(R.raw.gamehelp);
You are calling this in class body, but getResources() is not available here. What you should do is:
InputStream iFile;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.help_page);
iFile = getResources().openRawResource(R.raw.gamehelp);
}

How to fix error with rich text editor(android-RTEditor)?

I would like to use RTEditor in my app. but I have some errors in code. My question is what can I do with that? Instruction about RTEditor is here: https://github.com/1gravity/Android-RTEditor
and that's part of my code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.RTE_BaseThemeDark);
setContentView(R.layout.activity_editor);
newRowEdit = (EditText) findViewById(R.id.editNote);
newRowEdit1 = (EditText) findViewById(R.id.rtEditText);
// create RTManager
RTApi rtApi = new RTApi(this, new RTProxyImpl(this), new RTMediaFactoryImpl(this, true));
RTManager rtManager = new RTManager(rtApi, savedInstanceState);
// register toolbar
ViewGroup toolbarContainer = (ViewGroup) findViewById(R.id.rte_toolbar_container);
RTToolbar rtToolbar = (RTToolbar) findViewById(R.id.rte_toolbar);
if (rtToolbar != null) {
rtManager.registerToolbar(toolbarContainer, rtToolbar);
}
// register editor & set text
RTEditText rtEditText = (RTEditText) findViewById(R.id.rtEditText);
rtManager.registerEditor(rtEditText, true);
rtEditText.setRichTextEditing(true, message);
String text = rtEditText.getText(RTFormat.HTML);
}
Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mRTManager.onSaveInstanceState(outState);
}
#Override
public void onDestroy() {
super.onDestroy();
mRTManager.onDestroy(isFinishing());
}
The error is: cannot resolve symbol: message and cannot resolve symbol:mRTManager.
Can anyone help me please?
I can't see the whole code but it seems that both variables are not declared inside the corresponding methods. Make sure they are declared as class member variables
Replace mRTManager with rtManager, and make sure rtManager is declared outside onCreate method.

How to display the EditText fields text in the TextView

Of late I was just trying to create a mems geneator app, yes I had been following the new bostons and in that buckey created them using 2 fragments and here i just want to do in a single main activity, but I just can't figure out how to retrieve the text from the edit text field and set the text of Text view to it. I know it's pretty a newbie question but still I don't know how to code it so please help...
I had just imported some widgets,views,etc and done some modified the on create function and my on create function is:
public class MainActivity extends AppCompatActivity {
public static EditText topText;
public static EditText bottomtext;
public static TextView top;
public static TextView bottom;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
topText = (EditText) findViewById(R.id.topedit);
bottomtext = (EditText) findViewById(R.id.bottomedit);
top = (TextView) findViewById(R.id.top);
bottom = (TextView) findViewById(R.id.bottom);
Button myButton = (Button) findViewById(R.id.button);
myButton.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View V) {
top.setText((CharSequence) topText);
bottom.setText((CharSequence) bottomtext);
}
}
);
}
Simply do this:
if(topText.getText()!=null){
top.setText(topText.getText().toString());
}
if(bottomtext.getText()!=null){
bottom.setText(bottomtext.getText().toString());
}
Try this to get text of the EditText field:
CharSequence text = topText.getText();
And set the text above for the textView:
top.setText(text);
Use this short example to understand the concept
store=ret.getText().toString();
show.setText(store);
Explanation
ret: is the name of the edit text field;
store: is used to hold anything gotten from ret (text field)
show.setText(store) displays the result in the textview
This question has already been answered:
"Use getText() on your EditText object".
Get Value of a Edit Text field
Next time do a quick search ;)

Write arabic in Java file in Android

Hi I want to write Arabic in Java in Android and this is the code
public class hospital extends Activity {
ListView listView,l1;
ArrayAdapter<String> adapter,adapter1,adapter2;
String[] location,loc1,loc2;
Button home,map;
TextView name,address,phone;
WebView maps;
String hospital_name,hospital_address,hospital_phone,url;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hospital_location);
home = (Button)findViewById(R.id.home);
home.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(hospital.this, MainActivity.class ));
finish();
}
});
listView = (ListView) findViewById(R.id.hospital_list_location);
location = new String[] { "Al rabia","Tabarbor","Tla' al Ali","Daheat elrawda" };
This code will write like in the photo
And when I want to write Arabic in location like this. location=new String[]{"مستشفى هبه"}
It's want work and appear a strange words
Android can't handle the simple arabic letters (unicode 0x06xx). You need to use arabic presentation characters (0xFExx), and join the letters together yourself.
You also need to find a nice arabic font on the web- I use "AGA Rashheeq Bold"- and put it into your assets, then load it like this
Typeface tfArabic = Typeface.createFromAsset (getAssets (), "AGA-Rasheeq-Bold.ttf");
Then use something like this to use the font:
NextButton.setTypeface (tfArabic);

android imageview.setBackgroundResource() doesn't work

I have an imageview that should be changed on click
public class Settings extends Activity implements OnClickListener
{
private ImageView im1;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
im1 = (ImageView) findViewById( R.id.imageView1 );
im1.setOnClickListener(this);
}
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
if (v == im1 )
{
Log.d("test", "hey!");
v.setBackgroundResource(R.drawable.img1);
}
}
}
when clicked the method runs and prints out "hey!" but the image won't change?
EDIT: forgot to mention that imageview contains another image provided by xml layout file
By convention, you should be using setImageResource(R.drawable.img1); (or setImageDrawable(getResources().getDrawable(R.drawable.img1));) instead of setBackgroundResource(R.drawable.img1);.
ImageView i = (ImageView) findViewById( R.id.imageView1 );
i.setImageResource(R.id.logo);
or
i.setBackgroundResource(R.drawable.icon);
UPDATE
Now, you can use like below,
imageView.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.img1));
Try This In API 25
imgSchedule.setImageDrawable(getResources().getDrawable(R.drawable.circle_image_selected));
Its no convention that setimageResource() should be used.
Both the APIs can be used.
Also, in your case, it just looks like the case of out of sync resources.

Categories

Resources