I'm localizing my application and currently facing a problem. I have a textview which shows when the application was last updated abd I have a string resource "Last Updated: " for that. I used
lastUpdate.setText(R.string.last_updated + dateString);
My code works perfect in English language but in case of other languages the Textview acts strange and shows numbers in place of the text.
For example: When I set the language to Hindi the text Last Updated gets changed to 2131689604. How can this issue be overcome?
Get string by using this
lastUpdate.setText(getResources().getString(R.string.last_updated) + dateString);
Related
I am a newbie of Android Studio.
I have a problem with display the logs in my app.
For example:
String timeStamp = new
SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cycle);
TextView textView = (TextView) findViewById(R.id.cykleoncreate);
Log.d("[ " + timeStamp + "]", "[onCreate]");
I only want to display this log in my app. How can I do it?
you can display the logs by Toast or set in textView
Toast.makeText(getApplicationContext(),timeStamp,Toast.LENGTH_SHORT).show();
or
textView.setText(timeStamp);
What you have done is write but you can get confused as in where's the log, Log has tag and message, Tags are usually Activity or fragment name so that its easier for one to locate from where did a particular error or message came from, and you can include anything in the message part of it.
we have different types of logs this can be found here
You will find a logcat button on bottom side left side of android studio from there you can select depending on which type you want to see? in your case it's debug
Ok, it works well
textView.setText(timeStamp)
But I don't know why it doesn't work, when I want to build my string:
textView.setText(timeStamp + "my string")
Actually Log is not displaying in app , it will display in android studio . For displaying in app , we need to use Toast . your code is working fine you can check it in your android studio , as its shown in image We can display like this Log.d("onCreate", timeStamp); and search using TAG as onCreate.
I developed a web service using java which request from the database Arabic data.
I'm using this web service in android using volley.
The problem is when i get the data and set it in text field it's show some weird symbols (اÙÙÙسÙ).
The collation in database is : Arabic_CI_AS
I tried many solutions but nothing works!
I would really appreciate your help
This might be related to text direction attribute for the TextView, you have to set it to "anyRtl". Not sure which version that you are working on but this is available since 17+.
This above attribute can be set in one of the following ways:
From XML: Add this line: android:textDirection="anyRtl" to your
TextView tag.
From code:
textview.setTextDirection(View.TEXT_DIRECTION_ANY_RTL);
Haven't tried it though, hope this helps.
Few Links:
https://android.stackexchange.com/questions/25359/how-to-make-an-android-device-to-display-complex-rendering-of-indic-characters
Display Arabic text in android textview
https://developer.android.com/reference/android/text/TextDirectionHeuristics
In my app which has tabbed activity, I am sending a simple message to textView and logcat (message indicating which tab I am selecting).
However, textView shows correct string but logcat shows different one.
Steps to reproduce this problem are very simple:
Start a new Android Studio project (My Android Studio version: 1.5.1)
Give any application name (e.g. MyApplication)
Keep only "Phone and Tablet" selected
Select "Tabbed Activity"
Choose Navigation Style "Action Bar Tabs (with View Pager)"
Click finish
This will generate and build project. If we run this application it will show string "Hello World from section: 1" on first tab, "Hello World from section: 2" on second and "Hello World from section: 3" on third tab.
Now, go in MainActivity.java and in method onCreateView (of class PlaceholderFragment) just below this line (where it send string to textView),
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
add this line
Log.i("onCreateView", "From tab number: " + getArguments().getInt(ARG_SECTION_NUMBER));
Expected behaviour:
textView and logcat both should display same message as we tap on tabs.
Actual behaviour:
textView shows correct strings. However in logcat, tab numbers shown are incorrect and hence not in consistent with textView output.
This is indeed weird and height of all is, instead of this when I tried forming string on my own and passing it to textView and LogCat
String strMessage = "Msg from tab number: " + getArguments().getInt(ARG_SECTION_NUMBER);
textView.setText (strMessage);
Log.i("onCreateView", strMessage);
It still has SAME behaviour i.e. shows correct strings in textView for relevant tabs but Logcat shows completely different tab numbers. Same string I am sending but only number changes ???
I found root cause of this on my own. Looks like textView.setText called in onCreateView sets the text value and it never changes afterwards. I wanted to find out which tab currently is active or visible so that I could update its contents. And I was under wrong impression that onCreateView gets called when tab is visible. Finally I found this answer which solved my problem.
How can I change the text within my textview based on the language set on the device without using string resources? I don't want to use string resources. As an example could someone please tell me what code I can use if the device language is in French and whether or not I'd need to use else if or else for including other languages.
TextView txt = (TextView)v.findViewById(R.id.WCBank_textView1);
txt.setText(Html.fromHtml("<font color='#E32017'>Red apples,</font>" +
"<font color='#FFD300'> yellow bananas,</font>" +
"<font color='#00782A'> green grapes</font>"
));
I still prefer string resources for i18n (not for the individual words, but for the whole expression). That said, you could try something like this:
if (Locale.getDefault().equals(Locale.FRENCH) || Locale.getDefault().equals(Locale.FRANCE)) {
// do the french stuff...
txt.setText(...);
} else if ...
When I was printing java code in Eclipse, the way of printing was WYSIWYG. So if I folded some selected sections (for example import section, but also any other foldable section), it was printed as folded.
In Android Studio code is printed allways fully unfolded. Does anybody know some way (plugin or some preferences setting etc.), how to set it to print also in the folding WYSIWYG style?
My problem is that I have done the printing part in android device when the printer is already configured on the device.
If the printer is not configured on the device, the exception occurs can you tell that how u know that printer is configured on device
e1=(EditText) findViewById(R.id.editText);
String name=e1.getText().toString();
PrintManager printManager=(PrintManager) this.getSystemService(Context.PRINT_SERVICE);
//PrintDocumentAdapter printAdapter = view.createPrintDocumentAdapter();
//Toast.makeText(getApplicationContext(),"hi nikhil"+name+""+printManager.getPrintJobs(),Toast.LENGTH_LONG).show();
String text=this.getString(R.string.app_name)+"Document";
// printManager.getPrintJobs();
printManager.print(text, new MyPrintDocumentAdapter(this), null);
Log.d("print", "print" + printManager);
on Android-Studio 4.0, I noticed that you can select the parts of a file you wish to print, and in the print dialog box, it will allow you to just print the selection. This isn't exactly what you're looking for, but it is a way to bypass the long lines of import statements and comments at the head of a file.