I want to show Reginonal language (Gurumukhi) in my Android application

I need the result as shown in the image below

This is in Gurumukhi Font

This is a Sikh holy book that uses a regional font (Gurumukhi), and I want to make an xml file for this text to use xml parsing for display in my application. But the problem is that when I insert this font into my XML file, it is converted to some alphabets and characters, as shown below.

jpujI swihb <> siq nwmu krqw purKu inrBau inrvYru Akwl mUriq AjUnI sYBM gur pRswid ] ] jpu ] Awid scu jugwid scu ] hY BI scu nwnk hosI BI scu ]1] socY soic n hoveI jy socI lK vwr ] cupY cup n hoveI jy lwie rhw ilv qwr ] BuiKAw BuK n auqrI jy bMnw purIAw Bwr ] shs isAwxpw lK hoih q iek n clY nwil ] ikv sicAwrw hoeIAY ikv kUVY qutY pwil ] hukim rjweI clxw nwnk iliKAw nwil ]1] hukmI hovin Awkwr hukmu n kihAw jweI ] hukmI hovin 

I placed the Gurumukhi font file in the assets folder and used the code below, which works fine

 Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/bulara_5.ttf"); textView = (TextView) findViewById(R.id.textView1); textView.setTypeface(tf); textView.setMovementMethod(new ScrollingMovementMethod()); textView.setText(" <>siq nwmu krqw purKu inrBau inrvYru") 

Thus, the text in this text view is converted to Gurumukhi, but How can i create my Xml file for this type of text in it . Or Give me some Good Suggestion that which way is better to work on this type of app and handle the text . I have to show 4-5 Holy books in one application, and each has 20-25 pages. Any help is appreciated.

+6
source share
3 answers

I made some changes to xml, keep in mind that changes are needed.

I made the data.xml folder in the resource directory, where the xml looks like the one below.

  <?xml version="1.0" encoding="UTF-8"?> <book1> <page1>&#60;&#62; siq nwmu krqw purKu inrBau inrvYru Akwl mUriq AjUnI sYBM gur pRswid ] ] jpu ] Awid scu jugwid scu ] hY BI scu nwnk hosI BI scu ]1] socY soic n hoveI jy socI lK vwr ] cupY cup n hoveI jy lwie rhw ilv qwr ] BuiKAw BuK n auqrI jy bMnw purIAw Bwr ] shs isAwxpw lK hoih q iek n clY nwil ] ikv sicAwrw hoeIAY ikv kUVY qutY pwil ] hukim rjweI clxw nwnk iliKAw nwil ]1] hukmI hovin Awkwr hukmu n kihAw jweI ] hukmI hovin jIA hukim imlY vifAweI ] hukmI auqmu nIcu hukim iliK duK suK pweIAih ] ieknw hukmI bKsIs ieik hukmI sdw BvweIAih ] hukmY AMdir sBu ko bwhir hukm n koie ] nwnk hukmY jy buJY q haumY khY n koie ]2] gwvY ko qwxu hovY iksY qwxu ]</page1> </book1> 

after that see my StudyParser.class as below

  import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.StringReader; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import org.xml.sax.SAXException; public class StudyParser { public StudyParser() { } public final static Document XMLfromString(String xml){ Document doc = null; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setCoalescing(true); try { DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(); is.setCharacterStream(new StringReader(xml)); doc = db.parse(is); } catch (ParserConfigurationException e) { System.out.println("XML parse error: " + e.getMessage()); return null; } catch (SAXException e) { System.out.println("Wrong XML file structure: " + e.getMessage()); return null; } catch (IOException e) { System.out.println("I/O exeption: " + e.getMessage()); return null; } return doc; } public static String getXMLstring(String xml){ String line = null; try { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(xml); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); line = EntityUtils.toString(httpEntity); } catch (UnsupportedEncodingException e) { line = "<results status=\"error\"><msg>Can't connect to server</msg></results>"; } catch (MalformedURLException e) { line = "<results status=\"error\"><msg>Can't connect to server</msg></results>"; } catch (IOException e) { line = "<results status=\"error\"><msg>Can't connect to server</msg></results>"; } return line; } public static String getXML(InputStream is)throws IOException { BufferedInputStream bis = new BufferedInputStream(is); ByteArrayOutputStream buf = new ByteArrayOutputStream(); int result = bis.read(); while(result != -1) { byte b = (byte)result; buf.write(b); result = bis.read(); } return buf.toString(); } public final static String getElementValue( Node elem ) { Node kid; if( elem != null){ if (elem.hasChildNodes()){ for( kid = elem.getFirstChild(); kid != null; kid = kid.getNextSibling() ){ if( kid.getNodeType() == Node.TEXT_NODE ){ return kid.getNodeValue(); } } } } return ""; } public static int numResults(Document doc){ Node results = doc.getDocumentElement(); int res = -1; try{ res = Integer.valueOf(results.getAttributes().getNamedItem("Categories").getNodeValue()); }catch(Exception e ){ res = -1; } return res; } public static String getValue(Element item, String str) { NodeList n = item.getElementsByTagName(str); return StudyParser.getElementValue(n.item(0)); } } 

after the code in my activity is shown below

  TextView txt; try{ txt = (TextView)findViewById(R.id.tv); String xml= StudyParser.getXML(getAssets().open("data.xml")); Document doc = StudyParser.XMLfromString(xml); NodeList n = doc.getElementsByTagName("book1"); Element eid = (Element) n.item(0); String Js=StudyParser.getValue(eid, "page1"); Typeface tf = Typeface.createFromAsset(getAssets(),"bulara_5.ttf"); txt.setTypeface(tf); txt.setText(Js); }catch(Exception e){ Log.e("error",e.toString()); } 
+4
source

Update Because you want to use XML, your layout.xml should look like

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <com.nannu.NanTV android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/book" android:textAppearance="?android:attr/textAppearanceLarge" /> </LinearLayout> 

and string.xml

 <?xml version="1.0" encoding="utf-8"?> <resources> <string name="book"><![CDATA[jpujI swihb\\<> siq nwmu krqw purKu inrBau inrvYruAkwl mUriq AjUnI sYBM gur pRswid ]] jpu ]Awid scu jugwid scu ]hY BI scu nwnk hosI BI scu ]1]socY soic n hoveI jy socI lK vwr ]cupY cup n hoveI jy lwie rhw ilv qwr ]BuiKAw BuK n auqrI jy bMnw purIAw Bwr ]shs isAwxpw lK hoih q iek n clY nwil ]ikv sicAwrw hoeIAY ikv kUVY qutY pwil ]hukim rjweI clxw nwnk iliKAw nwil ]1]hukmI hovin Awkwr hukmu n kihAw jweI ]hukmI hovin]]></string> </resources> 

Hey. Extend the text.

 package com.nannu; import android.content.Context; import android.graphics.Typeface; import android.util.AttributeSet; import android.widget.TextView; public class NanTV extends TextView{ private Context c; public NanTV(Context c) { super(c); this.c = c; Typeface tfs = Typeface.createFromAsset(c.getAssets(), "font/bulara_5.ttf"); setTypeface(tfs); } public NanTV(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.c = context; Typeface tfs = Typeface.createFromAsset(c.getAssets(), "font/bulara_5.ttf"); setTypeface(tfs); // TODO Auto-generated constructor stub } public NanTV(Context context, AttributeSet attrs) { super(context, attrs); this.c = context; Typeface tfs = Typeface.createFromAsset(c.getAssets(), "font/bulara_5.ttf"); setTypeface(tfs); } } 

Now your layout.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <com.nannu.NanTV android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Large Text" android:textAppearance="?android:attr/textAppearanceLarge" /> </LinearLayout> 

Activities

 package com.nannu; import android.app.Activity; import android.os.Bundle; public class NanDempoActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); NanTV nan = (NanTV)findViewById(R.id.textView1); nan.setText("jpujI swihb<> siq nwmu krqw purKu inrBau inrvYruAkwl mUriq AjUnI sYBM gur pRswid ]] jpu ]Awid scu jugwid scu ]hY BI scu nwnk hosI BI scu ]1]socY soic n hoveI jy socI lK vwr ]cupY cup n hoveI jy lwie rhw ilv qwr ]BuiKAw BuK n auqrI jy bMnw purIAw Bwr ]shs isAwxpw lK hoih q iek n clY nwil ]ikv sicAwrw hoeIAY ikv kUVY qutY pwil ]hukim rjweI clxw nwnk iliKAw nwil ]1]hukmI hovin Awkwr hukmu n kihAw jweI ]hukmI hovin"); } } 

Final output

+4
source

It seems that you are trying to use an 8-bit secret-encoded font for Gurmukhi letters, i.e. a font with a code number of 0..255 with each number associated with a character in accordance with some agreement depending on the fonts, and not any standard encoding.

Instead, use Unicode (UTF-8) and a Unicode-encoded font.

+1
source

Source: https://habr.com/ru/post/919212/


All Articles