Inner class in Android R file - java

Is there any way to create an inner class in the auto-generated R file?
Right now R file looks like this:
public static final class id {
public static final int button1=0x7f020000;
public static final int button2=0x7f020001;
public static final int text1=0x7f020002;
}
And to access it, you should type
R.id.button1
What do I want to get it something like this:
public static final class id {
public static final class activity1 {
public static final int button1=0x7f020000;
}
public static final int button1=0x7f020001;
}
So I can access it with
R.id.activity1.button1
It is possible to manually edit the file, but that's not the way I'm looking for
Update: I need this because my current project consists of around 20 activities, with 5-30 widgets on each of them. I'm skipping a lot of ids(mostly for buttons and layout), but still it's not comfortable to type something like
AcEventListToolbarUserImageView
(AcEventList - name of activity, Toolbar - frame, UserImageView - user picture)
Update 2: partial solution is:
in activity layout xml
<TextView android:id="#+id_ActivityName/WidgetName" />
in the java code it will be:
R.id_ActivityName.WidgetName
Pros:
see all your activity list after typing "R.id_";
see all widget ids for the activity you need by typing "R.id_ActivityYouNeed."

The auto-generation of the R.java file is purely there as a mechanism to allow you to easily reference the contents of your res hierarchy from your code. As such, you cannot create sub-hierarchies.
The simple solution is to add context to your item names instead. For example, use #+id/Activity1_button1, and you can access this as R.id.Activity1_button1.

The R file is created automaticallly and it SHOULD NOT be edited...even if u try to do that manually when u run ur code a new R file will be created. can u give a reason why you need to do that ?

I dont know if this helps, but you could define your own hierarchical structure of Id containing classes that are defined in terms of your actual resources. You would still need to type the fully qualified name, but only once.
Like this:
public static final class MyResources {
public static final class activity1 {
public static final int button1=R.id.blahblah_button1;
}
public static final int button1=0x7f020001;
}
then refer to them as:
MyResources.activity1.button1;

Related

Android resetting all fields of a class

Hey guys I have a class called UserData consisting of static fields, which is as follows:
public class UserData {
private static JSONObject fbProfilePicture;
private static boolean loggedOut=false;
private static Integer commonFriendID;
private static Integer userID1;
private static Integer UserID2;
private static JSONObject stolenTrio=null;
}
Actually the class contains alot more fields but I decided to show you a small version of my class.
In my app, I've a feature to delete account and create a new one, when I choose to do that, my app goes back to the sign up process, but there's a problem, I want to clear all fields of that class after deleting user.
How can I do that?
If you think you need a static / singleton kind of instance of UserData, I suggest doing it like this:
public class UserDataSingleton {
public static UserData userdata;
}
and make your UserData a simple Pojo with non-static fields.
This way you can reset your data with
UserDataSingleton.userData = new UserData()
Create a method that reset your values. You might want later to do more things into that method. Ex inform a listener that your object has been cleared.

can subclass initialize superclass variable

i have an activity say A that is extended by many other activities , i have a variable in activity A which is used by all other sub activities(classes) extending it. I want to know if i can set a value for that variable from a sub-activity such that the change will reflect in all the other subclasses extending Activity A.
i know its a basic question ,i am new to this any help is appreciated.
eg:
Activity A has
public String global = "ABC"
Activity B extent A
display(global); ---> ABC
Activity C extent A
display(global); ---> ABC
Activity D extent A
display(global); ---> ABC
now how can i change global in Activity D such that Activity B and C should also be affected.
Seems like you want a variable whos value persists and remains same throughout.
But since you only want to your inherited classes to be able to update or read the variable, you can do something like this:
class A
{
private static int your_var = 0;
public int get()
{
return your_var;
}
public void set(int a)
{
your_var = a;
}
}
class B extends A
{
}
class C extends A
{
}
In your static void main:
new B().set(101);
new C().get() // this will return value 101.
Note: Here only one copy of variable your_var is created. And since it is private, only the non static getter setter methods will be able to read or modify it. Thus making it accessible only by either the containing class itself or the child class objects.
You need to create a static variable in Activity A.
A static variable is a variable of the class and not of the objects.
You can have a singleton class which can hold global data and when need you can fetch the data from the single commonly shared instance of the singleton class. But there is another better way.
You need a class which sits on top of your activities. In any app we usually do have one such class (may be some Initilizer or main or manager class ) which wires the entities and initiates our application.
In android we have Application class which can hold global data. For reference see Android global variable
try the following:
in class A
public static String global = "ABC";
in class B
public class B extends A {
public static void main(String[] args) {
B b = new B();
A a = new A();
System.out.println(b.global);
System.out.println(a.global);
System.out.println(global);
}
}
alternatively you could try to work with encapsulation: mark the string as private and make the "global" string available through getters and setters. This is often a more flexible solution then working with static variables
http://www.tutorialspoint.com/java/java_encapsulation.htm

Meaning of R.layout.activity_main in android development (JAVA language)

What is the meaning of R.layout.activity_main ?
I understand that "." operator is used to define variables of a particular object but in this case its been used twice so I can't make anything out of it. Also what exactly is "R" and "layout"?
I mean obviously they are classes (right?) but what is their function ? Basically explain R.layout.activity_main !
Please comment if question too vague or too broad.
R.java is a class (with inner classes, like layout or string) generated during the build process with references to your app's resources. Every resource you create (or which is provided by Android) is referenced by an integer in R, called a resource id.
R.layout.* references any layout resource you have created, usually in /res/layout. So if you created an activity layout called activity_main.xml, you can then use the reference in R.layout.activity_main to access it. Many built-in functionality readily accepts such a resource id, for example setContentView(int layoutResid) which you use during the creation of your activity and where you probably encountered this particular example.
If you create a string resource (in strings.xml) like this:
<string name="app_name">Application name</string>
it will get a new reference in R.string.app_name. You can then use this everywhere where a string resource is accepted, for example the android:label for your application in AndroidManifest.xml, or on a TextView; either in the xml:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name"
/>
or in code: textview.setText(R.string.app_name).
You can access resources programmatically using the Resources class, to which you can get a reference by calling getResources on any context (like your activity). So for example you can get your app name described above in your activity by calling this.getResources().getString(R.string.app_name).
You can also supply different resources for different device properties/settings (like screen size or language), which you can access using the same references in R. The easiest example here, imho, is strings: if you add a new values folder in /res with a language specifier (so /res/values-nl for Dutch) and you add strings with the same identifier but a different translation and the resource management system cleverly figures out which one to provide for you based on your user's device.
I hope this helps a bit. For more information on resources see the documentation.
R is an auto-generated class, and describe the resources of your project. It contains static inner classes. layout is one of them. R.layout refers to the inner class called layout. activity_main is a public static final member of the class layout
In Android R is an Java-class that is auto-generated from your resources by the build process.
The R.layout member is a auto-generated class that contains all IDs for layouts.
R.layout.activity_main is a static final int member that represents the ID of the layout-file in layout/activity_main.xml.
Okay, so R is a generated class. If you're lucky enough you'll never see it nor have to touch it, otherwise you did something very wrong.
When you make a layout, or any change to a layout, Android Studio generates quite a couple files for you. This includes a R.java file. Here's a piece of an R.java class:
public final class R {
public static final class anim {
public static final int abc_fade_in = 0x7f050000;
public static final int abc_fade_out = 0x7f050001;
public static final int abc_grow_fade_in_from_bottom = 0x7f050002;
public static final int abc_popup_enter = 0x7f050003;
public static final int abc_popup_exit = 0x7f050004;
public static final int abc_shrink_fade_out_from_bottom = 0x7f050005;
public static final int abc_slide_in_bottom = 0x7f050006;
public static final int abc_slide_in_top = 0x7f050007;
public static final int abc_slide_out_bottom = 0x7f050008;
public static final int abc_slide_out_top = 0x7f050009;
}
public static final class attr {
public static final int actionBarDivider = 0x7f010062;
public static final int actionBarItemBackground = 0x7f010063;
public static final int actionBarPopupTheme = 0x7f01005c;
public static final int actionBarSize = 0x7f010061;
public static final int actionBarSplitStyle = 0x7f01005e;
public static final int actionBarStyle = 0x7f01005d;
public static final int actionBarTabBarStyle = 0x7f010058;
public static final int actionBarTabStyle = 0x7f010057;
public static final int actionBarTabTextStyle = 0x7f010059;
As you can see, in this case if I'd type
R.anim.abc_fade_in
I'd be selecting the value 0x7f050000;.
Every layout file is mapped out in this R file, and gets an ID by which android recognizes it. The layouts are located in R.Layout. So, R.layout.activity_main gets you the value of variable activity_main of the class layout of the class R.
And again, don't try finding or changing your generated R file. Things can go very wrong if you do that.
From https://stackoverflow.com/a/4953282/1393766
R is a class containing the definitions for all resources of a particular application package. It is in the namespace of the application package.
If you want to inflate a layout inside your activity class,you can use R.layout.activity_main where layout specifies that your resource is a layout and it's name is activity_main.
If you want to use a drawable image in a layout inside your activity class,you can use R.drawable.image_name where drawable specifies that your resource is a drawable image.
Also,R.java class is an autogenerated class which is not supposed to alter manually.

Can I assign a static nested class to a variable?

I have a few different button layouts for some controllers mapped inside nested classes. Here's an example:
public class ControllerMap{
public static class Type1{
public static final int BUTTON_A = 1,
BUTTON_B = 2;
}
public static class Type2{
public static final int BUTTON_A = 2,
BUTTON_B = 1;
}
}
I want to make a variable to reference which one to use throughout my code. Something like layout = ControllerMap.Type1;. I'm pretty sure this isn't actually possible, but is there any other way I can do this?
If you want them to be used as layout templates, you could do something like this:
public class Template
{
public static final Template type1 = new Template(1, 2);
public static final Template type2 = new Template(2, 1);
public int buttonA;
public int buttonB;
public Template(int buttonA, int buttonB)
{
this.buttonA = buttonA;
this.buttonB = buttonB;
}
}
And then you can use the layout variable like this:
public static Template layout = Template.type1;
Yes you could refer it from anywhere since both your class are public and class variables are public too like:
int button = ControllerMap.Type1.BUTTON_A;//from anywhere and is resolved at compile time
But if you need it runtime, you could just inject appropriate instance and create getter/setter instead of exposing the field directly.
I think you are trying to use 'static' in an OO way which will never work. In this case you can use the Strategy Pattern to solve your issue, but you will have to adapt your code.
Make your class Type1 and Type2 implement a interface (iController). Then anywhere in your code you can can assign:
iController controller = new Type1();

Get name of class during static construction

In C# I can assign the name of a class to a local static variable like this.
public class MyClass
{
private static string TAG = typeof(MyClass).Name;
}
I've found this very useful, because the value of the string automatically updated if the class is refactored to another name. Handy for tagging debug messages and such.
Is something like this possible in Java?
public class MyClass
{
private static String TAG = ????;
}
I know I could use getClass().getName() but that requires a reference to an object. Is there a way to do this on a static variable?
You don't need to assign the name of a class to field instead of writing.
MyClass.TAG
you can write
MyClass.class.getName();
If you really need to you can assign this to TAG but I don't see the point.
A trick is also available that does not require programmer's knowledge of the class name beforehand:
public class MyClass
{
private static String TAG =
new Object() { }.getClass().getEnclosingClass().getName();
}
This trick uses a nested anonymous Object subclass to get hold of the execution context. It has a benefit of being copy/paste safe in case of cloning your code across different classes...

Categories

Resources