How to resize the layout properly? - Android - java

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>

Related

How can I fix the alignment issue in my android application

I have created a login page with :
1 small image,
2 text boxes,
1 button
but I want them to align in all types of devices like when I tried on my device and it looks fine but not for the other devices that are bigger-smaller in screen-sizes
Thanks in advance
Here is the code :
<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"
android:hardwareAccelerated="true"
android:background="#color/white"
tools:context=".MainActivity">
<View
android:id="#+id/view2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
android:hardwareAccelerated="true" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="37dp"
android:background="#color/blend"
android:contentDescription="#string/todo"
app:srcCompat="#drawable/imagelogo" />
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="58dp"
android:layout_marginBottom="294dp"
android:ems="10"
android:hint="#string/User_name"
android:inputType="text" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginEnd="147dp"
android:layout_marginBottom="227dp"
android:ems="10"
android:hint="#string/Pass_word"
android:inputType="textPassword" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="97dp"
android:layout_marginBottom="138dp"
android:ems="13"
android:background="#color/colorPrimary"
android:text="#string/login" />
and the output :
Design + Blueprint
So i want all my things at same place in every type of devices
Add below into your project level Gradle file:
compile 'com.intuit.sdp:sdp-android:1.0.5'
Usage : In code wherever you are specifying dimensions like 10dp change it to #dimen/_10sdp. Like I have specified in this example ImageView.
<ImageView
android:id="#+id/your_image"
android:layout_marginTop="#dimen/_10sdp"
android:layout_marginBottom="#dimen/_10sdp"
android:layout_width="#dimen/_100sdp"
android:layout_height="#dimen/_100sdp"
android:src="#drawable/logo"/>
This will make your application look same on different devices.

xml layout isn't working for me

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.

How to position a button in android XML?

Hey I have the code bellow for my buttons in XML and I would like the Anthem Button to be moved to the left hand side near the bottom corner and I will add a stop button in the bottom right, Could anyone advise me on how to do this?
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/country1"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="45sp" />
<ImageView
android:id="#+id/ivFlag1"
android:layout_width="match_parent"
android:layout_height="64dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="55dp"
android:src="#drawable/sflag" />
<Button
android:id="#+id/bEdinburgh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/ivFlag1"
android:layout_centerHorizontal="true"
android:layout_marginTop="29dp"
android:gravity="center"
android:text="#string/Edinburgh"
android:textSize="20sp" />
<Button
android:id="#+id/bGlasgow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/bEdinburgh"
android:layout_centerHorizontal="true"
android:layout_marginTop="29dp"
android:gravity="center"
android:text="#string/Glasgow"
android:textSize="20sp" />
<Button
android:id="#+id/bAberdeen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/bGlasgow"
android:layout_centerHorizontal="true"
android:layout_marginTop="29dp"
android:gravity="center"
android:text="#string/Aberdeen"
android:textSize="20sp" />
<Button
android:id="#+id/bAnthem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/bAberdeen"
android:layout_centerHorizontal="true"
android:layout_marginTop="29dp"
android:background="#drawable/custom"
android:textSize="20sp" />
Set following attributes in the Anthem button
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
and following to the Stop button
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
In your Anthem Button, set android:layout_alignParentLeft or android:layout_alignParentRight to true will do the trick.

"Include Other Layout" causes build to fail in emulator. No errors or associated warnings

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!

Android XML Issue - images not displaying in AVD

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.

Categories