0

I am working on a little Android Studio (version 2.2.3) application.

After adding a second activity with a lot of components I noticed that when I type R.id in the first activity, the auto-completion proposes me the components from the second activity.

Is this normal ?

Screenshot resuming my problem

And here is a working example of my issue (I took the screenshot from it), for simplicity I just created two empty activities each one with a button.

AndroidManifest.xml :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".Main2Activity"></activity>
</application>

MainActivity.java :

package com.example.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;


public class MainActivity extends AppCompatActivity {
    private Button btMain1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Here is the issue,
        //autocompletion of R.id. shows every layout and their composents
        btMain1 = (Button) findViewById(R.id.); 
    }
}

activity_main.xml :

<?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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.myapplication.MainActivity">


    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="198dp"
        android:id="@+id/btMain1" />
</RelativeLayout>

Main2Activity.java :

package com.example.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;

public class Main2Activity extends AppCompatActivity {
    private Button btMain2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
    }
}

activity_main2.xml :

<?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:id="@+id/activity_main2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.myapplication.Main2Activity">

    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="198dp"
        android:id="@+id/btMain2" />
</RelativeLayout>
Charuක
  • 12,953
  • 5
  • 50
  • 88
L. Faros
  • 1,754
  • 3
  • 16
  • 35

3 Answers3

4

Yes. R.id will contain every single id defined in your app. Each id is just a number, and many of them are not used in any given activity or layout.

dsh
  • 12,037
  • 3
  • 33
  • 51
2

R.java is a generated file containing all the resource identifiers for your application.

R.id is just one subclass. You also would see R.layout auto-complete, for example on setContentView

The auto-completion will pull everything because there is no isolation inside of activities, fragments, services, etc.

(snippet of mine)

/* AUTO-GENERATED FILE.  DO NOT MODIFY.
 *
 * This class was automatically generated by the
 * aapt tool from the resource data it found.  It
 * should not be modified by hand.
 */

public final class R {

    // ...

    public static final class id {
        public static final int action0=0x7f0e0090;
        public static final int action_bar=0x7f0e0060;
        public static final int action_bar_activity_content=0x7f0e0000;
        public static final int action_bar_container=0x7f0e005f;
        public static final int action_bar_root=0x7f0e005b;
        public static final int action_bar_spinner=0x7f0e0001;
        public static final int action_bar_subtitle=0x7f0e0041;
Community
  • 1
  • 1
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
1

Your question been asked so even before I thought to learn android. Android: What is R? Why is it so Cryptic?

R is a class containing the definitions for all resources of a particular application package. It is in the namespace of the application package.

For example, if you say in your manifest your package name is com.foo.bar, an R class is generated with the symbols of all your resources in com.foo.bar.R.

There are generally two R classes you will deal with

  1. The framework resources in android.R and
  2. Your own in your namespace

It is named R because that stands for Resources, and there is no point in making people type something longer, especially since it is common to end up with fairly long symbol names after it, that can cause a fair amount of line wrapper.

Now in my words, If we are referencing our own resources that you have created, mostaly we use R. So android studio gives us suggestions based on that.

You should have also noticed android.R which is meant for utilizing resources built in to the operating system.

So yes you will get suggestions based on which R you use and now you should know suggestions based on that R is not for a particular Activity or view that's the reason it suggests them all.

Also there is an article, you can gain more knowledge about R.java , android.R and resources.

Community
  • 1
  • 1
Charuක
  • 12,953
  • 5
  • 50
  • 88
  • @L. Faros yes if you surf a bit you can get lot of details about R based topics worth reading them :) – Charuක Feb 02 '17 at 18:57
  • 1
    @L.Faros `R` for **R**esources. No reason to type like `getResources().getId("name")` – OneCricketeer Feb 02 '17 at 20:08
  • @cricket_007 Yes I agree with that, my point was more that I find weid to have access to all id in every activities. For instance I would expect MainActivity.java to access only the resources from activity_main.xml. I am starting to have a lot of id and it become messy to find the one I want if I am not sure of his name – L. Faros Feb 03 '17 at 11:53
  • @L.Faros Is there a reason you need activity level isolation for your resources? You can have one R.id.list for a ListView in every single one of your Activities and they do not conflict with each other. The findViewById call will find the correct reference based on the setContentView layout used – OneCricketeer Feb 03 '17 at 14:17
  • Well, the reason is mainly for simplicity, as of now the autocompletion is pretty much useless to me since I have to know the particular name of an id. Als I have posted a question about this : [here](http://stackoverflow.com/questions/42026601/is-it-possible-to-separate-resources-access-in-android-studio) Maybe I should not have posted a new question but I am new on SO – L. Faros Feb 03 '17 at 14:34