• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Android Studio constraint issues

I am using Android Studio 3.1.2 and am having a few problems L. I am trying to create a “hello world” with a button - pretty simple eh! (it’s a fresh Android Studio install).

1) When I tried to create a button, the button didn’t appear on the screen. Following a video on YouTube I changed the Gradle Script->build.gradle(Modmule: App)-> implementation 'com.android.support:appcompat-v7:28.0.0-alpha1ule'

This allowed me to at least place the button on the page.

2) When I run the emulator the buttons are not in the place where I placed them (they are in the top left of the screen) and I get a warnings “Missed Constraints in ConstraintLayout…”?

Any ideas please? As this was pretty much “vanilla” (clean install, simple button), so why am I hitting problems with something so simple please?

I would really appreciate help!

Cheers.
 
In order to help you we need to see the XML layout file being used.
Post this using [code][/code] tags.
As previously stated in your similar thread here https://androidforums.com/threads/getting-started-with-android-studio.1302691/ to which you didn't respond, Constraint layout is an extremely flexible layout manager, but is quite complicated for beginners to understand. You would be advised to start with a simple LinearLayout for a basic application.
But as I said, difficult to advise, without seeing the layout file you're using.
 
In order to help you we need to see the XML layout file being used.
Post this using [code][/code] tags.
As previously stated in your similar thread here https://androidforums.com/threads/getting-started-with-android-studio.1302691/ to which you didn't respond, Constraint layout is an extremely flexible layout manager, but is quite complicated for beginners to understand. You would be advised to start with a simple LinearLayout for a basic application.
But as I said, difficult to advise, without seeing the layout file you're using.

Thanks.

I haven't put any code in, it's just a button! Is there any thing else that I can provide please?

Cheers
 
Make sure you click the "Infer Constraints" button once you done adjusting the layout.

S8eERtB.png
 
Last edited:
[
Thanks.

I haven't put any code in, it's just a button! Is there any thing else that I can provide please?

Cheers

I didn't say "code", I said the XML layout file. If you don't understand what this is, pick up a good book on Android application development. It's not a great idea to just dive in to development, with no knowledge of the basics.
Are you following a tutorial, or book? Do you know Java? If not then you need to first learn this programming language.

And you may not have written any code, but Android Studio has. When you create a basic project template, there is boiler plate code generated automatically for you. When you drag components around in the UI designer, Android Studio is changing things in the XML file behind it.
 
[


I didn't say "code", I said the XML layout file. If you don't understand what this is, pick up a good book on Android application development. It's not a great idea to just dive in to development, with no knowledge of the basics.
Are you following a tutorial, or book? Do you know Java? If not then you need to first learn this programming language.

And you may not have written any code, but Android Studio has. When you create a basic project template, there is boiler plate code generated automatically for you. When you drag components around in the UI designer, Android Studio is changing things in the XML file behind it.
Hi, I started coding back in the 1980s, and worked in IT until a few years ago when I had a stroke. Most of the Android stuff isn’t a problem (including XML) – but I am starting out in Android.

The problem…I think…happens before I even have some fun doing some code J or anything eles: when I create a new project and then I try to place a new button, but nothing happens L. I’ve followed something on YouTube to get me through to the next step, but I hit more issues.

I’ve followed quite few tutorials, and there don’t see any problems on them compared to what I am getting – but I may be missing something?

I am slow (thanks to the stroke) and some of the words on these posts may not be brilliant (thanks to Aphasia), but I must be missing something, or is there something like an issue with my laptop (BIOS?)?

I really appreciate the help.
 
I've been doing some experimentation with ConstraintLayout. It's not something I've used before, but I can see the benefits. The main one being that you can drag components around in the UI editor, and pretty much place them where you want. That's very cool.

What I did was start with the basic UI layout, and add a Button. You can see from the first screenshot here that the editor is telling me that the Button will be shown at co-ordinates (0,0) when the app is run, because I've defined no constraints. That pretty much explains what you're seeing in your first post.

So I then added some 'constraints'. These are rules which determine where the component is placed, in relation to something else. If you right click on the Button in the editor, you get a sub menu called "constrain". Using these options you can set horizontal and vertical constraints. Once these constraints are added, you can then continue to drag the Button around to different places on the screen. I suppose you can add constraints in relation to other components on the screen, so they will reactively resize for different screen configurations.

You can also use the 'infer constraints' button, as @wseng92 mentioned in the above post. This will add default constraints to your components. That should have resolved your problem. If not then there are some other issues. Can you attach a screenshot of your layout editor?

Anyway, this is all really good, and I've learned something new, so thank you for leading me there.


constraint_layout.png

constraint_layout2.png
 
Last edited by a moderator:
(From the other previous post) When I go to app/res/layout there is only one XML file rather than two files: activity_main.xml is there, but I can’t see content_main.xml?

Looking at your later post, when I try and put a Button on, I get (I just noticed) 1 warning & 2 Errors.See the picture.

Is the first problem (missing XML) the important problem please?

Many thanks for your time and help!
 

Attachments

  • AndroidStudio1.jpg
    AndroidStudio1.jpg
    338.8 KB · Views: 181
You see that red exclamation mark next to the Button? That's because it has no constraints.
You can either click the 'Infer constraints' magic wand in the toolbar, or add the constraints as I described above.
 
You see that red exclamation mark next to the Button? That's because it has no constraints.
You can either click the 'Infer constraints' magic wand in the toolbar, or add the constraints as I described above.
I think that had worked – THANKS!

But… going to app/res/layout activity_main.xml is there, but content_main.xml is missing, Isn’t content_main.xml needed for this to work?
 
It depends how the project layout is structured during project creation. The different project templates may have variations in the way they build the layouts.
There's nothing wrong with putting all the content within activity_main.xml.
 
Back
Top Bottom