create a class xml handler
public class Dict_Connect_Dont_Xml_Handler extends DefaultHandler{
private String currentValue = "";
private Boolean currentElement=false;
private Boolean bool_language=false;
@Override
public void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException
{
currentValue="";
currentElement=true;
if(localName.equalsIgnoreCase(Dict_Connect_Dont_Constant.LANGUAGE))
{
bool_language=true;
}
}
@Override
public void endElement(String uri, String localName, String qName)throws SAXException
{
currentElement = false;
if(bool_language)
{
/**************************************************CONNECT_DONT SCREEN TAGS*******************************************************/
if (localName.equalsIgnoreCase(Dict_Connect_Dont_Constant.CONNECT_DONT.BS11 + Constant.LANGUAGE_TAG[Dict_Connect_Dont_Constant.Language_Postion]))
{
Dict_Connect_Dont_Constant.STORE_IN_SHARED_PREFERENCES(Dict_Connect_Dont_Constant.CONNECT_DONT.BS11 + Constant.LANGUAGE_TAG[Dict_Connect_Dont_Constant.Language_Postion],currentValue);
}
else if (localName.equalsIgnoreCase(Dict_Connect_Dont_Constant.CONNECT_DONT.BS12 + Constant.LANGUAGE_TAG[Dict_Connect_Dont_Constant.Language_Postion]))
{
Dict_Connect_Dont_Constant.STORE_IN_SHARED_PREFERENCES(Dict_Connect_Dont_Constant.CONNECT_DONT.BS12 + Constant.LANGUAGE_TAG[Dict_Connect_Dont_Constant.Language_Postion],currentValue);
}
else if (localName.equalsIgnoreCase(Dict_Connect_Dont_Constant.CONNECT_DONT.BD11 + Constant.LANGUAGE_TAG[Dict_Connect_Dont_Constant.Language_Postion]))
{
Dict_Connect_Dont_Constant.STORE_IN_SHARED_PREFERENCES(Dict_Connect_Dont_Constant.CONNECT_DONT.BD11 + Constant.LANGUAGE_TAG[Dict_Connect_Dont_Constant.Language_Postion],currentValue);
}
else if (localName.equalsIgnoreCase(Dict_Connect_Dont_Constant.CONNECT_DONT.BD12 + Constant.LANGUAGE_TAG[Dict_Connect_Dont_Constant.Language_Postion]))
{
Dict_Connect_Dont_Constant.STORE_IN_SHARED_PREFERENCES(Dict_Connect_Dont_Constant.CONNECT_DONT.BD12 + Constant.LANGUAGE_TAG[Dict_Connect_Dont_Constant.Language_Postion],currentValue);
}
else if (localName.equalsIgnoreCase(Dict_Connect_Dont_Constant.CONNECT_DONT.BD13 + Constant.LANGUAGE_TAG[Dict_Connect_Dont_Constant.Language_Postion]))
{
Dict_Connect_Dont_Constant.STORE_IN_SHARED_PREFERENCES(Dict_Connect_Dont_Constant.CONNECT_DONT.BD13 + Constant.LANGUAGE_TAG[Dict_Connect_Dont_Constant.Language_Postion],currentValue);
}
else if (localName.equalsIgnoreCase(Dict_Connect_Dont_Constant.CONNECT_DONT.BW11 + Constant.LANGUAGE_TAG[Dict_Connect_Dont_Constant.Language_Postion]))
{
Dict_Connect_Dont_Constant.STORE_IN_SHARED_PREFERENCES(Dict_Connect_Dont_Constant.CONNECT_DONT.BW11 + Constant.LANGUAGE_TAG[Dict_Connect_Dont_Constant.Language_Postion],currentValue);
}
if (localName.equals(Dict_Connect_Dont_Constant.LANGUAGE))
{
bool_language=false;
}
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if (currentElement) {
currentValue = currentValue + new String(ch, start, length);
}
}
}
Create another class which store constants
==============================
public class Dict_Connect_Dont_Constant {
public static String LANGUAGE = "language";
public static int DEFAULT_LIST_INDEX = 0;
public static int[] LIST_ID = {R.raw.connect_dont_en,R.raw.connect_dont_nl,R.raw.connect_dont_de};
public static int Language_Postion=0;
private static SharedPreferences mSharedPreferences_Write = null;
public static void Read_Language_XML(Context context, final int position) {
try {
Dict_Connect_Dont_Constant.Language_Postion=position;
mSharedPreferences_Write = context.getSharedPreferences(Constant.SHAREDPREFERENCE.COMMAN_SHAREDPREFERENCE, Context.MODE_WORLD_WRITEABLE);
SAXParserFactory mSAXParserFactory = SAXParserFactory.newInstance();
SAXParser mSAXParser = mSAXParserFactory.newSAXParser();
XMLReader mXMLReader = mSAXParser.getXMLReader();
Dict_Connect_Dont_Xml_Handler mDict_Connect_Dont_Xml_Handler= new Dict_Connect_Dont_Xml_Handler();
mXMLReader.setContentHandler(mDict_Connect_Dont_Xml_Handler);
InputSource mInputSource = new InputSource(context.getResources().openRawResource(LIST_ID[position]));
mXMLReader.parse(mInputSource);
} catch (Exception e) {
e.printStackTrace();
}
}
public static class CONNECT_DONT {
public static final String CONNECT_DONT_INDEX = "%s%s%s%s";
public static final String BS11 = "xbs11";
public static final String BS12 = "xbs12";
public static final String BD11 = "xbd11";
public static final String BD12 = "xbd12";
public static final String BD13 = "xbd13";
}
public static void STORE_IN_SHARED_PREFERENCES(String key, String value) {
SharedPreferences.Editor mSharedPreferencesEdit = mSharedPreferences_Write
.edit();
mSharedPreferencesEdit.putString(key, value);
mSharedPreferencesEdit.commit();
}
}
//Create class from where u want to call xml
============================
public class HomeActivity extends Activity implements OnClickListener {
private SharedPreferences mSharedPreferences_Read = null;
private SharedPreferences mSharedPreferences_Write =null;
private int intLanguage_Position=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.xhomeactivity);
mSharedPreferences_Read = getSharedPreferences(Constant.SHAREDPREFERENCE.COMMAN_SHAREDPREFERENCE, MODE_WORLD_READABLE);
mSharedPreferences_Write = getSharedPreferences(Constant.SHAREDPREFERENCE.COMMAN_SHAREDPREFERENCE, MODE_WORLD_WRITEABLE);
String DEFAULT_LANGUAGE = Locale.getDefault().getLanguage().toString().trim();
for (int i = 0; i < Constant.LANGUAGE_CODE.length; i++) {
if(DEFAULT_LANGUAGE.equalsIgnoreCase(Constant.LANGUAGE_CODE[i].toString().trim())) {
SharedPreferences.Editor editor = mSharedPreferences_Write.edit();
editor.putInt(Constant.SHAREDPREFERENCE.LANGUAGE_POSITION,i);
editor.commit();
break;
}
}
}
@Override
protected void onResume() {
intLanguage_Position = mSharedPreferences_Read.getInt(Constant.SHAREDPREFERENCE.LANGUAGE_POSITION, Constant.SHAREDPREFERENCE.DEFAULT_LANGUAGE_POSITION);
if(mSTYLEPREFERENCE_MENU_DESCRIPTIONS_ENDENL == null){
mSTYLEPREFERENCE_MENU_DESCRIPTIONS_ENDENL = new STYLEPREFERENCE_MENU_DESCRIPTIONS_ENDENL();
mSTYLEPREFERENCE_MENU_DESCRIPTIONS_ENDENL.execute("");
}
if(intLanguage_Position==Constant.SHAREDPREFERENCE.ENGLISH ){
imgLanguage.setImageResource(R.drawable.ic_english);
imgCodeOfUnderstanding.setImageResource(R.drawable.begrips_code_logo_en);
}
else if(intLanguage_Position==Constant.SHAREDPREFERENCE.NEDERLANDS){
imgLanguage.setImageResource(R.drawable.ic_nederlands);
imgCodeOfUnderstanding.setImageResource(R.drawable.begrips_code_logo_nl);
}
else if(intLanguage_Position==Constant.SHAREDPREFERENCE.DEUTSCH){
imgLanguage.setImageResource(R.drawable.ic_deutsch);
imgCodeOfUnderstanding.setImageResource(R.drawable.begrips_code_logo_de);
}
super.onResume();
}
//this asynctask is for parsing STYLEPREFERENCE_MENU_DESCRIPTIONS_ENDENL for all static content in app
private STYLEPREFERENCE_MENU_DESCRIPTIONS_ENDENL mSTYLEPREFERENCE_MENU_DESCRIPTIONS_ENDENL = null;
private ProcessDialog mProcessDialog = null;
private class STYLEPREFERENCE_MENU_DESCRIPTIONS_ENDENL extends AsyncTask<String, String, Boolean>
{
@Override
protected void onPreExecute() {
super.onPreExecute();
mProcessDialog = new ProcessDialog(HomeActivity.this, "", Constant.PROGRESS_DIALOG.PROGRESS_MESSAGE);
mProcessDialog.setCancelable(false);
mProcessDialog.setCanceledOnTouchOutside(false);
}
protected Boolean doInBackground(String... location) {
if(ReadSharedPreference(Constant.SHAREDPREFERENCE.STYLEPREFERENCE_MENU_DESCRIPTIONS_ENDENL_ALL)){
Dict_Content_Constant.Read_Language_XML(HomeActivity.this);
STORE_IN_SHARED_PREFERENCES(Constant.SHAREDPREFERENCE.STYLEPREFERENCE_MENU_DESCRIPTIONS_ENDENL_ALL,Constant.SHAREDPREFERENCE.STYLEPREFERENCE_MENU_DESCRIPTIONS_ENDENL_ALL);
}
return true;
}
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
if(result == true){
if(mSTYLEPREFERENCE_NAME == null){
mSTYLEPREFERENCE_NAME = new STYLEPREFERENCE_NAME();
mSTYLEPREFERENCE_NAME.execute("");
}
}
mSTYLEPREFERENCE_MENU_DESCRIPTIONS_ENDENL = null;
}
}
public void SetText(){
btnConnect.setText(mSharedPreferences_Read.getString(Dict_Wording_Constant.WORDINGS.HOMESCREEN_CONNECT_BUTTON + Constant.LANGUAGE_TAG[intLanguage_Position],""));
}
private void STORE_IN_SHARED_PREFERENCES(String key, String value) {
SharedPreferences.Editor mSharedPreferencesEdit = mSharedPreferences_Write.edit();
mSharedPreferencesEdit.putString(key, value);
mSharedPreferencesEdit.commit();
}
private boolean ReadSharedPreference(String key){
if(mSharedPreferences_Read.getString(key, "").trim().equals("")){
return true;
}
else{
return false;
}
}
}
public class Dict_Connect_Dont_Xml_Handler extends DefaultHandler{
private String currentValue = "";
private Boolean currentElement=false;
private Boolean bool_language=false;
@Override
public void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException
{
currentValue="";
currentElement=true;
if(localName.equalsIgnoreCase(Dict_Connect_Dont_Constant.LANGUAGE))
{
bool_language=true;
}
}
@Override
public void endElement(String uri, String localName, String qName)throws SAXException
{
currentElement = false;
if(bool_language)
{
/**************************************************CONNECT_DONT SCREEN TAGS*******************************************************/
if (localName.equalsIgnoreCase(Dict_Connect_Dont_Constant.CONNECT_DONT.BS11 + Constant.LANGUAGE_TAG[Dict_Connect_Dont_Constant.Language_Postion]))
{
Dict_Connect_Dont_Constant.STORE_IN_SHARED_PREFERENCES(Dict_Connect_Dont_Constant.CONNECT_DONT.BS11 + Constant.LANGUAGE_TAG[Dict_Connect_Dont_Constant.Language_Postion],currentValue);
}
else if (localName.equalsIgnoreCase(Dict_Connect_Dont_Constant.CONNECT_DONT.BS12 + Constant.LANGUAGE_TAG[Dict_Connect_Dont_Constant.Language_Postion]))
{
Dict_Connect_Dont_Constant.STORE_IN_SHARED_PREFERENCES(Dict_Connect_Dont_Constant.CONNECT_DONT.BS12 + Constant.LANGUAGE_TAG[Dict_Connect_Dont_Constant.Language_Postion],currentValue);
}
else if (localName.equalsIgnoreCase(Dict_Connect_Dont_Constant.CONNECT_DONT.BD11 + Constant.LANGUAGE_TAG[Dict_Connect_Dont_Constant.Language_Postion]))
{
Dict_Connect_Dont_Constant.STORE_IN_SHARED_PREFERENCES(Dict_Connect_Dont_Constant.CONNECT_DONT.BD11 + Constant.LANGUAGE_TAG[Dict_Connect_Dont_Constant.Language_Postion],currentValue);
}
else if (localName.equalsIgnoreCase(Dict_Connect_Dont_Constant.CONNECT_DONT.BD12 + Constant.LANGUAGE_TAG[Dict_Connect_Dont_Constant.Language_Postion]))
{
Dict_Connect_Dont_Constant.STORE_IN_SHARED_PREFERENCES(Dict_Connect_Dont_Constant.CONNECT_DONT.BD12 + Constant.LANGUAGE_TAG[Dict_Connect_Dont_Constant.Language_Postion],currentValue);
}
else if (localName.equalsIgnoreCase(Dict_Connect_Dont_Constant.CONNECT_DONT.BD13 + Constant.LANGUAGE_TAG[Dict_Connect_Dont_Constant.Language_Postion]))
{
Dict_Connect_Dont_Constant.STORE_IN_SHARED_PREFERENCES(Dict_Connect_Dont_Constant.CONNECT_DONT.BD13 + Constant.LANGUAGE_TAG[Dict_Connect_Dont_Constant.Language_Postion],currentValue);
}
else if (localName.equalsIgnoreCase(Dict_Connect_Dont_Constant.CONNECT_DONT.BW11 + Constant.LANGUAGE_TAG[Dict_Connect_Dont_Constant.Language_Postion]))
{
Dict_Connect_Dont_Constant.STORE_IN_SHARED_PREFERENCES(Dict_Connect_Dont_Constant.CONNECT_DONT.BW11 + Constant.LANGUAGE_TAG[Dict_Connect_Dont_Constant.Language_Postion],currentValue);
}
if (localName.equals(Dict_Connect_Dont_Constant.LANGUAGE))
{
bool_language=false;
}
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if (currentElement) {
currentValue = currentValue + new String(ch, start, length);
}
}
}
Create another class which store constants
==============================
public class Dict_Connect_Dont_Constant {
public static String LANGUAGE = "language";
public static int DEFAULT_LIST_INDEX = 0;
public static int[] LIST_ID = {R.raw.connect_dont_en,R.raw.connect_dont_nl,R.raw.connect_dont_de};
public static int Language_Postion=0;
private static SharedPreferences mSharedPreferences_Write = null;
public static void Read_Language_XML(Context context, final int position) {
try {
Dict_Connect_Dont_Constant.Language_Postion=position;
mSharedPreferences_Write = context.getSharedPreferences(Constant.SHAREDPREFERENCE.COMMAN_SHAREDPREFERENCE, Context.MODE_WORLD_WRITEABLE);
SAXParserFactory mSAXParserFactory = SAXParserFactory.newInstance();
SAXParser mSAXParser = mSAXParserFactory.newSAXParser();
XMLReader mXMLReader = mSAXParser.getXMLReader();
Dict_Connect_Dont_Xml_Handler mDict_Connect_Dont_Xml_Handler= new Dict_Connect_Dont_Xml_Handler();
mXMLReader.setContentHandler(mDict_Connect_Dont_Xml_Handler);
InputSource mInputSource = new InputSource(context.getResources().openRawResource(LIST_ID[position]));
mXMLReader.parse(mInputSource);
} catch (Exception e) {
e.printStackTrace();
}
}
public static class CONNECT_DONT {
public static final String CONNECT_DONT_INDEX = "%s%s%s%s";
public static final String BS11 = "xbs11";
public static final String BS12 = "xbs12";
public static final String BD11 = "xbd11";
public static final String BD12 = "xbd12";
public static final String BD13 = "xbd13";
}
public static void STORE_IN_SHARED_PREFERENCES(String key, String value) {
SharedPreferences.Editor mSharedPreferencesEdit = mSharedPreferences_Write
.edit();
mSharedPreferencesEdit.putString(key, value);
mSharedPreferencesEdit.commit();
}
}
//Create class from where u want to call xml
============================
public class HomeActivity extends Activity implements OnClickListener {
private SharedPreferences mSharedPreferences_Read = null;
private SharedPreferences mSharedPreferences_Write =null;
private int intLanguage_Position=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.xhomeactivity);
mSharedPreferences_Read = getSharedPreferences(Constant.SHAREDPREFERENCE.COMMAN_SHAREDPREFERENCE, MODE_WORLD_READABLE);
mSharedPreferences_Write = getSharedPreferences(Constant.SHAREDPREFERENCE.COMMAN_SHAREDPREFERENCE, MODE_WORLD_WRITEABLE);
String DEFAULT_LANGUAGE = Locale.getDefault().getLanguage().toString().trim();
for (int i = 0; i < Constant.LANGUAGE_CODE.length; i++) {
if(DEFAULT_LANGUAGE.equalsIgnoreCase(Constant.LANGUAGE_CODE[i].toString().trim())) {
SharedPreferences.Editor editor = mSharedPreferences_Write.edit();
editor.putInt(Constant.SHAREDPREFERENCE.LANGUAGE_POSITION,i);
editor.commit();
break;
}
}
}
@Override
protected void onResume() {
intLanguage_Position = mSharedPreferences_Read.getInt(Constant.SHAREDPREFERENCE.LANGUAGE_POSITION, Constant.SHAREDPREFERENCE.DEFAULT_LANGUAGE_POSITION);
if(mSTYLEPREFERENCE_MENU_DESCRIPTIONS_ENDENL == null){
mSTYLEPREFERENCE_MENU_DESCRIPTIONS_ENDENL = new STYLEPREFERENCE_MENU_DESCRIPTIONS_ENDENL();
mSTYLEPREFERENCE_MENU_DESCRIPTIONS_ENDENL.execute("");
}
if(intLanguage_Position==Constant.SHAREDPREFERENCE.ENGLISH ){
imgLanguage.setImageResource(R.drawable.ic_english);
imgCodeOfUnderstanding.setImageResource(R.drawable.begrips_code_logo_en);
}
else if(intLanguage_Position==Constant.SHAREDPREFERENCE.NEDERLANDS){
imgLanguage.setImageResource(R.drawable.ic_nederlands);
imgCodeOfUnderstanding.setImageResource(R.drawable.begrips_code_logo_nl);
}
else if(intLanguage_Position==Constant.SHAREDPREFERENCE.DEUTSCH){
imgLanguage.setImageResource(R.drawable.ic_deutsch);
imgCodeOfUnderstanding.setImageResource(R.drawable.begrips_code_logo_de);
}
super.onResume();
}
//this asynctask is for parsing STYLEPREFERENCE_MENU_DESCRIPTIONS_ENDENL for all static content in app
private STYLEPREFERENCE_MENU_DESCRIPTIONS_ENDENL mSTYLEPREFERENCE_MENU_DESCRIPTIONS_ENDENL = null;
private ProcessDialog mProcessDialog = null;
private class STYLEPREFERENCE_MENU_DESCRIPTIONS_ENDENL extends AsyncTask<String, String, Boolean>
{
@Override
protected void onPreExecute() {
super.onPreExecute();
mProcessDialog = new ProcessDialog(HomeActivity.this, "", Constant.PROGRESS_DIALOG.PROGRESS_MESSAGE);
mProcessDialog.setCancelable(false);
mProcessDialog.setCanceledOnTouchOutside(false);
}
protected Boolean doInBackground(String... location) {
if(ReadSharedPreference(Constant.SHAREDPREFERENCE.STYLEPREFERENCE_MENU_DESCRIPTIONS_ENDENL_ALL)){
Dict_Content_Constant.Read_Language_XML(HomeActivity.this);
STORE_IN_SHARED_PREFERENCES(Constant.SHAREDPREFERENCE.STYLEPREFERENCE_MENU_DESCRIPTIONS_ENDENL_ALL,Constant.SHAREDPREFERENCE.STYLEPREFERENCE_MENU_DESCRIPTIONS_ENDENL_ALL);
}
return true;
}
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
if(result == true){
if(mSTYLEPREFERENCE_NAME == null){
mSTYLEPREFERENCE_NAME = new STYLEPREFERENCE_NAME();
mSTYLEPREFERENCE_NAME.execute("");
}
}
mSTYLEPREFERENCE_MENU_DESCRIPTIONS_ENDENL = null;
}
}
public void SetText(){
btnConnect.setText(mSharedPreferences_Read.getString(Dict_Wording_Constant.WORDINGS.HOMESCREEN_CONNECT_BUTTON + Constant.LANGUAGE_TAG[intLanguage_Position],""));
}
private void STORE_IN_SHARED_PREFERENCES(String key, String value) {
SharedPreferences.Editor mSharedPreferencesEdit = mSharedPreferences_Write.edit();
mSharedPreferencesEdit.putString(key, value);
mSharedPreferencesEdit.commit();
}
private boolean ReadSharedPreference(String key){
if(mSharedPreferences_Read.getString(key, "").trim().equals("")){
return true;
}
else{
return false;
}
}
}
No comments:
Post a Comment