Can't add form widgets to main.xml file Graphical Layout - android

I'm new to Android and I wonder why I can't drag and drop things like buttons to the display in the main.xml Graphical Layout? It seems like it's locked or something else that I can't figure out on myself. Help is preciated! Thanks!
EDIT: This is my code in xml file
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/hello"
/>

You are not able to do so because you don't have a Layout in your XML. Given here is an example as to how to build a basic layout. You can also refer to the Relative Layout tutorial or Linear Layout tutorial for more details, as these are widely used layouts.
Given below is the sample code for your XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
</LinearLayout>
You can replace Linear Layout with Relative Layout at your convenience. But do keep in mind that you can't use the graphical layout without having a layout. To drag and drop a layout, refer the image below:
Hope this helps.

If that's your code, then it seems that you might be trying to drag elements into the TextView since it is filling the entire layout. Try to remove the TextView and see if you are then able to drag and drop elements again. Either that or change it to:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>

I am a begginer Android Studio to. I have same problem about drag and drop in design screen (in GUI). I solve the problem by using "res/layout/content_main.xml" (left side of the screen). It works! but ı am not sure that is it the right way :)))

Drag and drop the elements onto the Outline area instead of the display itself.Worked for me.

Related

Why android studio designer is not working for relative layout

is not auto generating XML codes for relative layout, it only works for constraint layout.
When I try to position a view inside a relative layout it only gets positioned at the top corner of the screen.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="#+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</RelativeLayout
What should I do?
I faced the same issue and the i tried the following step;
Click on the magnet icon (next to the eye icon, top left, next to the Palette menu) so that it's not crossed out: this turns on Autoconnect.
this will now help you to move the button where ever you want to place it.
Try it and let me know.
Yes if you pull the component on ConstraintLayout, it will guess what you want and set the attributes for you.
For RelativeLayout, you need to make the attribute view visible and edit from there. You need to edit parameters like these:
android:layout_below="#id/name"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/times"

All controls in my app are locked with the left side of the screen

All the textviews and seekbars that I dragged into the android screen in the Android ADK seem to be locked to the left side of the screen by default:
This is the xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/freqIndxTextView_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/freqIndxTextView_string" />
.
.
.
.
.
</LinearLayout>
I am supposing the problem is that I have not understood how Linearlayout works. I can move the textviews and seekbars up and down, but their left side seem to be stuck to the left edge of the screen. How do I remove this feature? Is it possible to drag and drop controls on the IDE like we can do in Visual C++?
Try to use margin in the xml, like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/freqIndxTextView_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="#string/freqIndxTextView_string" />
</LinearLayout>
Here is instruction for you. Hope it helps.:)
http://android4beginners.com/2013/07/lesson-2-2-how-to-use-margins-and-paddings-in-android-layout/
As #QuantumTiger wrote, you can use
android:paddingLeft="16dp"
You can use too
android:margingLeft="16dp"
There is a little difference between them.
Anyway, you can find much more information here.
Either add padding to the LinearLayout, or change the gravity to center
android:paddingLeft="16dp"
Or
android:gravity="center"

Android: Better way to generate buttons

So far, my app is pretty simplistic and small. It has a few buttons here and there, but I can see it quickly getting out of hands with a ton of buttons. So my problem really, my activity_main.xml looks ridiculously ugly. It has a bunch of tags, so I was wondering, what's the "correct" way to generate buttons when you have a lot of these?
Use XML do define views. I would always recommend you to use xml, even if there are 20 Buttons.
Alternativly you can also set the button programmatically in code and at them to your layout. Just set the layout parameters and you are done.
Otherwise (if you have a list of many buttons and you need to scroll) a ListView or GridView would a good choice.
To make things cleaner in your XML, you can use include to reference other XML which defines a generic button with all its common properties.
So the button would be defined under genericbutton.xml placed in the layout folder:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick" />
</merge>
And then your main activity_main.xml will have something like this for three buttons:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include
android:id="#+id/button1"
layout="#layout/genericbutton" />
<include
android:id="#+id/button2"
layout="#layout/genericbutton" />
<include
android:id="#+id/button3"
layout="#layout/genericbutton" />
</LinearLayout>
But in this case, you either set the same text for the buttons inside the genericbutton.xml or one by one in Java code. You cannot set it in the include tags.

Android - Background at bottom

I should like to have an android app which has its background image on the bottom of the view.
Hopefully this could be achieved by only using XML.
My background image (bg.png) is located at the folder "res/drawable-mdpi".
The XML code is now:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/bg"
android:layout_alignParentBottom="true"
android:layout_marginBottom="0dip" />
</LinearLayout>
But this is stretching the whole image over the whole page. I only want it stretched in horizontal direction.
The height has to be in proportion with the (stretched) width.
And this image has to be the background of a view/layout, I want to keep using the layout and add items/objects to it.
I hope someone could help me with this, I've been looking on the internet for it for a few hours and I couldn't find anything more helpful then I have now.
Thanks in advance,
Dennis
Firstly Change your LinearLayout to a RelativeLayout (android:layout_alignParentBottom only works with RelativeLayout).
Secondly use android:src="#drawable/bg" on your ImageView (not background)
if you really want to use the LinearLayout, you can try adding this attribute to your imageview android:layout_gravity="bottom"

How to draw 2 PNG Images to the screen at the same time

I'd like to know how to draw two PNG pictures onto the screen.
My XML layout: (named paperxml.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/paperid"
android:src="#drawable/paperrepresentation"
/>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rockid"
android:src="#drawable/rockrepresentation"
android:layout_alignTop="#id/paperid"
/>
</RelativeLayout>
What would be the java code to instantiate the XML layout and display both ImageViews on the screen at the same time?
Simply calling setContentView(R.drawable.paperxml); crashes my application on startup.
Replace the xml with:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView android:id="#+id/paperid"
android:src="#drawable/paperrepresentation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView android:id="#+id/rockid"
android:src="#drawable/rockrepresentation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Explanation:
RelativeLayout doesn't use android:orientation="vertical".
Every view should have android:layout_width and android:layout_height.
Add the xmlns:android thing just in the first element.
Calling setContentView(R.drawable.paperxml); is not crashing your code - it's your XML file. Macarse has the correct answer to your problem, and keep your code the same!
You may also want to look at the View Tutorials for some examples of setting up your XML and using different View objects.
I put the XML in but it only displays one ImageView Here's a screenshot of the emulator I took. i852.photobucket.com/albums/ab87/thomasjakway1/Capture.png Its worth mentioning that the file shown is paperrepresentation
If you look hard enough you will see that at the bottom there is a second very tiny image. You just need to increase the scale.

Categories

Resources