I'm fairly new to android but I've managed to figure out how all the layout stuff works with XML. That's all fine.
In the Graphical Layout tab in Eclipse I have a few buttons and a logo which is a .gif image. I have place the .gif image into my XML file as an ImageView within a LinearLayout and it shows up on the Graphical Layout.
But when I run it in the Android Virtual Device (AVD) everything shows up but the image. Anyone know why this is happening? See XML code below...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/natlib"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="-8dp"
android:padding="#dimen/padding_medium"
android:text="#string/welsh_libs"
android:textColor="#79438F"
android:textSize="22dip"
android:textStyle="bold"
tools:context=".MainActivity" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="137dp"
android:layout_height="wrap_content"
android:layout_marginBottom="25dp"
android:background="#A4C81C"
android:text="#string/ask_lib" />
<Button
android:id="#+id/button2"
android:layout_width="137dp"
android:layout_height="wrap_content"
android:layout_marginBottom="25dp"
android:background="#FF0066"
android:text="#string/find_book" />
<Button
android:id="#+id/button3"
android:layout_width="137dp"
android:layout_height="wrap_content"
android:layout_marginBottom="25dp"
android:background="#3F83F1"
android:text="#string/find_lib" />
<Button
android:id="#+id/button4"
android:layout_width="137dp"
android:layout_height="wrap_content"
android:layout_marginBottom="25dp"
android:background="#FE0002"
android:text="#string/register" />
<Button
android:id="#+id/button5"
android:layout_width="137dp"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:background="#FBFC3F"
android:text="#string/login" />
<ImageView
android:id="#+id/image1"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_marginLeft="220dp"
android:layout_marginTop="20dp"
android:layout_weight="0.37"
android:contentDescription="#string/desc"
android:src="#drawable/wag_logo"
android:visibility="visible" />
</LinearLayout>
set android:layout_height greater than 0...try this
<ImageView
android:id="#+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="220dp"
android:layout_marginTop="20dp"
android:layout_weight="0.37"
android:contentDescription="#string/desc"
android:src="#drawable/wag_logo"
android:visibility="visible" />
Which folder did you place the image in? /res/drawable?
Also, which folder did you place the layout in? /res/layout?
The /res/ folder and it's subfolders are used in Localization.
So if you only have it in /res/drawable-xhdpi/, it'll only show up on very large devices.
Also, if you modified your layout file at /res/layout-large/ and didn't modify the one at /res/layout, only the large version would get the modifications.
Related
The image is covering the textView and buttons(I cant even see them at all so I'm guessing they are behind the image). Image is attached. I want the textview and buttons to appear below the imageview. Below is my xml file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="#+id/imageView9"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitStart"
android:src="#drawable/bw" />
<EditText
android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button2" />
</LinearLayout>
You can change the main layout to RelativeLayout, set the ImageView at the top and wrap the views below the ImageView, inside a separate layout.
By setting android:layout_above="#+id/linear" for the ImageView, you will be sure that the ImageView will never cover the other views.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView9"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/linear"
android:layout_alignParentTop="true"
android:scaleType="fitStart"
android:src="#drawable/bw" />
<LinearLayout
android:id="#+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical"
android:gravity="center_horizontal">
<EditText
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button" />
</LinearLayout>
</RelativeLayout>
Try changing the parent layout to a RelativeView, or a ConstraintView. If you don't want to redo your positioning of your layout, you can wrap everything except the Image in a LinearLayout.
<RelativeView>
<ImageView>
<LinearLayout>
//...All your stuff
</LinearLayout>
<RelativeView>
This isn't one way of course, try to experiment. Feel free to ask if you still encounter problems.
I am making an alarm app but i wanted to change it to be a reminder and basically you type in the reminder and when the alarm goes off you can click to do text to speech on the reminder you saved. However, between testing and adding the text to speech my android app no longer shows any objects when running in the emulator but when designing i can see them all. I have tried to copy all the code into a new project but it does the same.
Here's the image of the emulator and designer.
It doesn't say but its a Relative Layout
There is the files of the project.
http://www.mediafire.com/file/uhybhd5sygu2714/AlarmFix.zip
First dont combine Contraintlayout attributes (such as : app:layout_constraintBottom_toBottomOf) it will not works with Relative/LinearLayout, just you need to use attributes such as android:layout_below... RelativeLayout.LayoutParams
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="#+id/tvUpdate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_below="#+id/linearalarm" />
<EditText
android:id="#+id/etReminder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="Your Reminder"
android:inputType="textPersonName"
android:textAlignment="center" />
<TimePicker
android:id="#+id/timePicker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/etReminder" />
<LinearLayout
android:id="#+id/linearalarm"
android:layout_below="#id/timePicker"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/alarm_on"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ALARM ON" />
<Button
android:id="#+id/alarm_off"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ALARM OFF" />
<Button
android:id="#+id/btnPlayRem"
android:layout_weight="2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PLAY LATEST REMINDER" />
</LinearLayout></RelativeLayout>
Im trying to make an app for the sony smart watch 2.
Im trying to make a basic converter app which consists of one text box, two radio buttons and one button. Yet when testing on the watch everything is massive. If I resize things in eclipse things disappear and other things what is happening? Picture of it on watch:http://content.screencast.com/users/jeremyc12/folders/Jing/media/050d6158-1daa-4ebe-8eb2-ed8a46e3d024/2014-04-23_1334.png
Here is my xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="#dimen/smart_watch_2_control_width"
android:layout_height="#dimen/smart_watch_2_control_height"
android:background="#color/myColor"
android:onClick="calculate"
tools:ignore="PxUsage" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:inputType="numberSigned"
android:width="5px" >
<requestFocus />
</EditText>
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/editText1"
android:layout_alignBottom="#+id/editText1"
android:layout_alignParentRight="true"
android:layout_marginRight="16dp"
android:checked="true"
android:text="#string/kmh" />
<RadioButton
android:id="#+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button2"
android:layout_alignLeft="#+id/radioButton1"
android:text="#string/miles" />
<Button
android:id="#+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/editText1"
android:text="#string/calc" />
Two issues I see here:
As Aritra pointed out, make sure in your XML layout that you aren't doing any scaling. So don't use "dp" just "px".
There are only a few Views in Android supported on SW2. See here for details:
http://developer.sonymobile.com/reference/sony-addon-sdk/com/sonyericsson/extras/liveware/aef/control/Control.Intents#EXTRA_DATA_XML_LAYOUT
EditText, RadioButton and Button views are not included there so thats why you're seeing some strange issues even without scaling.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff0000"
android:onClick="calculate" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:inputType="numberSigned"
android:width="5px" >
<requestFocus />
</EditText>
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_toRightOf="#+id/radioButton2"
android:checked="true"
android:text="kmh"
android:textSize="8sp" />
<RadioButton
android:id="#+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:text="miles"
android:textSize="8sp"/>
<Button
android:id="#+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/radioButton2"
android:layout_toRightOf="#+id/editText1"
android:text="calculate"
android:textSize="8sp"/>
</RelativeLayout>
I have a lot of info to put on one xml page. Now i got what I want in the positions i want. However instead of fitting all my data on 3 key parts without running into each other, It keeps running into the next TextView. I was wondering how to have it auto adjust.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/back_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="#string/back" />
<TextView
android:id="#+id/makeup_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/info_title"
android:layout_marginTop="97dp"
android:text="#string/part_section_2" />
<TextView
android:id="#+id/part_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/info_title"
android:layout_marginLeft="19dp"
android:layout_marginTop="38dp"
android:text="put text here for info" />
<TextView
android:id="#+id/part_safety"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/part_makeup"
android:layout_below="#+id/safety_title"
android:layout_marginTop="28dp"
android:text="put text here for safety" />
<TextView
android:id="#+id/info_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="42dp"
android:text="#string/part_section_1" />
<TextView
android:id="#+id/part_makeup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/part_info"
android:layout_below="#+id/makeup_title"
android:layout_marginTop="33dp"
android:text="put text here for makeup" />
<TextView
android:id="#+id/safety_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/part_makeup"
android:layout_marginTop="48dp"
android:text="#string/part_section_3" />
<TextView
android:id="#+id/part_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:textColor="#b70000"
android:textSize="16sp"
android:text="put text here for part title" />
</RelativeLayout>
would ScrollView work better? and how would switch this from this to ScrollView.
skin is 480x800
density is high(240)
Strings file - http://pastie.org/8534713
Changing to ScrollView (source):
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- your Button and TextView widgets -->
</RelativeLayout>
</ScrollView>
Edit:
As well as that, try changing your back button XML to this:
<Button
android:id="#+id/back_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/part_safety"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="#string/back" />
The problem was android:layout_alignParentBottom="true". The bottom ended up not going to the bottom of the page like you had before. The ScrollView cut back on the bottom of the layout.
I've added a couple small relative layouts to the main layout of an activity using "Include Other Layout" in the graphical editor. However, for some reason when I try to load the activity in the emulator, it fails. There are no warnings other than a bunch of generic stuff in LogCat that looks exactly the same as any other time I have an error. In the .Java file for this activity, I've successfully made reference to the views within these included layouts without any problems.
This are the elements from the activity's .xml file that include the layouts.
<include
android:id="#+id/include1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/lblParklandAssistDesc"
android:layout_below="#+id/lblParklandAssistDesc"
layout="#layout/parklandweight" />
<include
android:id="#+id/include2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/include1"
android:layout_below="#+id/include1"
layout="#layout/parklandbsa" />
And these are the layouts that are included
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/btnWeightUp"
style="?android:attr/buttonStyleSmall"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="#string/Up" />
<Button
android:id="#+id/brnWeightDown"
style="?android:attr/buttonStyleSmall"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/btnWeightUp"
android:text="#string/Down" />
<TextView
android:id="#+id/lblParklandWeightTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/btnWeightUp"
android:text="#string/lblWeight"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/txtWeight"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:layout_below="#+id/lblParklandWeightTitle"
android:layout_toRightOf="#+id/btnWeightUp"
android:ems="10"
android:inputType="number"
android:text="100" >
<requestFocus />
</EditText>
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/lblParklandWeightTitle" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/lb" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/kg" />
</RadioGroup>
</RelativeLayout>
Then the other one.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/btnBSADown"
style="?android:attr/buttonStyleSmall"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/btnBSAUp"
android:layout_below="#+id/btnBSAUp"
android:text="#string/Down" />
<TextView
android:id="#+id/lblParklandBSAtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="58dp"
android:text="#string/lblParklandBSAtitle"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/btnBSAUp"
style="?android:attr/buttonStyleSmall"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="#string/Up" />
<EditText
android:id="#+id/txtBSA"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/lblParklandBSAtitle"
android:layout_below="#+id/lblParklandBSAtitle"
android:ems="10"
android:inputType="number"
android:text="3" />
</RelativeLayout>
I don't have the foggiest idea as to what could be going on or frankly even where to look. I've been working with eclipse and java with android for almost two weeks, have made one app, and have been having great luck and easy learning with everything. This has left me stumped. Any help would be greatly appreciated!
New Addition(added .Java Content because it seems that the xml layouts themselves may not be the problem):
In the top of the activity's class I declare the variables that will handle the other layouts like this:
View parklandWeight;
View parklandBSA;
Then, when I assign the object to it - I do so like this:
parklandWeight = (View) findViewById(R.layout.parklandweight);
parklandBSA = (View) findViewById(R.layout.parklandbsa);
The only error that appears to be from this activity.java in the LogCat occurs just after this second assignment. On another assignment that refers a button inside one of these layouts:
btnWeightUp = (Button) parklandWeight.findViewById(R.id.btnWeightUp);
Could this be the root of my issue? I've read at least five or six different threads here in StackOverflow where this is what is advised to do. Thanks in advance.
I have tried these code runs perfect... Try these.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:ignore="HardCodedText" >
<include
android:id="#+id/include2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/include1"
android:layout_alignParentTop="true"
android:layout_below="#+id/include1"
layout="#layout/top" />
<Button
android:id="#+id/btnWeightUp"
style="?android:attr/buttonStyleSmall"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/include2"
android:text="Up" />
<Button
android:id="#+id/brnWeightDown"
style="?android:attr/buttonStyleSmall"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:layout_below="#+id/btnWeightUp"
android:text="Down" />
<TextView
android:id="#+id/lblParklandWeightTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/include2"
android:layout_toRightOf="#+id/btnWeightUp"
android:text="lblWeight"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/txtWeight"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:layout_below="#+id/lblParklandWeightTitle"
android:layout_toRightOf="#+id/btnWeightUp"
android:ems="10"
android:inputType="number"
android:text="100" >
<requestFocus />
</EditText>
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/include2"
android:layout_toRightOf="#+id/lblParklandWeightTitle" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="lb" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="kg" />
</RadioGroup>
</RelativeLayout>
I was typing the variables for the layouts wrong. Instead of using
View layoutVariable;
I needed to be typing by the actual layout type as below.
RelativeLayout parklandWeight;
RelativeLayout parklandBSA;
Then, when assigning the actual layout object to the variable, I should have been typing it as the layout type, of course. But, more than that, in the findViewById method I was using R.layout.layout_id when I should have been using R.id.layout_id. I was also referring to the xml file name where I should have been referring to the same +id that is used in the include tag in the xml file.
This is the way I should have been doing it:
parklandWeight = (RelativeLayout) findViewById(R.id.include1);
parklandBSA = (RelativeLayout) findViewById(R.id.include2);
I hope this helps any newcomers to android development who run into the same problem. I hope you all have an incredible day!