Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understand the R class in Android [duplicate]

Tags:

java

android

In android, I'm not sure I quite understand the R class. I'm going through the sudoku example, and I have this snippet of code:

switch (v.getId()) // the id of the argument passed is evaluated by switch statement {     case R.id.about_button: //      Intent i = new Intent(this, about.class);     startActivity(i);     break;     // More buttons go here (if any) ... } 

I'm brand new to Java, but from what I gather it looks like it's taking input (the touch screen being touched on the button) and evaluating the argument. Then the case statement is setup if the about button is recognized, and a new interface screen is created and then navigated to on the phone.

Is this right?

If I got the gist of that correct, why is the deal with the "R" class?

Why is it called to recognize the ID of the button?

I thought the super class (in this project) was the SudokuActivity class.

like image 835
Skizz Avatar asked Jul 23 '11 23:07

Skizz


People also ask

What is the R class in Android?

R class is generated by Android tools from your resources before compiling your code. It contains assigned numeric constant for each resource that you can refer in your project. For example, you have XML resource file that contains about_button .

What is the main function of class R in Android?

R is the class that contains all the resource ids for your application.

What does R stand for in Android Studio?

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.

How is the r file generated in Android Studio?

Android R. java is an auto-generated file by aapt (Android Asset Packaging Tool) that contains resource IDs for all the resources of res/ directory. If you create any component in the activity_main. xml file, id for the corresponding component is automatically created in this file.


1 Answers

R.java is the dynamically generated class, created during build process to dynamically identify all assets (from strings to android widgets to layouts), for usage in java classes in Android app. Note this R.java is Android specific (though you may be able to duplicate it for other platforms, its very convenient), so it doesn't have much to do with Java language constructs. Take a look here, for more details.

like image 82
omermuhammed Avatar answered Sep 20 '22 09:09

omermuhammed