I Want To Show The Reginonal Language(gurumukhi) In My Android App
I want the output as shown in below image This is a Sikh holy book which uses the Regional font(Gurumukhi) and i want to make an xml file of this text to use the xml parsing to s
Solution 1:
I have made some changes in xml keep in mind that changes are required.
I have made an data.xml in asset folder the xml is as look like as shown below
<?xml version="1.0" encoding="UTF-8"?><book1><page1><> 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 shown 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;
publicclassStudyParser {
publicStudyParser() {
}
publicfinalstatic Document XMLfromString(String xml){
Documentdoc=null;
DocumentBuilderFactorydbf= DocumentBuilderFactory.newInstance();
dbf.setCoalescing(true);
try {
DocumentBuilderdb= dbf.newDocumentBuilder();
InputSourceis=newInputSource();
is.setCharacterStream(newStringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e) {
System.out.println("XML parse error: " + e.getMessage());
returnnull;
} catch (SAXException e) {
System.out.println("Wrong XML file structure: " + e.getMessage());
returnnull;
} catch (IOException e) {
System.out.println("I/O exeption: " + e.getMessage());
returnnull;
}
return doc;
}
publicstatic String getXMLstring(String xml){
Stringline=null;
try {
DefaultHttpClienthttpClient=newDefaultHttpClient();
HttpPosthttpPost=newHttpPost(xml);
HttpResponsehttpResponse= httpClient.execute(httpPost);
HttpEntityhttpEntity= 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;
}
publicstatic String getXML(InputStream is)throws IOException {
BufferedInputStreambis=newBufferedInputStream(is);
ByteArrayOutputStreambuf=newByteArrayOutputStream();
intresult= bis.read();
while(result != -1) {
byteb= (byte)result;
buf.write(b);
result = bis.read();
}
return buf.toString();
}
publicfinalstatic 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"";
}
publicstaticintnumResults(Document doc){
Noderesults= doc.getDocumentElement();
intres= -1;
try{
res = Integer.valueOf(results.getAttributes().getNamedItem("Categories").getNodeValue());
}catch(Exception e ){
res = -1;
}
return res;
}
publicstatic String getValue(Element item, String str) {
NodeListn= item.getElementsByTagName(str);
return StudyParser.getElementValue(n.item(0));
}
}
after code in my activity is as shown below
TextView txt;
try{
txt = (TextView)findViewById(R.id.tv);
String xml= StudyParser.getXML(getAssets().open("data.xml"));
Documentdoc= StudyParser.XMLfromString(xml);
NodeListn= doc.getElementsByTagName("book1");
Elementeid= (Element) n.item(0);
String Js=StudyParser.getValue(eid, "page1");
Typefacetf= Typeface.createFromAsset(getAssets(),"bulara_5.ttf");
txt.setTypeface(tf);
txt.setText(Js);
}catch(Exception e){
Log.e("error",e.toString());
}
Solution 2:
Update Since you want to use via XML Your layout.xml should be like
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"
><com.nannu.NanTVandroid: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><stringname="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>
Hello Extend your textview
package com.nannu;
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
publicclassNanTVextendsTextView{
private Context c;
publicNanTV(Context c) {
super(c);
this.c = c;
Typefacetfs= Typeface.createFromAsset(c.getAssets(),
"font/bulara_5.ttf");
setTypeface(tfs);
}
publicNanTV(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.c = context;
Typefacetfs= Typeface.createFromAsset(c.getAssets(),
"font/bulara_5.ttf");
setTypeface(tfs);
// TODO Auto-generated constructor stub
}
publicNanTV(Context context, AttributeSet attrs) {
super(context, attrs);
this.c = context;
Typefacetfs= Typeface.createFromAsset(c.getAssets(),
"font/bulara_5.ttf");
setTypeface(tfs);
}
}
Now Your layout.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"
><com.nannu.NanTVandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Large Text"android:textAppearance="?android:attr/textAppearanceLarge"
/></LinearLayout>
Activity
package com.nannu;
import android.app.Activity;
import android.os.Bundle;
publicclassNanDempoActivityextendsActivity {
/** Called when the activity is first created. */@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
NanTVnan= (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");
}
}
Solution 3:
It seems that you are trying to use a privately-encoded 8-bit font for Gurmukhi letters, i.e. a font with code number range 0..255 with each number mapped to a character according to some font-dependent convention, not by any standard encoding.
Consider using Unicode (UTF-8) and a Unicode-encoded font instead.
Post a Comment for "I Want To Show The Reginonal Language(gurumukhi) In My Android App"