Hello friends I want to integrate multiple languages in my application so I made multiple values folder for different languages.
Now my question is how can I display my dynamic textview value which comes from my web api which is string value. But How can I display language as per my choice from application. Below I have mentioned my code. Please help me. Thanks!
Main.java
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
textView=(TextView)findViewById(R.id.textView1);
textView.setText("My custom text which comes from web api");
}
You can use dynamic value in TextView with localization in resource folder.
You can localize text by with country code and language code.
like:
res/values-en/string.xml
res/values-hi/string.xml
res/values-IN-gu/string.xml
Read More
You can achieve the language localisation from the android framework using strings.xml, create one strings.xml for each language inside the proper values folder. Refer this LINK to do this.
From server you can get the text key which you define in stings.xml then in your textview set this key and android will automagically show the proper localised text.
Related
Bold textStyle attribute of textview in xml is not working after applying custom fonts in app
I have completed developement of an android app. Now I have to change font family for whole app. I have used baskerville-old-face.ttf fonts and used Calligraphy library to apply these fonts into my app.
Following is dependency I have used:
compile 'uk.co.chrisjenx:calligraphy:2.2.0'
Following is the code to initilize Calligraphy library:
CalligraphyConfig.initDefault(new CalligraphyConfig.Builder().setDefaultFontPath("fonts/baskerville.ttf").setFontAttrId(R.attr.fontPath).build());
And following is the code I have used in each Avtivity to use custom fonts:
#Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
It is working fine and all fonts are changed but there is only one problem that text style (bold) is not working with xml attribute. I don't want to change each textview attribute programmatically (Using JAVA code) because there are more than 100 text views in my whole app.
I am not getting what I am doing wrong or what I am missing. Please help !
Your help will be highly appreciated. Thank you.
I am not sure that this is the best answer but I was seen same problem. you can use the baskervilleBold.ttf font in your code instead of baskerville.ttf if there is exist.
I'll be doing an app for my thesis. My thesis is a Offline Map which includes landmarks/routing. My thesis Maps is only for my City here in the philippines. I tried to use OSMDroid and failed to produce the wanted result.
What I want is (if possible) I want this to happen in my app:
Install APK
Prompt the user which country he/she wants to use
Download the chosen country for offline use.
Done
Here's my code in OSMDroid
public class MainActivity extends Activity {
public static final GeoPoint myCity = new GeoPoint(14.54321,120.23451);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MapView mapView = (MapView) findViewById(R.id.map);
mapView.setClickable(true);
mapView.setBuiltInZoomControls(true);
mapView.setMultiTouchControls(true);
mapView.setUseDataConnection(true);
mapView.setTileSource(TileSourceFactory.MAPQUESTOSM);
IMapController mapViewController = mapView.getController();
mapViewController.setZoom(13);
mapViewController.setCenter(myCity);
}
}
Here's what I've done:
I Use Mobac to a portion of the map
Save it to my Internal Phone Memory(i named it MapquestOSM)
Run the program
But when I run the program it's just Blank.
I Don't Mind using OSMDroid again as long as it will produce the same result.
I'm willing to bet you didn't do anything to tell osmdroid that you want a different map source. Here's a few pointers to help
if you use mobac to create a zip file, it source names have to match. i.e. if you get Mapnik tiles, then use the Mapnik tile source in osmdroid. Mapquest tiles, mapquest source.
zip files have an upper limit in android, use sqlite instead (also head the first point).
tell the MapView you want offline maps (setUseDataconnection(false);)
If you still have issues (and are using zip files) try altering the contents of the zip to have the first directory = Mapnik. Then using the default tile source in osmdroid.
Until my PR gets merged (to switch tile providers at runtime) and a new release cut, that's really your only option.
Edit: osmdroid only looks in /sdcard/osmdroid/ for map tiles.
I've been following this series of Android Development Tutorials and I've encountered some problem trying to tweak one of the projects I've created.
Basically all I want to know is how to apply the WebView correctly in my project - in the easiest and most simple way possible.
What I've done:
enabled permission to the internet on the manifest
created the webview on the main_activity.xml
imported the WebView webkit to my MainActivity Java class
And all I've done after that is simply loading the url on my onCreated method like this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView v = (WebView) findViewById(R.id.weblol);
v.loadUrl("www.google.com");
}
However nothing seems to happen and the block of the WebView remains blank white
Care to explain to a noob what am I doing wrong ?
You should use http://www.google.com instead of www.google.com.
Only you to this,webview can notice this it is a URL.
Otherwise.you can use local file in webview ,like file:///android_asset/xxx in assets folder.
That's all.
I'm a new to android programming. I'm having issues with the layout in my activity. My menu appears like this:
and I've done all the layout work directly through the source code:
enterNameTxt.setText("Enter User Name");
enterNameTxt.setY(200);
enterNameTxt.setX(-600);
userNameTxt.setY(300);
userNameTxt.setX(100);
userNameTxt.setWidth(200);
enterSpeedTxt.setText("Enter Speed");
enterSpeedTxt.setX(-500);
enterSpeedTxt.setY(100);
userSpeedTxt.setX(-400);
userSpeedTxt.setY(700);
userSpeedTxt.setWidth(200);
configButton.setWidth(400);
configButton.setText("Back to Game");
configButton.setY(1000);
and as you can see the speed option doesn't even show up on the screen. And I keep playing with setX, setY, setWidth options but it keeps getting messy.
Is it wrong to do the layout directly through the source code?
I have a two activities but only a layout xml file for one of them. Am I supposed to create another xml file in res/layout section for the menu activity?
I just don't understand when I use the source code and when I should use the layout...
Thanks in advance
Is it wrong to do the layout directly through the source code?
No it's not. But you are strongly advised to use xml layout because it gives you a visual presentation of what you are trying before runtime.
I have a two activities but only a layout xml file for one of them. Am I supposed to create another xml file in res/layout section for the menu activity?
Yes. You have to create an xml file for every activity in your app.
I just don't understand when I use the source code and when I should use the layout...
You should use xml as much as you can. You should only use Java code to set layout attributes when it has to change at runtime. E.g populating a ListView with text from an database or a web service.
Use this code in you java file
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
LinearLayout ll = new LinearLayout(this);
LinearLayout.LayoutParams layout_params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
LinearLayout.LayoutParams box_params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 40);
LinearLayout.LayoutParams btn_params = new LinearLayout.LayoutParams(120, 40);
ll.setOrientation(LinearLayout.VERTICAL);
EditText et1 = new EditText(MainActivity.this);
et1.setHint("Enter User name");
EditText et2 = new EditText(MainActivity.this);
et2.setHint("Enter speed");
Button btn= new Button(MainActivity.this);
btn.setText("Back to Game");
btn.setGravity(Gravity.CENTER_HORIZONTAL);
ll.addView(et1, box_params);
ll.addView(et2, box_params);
ll.addView(btn, btn_params);
ll.setGravity(Gravity.CENTER);
rl.addView(ll,layout_params );
}
}
You will get required output but it is prefer to use xml file until you don't need dynamic changes in your UI. It is easy to maintain code and design screens using xml files and if there is no need of large dynamic changes you should use xml files. Through Xml files you can check you code on different resolution through graphical representation of code. You can create a xml file in layout folder and can link it to your activity. It is easy, time saving and provide you more accurate designs... :) Please check screen shot for dynamic creation of your required design.
Happy Coding !!!
My work computer that Eclipse is installed on does not have internet connectivity due to work related issues so all code and LogCat text has been hand typed instead of copy and pasted since I am on a separate laptop that Eclipse is installed right now. So bear with me for any typos.
Now to the issue. In the new version of my app, I am making it Spanish supported. I localized all my strings in strings.xml. Below is my Java code that I am not usuing to implement.
public class SplashScreen extends SwarmActivity {
Context c;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
loading = (TextView)findViewById(R.id.loading);
//loading.setText(c.getResources().setString(R.string.loading)); //This way gives NPE
//loading.setText(R.string.loading); //This way works
//loading.setText("Test"); //This way works
}
}
If I understand localization correctly, I have to getResources() first so the app knows what language of the string to display. But the getResources() is what is messing me up.
What do I need to do to get the string displaying correctly?
To answer your problem, your forgot to initialize your Context object. So c is null. Replace
loading.setText(c.getResources().setString(R.string.loading));
by
loading.setText(getResources().setString(R.string.loading));
But actually there is no need to do that.
Android loads the appropriate resources according to the locale settings of the device at run time.
You just have to respect this hierarchy in your project :
res/
values/
strings.xml
values-es / (here for spanish values)
strings.xml
values-fr /
strings.xml (here for french values)
You have this code
Context c;
public void onCreate(Bundle savedInstanceState) {
...
loading.setText(c.getResources().setString(R.string.loading)); //This way gives NPE
The member c is never set before it is used. This is the reason for the NullPointerException. You must first initialize c with View.getContext() for example.
Localization is handled automatically according to the device's capabilities and settings.
In your layout definition, you can define the text string with a reference to a string id and Android will automatically load the appropriate resource
In res/layout/splashscreen.xml:
...
<TextView android:id="#+id/loading"
android:text="#string/loading"
.../>
...
So there is no need to explicitly set the text string in your code, because Android will do so already. The only thing you have to do, is to define the appropriate text strings in the res/values*/strings.xml files.