Can't add any items to activity_main.xml in Android Studio - android

In Android Studio, I can't add any items from Palette to activity_main.xml in Design view. It just won't let me drag and drop them. Any idea why this happens?
Here is the PrintScreen:

According to new design method followed in android studio for android development you cannot add any elements to the activity_main.xml. Rather you should add them to content_main.xml.
You can learn more about it from this answer.

You can stop it from happening.
Answered by BNK:
If you create a new project, you can choose Empty Activity template
instead of Blank Activity. If you create a new activity, you can
choose Empty Activity instead of Blank Activity too.
how do I create an Empty Activity with a Fragment?
You start by creating an activity using the "Empty Activity" template.
Then, add a fragment class yourself or possibly via New > Fragment.
Please understand that all of the "New >" options, for activities,
fragments, etc., are all driven by templates. Those templates do what
they do. If you do not want what they are generating, don't use them,
and add the classes, resources, manifest entries, and such yourself.
Personally, I almost never use those templates, because it's simpler
IMHO just to create it all yourself than to rip out all the stuff you
don't need that gets generated.

I had same problem. In the design page, I saw error message "Render Error..."
I click Android Studio->check for update->update and restart
After android studio update, the drag&drop from Palette to design page works.

Check ConstraintLayout properties:
layout width and layout height should be "match parent", not "wrap content".
It works for me.

While you have marked a witget the editor don't let you drag.

Related

Problem migrating to Jetpack Compose. Trying to run the app from within Android Studio generates a wierd error

I've an application that uses Fragmnent Views with XML resources and I want to migrate it to Compose. Being the app quite big, I decided to temporarily have a mixed environment with Compose working together with the old XML resources, so I created a new Compose fragment and I succeeded to add the standard navigation action to navigate to it. The code builds successfully, but when I try to run the app I get the error of the picture below. It is particularly wierd because this error appears immediately, and not in the build panel, but in a popup dialog. Any hint ?
Ok, the documentation is not very clear, but in the end I found this:
"You can also include a ComposeView directly in a fragment if your full screen is built with Compose, which lets you avoid using an XML layout file entirely."
Being my app a hybrid one with XML navigation graphs, it looks like I need to define an XML resource anyway.
I hoped I cound avoid defining an XML layout, but it seems I need one for a fragment that has an old fashioned Activity as parent.
I'll post an update as soon as I discover something new
UPDATE
Adding an XML layout didn't fix the problem.
In fact the XML layout is not necessary. I don't really understand the cause of that error, but after cleaning a couple of times the project and clearing A.S. cache it disappeared. I succeeded to add a Compose Fragment to an old XML resources style app. The old navigation works without problems. Hope this post will help someone else save the time I have wasted...

Previewing the layout in android studio

I am new to android development. Also new in using android studio.
And when I started to create a new project, the content_main.xml and does look like this
But not to other xml
And here is the output
How can I even edit the layout, when I can't see it. Also when i drop anything from the palette, I cannot see it.
Try to change the android version to some lower in xml.
Change from 28
Do not use content main. It is for including inside activity_main.xml. Make everything inside your main layout and finnaly you can see the result.
And don't forget that ConstraintLayout needs constraints for all elements.
First of all read about other layouts and learn how to use them.

When i create New android project using eclipse it create two xml layout

My old eclipse version worked perfectly but after updating eclipse, when I create a new Android project, it creates two XML layouts. 1st is activity_main.xml and second is fragment.xml. So when i add some item in activity_main.xml it wont display anything.
So how to use fragment layout?
This is the new Project Structure for Android by developers.
If you want to view any UI widgets put them inside fragment.xml file.
And any java code should put inside PlacementHolder class that is inner class of Main.java.
As others mentioned that's just the recent new project style from Android Team. But I guess you were also looking for an introduction into Fragments when you asked:
So how to use fragment layout?
Basically:
Android team has completely covered Fragments in their documentation. Start from here and then refer here. Then download this official source code and examine it closely.
Now I'm gonna share a personal experience:
I suggest you first study this tutorial on fragments from Vogella's website.
Why? Because the Android documentation is slow and rather boring there fore hard to understand, whereas the Vogella version is to-the-point and sharp.
You can then go back and study Android documentation.
at least read this: http://developer.android.com/guide/components/fragments.html it will give you an idea how the fragments work. And better switch now to Android Studio, the sooner the better.

setting my main project default to linear instead of relative

Everytime I follow a tutorial book or you tube video, I have also used the cheat help in eclipse. My programming always comes out with errors. here is one example why does all the tutorials main activity comes out showing linear layout and my projects default is relative layout. So how do I change the default to linear when creating new project.
I know I can open a new layout xml but all the tutorials opens a new project in linear.
I am a massive fan of android and I really want to make some apps, but all the tutorials never seem to correspond with the results I get and they all make it look so easy.
go to youre layout.xml
make sure to be in the graphical layout (check bottom bar)
top-right in 'outline' right-click on the relative layout
choose 'change layout'
choose youre desired layout, (linearlayout(vertical) probably in ur case)
I found it after digging a lot in Android Studio After visiting JetBrains and Intelij documentation and help sections i found nothing much helpful but i found way on my own, I had answered it here, With all explanation and screenshots.

Android Graphical Layout Editor in eclipse can't see my custom class

I have created a new class that extends the RelativeLayout and when viewing this using the Grahical Layout editor, I always see this error:
The following classes could not be found:
- eg.package.StripedBackgroundLayout
Has anyone experience this before and how to resolve it please?
I'm using ADT 12.0.0
Yes, you currently cannot resolve this. The Graphical Layout Editor is not equipped to handle custom classes at the moment. It can be achieved with some hefty modification, but it isn't worth the time. It will be included in later Graphical Layout editor updates.
Another funny thing about eclipse is that, when I we open the graphical layout view for the first time it shows your custom view's when you clear the project.. till any modification in layout, then you just need to clear it again.

Categories

Resources