Strings from strings.xml available in application but not used by widgets - android

I have been making an Android app. Android Studio rightly scolds me if I hardcode strings in the layout editor, so I put them in res/values/strings.xml. For a simplified example:
<resources>
<string name="thing">Do a thing</string>
</resources>
I then set a button's text property to #string/thing. This correctly displays "Do a thing" in the layout editor, as expected. However, when I actually load my app on my phone with the default Run or Debug commands in Android Studio, the button is blank. This is interesting, as when I manually invoke resources.getString(R.string.thing), I do get "Do a thing" back.
I can manually set the widgets' text fields this way, by doing acrobatics like:
findViewById<Button>(R.id.myButton).text = resources.getString(R.string.thing)
but this is a lot of work that as far as I know should be done automatically. This has happened to me in a Java app too, so the problem isn't Kotlin-specific. I'm using Android Studio 3.6 on Linux and the device is a Redmi Note 3 Pro with LineageOS 16 (Pie).
The button is also blank when the app is launched in a virtual AVD, so clearly I must be doing something wrong.
Here are the full activity layout and string XML files.

Each of your Button widgets has tools:text instead of android:text:
<Button
android:id="#+id/nextButton"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
tools:text="#string/nav_next" />
android:text is used for fixed text to be displayed at runtime and, by default, in the development tools.
tools:text is for sample text to be displayed in development tools only. Mostly that is for cases where the text that you really want to use is not known until runtime (e.g., needs to be loaded from a database). Using tools:text lets you get a sense of what the layout will really look like in the tools, while not pre-populating that text when your app runs.
So, switch from tools:text to android:text and you should get the results that you seek.

Related

Android - EditText not showing any text

I seem to have quite a specific problem. I have two tablets, one runs on Android 6.0.1, the other on 5.1.1.
I have an activity and a fragment (the fragment is located on an activity). Each has an EditText element with the following layout:
<EditText
android:id="#+id/etInputForm_1_1"
android:layout_width="250dp"
android:layout_height="30dp"
android:layout_below="#id/tvDescription_1"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:cursorVisible="true"
android:imeOptions="actionNext"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingStart="5dp"
android:textColor="#color/Black"
android:textSize="15sp"/>
The problem is, if I test it on the device with Android 6.0.1, everything seems to be ok, I can input text wherever I want to, however on the device with 5.1.1, the EditText in the fragment always seems to be empty no matter how many letters I type (the EditText in the activity still works fine).
But the logs still show that the text IS there (however we can't see it).
Anyone had similar problem and a solution to this?
I had a similar issue where the edit text hint wouldn't go away (representing text was in the field) and no text would show up when I was typing but my logs showed text was indeed in the field. I also had an issue where my spinners would not show the selected option
I solved it by enabling hardware acceleration for my application. Since it is enabled by default (I believe) I removed this line from my <application></application> section of my manifest file: android:hardwareAccelerated="false"
My issue resolved after removing following line from edit text.
android:textAllCaps="true"
The problem was resolved by using android.app.Fragment instead of android.support.v4.app.Fragment. Seems that the support fragment class is buggy when using on tablets with Android 5.1.1.

Objects visibility

I've done a lot of search but can't find the difference between tools:visibility = "visible" and android:visibility = "visible" ? Which situation I must use tools or android?
Here is the Simple Explanantion:
tools:visibility = "visible" is used to manipulate view visibility on the IDE.It wont affect the view in the real time.It just used for Designing purpose in Android Studio
while
android:visibility = "visible" is the actual code which will be executed in run-time and will make changes to your views
Ref: http://tools.android.com/tips/layout-designtime-attributes
tools: attributes only contribute to design time preview while editing layouts while
android: actually affects how it will be displayed on actual device.
You can find further information here and here.
The exactly question should be
"What the difference between android:... and tools:... on Layout
XML files"
tools is one of the Design Attributes that can facilitate layout creation in XML in the development framework.This attribute is used to show the development framework what activity class is picked for implementing the layout. Using “tools:context”, Android Studio chooses the necessary theme for the preview automatically
Android is used in run-time app, when you launch your apk in a device
according to here
If you see the Design Time Layout Attributes
The tools namespace is a specially recognized namespace by the Android tools, so all the attributes you define on view elements in the tools-namespace will be automatically stripped when the application is packaged and there is no runtime overhead.
So if we need to test something in layout editor only during development time which doesn't affect at runtime, we can use the tools namespace.
Example:
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First"
tools:visibility="invisible" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second"
tools:visibility="visible" />
If the above layout is rendered in Android Studio designer view, first Button will be invisible. But at run time, it will be visible.
TOOLS values will be used only in layout preview in Android Studio.t
ANDROID values will be used in app as normal.
So if you set values for main container:
tools:visibility:"gone"
android:visibility:"visible"
The main container in layout preview in AS will be gone, but if you launch app on emulator / device it will be visible.

Android Layout Editor Freaks Out on Question Mark

This is strange, yet I see it all the time as I have lots of reasons to display just a simple question mark in this app.
When editing with the Eclipse graphical layout editor, everything is fine...until I tell a TextEdit or a Button to display just a question mark. Here's my code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:id="#+id/test_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/question_mark" />
</LinearLayout>
And the string is defined in res/values/strings.xml. The relevant line is:
<string name="question_mark">\u003f</string>
The error message I get is:
Missing styles. Is the correct theme chosen for this layout? Use the
Theme combo box above the layout to choose a different layout, or fix
the theme style references.
Couldn't find theme resource for the current theme
Change the text, and the error message goes away.
You can see that in my struggles, I'm even trying to use the unicode version of a question mark. And yes, \? doesn't work either.
Note that this only happens when the graphical layout editor is set to API 7 or greater.
Now the graphical layout editor displays the question mark properly, and the emulator and my phone display the question mark without any problems. I'm just annoyed with the error message taking up 1/4 of my screen for all my layouts (and obscuring other error messages that may crop up).
Any suggestions?
First, test this again on the ADT 21 release that shipped today.
If the problem continues, create a sample project that demonstrates the issue, and post it along with step-by-step instructions on the Android issue tracker.
Well, this is a really crappy hack, but it kind of works--if you stand on your head!
You can use the unicode \u00bf as in
<TextView
android:id="#+id/test_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="\u00bf" />
It's not a normal question mark, but it's close enough for a hack and gets that annoying error message out of your hair. And it might even increase the humor quotient of your app!
But PLEASE, if anyone out there has a real fix, post it!
Wow, I entered a bug report...[time marches on]...finally, I get a few emails as the Google team starts to look at it.
Today, I received some good news. It looks like the bug has been fixed (and they found a few related bugs, which have been fixed as well). The fix will be in the next release, Version 21.1 Preview 2. You can read the official details here.
Looking forward to it!

How can I get the native Android Editbox

I have created an Editbox in XML using this code:
<EditText
android:id="#+id/txtEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="50dp"
android:inputType="textEmailAddress" >
</EditText>
The textbox renders like this:
How can I get the native android EditText with the orange borders when focused, white background etc. ?
I tried adding
android:background="#android:color/white"
but that only changes the background to white.
The style you are seeing is "native" for the Honeycomb (3.0) Android version, specifically, the new Holo-dark theme. The orange-borders-and-white-background look was last used in 2.3.* and has since been left behind.
Agree with the answer of neutrino: The style you are seeing is "native" for the Honeycomb (3.0) Android version, specifically, the new Holo-dark theme.
But still if you wants the EditText that you want then you need to set the style/theme inside the AndroidManifest.xml file:
<activity android:theme="#android:style/Theme.Light">
when we add view's from layout, it renders from framework, then add the properties we provided . so in your code, EditText is native EditText, which varies from device to device . so if you want look and feel over a particular device make your own style for that and use it .
Had the same problem...
Try using:
EditText usr=(EditText)findViewById(R.id.editText1);
usr.setBackgroundResource(17301528);
If you really want to older versions of the EditText iamges (from sdk/android-8/platforms/data/res folder), place thema in the drawable folder, create a StateListDrawable out of it, and set the background of the EditText as that Drawable. But why bother ? Let app users enjoy the default look and feel of their device.

Using NumberPicker in Android 3

I'm trying to use the NumberPicker widget in Android 3.1. I figure it is now a standard widget, part of a public API, and not an internal widget anymore.
However, it doesn't work. I can see a textEdit with "0" in it, but there are no buttons, I can't scroll it, and I can't enter a new number with keyboard, either.
Update:
Here's the xml I used for testing:
<NumberPicker
android:id="#+id/numberPicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
By saying doesn't work I mean this: it doesn't have arrows displayed (see the screenshot), and on emulator the keyboard doesn't show up. On a real device it does, but I can't type in the text, I can only erase it.
Any suggestions? I really hate to write my own widget when there is a standard one.
Try to set the values that are possible in the NumberPicker via
setMaxValue setMinValue or setDisplayedValues
maybe the NumberPicker is not working if it doesn't know which values to display.

Categories

Resources