Add a fragment without xml file in Android studio chipmunk - android-studio

I want to add a fragment to a project I downloaded without the XML file as the XML file I'm gonna use is already provided, except that in previous versions of android studio you could uncheck the create layout file checkbox
in the latest version, it doesn't give you this option and if I create a layout file and then delete it, it gives me an error, how to fix this, please?

You could create a simple class that extends from Fragment, using your existing layout file to it:
public class YourFragment extends Fragment {
public YourFragment() {
super(R.layout.your_existant_layout);
}
}

Related

Import image Android Sudio

I got a school project where i need to make a cooking app, I struggle with programmaticaly importing an image to get a preview of the dish in my class.
The images are located in the drawable-v24 folder but i can't figure out ou to get the right path to it.
I want it to be a Drawable to create an image view in my layout.
package com.example.projet_info0306;
import android.graphics.drawable.Drawable;
public class Recette {
private String nom;
private Etape[] steps;
private Drawable Demo;
public Recette(String n,Etape[] e,String PathImg,) {
nom=n;
steps=e;
Demo= Drawable.createFromPath("#drawable/"+PathImg+".png");
}
}
If you want to show the Image in your App, just go to your activity_main.xml, add an ImageView to the layout and give it an ID.
You can now either link up the image statically in the XML by unsing the android:src tag or you can do it programatically by using:
Imageview imageview;
imageview = findViewByID(R.id.[yourID]);
imageview.setDrawable(R.drawable.[yourDrawable]);
NOTE: The code snippet you showed is in a normal Class not in an Activity. If you want to add one just go to your projects folder and right click on App. Android Studio will then lets you create an Activity. Or if your are not that far in the process, just create a new project and add an Activty from the beginning.

Android Studio with Kotlin How Does Control Pass From MainActivity to Another Class

I'm using android studio 4.1.1 and kotlin.
I'm trying to learn programming Android apps using Android Studio and Kotlin. I have a question that a couple of tutorials and searching threads have not answered for me. How does control pass from the MainActivity.kt class to another class, i.e: FirstFragment.kt?
I'm doing fine with other things I'm learning in tutorials, but I cannot figure this out.
Example:
I create a new Basic Activity app in Android Studio with Kotlin. In the java directory, I see these classes: FirstFragment.kt, MainActivity.kt and SecondFragment.kt.
In the res/layout/ directory, I see: activity_main.xml, content_main.xml, fragment_first.xml and fragment_second.xml.
Question 1) When the app loads in an emulator, I see the button and textView from the fragment_first.xml. How does this happen? I cannot see in the code where it says, "load fragment_first.xml".
MainActivity.kt has setContentView(R.layout.activity_main) which loads that layout. activity_main.xml has <include layout="#layout/content_main" />. But from there, I do not know where to find the code that loads the fragment_first.xml layout.
I believe that when fragment_first.xml loads, control passes from MainActivity.kt to FirstFragment.kt, yes? Question 2). Is this because of the onCreate function?
Thanks for helping me to understand this better.
There are multiple ways to load a fragment. Traditionally, you use the FragmentManager to create a Fragment transaction that tells the Activity to place a Fragment into the view hierarchy and start managing its lifecycle.
More recently, they have added the Navigation component, which can be used in place of directly using the FragmentManager.
In the current new project Basic Activity, the layout activity_main.xml includes the layout content_main. In content_main.xml, you'll see that it includes a fragment element that is set to the Fragment (using the name parameter) NavHostFragment, which is a special Fragment that the Navigation component uses to host children fragments that are specified in a navigation XML file. This fragment element also specifies the navigation xml file using the navGraph property. It specifies nav_graph.xml, which you'll find in the res/navigation directory.
If you open nav_graph.xml, you'll see that it has FirstFragment set as the home fragment, so that is what pulls it up as the first fragment that you see.
Basically, the control flow is:
Activity's onCreate() sets content view to activity_main, which includes content_main.
content_main includes a NavHostFragment that is automatically inserted in the view hierarchy so the Activity starts running it.
The NavHostFragment is set in the XML to use nav_graph, which tells it to load FirstFragment first.

Can't find the Blank activity with fragment in android studio (3.3.2)

So I just started with Android studio and i am following the Android NanoDegree from Udacity and in one of the lessons i am supposed to create a Blank activity with fragment which does not exist any more i understand that the Empty activity is the same as the Blank activity i have searched and all i could find was old questions and the answer was to choose an empty activity then check the add fragment option but i can't find the fragment option at all (See Image).
Can any one tell me what i should do to match the required activity from the lesson ... it has MainActivity.java and fragment_main.xml and activitymain.xml and a place holder fragment in the mainactivity.java file
In Android Studio, if you right click on your project in the project view, under New -> Fragment -> Fragment (blank).
I tried to add a screenshot but I'm not allowed yet.

Android studio showing decompiled class not java class

My KeyboardView was opening fine and whenever I click on this extended class it was showing .java class but I tried to edit this class and there were options to edit for this session. After that whenever I click on this KeyboardView extended class it's not showing java class it's only showing .class. How can I resolve this problem?
EDIT
there were these option and i selected the first one
after that this happened
Solved this problem by installing new sdk

How do I add a .java file using Android Studio?

(Noob alert!)
I want to add an SQLite component to a simple (one Java source file) app and would like to put it in a separate file. I was hoping to find something that would let me click a couple check boxes and creae the file using a template with the standard overrides and class declarations (e.g. ... MyClass extends SQLiteOpenHelper {...} and so on. Instead I can't even figure out how to add an empty .java file to the project. Google's Android Studio Tips 'n Tricks suggest navigating to the 'appropriate directory in the Project pane' and hit N. That gets me a dialogue that rejects my class name and seems to open a header file if I enter the name if the class I wish to extend (and without apparently adding anything to my project.)
I do not even see a way to add an existing file to the project. OK... I now see that if I create the .java file in the app directory (along side the MainActivity.java file) that Android Studio automatically includes it.
Is this Standard Operating Procedure? It leaves me feeling like I'm not leveraging the capabilities that Android Studio provides.
I'm using AS 0.5.2, openJDK 1.7.0
Thanks!`
If you're creating a new Java class from the Project pane, you don't need to add the ".java" to the name; you're specifying the name of the Java class, not the source file. It will figure out the filename automatically.

Resources