980

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.?

R. Zagórski
  • 18,351
  • 5
  • 59
  • 86
android developer
  • 106,412
  • 122
  • 641
  • 1,128

9 Answers9

469

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

Tim
  • 38,263
  • 17
  • 115
  • 131
Nikolay Elenkov
  • 51,084
  • 9
  • 81
  • 82
  • i see . according to the screenshot here : http://tools.android.com/_/rsrc/1337185954574/recent/newconfigchooser/activity.png , it means that doesn't have to be a class that extends Context , right? if so, i think it does more that what you are saying , though i'm not sure what . – android developer Jun 18 '12 at 08:36
  • 2
    `Activity` extends `Context`, so not sure what you mean? It might be doing more indeed, if you are interested, check the source code, it's available. I don't know any details. – Nikolay Elenkov Jun 18 '12 at 08:39
  • oops . i didn't read the screenshot's text correctly .sorry . when hovering over what i've shown , it also says that it can be a fragment , but fragments don't have their theme written anywhere , no? anyway , i'm still not sure what is this new attribute for . wonder if the new google io will tell about this. – android developer Jun 18 '12 at 08:52
  • they always show what's new and give tips . i will wait . for now , get a +1 .:) – android developer Jun 18 '12 at 09:27
  • 26
    they made a new video that shows this features: http://www.youtube.com/watch?feature=player_embedded&v=Erd2k6EKxCQ#t=1166s – android developer Jul 04 '12 at 21:27
  • How does the system determine in which package is MainActivity? – QED Dec 28 '13 at 22:25
  • I found this documentation helpful too: https://sites.google.com/a/android.com/tools/tech-docs/tools-attributes – Pragmatic geek Sep 23 '16 at 20:19
  • 1
    latest documentation link https://developer.android.com/studio/write/tool-attributes.html – Clive Sargeant Dec 06 '17 at 12:18
386

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)

And yeah, the link Nikolay listed above shows how the new configuration chooser looks and works

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.

RevanthKrishnaKumar V.
  • 1,779
  • 1
  • 19
  • 33
Tor Norbye
  • 8,904
  • 3
  • 27
  • 24
  • how does it know the full path to the activity if it has no base package ? does it look at the manifest file? – android developer Jun 20 '12 at 05:59
  • 2
    Yes, it's treating it the same way as activity registrations in the manifest file, where you can also omit the package in the name attribute. It prepends the package declaration from the manifest file root element, if necessary. – Tor Norbye Jun 20 '12 at 14:31
  • nice . will all of this (and more) be shown at google io 2012 ? i can't wait to hear about the new features . :) – android developer Jun 20 '12 at 16:23
  • 1
    In the generated XML for a new project it puts the `tools:context` value on the `TextView` field. Since this sounds like like a global use case to apply a theme to the whole layout, why is it not placed in the root layout? – Jason Robinson Oct 11 '12 at 20:03
  • @androiddeveloper I actually came to know about this after the IO '12. Came straight from the video. :) – Dheeraj Bhaskar Dec 28 '12 at 11:49
  • they've shown it here : http://www.youtube.com/watch?feature=player_embedded&v=Erd2k6EKxCQ#t=1166s – android developer Dec 28 '12 at 11:52
  • Does that mean this `tools:context` is equivalent to `Context context)` we sometimes see declared a variable inside implemented methods of some abstract class e.g: SQLiteOpenHelper ? – hayonj Jul 24 '13 at 20:39
  • 3
    I've added a document which documents our current tools attributes: http://tools.android.com/tech-docs/tools-attributes – Tor Norbye Oct 03 '13 at 22:00
95

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

Alex Lockwood
  • 81,274
  • 37
  • 197
  • 245
anothercoder
  • 1,379
  • 10
  • 16
15

1.Description

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:

2.Sample

Take a simple tools:text, for example, some more image, convenient to further understand the tools: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" />

enter image description here

TextView1 adopted the android: text, and use the tools:text in the TextView2, on the right side of the Layout editor will display the sample name1, the sample name2 two font, if after you run the code to compile, generated apk, terminal display only the sample name1, does not show the sample name2 the words. You can try to run, see how the effect.

3.Specific description

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.)

4.summary

To sum up, these properties mainly aimed at above the right tools, the simulator debugging time display status, and compile doesn't work,

KeLiuyue
  • 7,458
  • 4
  • 19
  • 35
7

“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/

Mihir Patel
  • 384
  • 5
  • 13
3

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"

enter image description here

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

Ashik Azeez
  • 368
  • 4
  • 7
0

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.

0
<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.

Basudev Adhikary
  • 437
  • 2
  • 11
0

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"