Anyone please help me figure out why is the following button1 is not visible in my application?
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".AirplaneActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/button_activate"
android:onClick="activateAirPlaneMode"/>
</RelativeLayout>
Did you call setContentView(R.layout.layout_name);, where layout_name is the name of your xml file in layouts (assuming it's an Activity)?
There doesn't seem to be anything wrong with your XML file so it must be something else.
If you provide more context/code, it may be more helpful.
Related
I am trying to create a layout in Android Studio but the preview of the layout is not showing at all. There is no errour though.
How can i get it to work properly? I will add a screenshot.
Preview not loading the layout
Thanks in advance.
<?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:id="#+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.diceout.MainActivity"
tools:showIn="#layout/activity_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Let's Play!" />
<Button
android:id="#+id/rollButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="155dp"
android:layout_marginStart="155dp"
android:layout_marginTop="25dp"
android:text="Roll" />
</RelativeLayout>
TextView
#ivan franken Could you post your #layout/activity_main Xml code. I saw this line tools:showIn:#layout/activity_main
in code on your content_main.xml. I tested it in my project and got similiar result to yours. But when I deleted it, it worked fine.
I'm pretty new to Android development.
I'm using the following RelativeLayout.
I want my txtView1 to be above lstAnswers, but layout_below doesn't work and the views are overlayed.
<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"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:background="#ffbad6e6"
tools:context="com.example.user.myApp.QuestionDetails">
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtView1"
android:textColor="#ff0d5e52"/>
<ListView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:id="#+id/lstAnswers"
android:layout_centerHorizontal="false"
android:paddingTop="50dp"
android:layout_alignParentTop="true"
android:divider="#android:color/transparent"
android:dividerHeight="10dp"
android:layout_below="#id/txtView1"/>
</RelativeLayout>
I also tried to set layout_below using #+id, like this:
android:layout_below="#+id/txtView1"
but that gave me nothing.
Can anybody explain me how can I set txtView1 above lstAnswers?
Very easy.
Remove android:layout_alignParentTop="true" from your ListView.
So, android:layout_below="#id/txtView1" will be able to do its job.
not sure what I am doing wrong, nothing complicated 2 buttons inside a LinearLayout and I get
"This LinearLayout layout or its RelativeLayout parent is useless; transfer the background attribute to the other view" warning....here is the code, is boggling my mind
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#drawable/GCBS_Template_BKGND"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/Partfinder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Part Finder" />
<Button
android:id="#+id/Heaterfinder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Heater Finder" />
</LinearLayout>
If you just want to show two buttons vertically - there's no need to use the LinearLayout in this case. This is increasing your view level hierarchy- that's why the warning is shown.
You can simply use android:layout_below="#+id/Partfinder" to your 2nd button to achieve what you want.
In Eclipse Juno, you can Project's Properties -> Android Lint Preferences. Now, Look for UselessParent and set it's severity to ignore or click Ignore All.
But at the end, Your problem is your RelativeLayout only have one child. You can remove it like that:
<LinearLayout 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"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#drawable/GCBS_Template_BKGND"
tools:context=".MainActivity" >
<Button
android:id="#+id/Partfinder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Part Finder" />
<Button
android:id="#+id/Heaterfinder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Heater Finder" />
</LinearLayout>
The warning is very clear as per your xml layout creation. For just adding 2 buttons you may not need both Relative & Linear Layout but only one of the layouts is more than enough. So, by deleting the Linear Layout in your XML should help you proceed further like below.
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#drawable/GCBS_Template_BKGND"
tools:context=".MainActivity" >
<Button
android:id="#+id/Partfinder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Part Finder" />
<Button
android:id="#+id/Heaterfinder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Heater Finder" />
</RelativeLayout>
After this just drag and drop the buttons in layout view of the xml and position them wherever you want the buttons. That should put the XML script automatically in your XML file.
I got a little problem: I can't put views to the verry right or to the verry left of my screen... In the graphical layout preview of eclipse, it looks like this:
If I upload it to my Galaxy Tab 3 (10.1"), it looks exactly the same...
Here's the XML-Code:
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:id="#+id/layout"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="left"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
As you can see, android:layout_alignParentLeft and android:layout_alignParentTop are set to true...
#dimen/activity_vertical_margin and #dimen/activity_horizontal_margin have their default value of 16dp.
It don't concerns just TextViews, but also every other view I tried out. If I select other screen sizes than 10.1", the view is at the right place.
Anybody can help me?
Thank you!
Try this:
<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"
android:id="#+id/layout"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="left"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
You have given padding to the parent layout. That's why you couldn't place your view close to left.
Remove these lines from your RelativeLayout
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
Remove the paddings of your Relative 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"
android:id="#+id/layout"
tools:context=".MainActivity" >
Now I have a GridView in my activity which I fill with numbers from a txt file (and inserting via an adapter).
This works perfectly, but I want to add some buttons at the bottom to enable input from the user.
But when I add a button, I get this weird result.
But the result I want to have is this:
How can I accomplish this?
This is my current layout XML code:
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".LevelSpelen" >
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:columnWidth="#dimen/activity_horizontal_margin"
android:numColumns="3"
android:verticalSpacing="#dimen/activity_vertical_margin" >
</GridView>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/gridView1"
android:layout_marginLeft="18dp"
android:text="" />
What do you guys suggest? A gridLayout or something else?
Please let me know!