So, I have a Layout that is
android:visibility="invisible"
is there a way to force ui designer in intellij idea to display such elements at all times?
According to the documentation you can use the tools xmlns:
tools:visibility="visible"
Android Studio supports a variety of XML attributes in the tools
namespace that enable design-time features (such as which layout to
show in a fragment) or compile-time behaviors (such as which shrinking
mode to apply to your XML resources). When you build your app, the
build tools remove these attributes so there is no effect on your APK
size or runtime behavior.
To use these attributes, add the tools namespace to the root element
of each XML file where you'd like to use them, as shown here:
<RootTag xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" >
Use this tools:visibility="visible"
Related
I was wondering wheather when previewing the layout in Android Studio I can see all view regardless of their visibility in order to inspect all the elements in the preview without changing something in the code (just for debugging purposes).
Thanks in advance!
you can use:
android:visibility="gone"
tools:visibility="visible"
the tools namespace is there for this type of situation where it's only relevant for development.
and then just import:
xmlns:tools="http://schemas.android.com/tools"
in your root layout, if your IDE doesn't suggest it
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.
When I do changes to a layout resource file it's not updated/reflected when the apk file is buildt and installed from Android Studio 2.0 (Preview 3b).
Example:
I had a EditText and I added a TextInputLayout like this:
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_new_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp">
<EditText
android:id="#+id/edit_text_new_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/hint_your_new_password"
android:inputType="textPassword"
android:saveEnabled="false" />
</android.support.design.widget.TextInputLayout>
The result after updating the app is the same as before I added the TextInputLayout, just the EditText without TextInputLayout.
What I've tried:
Build/Clean Project
Build/Rebuild Project
File/Invalidate Caches / Restart..
Uninstall app first
Turn off Instant Run
I suspect this is probably a bug with the Preview 3b version of Android Studio 2.0 causing this. Any ideas? Maybe it's just a settings/configuration?
Temporary solution:
If you make a copy of the layout file and inflate it instead. Then the changes are updated in the app? But this is not ideally the best solution!
Also make sure you make the changes in the layout-v<target-api> folder if you have set specific layout for specific api level. For example layout-v23 in mine case.
This issue is typically caused by only updating the xml file in one layout folder and not the other folders containing the file's compatibility variants as well. For example, only editing the xml file in the layout system folder and not the layout-v14 folder too will cause this issue.
The fix: update the instance of the layout xml file not only in your layout folder, but the instances in all other layout folders too (layout-v14, layout v-21, etc.)
I wasn't able to reproduce this issue. I tried the same layout changes with Preview 3b and the changes showed up in the emulator fine. I simply hit the run button again.
Was this working for you in a previous version of Android Studio?
I've been using the following when creating layouts using the preview tool in Android Studio.
<TextView
android:id="#+id/my_text_view"
android:text="87%" />
The drawback is, if there is a delay before the real value is available, the UI will show 87% for a split second.
What are my options to handle this nicely?
For preview-only purposes, use the tools namespace:
<TextView
android:id="#+id/my_text_view"
tools:text="87%" />
This way the text only shows in IDE but not in runtime environment.
Declare tools with xmlns:tools="http://schemas.android.com/tools" in your XML's root element.
For example, in:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
...
Do I need to put it?
It defines the XML namespace of the document. You should put it, otherwise tags like <RelativeLayout> could be not recognied by the parser.
Namespaces are a way for XML documents to include tags from various vendors. By using xmlns attribute you declare, that, by default, you're using XML elements defined here: http://schemas.android.com/apk/res/android (note that this link is broken - this discussion explains why).
You also declare additional namespace, tools, which is not your default namespace, thus when referencing elements or attributes defined there, you must add tools prefix, on example:
tools:context=".SomeActivity"
Following is a useful link from Android dev portal: https://developer.android.com/studio/write/tool-attributes.html
It says
Android Studio supports a variety of XML attributes in the tools namespace that enable design-time features (such as which layout to show in a fragment) or compile-time behaviors (such as which shrinking mode to apply to your XML resources). When you build your app, the build tools remove these attributes so there is no effect on your APK size or runtime behavior.
i.e. tools namespace helps designing UI and all attributes with prefix 'tools' will be removed at build time.
In fact, when you do :
<RelativeLayout android:id> </RelativeLayout>
Instead of calling android:id, the xml will call http://schemas.android.com/apk/res/android:id . It just the page that declare all the attribute and views that you can use in your xml.
Here is an explanation.
http://www.w3schools.com/xml/xml_namespaces.asp