Reputation: 116020
Starting with a recent new version of ADT, I've noticed this new attribute on the layout XML files, for example:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity" />
What is "tools:context" used for?
How does it even know the exact path to the activity that is written there? Does it look at the package of the app, inside the manifest?
Is it limited to classes that extend Context or only activities? Is it usable for ListView items etc.?
Upvotes: 1032
Views: 279442
Reputation: 9150
That attribute is basically the persistence for the "Associated Activity" selection above the layout. At runtime, a layout is always associated with an activity. It can of course be associated with more than one, but at least one. In the tool, we need to know about this mapping (which at runtime happens in the other direction; an activity can call setContentView(layout) to display a layout) in order to drive certain features.
Right now, we're using it for one thing only: Picking the right theme to show for a layout (since the manifest file can register themes to use for an activity, and once we know the activity associated with the layout, we can pick the right theme to show for the layout). In the future, we'll use this to drive additional features - such as rendering the action bar (which is associated with the activity), a place to add onClick handlers, etc.
The reason this is a tools: namespace attribute is that this is only a designtime mapping for use by the tool. The layout itself can be used by multiple activities/fragments etc. We just want to give you a way to pick a designtime binding such that we can for example show the right theme; you can change it at any time, just like you can change our listview and fragment bindings, etc.
(Here's the full changeset which has more details on this)
One more thing: The "tools" namespace is special. The android packaging tool knows to ignore it, so none of those attributes will be packaged into the APK. We're using it for extra metadata in the layout. It's also where for example the attributes to suppress lint warnings are stored -- as tools:ignore.
Upvotes: 397
Reputation: 506
One missing thing which the other answers are missing is the context part of any class that can be used with any class that extends context.
tools:context is an attribute that was introduced in Android Studio and the Android Gradle plugin. It is used in layout files to specify the context in which the layout will be used.
The tools namespace is used to specify attributes that are not used at runtime, but are used by the Android Studio layout editor and other tools to give you more control over how your layouts are rendered. The tools:context attribute allows you to specify the fully-qualified name of the Java class that represents the context for the layout. This is used by the layout editor to determine which theme and attributes should be applied to the layout when it's rendered in the editor.
It is looking at the package of the app, inside the manifest. It is used to know the exact path to the activity that is written there.
The tools:context attribute can be used with any class that extends Context, such as an Activity, Service, or Application. However, it is most commonly used with Activity classes as they are the main entry point for most apps.
The context attribute is not limited only to activities, it could be used for any java class that extends context. It can be usable for any context-based class, for example in ListView items, But it is mostly used in activities.
Upvotes: 0
Reputation: 412
According to the developer.android.com
Intended for: Any root <View>
Used by: Lint, Android Studio layout editor
This attribute declares which activity this layout is associated with by default. This enables features in the editor or layout preview that require knowledge of the activity, such as what the layout theme should be in the preview and where to insert onClick handlers when you make those from a quickfix (figure 2).
Figure 2. Quickfix for the onClick attribute works only if you've set tools:context
You can specify the activity class name using the same dot prefix as in the manifest file (excluding the full package name). For example:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity" >
Upvotes: 1
Reputation: 19
This attribute declares which activity this layout is associated with by default. This enables features in the editor or layout preview that require knowledge of the activity.
Upvotes: 0
Reputation: 1500
This attribute helps to get the best knowledge of the activity associated with your layout. This is also useful when you have to add onClick handlers on a view using QuickFix.
tools:context=".MainActivity"
Upvotes: 1
Reputation: 364
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
//more views
</androidx.constraintlayout.widget.ConstraintLayout>
In the above code, the basic need of tools:context is to tell which activity or fragment the layout file is associated with by default. So, you can specify the activity class name using the same dot prefix as used in Manifest file.
By doing so, the Android Studio will choose the necessary theme for the preview automatically and you don’t have to do the preview settings manually. As we all know that a layout file can be associated with several activities but the themes are defined in the Manifest file and these themes are associated with your activity. So, by adding tools:context in your layout file, the Android Studio preview will automatically choose the necessary theme for you.
Upvotes: 0
Reputation: 52956
This is the activity the tools UI editor uses to render your layout preview. It is documented here:
This attribute declares which activity this layout is associated with by default. This enables features in the editor or layout preview that require knowledge of the activity, such as what the layout theme should be in the preview and where to insert onClick handlers when you make those from a quickfix
Upvotes: 505
Reputation: 444
This is best solution : https://developer.android.com/studio/write/tool-attributes
This is design attributes we can set activty context in xml like
tools:context=".activity.ActivityName"
Adapter:
tools:context="com.PackegaName.AdapterName"
You can navigate to java class when clicking on the marked icon and tools have more features like
tools:text=""
tools:visibility:""
tools:listItems=""//for recycler view
etx
Upvotes: 3
Reputation: 8237
tools: context = "activity name"
it won't be packaged into the apk
.Only ADT
Layout Editor in your current Layout file set corresponding rendering context, show your current Layout in rendering the context is the activity name corresponds to the activity, if the activity in the manifest
file set a Theme, then ADT
Layout Editor will render your current Layout according to the Theme.Means that if you set the MainActivity
set a Theme. The Light (the other), then you see in visual layout manager o background control of what should be the Theme. The Light looks like.Only to show you what you see is what you get results.
Some people see will understand some, some people see the also don't know, I'll add a few words of explanation:
Take a simple
tools:text
, for example, some more image, convenient to further understand thetools:context
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sample name1" />
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="sample name2" />
TextView
1 adopted theandroid: text
, and use thetools:text
in theTextView
2, on the right side of the Layout editor will display thesample name1
, thesample name2
two font, if after you run the code to compile, generatedapk
, terminal display only thesample name1
, does not show thesample name2
the words. You can try to run, see how the effect.
1.The tools: context = "activity name"
it won't be packaged into the apk
(understanding: the equivalent of this is commented, the compiled no effect.)
2.Only ADT
Layout Editor (i.e., for the above icon on the right side of the simulator) in the current Layout file set corresponding rendering context, the Layout of the current XML in rendering the context is the activity name corresponds to the activity, if the activity in the manifest file set a Theme, then ADT
Layout Editor will render your current Layout according to the Theme.Means that if you set the MainActivity
set a Theme. The Light can also be (other).(understand: you added tools: context = "activity name"
, the XML layout is rendering specified activity, establishes a Theme in the manifest file, pictured above right simulator Theme style will also follow changes corresponding to the Theme.)
To sum up, these properties mainly aimed at above the right tools, the simulator debugging time display status, and compile doesn't work,
Upvotes: 15
Reputation: 103
tools:context=".MainActivity"
thisline is used in xml file which indicate that which java source file is used to access this xml file.
it means show this xml preview for perticular java files.
Upvotes: 1
Reputation: 412
“tools:context” 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.
If you’d like to know more about some other attributes and useful tools for Android app development, take a look at this review: http://cases.azoft.com/4-must-know-tools-for-effective-android-development/
Upvotes: 7
Reputation: 1449
According to the Android Tools Project Site:
tools:context
This attribute is typically set on the root element in a layout XML file, and records which activity the layout is associated with (at designtime, since obviously a layout can be used by more than one layout). This will for example be used by the layout editor to guess a default theme, since themes are defined in the Manifest and are associated with activities, not layouts. You can use the same dot prefix as in manifests to just specify the activity class without the full application package name as a prefix.
<android.support.v7.widget.GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
Used by: Layout editors in Studio & Eclipse, Lint
Upvotes: 97