Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the class R in Android?

Tags:

android

In AndroidStudio, when I create a project using an empty activity, I get the following piece of code in the MainActivity.java file:

package my.company.my_proj;  import android.support.v7.app.AppCompatActivity; import android.os.Bundle;  public class MainActivity extends AppCompatActivity {      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);     } } 

where a cryptic class named R is used. What is the purpose of this class R?

like image 443
Android Eve Avatar asked Feb 10 '11 03:02

Android Eve


People also ask

What is the R object in Android?

R is a static class that lists all your resources (usually defined in XML, but all available in your res folder). edit: According to here: The android java class cannot recognize the R file one of your classes might actually be importing the R.

What does R ID mean Android?

android. R. id. myView is an identifier of a View class. It represents an id for corresponding view defined in XML.

Where is the R file in Android Studio?

R. java is the generated file by ADT or Android studio. It will be located under app\build\generated\source\r directory.

What is Package R in Android Studio?

The R class is generated automatically from the application's resources. It contains the id s for these resources and is contained in the package named in the <manifest> tag in the corresponding AndroidManifest. xml file. If there are no errors in the resource XML files, the R.


1 Answers

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.

like image 171
hackbod Avatar answered Sep 18 '22 13:09

hackbod