Last updated on April 4th, 2012 at 01:16 am
Dynamic String using String.xml in Android
This is a note for me, that I found a way to load string from String.xml in Android development.
Here is the original code that I use before optimize.
[adrotate banner=”2″]
if ("A".equals(SpeakTextString)) { textViewTextToSpeak.setText(getString(R.string.A)); } else if ("B".equals(SpeakTextString)) { textViewTextToSpeak.setText(getString(R.string.B)); } else if ("C".equals(SpeakTextString)) { textViewTextToSpeak.setText(getString(R.string.C)); } else if ("D".equals(SpeakTextString)) { textViewTextToSpeak.setText(getString(R.string.D)); } else if ("E".equals(SpeakTextString)) { textViewTextToSpeak.setText(getString(R.string.E)); } else if ("F".equals(SpeakTextString)) { textViewTextToSpeak.setText(getString(R.string.F)); } else if ("G".equals(SpeakTextString)) { textViewTextToSpeak.setText(getString(R.string.G)); } else if ("H".equals(SpeakTextString)) { textViewTextToSpeak.setText(getString(R.string.H)); } else if ("I".equals(SpeakTextString)) { textViewTextToSpeak.setText(getString(R.string.I)); } else if ("J".equals(SpeakTextString)) { textViewTextToSpeak.setText(getString(R.string.J)); } else if ("K".equals(SpeakTextString)) { textViewTextToSpeak.setText(getString(R.string.K)); } else if ("L".equals(SpeakTextString)) { textViewTextToSpeak.setText(getString(R.string.L)); } else if ("M".equals(SpeakTextString)) { textViewTextToSpeak.setText(getString(R.string.M)); } else if ("N".equals(SpeakTextString)) { textViewTextToSpeak.setText(getString(R.string.N)); } else if ("O".equals(SpeakTextString)) { textViewTextToSpeak.setText(getString(R.string.O)); } else if ("P".equals(SpeakTextString)) { textViewTextToSpeak.setText(getString(R.string.P)); } else if ("Q".equals(SpeakTextString)) { textViewTextToSpeak.setText(getString(R.string.Q)); } else if ("R".equals(SpeakTextString)) { textViewTextToSpeak.setText(getString(R.string.R)); } else if ("S".equals(SpeakTextString)) { textViewTextToSpeak.setText(getString(R.string.S)); } else if ("T".equals(SpeakTextString)) { textViewTextToSpeak.setText(getString(R.string.T)); } else if ("U".equals(SpeakTextString)) { textViewTextToSpeak.setText(getString(R.string.U)); } else if ("V".equals(SpeakTextString)) { textViewTextToSpeak.setText(getString(R.string.V)); } else if ("W".equals(SpeakTextString)) { textViewTextToSpeak.setText(getString(R.string.W)); } else if ("X".equals(SpeakTextString)) { textViewTextToSpeak.setText(getString(R.string.X)); } else if ("Y".equals(SpeakTextString)) { textViewTextToSpeak.setText(getString(R.string.Y)); } else if ("Z".equals(SpeakTextString)) { textViewTextToSpeak.setText(getString(R.string.Z)); }
Can be written in few code loading string from String.xml dynamically
String stringName = "" + SpeakTextString.toUpperCase(); textViewTextToSpeak.setText(getString(getStringResource(getApplicationContext(),stringName))); public int getStringResource(Context context, String name) { int resId = context.getResources().getIdentifier(name, "string", "com.juzhax.learnabc"); return resId; }
While my res/values/strings.xml is
< ?xml version="1.0" encoding="utf-8"?> <resources> <string name="LearnABC">Learn ABC</string> <string name="app_name">Learn ABC</string> <string name="A">Apple</string> <string name="B">BlueTooth</string> <string name="C">Chat</string> <string name="D">Download</string> <string name="E">Email</string> <string name="F">Facebook</string> <string name="G">Google</string> <string name="H">Hewlett Packard</string> <string name="I">iPhone</string> <string name="J">Java</string> <string name="K">KingSton</string> <string name="L">Lenovo</string> <string name="M">Messenger</string> <string name="N">Nikon</string> <string name="O">Opera</string> <string name="P">Photo shop</string> <string name="Q">QR Code</string> <string name="R">RSS</string> <string name="S">Skype</string> <string name="T">Twitter</string> <string name="U">USB</string> <string name="V">Vista</string> <string name="W">Wifi</string> <string name="X">X Box</string> <string name="Y">YouTube</string> <string name="Z">Zip</string> </resources>
XML
i need to change key randomly so 1,2 and 3 just order randomly in my project