In android layout xml file a framelayout element is defined like #android:id/tabcontent
how to refer that element in java code
= (Framelayout) findViewById(R.id. _ __
so then what is the difference between
#android:+id/tabcontent
#+id/tabcontent
#android:id/tabcontent
#id/tabcontent –
It should be android.R.id.tabcontent
Always remember whenever you use predefined android resources you must use android.R
Defining the id as "#+id/tabcontent" you could easily find it as (Framelayout) findViewById(R.id.tabcontent)
if you declared the id of frame layout like #android:id/tab??? so you have to find this framelayout using this.
(Framelayout) findViewById(android.R.id.tabcontent)
The + sign adds this id to the automatically created R.java file (so you can reference the resource using R.id) the android: means that the resource is inside android.R.java file (similar to R.java, but refers to android system resources). So, as in the other answers #android:id refers to android.R.id
Related
I'm basically writing my first hello world in android studio and the java file says that the xml layout file and other resources in the res file don't exist. I took the references to these things from books/tutorials so they should work.
The book said to use
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main.xml);
}
But when that didn't work I changed it to res.layout.activity_main.xml
my project directory looks like this
How do I make it work?
http://i.stack.imgur.com/N71sl.png
//EDIT
http://i.stack.imgur.com/HUgsm.png
You are referencing res not R.
do something like this: setContentView( R.layout.activity_main);
Leave off the ".xml"
update your android studio in stable channel and restart android studio and build the project again .
You need to understand the concept of the R class in android.
The R class is auto generated every time you build your project and cannot be modified.
This class contains a map of all the projects assets. Every time you created or import a resource, set a dimension, a string, create a layout, add or change id etc. the R file is changed in the background.
So if you want to access a resource you simply call it using the R class.
For example:
layout: R.layout.activity_main
string: R.string.hello_world
id: R.id.et_main_helloWorld
And so on.
More info can be found here.
Make sure you also check Providing Resources and Accessing Resources for a better understanding.
Good luck and happy coding.
In the end I started a new project and added in everything piece by piece, I had put a mp3 file in the values directory which was messing up the R file.
My Android Studio doesn't recognize fonts in .ttf or .otf format.
I searched for them at File--> Settings -->File Types , but there i can't find a category which recognizes this formats...
Am I alone with this problem?
Because of my new Membership I can't upload any pics, so here a link to a picture of my problem
It is common for android studio (even in latest version) . I also have "?" mark in project explorer, however it is not problem in programming.
You can use your custom fonts directly in java.
First get a reference to the TextView displaying the text that you want to apply your custom font to. Then call the TypeFace class’s createFromAsset() method to create a new type face using your custom font. You pass an instance of the app's AssetManager as the first parameter by calling getAssets(). This enables you to access the custom font asset file which you pass as the second parameter.
Finally, call setTypeFace() to apply your custom font to the text.
TextView textViewCustom= (TextView)findViewById(R.id.textCustomViewFont);
Typeface custom_font = Typeface.createFromAsset(getAssets(),"fonts/xyz.ttf");
textViewCustom.setTypeface(custom_font);
Android custom fonts tutorial TypeFace java
Create a new TypeFace using your custom font, xyz.ttf which is saved in the assets/fonts folder. You can do it in XML also.
I want to add view programatically to my parent view, my view is a RadioButton. I also want to set an Id for the view that I add in RadioGroup. But when I do this it gives me an error in: mRadioButton.setId(321);
Reports two types of problems:
Supplying the wrong type of resource identifier. For example, when calling Resources.getString(int id), you should be passing R.string.something, not R.drawable.something.
Passing the wrong constant to a method which expects one of a specific set of constants. For example, when calling View#setLayoutDirection, the parameter must be android.view.View.LAYOUT_DIRECTION_LTR or android.view.View.LAYOUT_DIRECTION_RTL.
I dont know why it gives me this error.
I found that one solution would be to create MyRadioButton class which extends RadioButton and there I could add a variable int MYID; and then add getters and setters for this view.
But this is a workaround, does anyone know the solution of this problem ?
Thanks!
EDIT:
Another workaround is to use setTag()
(I have used it for a horizontal view with ImageView's added dynamically and it helps me to detect which one was clicked)
If you are only supporting API 17 and higher,
you can call View.generateViewId
ImageButton mImageButton = new ImageButton(this);
mImageButton.setId(View.generateViewId());
Otherwise for apis lower than 17,
open your project's res/values/ folder
create an xml file called ids.xml
with the following content:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="imageButtonId1" type="id" />
</resources>
then in your code,
ImageButton mImageButton = new ImageButton(this);
mImageButton.setId(R.id.imageButtonId1);
I get an error like this in logcat when i try to run my script. I compared it and it is the onClickListener. Any suggestions to fix this problem? Still a beginner.
The problem exists here:
View splashscreen = (View) findViewById(R.layout.splash);
splashscreen.setOnClickListener(this);
You are getting an exception because splashscreen is null, and calling setOnClickListener() on a null pointer is not allowed. The reason that pointer is null is because you need to obtain a reference to the view from your XML using a proper ID. Your splash.xml file located in res/layout is being loaded as the content view for the Activity, but you should have a proper R.id value associated with that particular view.
In splash.xml, the View you declare for this purpose should have an android:id="#+id/something" attribute in it's XML declaration (I picked "something" out of the air, this identifier can be whatever you want). You would then call:
//Hint: You don't have to cast the result if the pointer is a plain vanilla View
View splashscreen = findViewById(R.id.something);
splashscreen.setOnClickListener(this);
Then you will get a valid reference to the view and your set method will not fail.
HTH
As Devunwired said, I think maybe you wrong about layout and id.
See http://developer.android.com/reference/android/view/View.html
Yes, Devunwired is right. You must have to give the Resource id to the Perticular view. Instead of that you are going to give the reference of the Layout file name as "splash.xml".
Also try to make the Id name different from the layout file name. Its not and error but sometimes happend that make possible to arrise problem to understand and to give the resource id to different reference.
Thanks.
Instead of using Thread, you can try Handler:
Handler handler = new Handler();
handler.post(new Runnable(){
public void run(){
//TODO
}
});
By the way, posting more exception stack traces would be more helpful.
In the Java code of an Android project if you want the reference for a view resource you can do something like:
View addButton = findViewById(R.id.button_0);
In the above R.id.button_0 is not a String. Is it possible to dynamically refer to a resource by a string, such as "R.id.button_0"?
I'd like to refer to a button by "R.id.button_%i" where %i is replaced by some valid index.
int resID = getResources().getIdentifier("button_%i",
"id", getPackageName());
View addButton = findViewById(resID);
where %i is replaced by some valid index.
The getResources() method belongs to the Context class, so you can use that directly from an Activity. If you are not inside an activity, then use a context to access: (myCtxt.getResources()).
You could try putting all the ids you want in an array and then using that array to dynamically refer to your resources instead.