Change behavior of "Add new Android Components" on Android Studio - android

I am having a problem with my build.gradle file every time that I insert a new Fragment from AndroidStudio. When I use the tool that AndroidStudio provides to insert a new component such as fragments or activities, it creates the fragment class .java and the associated layout, but it also modifies my build.gradle, for example, my dependencies are reordered but the build.gradle file is broken after that because the order is not done properly. I attach an image with the example.
Why is it behavior happening and how could I prevent it?
I am using AndroidStudio v2.1.1 stable channel

Related

SafeArgs are not generating the needed actionclass

I am building a basic app. When i press a button i want to navigate to a new fragment and that fragment should have a number of items in it.
According to the documentation, Gradle should generate an inner class in my MainFragmentDirections.class, which should represent the various actions this destination has, but Gradle does not do that. Everything else works fine, so i have implemented the dependencies and plugins corretly
Try to Clean Project then Rebuild Project. I have sometimes the same problem when I add SafeArgs, this tips works for me

Using another project as a library

still learning android and this is where i am stuck.
I want to use this project as a tutorial for my app
https://github.com/PaoloRotolo/AppIntro
It has a wiki, and it says i need to add gradle dependency which i'm pretty sure i'll handle properly.
Whats bothering me is how do i extend that app to my class? where do i put that project/library inside my project?
Sorry if this question is newb-ish, i tried alot of things and they didn't work.
Thank you for your help!
Since you said you'd handle adding the gradle dependence I believe you are asking how to implement an Intro screen? Did you read the How to use part on the home page of the library?
Everything is explained there. What to extend and what each method does. For the actual creation of each fragment I will give you a hint.
After creating a new class for your Intro screen, if you don't have a specific layout for your fragment just use that part of the code:
addSlide(AppIntroFragment.newInstance(title, description, image, background_colour));
where the parameters are variables that you choose. For image you could use any of your images inside the drawable folder like R.drawable.intro_ico and for background colour - ContextCompat.getColor(this, R.color.grey)
'grey' color has to be created in your colors.xml or choose anything you like.
EDIT: And if you were wondering whether you should download something and where, no you are all done only by adding these lines of code to your build.gradle. When you add them, Android Studio asks to Sync the gradle file, and then it downloads the library.
For some libraries, you'd need to download a jar file or git clone from a repository and then maybe add the downloaded folder as a Module to your project by going Android Studio>New>Import Module>locate the folder . However, for that one, adding the dependency is enough as it gets downloaded after the Sync.
it's really very simple!
you just add these lines :
repositories {
mavenCentral()
}
dependencies {
compile 'com.github.paolorotolo:appintro:3.4.0'
}
in your build.gradle file under app folder.

Two activities are created when I try to create a new Blank Activity in Android Studio

Whenever I try to create a new blank activity in the layout folder two layouts are created.For eg:When I try to create a Blank Activity by name lola following two activities are created i.e. activity_lolla.xml and content_lolla.xml:
Not two activities, but two layout files... U can use "Empty Activity" template to avoid that. In this case you also would add android:label="#string/title_of_your_new_activity" in the manifest file ander activity node
I am going to make an assumption that you are either:
A)Just beginning to learn Android from tutorials that uses older version of Android Studio
B)Recently upgraded to new Android Studio 1.4
If you look at activity_lola.xml, you should see in middle, there should be a line of code like this:
<include layout="#layout/content_lola" />
Both of these are layout files but only activity_lola is considered an activity. The content_lola is layout that is included inside the activity_lola.
So basically if you belong to assumption A, you can get the "Blank Activity" that books and other tutorials are referring to by selecting "Empty Activity".
Hope this helps and happy coding! =P

Creating Activity | Android Studio ? [Some concerns]

Till Eclipse, creating a second activity and class had a brevity . However, with the new official Android studio , I feel more of an abstraction . Earlier, while creating an activity it specified many things, nevertheless asked the layout of an XML file. And here are my doubts:
1) Is there an option to create Java file in Android Studio just like we use to do with Eclipse ?
2) Which is the best way to create an activity in Android Studio with our specified layout?
1) You can go into the project directory and right click the folder where you want the activity to be (typically in src/java/com.xxx.xxx/) and select New -> JavaClass and create YourActivity and make sure that YourActivity extends Activity. Be sure to add the activity to your manifest!
2) There is no best way as it is a matter of preference. Personally I do what I said in step 1 and create the xml layout file myself in the res folder. I find that Android Studios auto activity creation to be a bit annoying as they add menu xml files that I do not need.

Issues in referencing new elements in an existing Android application

I have an existing android app. I wanted to change its menu structure without disturbing the existing code. For this I added 2 new java files and relative xmls in the existing project and updated the manifest.xml with the new starter activity.
The issue which I am facing is in the java code. I am not able to reference the elements of the new xmls xxx.findViewById(R.id.xxx)
The error coming up: cannot be resolved or is not a valid field
Please suggest something, I am a newbee
When you build an application, Android will create a file called "R.java". This file basically is an index to all resources in your project (strings, layouts, drawables, etc.). If you add new resources by hand, you cannot refer to these resources because they have no ID in the R class.
According to this page you can use the aapt tool to (re?)generate the R file, but I have no experience with this myself.

Categories

Resources