I copied a .java class and .xml layout. I thought I renamed everything in order to create a new class, but the new class is loading the old layout when I start a new Intent. Am I missing someplace where I need to enter the name of the new layout file I have created from a copy of another class' layout?
I think I missed this line:
setContentView(R.layout.xxx)
This should do it!
If you have not set the launcher activity in manifest then you have to specify which Activity starts first
If you have set old XMl in setContentView(R.layout.xxx) then this loads old layout so check for proper xml.
After trying the other answers, try checking the following attribute on your layout element:
tools:context=".SignUpActivity">
That was it for me.
Related
I need to use the Android Camera.
Therefore I am using following Template: https://github.com/android/camera-samples/tree/main/Camera2SlowMotion
I added a Butten to the fragment_camera.xml
https://github.com/android/camera-samples/blob/main/Camera2SlowMotion/app/src/main/res/layout/fragment_camera.xml
<Button
android:id="#+id/change_focus_button"
android:layout_width="115dp"
android:layout_height="81dp"
android:text="#string/focusButtonText"
tools:layout_editor_absoluteX="13dp"
tools:layout_editor_absoluteY="473dp" />
In the CameraFragment.kt I now want to call a function when the button is clicked.
In my understanding I therefore need to create a setOnClickListener.
https://github.com/android/camera-samples/blob/main/Camera2SlowMotion/app/src/main/java/com/example/android/camera2/slowmo/fragments/CameraFragment.kt
I thought about adding following line in Line 309. that would call a function i then create. But the "." after fragmentCameraBinding.changeFocusButton is always red and won't go away.
fragmentCameraBinding.changeFocusButton.setOnClickListener { chnageFocuslen() }
What do I need to change / where do I need to add the OnClickListener?
Thank you very much in advance!
I had to download the sample because I couldn't see what the issue was from the snippets in your post.
The issue is that CameraFragment has two layout files, one in the default folder layout, and one in layout-land for landscape orientation. So you need to add your change_focus_button XML to both layout files to make it not nullable when you access it via fragmentCameraBinding in CameraFragment.
Recently I came across the use-case of using tools:context in my layout files for activities.
I learned that I can use it to associate an Activity class file to my layout file.
But how can I use it to associate a Fragment class file to any layout?
Bacuase as I have tried, it is only showing Activity list in autocomplete suggestion.
There is a bug in Android Studio, you have to write it manually, it will appear red(indicating it's unresolved) until you finish typing, then it will appear in green
Have you tried it doing like this:
Let's say Relative layout is your parent layout in your fragment's layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Folder.Fragment" >
Now this .xml file will be associated with your .Fragment class which is a fragment.
Key point: Autocomplete is not showing name, manually enter it and it will compile.
Create a fragment with a layout file and see the layout file for tools:context.
I just had the same problem and typing in the fragment name didn't get the fragment name recognized in the tools:context=".NameFragment" statement.
What did work was first deleting both the class and xml files for that fragment and then:
Right click on the package name and choose New->Fragment->Fragment (Blank)
Enter in your fragment name (the layout name is automatically created on the same form) and click Finish.
This method created both the class and xml files at the same time. and at this point, the tools:context statement in my xml file had my fragment name automatically and was not red but green text (indicating that it was recognized). I'm seeing this on Android Studio Bumblebee 2021.1.1.
Earlier I had created the xml file first and then created the class file (and had problems with the fragment class file name not being recognized by the tools:context statement).
This question is about Android development. In general, how do you tie a layout XML file to a certain Activity java file? This is relevant in a case such as a button with the android:onClick attribute specified. Suppose you have a layout XML file with a button like the following:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage" />
So when this button is clicked, a sendMessage(View view) method is called. If more than one Activity has a sendMessage(View view) method, how does Android decide which one to call? There's literally absolutely nothing associating layout files with Activity files. In Android Studio, I've found that the android:onClick="sendMessage" defaults to the main activity's sendMessage method if there is more than one choice. I've also found that Android Studio doesn't let you simply specify the full method path (for example, android:onClick="com.example.Activity2.sendMessage"). Am I missing something?
Edit: I would like to add that Android Studio shows the sendMessage defaulting to a specific activity, REGARDLESS of which Activities have specified the layout via setContentView()
The android:onClick will call the method on the Activity your layout is inflated and attached to. There should be no tight coupling between a layout and it's controller, so thats why you can't specify a "full path".
I'd recommend using a programatical OnClickListener instead of the xml attribute. It gives you much greater control.
There is a tools:context attribute that you can use to tell Android Studio in what context the layout is meant to be used. You specify a class there and then it should take this class to find the correct method
But this attribute is only for the IDE. During Runtime it binds to the method of the activity that inflated the layout.
Of course it will call the method from the activity class of that view. So simply it will call from main activity class if that view is from main activity and if the view is from , for example Activity2, then it will look for the sendMessage method in the Activity2. If it failed to find the sendMessage method in the Activity2 class then it will throw a NullpointerException.
I have just begun learning Android App development. I have Android Studio 1.4. In my layout folder I have two XML files (content_main.xml and activity_main.xml). I use online tutorials to learn but they have only activity_main.xml in them. So what i want to know is what are the functions that should be used in these respective files. can i just use activity_main.xml and just let the other be ? and vice versa.
The modern Android approach is based on Fragments, which are, in a way, "small Activities", which you can put in Activities, gaining lots of flexibility.
Therefore, activity_main.xml is simply the Activity layout containing a container (FrameLayout most probably) and content_main.xml is the layout for a Fragment put into this container somewhere within MainActivity.java. You should study the code in there to understand it better :)
As I know, there must be include statement in your activity_main.xml file as follows :
<include layout="#layout/content_main" />
that means it is calling the content_main.xml which has actual elements to be hold.
There will be no problem if you cut and paste all the content of content_main.xml file and paste it in activity_main.xml file in place of include statement(tag).
You can delete your content_main.xml after doing as above.
In your activity setContentView() statement should be look like as below :
setContentView(R.layout.activity_main);
According to new design pattern in android studio activity_main.xml will determine how the look of the main activity should be. And on the other hand content_main.xml will determine the contents in the activity_main.xml. That is content_main.xml will contain the textview, edittext, button etc component. And the content_main.xml will be included by the activity_main.xml .
So we can think of content_main.xml just like partial in HTML.
activity_main.xml will contain your activity global design, and
content_main.xml the contents.
What is the role of content_main.xml in android studio 1.4?
So it seems the content_main.xml is a part of a new design pattern introduced in Android Studio 1.4.
For the moment, to get along with the tutorials you can find pick the 'empty activity' when creating a new project. It will not contain the content_main.xml.
As mentioned before, the layout file used for your activity is set with setContentView(R.layout.activity_main); in the onCreate function of the activity.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
I have not seen Android Studio creating two layout files for one activity. Perhaps the content_main.xml was generated for a previous activity, wasn't it?
Anyway, it doesn't matter what is the name the layout file. Choose one and go for it. Just remember to set the right one in your Activity:
#Override
protected void onCreate(Bundle savedInstanceState){
setContentView(R.layout.your_layout_here);
}
use that one which is set in Activity class. i mean set to setContentView(). or please provide your code if you want more description.
The issue:
Am now Make some Re-skinning on exciting code and am following the documentation attached with the purchased code. Unfortunately, the documentation doesn't containing the ''Incorporation Google Mobile Ads into app''
I try to follow the instructions in developers Google Website.
to this point all the things are OK but when I came to Add an AD banner especially in the :
Create your banner in XML Section
They say Define a com.google.android.gms.ads.AdView in res/layout/main.xml.
But i found that my code not containing a main.xml.
Screenshot:
http://www.shy22.com/upfilpng/o2h28564.png
I try to create a new xml file and call it Main.xml automatically another file display to me here is:
http://www.shy22.com/upfilpng/dus28564.png
Any help plz
Try creating an xml file in your layout folder called "main.xml". This should have NO capital letters.
Also, if you're in Eclipse, you can try Refactor->Move, to put it in the correct location.
The AdView can be added to any layout - it doesn't have to be named "main.xml". You just put their AdView xml inside any layout where you want an ad to appear.