How to get layout from another package in android? - android

I start a project MyFileChooser which contains package com.MyFileChooser.In my Project i have a layout abc.xml.I created another package com.FileChooser in the same project.In a class file inside the second package,i need to set abc.xml layout.But it is not possible to do so.Also i cannot acces the EditText inside abc.xml.

just import
import com.MyFileChooser.R;

Try to get layout by setContentView(R.layout.abc); now you can get all the tools of abc.xml by findviewbyid.
And no need to give any kind of Reference. Now just go to Project->Clean the Project before you Build.

Related

Android studio package name issue

Can't seem to find the issue with this. Any help would be appreciated. Thanks
enter image description here
Your AboutCompany is inside the root package (java) and not inside com.example.david.myapplication.
Drag and drop it to the right folder as in the package declaration of the class.
Or
Alt+Enter on the line and select "Move AboutCompany to package ...."
Another options would be removing the package (meaning default package) declaration, but this is discouraged since you cannot import the class from other packages.

Does anyone know the what's causing "Package name is not a valid package name" when adding a new layout?

I've receiving this error in every project I make, new or otherwise.
I've attempted reinstalling Android Studio fresh without any luck and I've also scoured the Internet for the exact error and nothing seems to be coming up.
Does anyone have an idea of what's causing this error when I add a new layout to my project?
Instead of adding the new activity by right clicking on the file labeled "layout", right click on the file labeled "res" to add a new activity and it should work fine.
As your screenshot shows,current package name is layout,but maybe you don't have a package named layout,so such error existed.
If you want to add a layout file to your project,you should right click layout folder under res folder,and create a layout file.
Hope this helps
Option 1:
Click the Project tab. From there you can see a dropdown. Click the dropdown and go to Packages and then you can see the layout folder. From there you can create another Blank Activity.
Option 2:
From the dropdown again go to Android. Click the java folder, you can see the package name com.example... Write it down on a paper. Then go to layout and create a blank activity. Change the package name to what you had just wrote down on the paper.
Hope it helps.
Just to add some details to what Infamous said.
You can copy the package name from another existing activity.
package com.mycompany.myself.myapplication;
I agree, I'm surprised that Android Studio is not able to default that with the right value, given during the project creation:
Project structure/app/Flavors/defaultConfig/Application Id
In Android Studio, in the java directory, select the package, for example com.mycompany.myfirstapp, right-click, and select New > Activity > Blank Activity.
1 - to find the right package name
go to (main activity) page .
2 - First line you will see this for example :
package alqahtani.dhafer.myapp;
copy this text alqahtani.dhafer.myapp
3 - go to layout --> right click --> new activity --> paste package name in layout name .
just "layout" cannot be a package name check other activities and try to take that packag name on top of the file
When adding a new activity to my application, it shows the following problem: Package name is not set to a valid package name, and does not allow adding the activity.
** Solution 1 : Instead of adding the new activity by right-clicking on the file named " layout ", right click on the " res **" file to add a new activity.
And no longer shows the problem: Package name is not set to a valid package name, this is because I could not find the package layout.
** Solution 2 **: In the field ** Package name **, put us as package ** layout ** delete it and put the full name of your application package. ** Example **:
com.mx.softmolina.example
http://blog.softmolina.com.mx/wp/2016/11/30/solucion-package-name-is-not-set-to-a-valid-package-name-agregar-nueva-actividad/
I hope it will be you useful.
Regards!! :)
Change the package name Layout to project package name.
After changing the Package name. It will allow to finish process.
OR
Copy project package name from any java class and paste in Package name section.
I was seeing the same error message (Package name is not a valid package name) when I was trying to add a new activity. The problem was that I was trying to add a new activity to the wrong place. I was trying to add the activity (new -> activity -> blank activity) in the res/layout directory. The solution was to add the activity in the java/your_app directory.
If you want to add a layout file to your project, follow below way -
app -> res -> New -> Acivity -> select activity which u want.
For me it was because I right-clicked on the Activity node in my project explorer. If I click on the "app" root node, this doesn't happen.
It's simply because you are trying to create new Activity / Layout file in a wrong package. To do so, You have to navigate to app >> java >> firstfolder.
Right Click on this first folder and go to NEW >> Activity >> EmptyActivity or AnyOtherTypeYouWant. Then enter the correct name for your Activity and you will face no errors.
As simple as that.

why I cant see my layout file in eclipse

I have created a class which extends ArrayAdapter class. But while doint
View v = inflater.inflate(R.layout.xxxx,..) I cannot see my layout file. I dont know what does that mean. In other activity I can see it very well using R.layout.xxx.!
There is a simple solution for this issue.
Suppose your package name is
com.xyz.package
Now If you will look at the top of your code
you would find
import android.R;
replace this with
import com.xyz.package.R;
after replacing above import if you find still error that means your layout.xml file in res directory has errors. rectify it, clean and build it in eclipse.

How to link R.ids to the actual button android testing

Right now im testing on Android 4.1.1. using Robotium and trying to click on a image button using the id, because solo.clickonImage or solo.clickonImageButton is not working.
The thing is, I don't know how to import or include the R.java file to get the ID. I could see it from the hierarchy viewer, but it still gives me an error when i place the variable in the code.
in my test scripts my R.java doesn't have much in it, therefore can't read id.
How am I suppose to import the developers ID into my test project?
EDIT:
actually i found this programmatically add id to R.id . now my question is, how does my program know what the id is pointing to. example i've created:
< item name = "camera_menu_upload" type="id"/>
How does my script know what camera_menu_upload is on the screen??
You're probably importing the wrong R file. Check your imports and make sure it's importing com.yourcompany.yourapp.R.
to get the button, view, textview, checkbox... use this example:
Button buttonview = (Button) solo.getCurentActivity().findViewById(R.id.mybutton);
solo.clickOnView(buttonview);
if you dont know the id use Hierarchy Viewer to see the button id.
Check out package name in your AndroidManifest file:
<manifest package="com.example.project" . . . >
You need to import R class from your application main package, for example: com.companyname.appname.R
I don't quite understand your question, but is this what you are looking for?
imageButton IB = (imageButton)findViewById(R.id.blablablabla);
if you are using actionbarsherlock there and trying to click on a button, which is a part of actionbarsherlock, you would have to import its r.java file for using the id of that button. i.e. the r.java from application(gen).
r.java file stores different values for different id and accordingly it searches for the value to be resent on the screen. as it finds it, it performs the desired operation over it.

eclipse error with android: id cannot be resolved or is not a field

I just started playing around with android development, and already with just an attempt at making a button, I have encountered a problem.
The error I'm given in the following code is right on "R.id.button1".
It says id cannot be resolved or is not a field.
Do I need to manually reference every single object I make in the layout xml file? I found that this did work, but it does seem to be a bit much for every button I want to make...
package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
private Button button1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
finish();
}
});
}
}
I've been wasting a lot of time (two weeks) because of the same problem until I discovered the problem wasn't mine but Eclipse's.
I guess there's a lot of people with the same problem.
Just try this: Save your project, close Eclipse and then open it again. So simple.
Do I need to manually reference every single object I make in the layout xml file
Yes, otherwise you won't be able to do anything with those views. It's not that bad actually. So, each time you create a view in your XML, and you want to reference it, put an ID:
<View
android:id="#+id/the_id"/>
And then, from your code you can reference it using the R class. You can type, in the example, R.id.the_id and then Ctrl+Shift+O to make Eclipse auto import the needed files.
You can speed up your productivity by using frameworks like Roboguice; I think it's for lazy people, though.
This answer is not applicable to this question (looking at code you have provided). Just adding it if someone else stumbles here and above mentioned answers do not help.
If cleaning (Project --> clean) doesn't helps or saving and restarting eclipse doesn't help either, check for the following incorrect import.
import android.R;
Which Eclipse sometimes add by mistake on auto-import (Ctrl+Shift+O).
Remove that line (import) and it's done :D
Following this EXCELLENT tutorial , I encountered the same problem. After reading Carmello's answer (Sept 17, 2011. 07:23) I simply clicked File->Save All, and voila, 'button0' was automagically defined, and even syntax highlighted.
If "R.id.button1" is not defined, then you'll get a compile error, just as you saw. If you don't define this in the layout, then it won't be defined.
You don't have to specify every object you create in the layout, but you do if you try to reference it from "R.*". You can manually create buttons and other objects that are not specified in the layout.
I ran through the same issues for time being. Plz, do not forget to define as follows:
<View
android:id="#+id/button1" />
if you are using the id in your .java class.
Button b =(Button) findViewById(R.id.button1);
Being said that, the id defined in xml file must match with the id in findViewById().
Go to the 'R.java' file under the 'gen' folder and check whether your 'button1' is present under the class 'id'.If not,then this could be the reason you got that error.When you use the statement " R.id. " make sure that the is present under the appropriate class,in this case under the 'id' class.
R.id is a generated object that assigns int numbers to resources. Try this go to your gen/mypackage/R.java and delete the file. As you can see it is re-generated. This file provides static references where as the context is more of the dynamic state of your app. If you have syntax errors that will prevent automatic re-generation of that R.java file so you will get lots or R. errors. As everyone else has said you can click save all icon or ctl+shift+s on windows. You can clean the project project/clean and that will clean up 95% of those exceptions. Yes eclipse is buggy that way but netbeans does not support android that well. this link may help
Good luck
Do these things,anyone of this will help you
Project -> Clean,
Right click -> Fix Project Properties
Restart Eclipse
make some fake modification in manifest and save
check your console for any error message
check your drawable folder, check the image names satisfy the rules

Categories

Resources