Safely remove an Android Activity - android

We all know how easily and safely to create a new Activity. Now I want to know how to remove an Activity from my project safely.
I've created some activity in my project and feel that they are now needless and I want to delete them. But if I delete manually these files:
The activity's XML file in the /layout folder
The activity's Java file in the /src folder
The <activity></activity> portion from the manifest file
Then,
Will no other data related that activity stays in my project? What are those?
Is there any simple removal procedure like creating an activity in Eclipse?

I found my answer. To remove an activity, let the name of the activity is activity:
Remove the activity.java file from src folder (java/your.package.name folder for AndroidStudio)
Remove the activity.xml file from layouts folder (layout folder for AndroidStudio)
Remove the activity.xml file from res/menu folder (this is an optional folder)
Remove the <activity></activity> block of the activity from the manifest file
If the <activity> block in the manifest file has a reference (usually in android:label tag) remove the referenced value (usually from values/strings.xml).
According to answers from #ojonugwa ochalifu and #Md Tarik Mahmud you should also right click on app folder then Refractor -> Remove Unused Resources

Just do it in a two steps in Android Studio.
Delete the Activity's Java file: YourActivity.java
Select app from the Project-Browser in Left Panel. Click Refactor from menu, click Remove Unused Resources.
You are done.

I don't think there is any procedure to delete an activity at once from everywhere.
Delete the entry from manifest first. Then delete the Activity class file. Once class file is deleted you will start getting errors where it is used. Look into each error separately and resolve them.

And the title string in res/values/xml/strings.xml

Just to make sure you have deleted everything after following #CoolBrain's answer, do:
Refractor -> Remove Unused Resources
This will remove residues of that activity you aren't sure you've deleted. Note however that this operation removes unused drawables, and views also

Right click on each of the file associated with the activity (in Android View), select refactor, then select safe delete. This will also tell you if there are any use (of that file) which makes it not safe to delete.

I found my answer. To remove an activity, let the name of the activity is activity:
Remove the activity.java file from src folder (java/your.package.name folder for AndroidStudio)
Remove the activity.xml file from layouts folder (layout folder for AndroidStudio)
Remove the activity.xml file from res/menu folder (this is an optional folder)
Remove the <activity></activity> block of the activity from the manifest file
If the <activity> block in the manifest file has a reference (usually in android:label tag) remove the referenced value (usually from values/strings.xml).
According to answers from #ojonugwa ochalifu and #Md Tarik Mahmud you should also right click on app folder then Refractor -> Remove Unused Resources
Thanks to #A. K. M. Tariqul Islam for answare.

Related

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.

Can I change the name of the main-Activity or not?

Does the name of Layout-Definition for view of the main-Activity always have to be main or can I change it?
Yes you can change the name of MainActivity class in android. But also change it's name in AndroidManifest.xml file.
You can also change the layout name from main.xml to *.xml. Whatever you want. It depends on how to name your files in a project.
Yes, you can change the name of MainActivity by, opening the directory of the java file in android studio, right-click on it and find refactor, then rename, put in the name you like then click on refactor.
you can change it as you want. by default it will be main.xml and you can change it in any way as you like.
also you change it activity setcontentview(R.layout.newname);
You can change it, but in both xml name and R.layout.* (setContentView)
Just use the refactor(shift+f6) so easily change the name. And check debugging if it did well.
Rename an Android Activity as follows:
Right-click the .java file and Refactor Rename.
then
verify the AndroidManifest.xml to make sure has the new name.

How to add menu in Android without getting "R" error

I am writing a simple application to get familiar with android programming. Today I want to play with menus. I follow this page!. I first create "menu" folder under "res" and then add "game_menu.xml" in it. However this "R" thing can't get that folder. I tried some refresh to get it 'auto-generated' but it didn't work. I clean for a rebuild, but this time R.java completely dissappeared. How can I make eclipse to recreate R or what is the problem?
Are you sure the R is not creating the appropriate link for the menu?
To find your xml menu, you must type: "R.menu." & hit ctrl+space and all your menu's will pop up.
Try this way.
And if you want to check your R file, if its creating the menu link, search for:
"public static final class menu" in R file
In that class you will find your xml menu's..
are you able to compile ? Check which R you have imported in your code. It should not be the android.R but the one from your project
You have most likely misspelled something in your xml file, or have some other error. If the xml files are not entirely correct (or other resource files), the R. file cannot be built. Go through the file and make sure that everything is error free.
A common mistake is to name the resources with capitals, although the names can only contain small letters.
another common mistake is to use unsupported folder names.

How do I rename the android package name? [duplicate]

This question already has answers here:
Rename package in Android Studio
(55 answers)
Closed 5 years ago.
Pressing Shift+F6 seems only to rename the last directory.
For example, in the project com.example.test it will offer to rename test only. The same applies if I navigate to package name in .java or Manifest file and press Shift+F6.
So is there a way to rename the package?
You can do this:
Change the package name manually in the manifest file.
Click on your R.java class and the press F6 (Refactor->Move...). It will allow you to move the class to another package, and all references to that class will be updated.
The best way to solve this is going to the AndroidManifest.xml: package="com.foocomp.fooapp:
Set the cursor on "foocomp"
Press Shift+F6
Rename Package to whatever you want
Repeat for "fooapp".
Works for me.
Also, replace in Path in Whole Project as it didn't change everything. Then Clean, Rebuild and it works --> In Android Studio / IntelliJ you should unmark "Compact Empty Middle Packages"
Eclipse:
Right click on the project > Android tools > Rename application package.
As simple as that...
In Android Studio:
open the build.gradle file > rename the applicationId under defaultConfig > synchronize
Goto your AndroidManifest.xml.
place your cursor in the package name like shown below don't select it just place it.
Then press shift+F6 you will get a popup window as shown below select
Rename package.
Enter your new name and select Refactor.
(Note since my cursor is on "something" only something is renamed.)
That's it done.
I found another solution for renaming a package in the entire project:
Open a file in the package.
IntelliJ displays the breadcrumbs of the file, above the opened file.
On the package you want renamed: Right click > Refactor > Rename.
This renames the package/directory throughout the entire project.
Deselect Hide Empty Middle Packages in Project Explorer Windows settings menu than you will be able to refactor each directory
Lets address the two use cases
**Rename a package name or Trim a package **
com.mycompany.mystupidapplicationname to com.mycompany.brandname
or
com.someothercompany.mystupidapplicationname to com.someothercompany.mystupidapplicationname
or
com.someothercompany.mystupidapplicationname to
someothercompany.brandname
Select Package in the drop down in Android Studio
Right click on the package which contains your MainActivity and refractor it
Select Rename Package and everything will work like a charm
But this will only rename the last part of package name
To rename the first or second part of package name . Create a new dummy file in the part of package which you want to rename. the file will be created inside mycompany.com.myapplicationame. Then move the file inside mycompany(right click => move) to make mycompany appear in tree view. This will make that part part of the tree view and will you help you to do a right click and rename that part.
Delete the dummy file
To Trim a package , simply right click and move the package part inside its grand parent
I found a good work around for this problem. Taking the example mentioned in the question, following are the steps for changing the package name from com.example.test to com.example2.test :
create a temporary directory, say temp inside the directory example (alongside directory test).
Go back to the Intellij project, select the folder com.example and Click Shift + F6. Now it offers to rename the text example. Here you can enter the new text you want and IntelliJ will do the rest for you.
Delete the temporary directory temp from the file system.
This idea can be used to refactor any part of the package name.
Good luck!!
This modification needs three steps :
Change the package name in the manifest
Refactor the name of your package with right click -> refactor -> rename in the tree view, then Android studio will display a window, select "rename package"
Change manually the application Id in the build.gradle file :
android / defaultconfig / application ID
Then clean / rebuild the project
Unfortunately all above didn't work for me. After having lots of trials,
What worked for me in Android Studio:
do a 'move' on the package to a new package name you want.(right
click on package and select Refactor -> Move)
If Refactor -> Move didn't work for you, then create a package with the name you want and move manually in the existing package to the new one, and then delete the old empty package.
Change package name in manifest (manually)
Have a replace for the old package name with the new package name
globally (in the full path by going to Edit -> Find -> Replace In
Path)
What I did was the following :
I simply created the package with the desired name , in the src folder , next to the current package with current name.
I dragged all contents of current package to new package , intellij popped a dialog box asking me if I want to refactor inside package references and project references to new package , I clicked 'yes' and TA-DAAA , worked like a charm.
In Android Studio 1.1, the simplest way is to open your project manifest file, then point to each part of the package name that you want to change it and press
SHIFT + F6 , then choose rename package and write the new name in the dialog box. That's all.
Most of the answers even the most voted answers didn't do the job properly, they seem to work and the builds work however, a closer look at the file structure and references will show you that not much was done. IntelliJ actually does this whole process automatically.
1) Go to Project Tab and make sure Packages is the active Tab in the drop down like so:
find your com.example package (note test as in com.example.test is not there) right-click and choose Refactor->Rename...
It will issue a warning that multiple directories... (which is actually the desired effect), click on Rename package.
Input the new package name and Refactor...
It will do searches and show you refactoring results.
Click on the Do Refactor button and it will do everything and even rename the folders and files involved... every reference is corrected to this new name.
2) Then Finally change the AndroidManifest.xml file
:)
Most of the answers even the most voted answers didn't do the job properly, they seem to work and the builds work however, a closer look at the file structure and references will show you that not much was done. IntelliJ actually does this whole process automatically.
I solved this issue by changing the directory name manually from the command line. Intellij then recognized the new package name automatically. I then had to do a search and replace for the package name in each file that imported it. This seems like an ugly workaround, but Intellij seemed unwilling to change the package name otherwise.
What I did was the following
Change manifest.
Close androidStudio
Open folder with Windows Explorer. Change folder names.
Open androidStudio, and do a search and replace.
Done!
If you want to rename full android package, this is the best way to do that:
Mark as checked - Comapct Emty Middle Packages
Right click on the packcage you want to change, and then
Refactor->Rename->Rename all
You can find video tutorial on this link:
https://www.youtube.com/watch?v=A-rITYZQj0A
In addition to #Cristian's answer above, I had to do two more steps to get it working correctly. I will sum all of it here:
Change the package name manually in the manifest file.
Click on your R.java class and the press F6 (Refactor->Move...). It will allow you to move the class to another package, and all references to that class will be updated.
Change manually the application Id in the build.gradle file : android / defaultconfig / application ID [source].
Create a new package with the desired name by right clicking on the java folder -> new -> package and select and drag all your classes to the new package. Android Studio will refactor the package name everywhere [source].
Solution 1: I changed src subfolder names, manifest file, activity headers and run/debug configuration. And still got building errors. If your project is not so big, just create a new project, copy-paste old files and change package names in headers of .java files and android manifest.
Solution 2: Inside IntelliJ 12, goto someFile.java and find your old package name
package com.myself.project2
click to which word that you want to change. Press Shift+F6.
Dialog comes, type new name.
Then goto Run/Edit configurations. Inside Launch radio button change package name to new one. It should work.
Other renaming options of IntelliJ are: http://www.jetbrains.com/idea/webhelp/rename-refactorings.html
Super simple approach:
Find R.java in the build/generated directory
Create new package directories, again under build/generated/etc
Move R.java to the newly created package.
A dialog will appear. Choose to refactor the existing code with the new package name.
Clean and Rebuild
Done!

How to change package name of Android Project in Eclipse?

I have an Android project created in Eclipse. I want to modify the package name and application of the project. How do I do that in Eclipse?
This is a bug in the Eclipse Android tools.
To fix: Right click on the project, go to Android tools -> Rename application package.
And also check AndroidManifest.xml if it updated correctly. In my case it didn't, and that should solve this problem.
As usual, by pressing F2 on the package name, you can rename or change the package name, and also by right-clicking and then select Rename option, you can change or rename the package name.
When you press F2, it will show you the dialog box as:
In this dialog, don't forget to check the "Update references" checkbox because by making "check" to this check-box, it will make changes to all the references of the package which are referred by other components of project.
None of these worked for me, they all introduced errors.
The following worked for me:
Right click the project and select Android Tools >> Rename
Application Package.
Enter the new Package name
Accept all the automatic changes it wants to make
Say yes to update the launch configuration
One extremely important notice:
NEVER use a direct package names as in something similar to passing a string value containing the package name. Use the method getPackageName(). This will make the renaming dynamic. Do whatever to reach the method getPackageName().
In Eclipse Juno, the correct way of renaming is:
Go and edit the manifest.
Remove every old package name in the manifest.
Put instead of the old package name, the new package name in every location inside the manifest. You might have classes (Activities that is) that need direct package name references.
Save the manifest.
Then right click the package name inside the project.
Select "Refactor".
Select "Rename".
Type the new package name.
Select "update references".
Press OK and you're done and watch out also what should be done to replace the new name.
Don't forget to also update the layout XML files with the new package name. You might have a custom View. Look for them.
Right click on your project name and select Android Tools , then click
Rename Application Package, then change your project name click OK and
click Finish.
To change the app name when in Android, go to res/values/strings.xml and change app_name to what you want.
Just right click on the project name and select Android tools and click Rename Application Package name and rename it. It's only for Android projects.
Press F2 and then rename it (or right click on the project and select Rename). It changes wherever that package is declared in your project. It's automatically renamed all over.
Goto the Src folder of your Android project, and open the Java file.
Change the package OldName.android.widget to newName.android.widget.
It gives you an error like this
The declared package "newName.android.widget" does not match the expected package "OLDName.android.widget.
To fix this problem, select Move filename.Java to Newname.android.widget and delete the old package folder.
Next step: Go to AndroidManifest.xml and change
package="OldName.android.widget" to package="newName.android.widget".
This worked perfectly for me (simple text replace):
Go to search -> file
Containing text: old pakage name
File name pattern: *
Scope: workspace
Press Replace
Replace: new package name
Now your project is full of errors.
Click your package in project explorer and press F2 (rename).
Fill in the new package name and check all checkmarks.
Press Preview.
Click OK.
Wait for Eclipse to refresh. If there are still errors, clean all projects in the workspace:
Project -> Clean
None of this worked for me until I combined the two answers mentioned in this post.
Step 1: Right click on the project -> Select Android Tools -> Rename application Package.
(This will change all the files in the gen folder and in AndroidManifest but will not change the package name in the src folder so I followed Step 2)
Step 2: Inside src folder Right Click your package name -> Refactor -> Rename -> Enter the new name that you entered in Step 1.
Just to make sure Check AndroidManifest if there is still the old package name and replace with the new one (in my case inside the "uses-permission" tag).
Then close the Eclipse and Reopen it. Uninstall the app from your device and install it again and everything should be working fine.
Hope this helps and saves your time.
OK here's what I had to do:
Problem:
I had a base project that I wanted to use as a starting point for several other projects that would be in the same workspace as the base project. The base project was already under GiT control in my own local repo. It was time to duplicate the base project, "MyBaseApp", to become the starting point for "MyKillerApp."
Solution:
I just duplicated the overall MyBaseApp directory as MyKillerApp and had the two directories side-by-side.
I edited file MyKillerApp->.project and changed the name from MyBaseApp to MyKillerApp. (This was necessary for step 3 to work.)
I opened Eclipse and did a File->Import of MyKillerApp (I left out some details about how to use the Import menu, but click around, you'll figure it out.)
I right clicked on MyKillerApp in the package manager and chose Android Tools->Rename Application Package to MyKillerApp. I checked all the options for renaming except the last checkbox about non-java files.
I highlighted src->com.myname.MyBaseApp in the package manager and did an F2 to MyKillerApp. Then I did the same to a few classes including my opening activity class thing that goes into your AndroidManifest.xml file.
I had to update a few function call names but the compiler pointed them out to me handily.
Thus I had a new project derived from a base project with no nomenclature left over from the base project and it was in its own local GiT repo.
Following worked for me in eclipse:
Go to AndroidManifest, search and replace old package name with new one and update everything when saving. Go to your root project package then press F2, write new name, and check Update References and Rename subpackages check boxes. After this everything was red in project (complaining about R import for every java file) because it wasn't changing closing tag of my custom view-s in layout xmls. After I changed these manually, everything was back to normal. That is it.
Renaming an Application- The Complete Guide
**A) for changing Just the application name
(App name which is displayed below icon)
in the Manifest.xml file, in <application tag,
android:label="YourAppName"
then do the same in All the <activity Tags
B) For changing EVERYTHING
(folder names, Package names, Refrences,app name, etc.)
*1) Renaming package names in gen folder and manifest.xml
Right Click on Your project
Android tools- Rename Application Package
*2) Renaming package names in src folder
Expand src folder, Click on package (Single click)
Press Alt+Shift+R
Check Update references and Rename subpackages
3) Renaming the app's main Folder (Optional)
click on the application's folder (Single click)
then Press Alt+Shift+R
4) Renaming application name- Refer "A)"**
Just get Far Manager and search through for the old name. Then manually (in Far Manager) replace everywhere. Sadly, this is the only method that works in 100% of the possible cases.
I had big trouble with this too.
I renamed the project and the package via refactoring, but it rendered the app useless. I renamed it back to the original and used the Android tools to rename the package and this worked, but my package folder and classes had the old name.
I then refactored the package folder. Now the app wouldn't run. My manifest was a mess. It doubled up my launcher name for example, com.ronguilmet.app/com.ronguilmet.com.class. I fixed that, and I had to manually edit all activities.

Categories

Resources