How to set default/fallback value in order to see something in layout preview, when attributes are set by style?
You can use Design-time view attributes.
From the documentation:
You can insert sample data in your layout preview by using the tools:
prefix instead of android: with any attribute from the Android
framework. This is useful when the attribute's value isn't populated
until runtime but you want to see the effect beforehand, in the layout
preview.
For example, if the android:text attribute value is set at runtime or
you want to see the layout with a value different than the default,
you can add tools:text to specify some text for the layout preview
only.
Related
In android Using setTypeface method we can set the font to the control of our wish programmatically, but i want to know is there a way we can avoid this and set the font in layout XML file itself?
I Just want to specify the path of the file & font should get updated automatically.
You can use Calligraphy library where you can specify font in XML itself.
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
fontPath="fonts/Roboto-Bold.ttf"/>
Yes. You need to extend the view you want to set its font in xml and then make an custom attribute (as enum) to be accessed from xml and call it for example "customFont".
Then in the extended view java code get the "customFont" value and change the font programaticaly.
Then you can use this custom view and set its font through xml ;)
Its possible for some limited font becuase you must define an enum for xml and see in java code wich font was selected and then set it in java code set the selected font.
Though you can set system font in xml without all these. And my explanation was for your custom fonts ;)
For example, I am using android.support.v4.view.PagerTabStrip and I want to call its setTabIndicatorColor with my choice of color. How to do it in layout XML file?
Best thing you can do is to consult the documentation. For PagerTabStrip it is here.
There's a Summary section where you can check all XML attributes defined for that class. In this case, you can see that there are only attributes inherited from ViewGroup and View. None of them sets the tab indicator color.
This means that you need to set it in the code.
Let's look at the example where there is an attribute, e.g. orientation for LinearLayout. There's a entry for android:orientation attribute which tells you exactly how to set it in XML.
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
I've read from another post that it's possible to set attributes like color to every TextView in an application: Setting global styles for Views in Android. However, I can't set layout_margin nor layout_height and layout_width attribute using that method to any textView, using "android:textviewStyle". On the other hand, if I use the style attribute and reference it to a style with all the attributes above, it works. Is there a way that you can use global styles and still set margin?
Thank you in advance
You can set padding instead of margin and you will have the same result.
There are always a few attribute values that Android doesn't explicitly define how to change and set dynamically for views. In my case, I'm trying to set the ListView's vertical scroll bar drawable. There's no method in the ListView class that lets you set this. I can only define this in the XML using android:scrollbarThumbVertical="#drawable/new_scroll_bar". Is there ANY workaround that would let me change attributes not otherwise defined dynamically?
It seems impossible, since the document doesn't refer to a corresponding method to set that XML attribute.
In the docs it says it corresponds to the global resource attribute:
android.R.attr.scrollbarThumbHorizontal
You can set this equal to a drawable programatically.
Ref: http://developer.android.com/reference/android/view/View.html#attr_android:scrollbarThumbVertical
Let me know if that is what you were trying to get to.