5

I have read about tools:context and found it pretty useful while development, as it helps me to know which activities I have used current Xml.

My Problem is:

I have 2 Activities, using same Xml file(Reusing same layout.xml file), but I am unable to link both using tools:context

How can I link 2 Activities using tools:context in Xml?

I have tried:

<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
    tools:context="com.example.mytestapp.MainActivity,com.example.mytestapp.Main2Activity">

    <!-- Other Layouts Here -->

</RelativeLayout>

This gives me Red lines

Also tried using:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:tools2="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context="com.example.mytestapp.MainActivity"
    tools2:context="com.example.mytestapp.Main2Activity">

    <!-- Other Layouts Here -->

</RelativeLayout>

This doesn't give Red lines but when I run them, I get following error:

Error:(2) error: duplicate attribute.
Error:(2) duplicate attribute.
Saif
  • 723
  • 6
  • 21
Firoz Memon
  • 4,282
  • 3
  • 22
  • 32
  • What exactly are you expecting to happen? The only benefit that adds is to the preview editor, which really shouldn't require any details about the Activity except the theme used for it – OneCricketeer Dec 07 '17 at 05:44
  • When I make change in xml file, I need to know all Activities that are connected in it, so that I can make changes easily – Firoz Memon Dec 07 '17 at 05:45
  • 1
    remove tools:context lines – kamal verma Dec 07 '17 at 05:47
  • 1
    I think you should instead use Project wide search for `R.layout.file`, which gives you all the Java files referencing that file. Or just add a XML comment. The context attribute isn't intended to be a list or source of reference, as far as I know – OneCricketeer Dec 07 '17 at 05:47
  • well Android Studio 3.0.1 is intelligent enough to show you all the links where your file is used using the class icon with the definition of root element so you don't need to add tool context – Umar Hussain Dec 07 '17 at 06:34
  • @UmarHussain I have Android Studio 3.0.1, I removed `tools:context` as suggested by you, but now when I click on `C` icon it displays `Nothing to show` – Firoz Memon Dec 07 '17 at 06:45
  • Have you solve this problem? – ousecTic Feb 08 '22 at 00:41
  • nope, didn't find any solution @ousecTic – Firoz Memon Feb 08 '22 at 04:27

0 Answers0