Getting Arabic text from .txt file? - android

Hey,
I have Arabic text in a txt file that I would like to show in a simple EditText,
Though when I do that here is what I get:
http://i55.tinypic.com/66g09z.png
Here is the code I use to get the text from the .txt file:
txt1 =(EditText) findViewById(R.id.EditText01);
try{
File f = new File(Environment.getExternalStorageDirectory()+"/1.txt");
FileInputStream fileIS = new FileInputStream(f);
BufferedReader buf = new BufferedReader(new InputStreamReader(fileIS));
String readString = new String();
//just reading each line
while((readString = buf.readLine())!= null){
txt1.setText(txt1.getText().toString()+readString);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
}
How can I get proper Arabic letters into the EditText?
Thanks

You need to make sure that a font with Arabic characters is installed, and set the font of the EditText to it. The former is the annoying part. (I think I made SwiftKey do it for me by telling it to download the appropriate language modules.)

What version of android do you run? Android has no support for Arabic locales up until 2.3 version. You can find more info about locales supported on corresponding SDK pages. For example, list of SDK 2.3 supported locales is here.

Related

Storing multi color text using a file in android

I'm currently working on an android project which allows user to write text using different colors and store them for later use(i.e., editing or reading).
Is their any way to store a file in android with multi color text ??
NOTE: I googled out for the solution but can't find anything useful.
I'm guessing the user has to perform some action to switch color?
If so - you can use that trigger to store the text position/length when switching and save a list of text position - color.
A commenter suggested HTML, and that may be a good choice. You are welcome to try Html.fromHtml() to populate your EditText with the contents of a simple HTML-formatted file, and you are welcome to try Html.toHtml() to generate HTML from the contents of your EditText. However, historically, those methods were not written to do a good job of implementing a "round trip", meaning that the contents of the EditText may wind up changing from its starting point to what it contains after doing Html.toHtml() (to generate and save the HTML) and Html.fromHtml() (to populate the EditText with the previously-saved HTML). If they do not work, you can either fork that Html class and try to modify it as needed, or write your own code to take a Spanned object and convert it to/from HTML, by examining the spans and generating HTML tags from them.
PROBLEM SOLVED:
Code for storing a multi color text from EditText to a txt file:
protected void onDestroy() {
super.onDestroy();
boolean writing_allowed= ExternalStorageWriting.isWritingPossible();
if(writing_allowed)
{
String store= Html.toHtml(et.getEditableText());
File myExternalFile = new File(getExternalFilesDir(filepath), filename3);
try {
FileOutputStream fos = new FileOutputStream(myExternalFile);
fos.write(store.getBytes());
fos.close();
} catch (Exception e) {
Toast.makeText(Notes.this, "Something went wrong...", Toast.LENGTH_SHORT).show();
}
}
}
Code for reading that txt file and displaying it in EditText :
private void setNotes()
{
String myData="";
try {
File myExternalFile = new File(getExternalFilesDir(filepath), filename3);
FileInputStream fis = new FileInputStream(myExternalFile);
DataInputStream in = new DataInputStream(fis);
BufferedReader br =
new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
myData = myData + strLine;
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
Spanned htmlText = Html.fromHtml(myData);
et.setText(htmlText);
}

Have to run app twice for it to recognize change

I have an app that can take a text file from the devices internal storage and reads the contents of the file to which it places it into an arraylist for me to compare against some other data.
Some example data in the text file is:
1.10
1.11
1.12
My app displays the last element of the array list, so in this example it would be '1.12' that is displayed.
The issue I am having is that if I change '1.12' to '1.13', it only updates to the new value when I run the app for the second time.
I can't figure out why this is. I have tested to make sure the file does update using adb and thats all updated but it appears as if the app is still reading the contents of the old version of the file rather than the new overwritten file on the first run. My code is below for reading the files contents.
public void readVersionList(){
download = (TextView) findViewById(R.id.download_version_path);
try {
Scanner scan = new Scanner(new File("/sdcard/version-nums.txt"));
ArrayList<String> list = new ArrayList<String>();
while (scan.hasNext()){
list.add(s.next());
}
scan.close();
updateVersionNum = list.get(list.size()-1);
download.setText(updateVersionNum);
Log.i("VERSIONNUM", updateVersionNum);
}catch (FileNotFoundException e){
e.printStackTrace();
}
catch (IOException e){
e.printStackTrace();
}
}

How to Read and Write to a csv file in Android?

I want to store 8 integers into a .csv file(the filename will be taken as an input from a EditText) and retrieve them when I want to.
To get the filename you can use this:
EditText fileNameEdit= (EditText) getActivity().findViewById(R.id.fileName);
String fileName = fileNameEdit.getText().toString();
Then write the file on disk:
try {
String content = "Separe here integers by semi-colon";
File file = new File(fileName +".csv");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
To read the file:
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader(fileName+".csv"));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
Then to have the Integers you can use split function:
String[] intArray = sCurrentLine.split(";");
Opencsv doesn't work on Android due to JDK issue.
If you see simular to the following:
05-04 16:13:31.821 25829-25829/? E/AndroidRuntime: FATAL EXCEPTION:
main Process: com.example.pk.opencsvpoc, PID: 25829
java.lang.NoClassDefFoundError: Failed resolution of:
Ljava/beans/Introspector;
It is because Android only ported over a subset of java.beans into its
version of java. You can see a list of what is support at
https://developer.android.com/reference/java/beans/package-summary
There are plans to switch over to reflection in opencsv 5.0 but I
doubt it will remove all our dependencies on java.beans. For now the
best suggestion for opencsv is to steer clear of the com.opencsv.bean
classes until Android fully supports java.beans or we are successful
in removing java.beans.
Another possibility is to try another csv library. I checked apache
commons csv and super-csv and apache does not convert to beans but
super-csv does using only reflection.
Source: https://sourceforge.net/p/opencsv/wiki/FAQ/#getting-noclassdeffounderror-when-using-android
CSV is normal text file, where values are divided by character ";" so, you can write it using BufferedWriter for example.
BufferedWriter

Find an unicode character in a txt - android

I am reading a txt file containing unicode characters. I need to find whether a specific unicode character exist in this file. The code so far is as follows,
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(getAssets().open("DistinctWords.txt"), "UTF-8"));
int i = 0;
String mLine = reader.readLine();
while ((mLine != null)) {
//process line
//unicode value taken from http://codepoints.net/U+0D85
if (mLine.contains("\u0D85")){
i++;
}
mLine = reader.readLine();
}
reader.close();
Log.i("tula", "Ayanna - " + String.valueOf(i));
} catch (IOException e) {
//log the exception
}
Problem: Value of "i" is always "0". When I open the same text file from notepad I can see the letter but my code fails to find it.
Like TronicZomB says, I think you need to be looking for the actual character, like:
while ((mLine != null)) {
//process line
if (mLine.contains("අ")){
i++;
}
mLine = reader.readLine();
}
You will want to use an editor that can handle the proper encoding:
Notepad on Windows will allow you to specify UTF-8 encoding on your file, but you have to set the encoding on the file to UTF-8 from ANSI.
On mac OS-x you can use TextEdit. In the preferences, with the open & save tab, you can set the document encoding.
On Linux StarOffice supposedly works, but I haven't used it.

Translate text into device language in Android

I want to translate my text into device language.
So I tried below code
String InputString="My text";
String OutputString = null;
Language fromLanguage = Language.ENGLISH;
Language toLanguage = Language.valueOf(Locale.getDefault().getDisplayLanguage().toUpperCase());
try {
Translate.setHttpReferrer("http://android-er.blogspot.com/");
OutputString = Translate.execute(InputString,
fromLanguage, toLanguage);
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
OutputString = InputString;
}
return OutputString;
If device language is English it executes well but i changed to any other language then it shows
java.lang.IllegalArgumentException: FRANÇAIS is not a constant in the enum type class com.google.api.translate.Language
not FRANCAIS if I select any language except English it shows IllegalArgumentException with selected language.
What have I done wrong, or is there another way to translate text into device language?
Read this document, it explains all you need to know: Android Localization

Categories

Resources