I have a styled TextView whose real text is populated dynamically at runtime. The Graphical Layout view is very useful for getting a feel on how this component works with others in terms of look and feel, etc. There is no sensible default to this text field and I wish it to be blank before being populated. If I don't specify any text in the TextView declaration then the TextView is blank. I can set the text manually using:
<TextView
...
android:text="Preview text"/>
and then switch to the Graphical Layout. However, I must remember to remove this or risk it being shipped in my production version.
Is there a way to specify text which is only seen in the Graphical Layout preview but not applicable at runtime?
EDIT: I'm using Eclipse ADT.
Yes you can with the design tools extension attributes in Android Studio.
See this page https://developer.android.com/studio/write/tool-attributes.html
Basically you define the tools namespace
xmlns:tools="http://schemas.android.com/tools"
Then use it to set your placeholder text.
<EditText
tools:text="John Doe"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
This actually works with most (if not all xml attributes).
e.g
tools:visibility="gone"
would set the preview visibility to "gone" but the runtime visibility would be unchanged.
I don't believe there is, the only possible way is when you declare your TextView, you say after, tv.setText(""); this way you will always find it blank at runtime
Related
For some reason, my textview Add a base doesn't show up when I launch the app on my phone. I have no idea why, 'cause it is displayed on Android studio :O
Replace tools:text="#string/add_base" with android:text="#string/add_base"
Attributes in the tools namespace are shown only when designing layouts. When you build your app, the build tools remove these attributes so there is no effect on your APK size or runtime behavior.
Reference: https://developer.android.com/studio/write/tool-attributes
tools:text="YOUR TEXT" can be used for your design and preview purpose.
If you add any text as a tools:text app will not use this text as that is not the actual text that should be rendered on the app
So instead of this, you should use android:text="YOUR TEXT"
I don't know all features of Android Studio, I have tried to search,but found nothing.
I wonder if it is possible to make Android Studio autoinsert attributes required for right-to-left support.
For example I have typed following line
android:layout_marginLeft="10dp"
Is it possible to make Android Studio insert marginStart attribute automatically ?
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
And vice-versa(for padding too).
This would save some time.
Maybe someone knows how to get such behavior, I will be grateful for any help. Thx.
Apparently, you can't do that. You have to write them yourself.
The difference between layout_marginLeft and layout_marginStart for example is that layout_marginLeft is executed for left to right languages like English, while `layout_marginStart' is only executed for right to left languages like Arabic.
So if you are going to add string translations to your app including arabic or any other right to left languages, you will need to write marginStart or marginEnd attributes... So anyway, if you didn't write layout_marginStart for example and your app doesn't support Arabic language or any other right to left languages, no error will occur to the user; it is just a warning that Android Studio tells you.
Definitely the same concept is applied to padding attributes. Hope that helps you.
Edit:
If you don't like to see Android Studio warning you these warnings, you can simply disable that by clicking on the yellow light bulb beside the yellow highlighted warning and selecting Edit 'Using left/right instead of start/end attributes' inspection settings, then uncheck it from the list.
But if you don't want to change the inspection settings, you can just add the following to your View that you don't want to use start/end attributes in it:
tools:ignore="RtlHardcoded"
and add that to your parent layout that contains that View:
xmlns:tools="http://schemas.android.com/tools"
In Android where can I set the text that automatically reduce size when is too long ?
Using android:autoText="true" doesn't work.
Add app:autoSizeTextType="uniform" to your Button. Check this link for more details.
Try to use:
android:ellipsize="end"
It will add 3 dots at the end of the text if it's too long.
use android:ellipsize="end" property when the text is longer than the size of the button
<Button android:text="Button"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:ellipsize="end">
</Button>
From android developer page it says This class accesses a dictionary of corrections to frequent misspellings, as well as this page it says If set, specifies that this TextView has a textual input method and automatically corrects some common spelling errors. The default is "false".
Must be a boolean value, either "true" or "false".
This may also be a reference to a resource (in the form "#[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.
This corresponds to the global attribute resource symbol autoText., so android:autoText="true" is not for adjusting the text size but it is something related to spell correction.
From what i understand about your problem, you are looking for a solution through which text is fully displayed on button and never truncated. If text is longer than button size, textsize should be reduced to fit in the text within the button bounds.
So there is one post related to autosize but it is related to Textview.
So you can use this as starting point and build your solution.
I have a popup which is shown when I receive a GCM notification. The popup is implemented as a LinearLayout which is setContentView'd in the popup activity. The layout render in Android Studio looks like this:
However, on the device and on the DebugMonitor View Hierarchy dump it does not show, although it is there:
The TextView has the default text "Where?" replaced in the extended Activity class:
String lightName = getIntent().getStringExtra(LIGHT_NAME_KEY);
final TextView lightNameLabel = (TextView) findViewById(R.id.lightNameLabel);
lightNameLabel.setText(lightName);
I am at a loss here. I grep'ed through the project files and there are no other uses of the TextView's id other than in the snippet above. Could you please give me some pointers where to investigate why the TextView doesn't show?
[edit] I am including the .xml snippet for the respective TextView:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/lightNameLabel"
android:layout_gravity="center_horizontal"
android:layout_marginTop="1dp"
android:text="Where?"
android:visibility="visible" />
[edit 2] A link to the whole layout .xml file: http://pastebin.com/2uqkzBSa
It was helpful that you showed a screenshot, as the problem is likely that you're displaying the layout in a dialog. If you select the Holo dialog theme in Android Studio's graphical editor pane, you'll observe that the default text color is white. Since you've provided a light background, the light text is simply illegible against it.
There are different solutions:
Provide a different theme when displaying the dialog as to ensure that the primary colors are dark; or
Define your own theme and provide it when displaying the dialog; or
Modify the layout to specify a text color.
This problem turned out to be much more tricky than it first appeared as it wasn't a problem on all but a few devices.
When creating a custom Preference by making it a subclass of Preference and also use a custom layout defined in an XML file in res/layout and use that layout in onCreateView() in the custom Preference it seems you completely loose the layout information the Preference-s normally have. Nothing strange with that, but if you still want a title (together with the new stuff), how do you specify the size of that title if you want it to be the same size as for other preferences? I was sure I had found the correct answer, setting textAppearance to textAppearanceLarge (either in the layout XML-file which seems like the better way or in the Java code in the Preference class) and this works fine on most devices.
This is from the custom Preference layout file:
<TextView
android:id="#+android:id/title"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
>
</TextView>
And this is from CustomPreference.onCreateView():
LayoutInflater mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout layout = (RelativeLayout)mInflater.inflate(R.layout.custompreference, parent, false);
// Setting up new stuff
return layout;
This works and I can change the size of the title to Small or Medium and I need no Java code for the title in the custom Preference class. textAppearanceLarge is 22dp which seems to be the size of the Preference titles on most devices.
But on some, for instance the HTC Sensation an Desire S the Preference title is smaller. The title of the custom Preference comes out too large on these devices. So, the layouts for non custom Preferences clearly don't use textAppearanceLarge but something else.
My question now is: What do they use? Where can I get it for use in my custom Preferece? I have searched the Internet like crazy but I simply fail to find the answer. All examples I can find use textAppearanceLarge (or simply 22dp), probably because nobody has ever noticed that it doesn't always work correctly.
Use android:textAppearance="?android:attr/textAppearanceListItem"