Hoping this is a simple one.
I have a very simple issue here that I cant seem to solve. I'm simply trying to reference an EditText in my XML layout to the code through the use of findViewById etc. For some reason, and I have tried cleaning the project and regenerating my R resource file, along with ctrl+o to check the imports I cant get Eclipse to 'see' the link to the EditText in the XML layout.
Hopefully someone can help me with this frustrating and simple error.
Currently the error is:
totalListPrice cannot be resolved or is not a field
This is the simple code used to reference 'totalListPrice; in my XML to the EditText in my code:
totalPrice = (EditText)findViewById(android.R.id.totalListPrice);
And this is my XML layout (the problematic EditText has been marked):
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="84dp"
android:orientation="horizontal"
android:focusableInTouchMode="false">
<ImageView
android:id="#+id/imgLink"
android:layout_width="78dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="#drawable/viewcon" />
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="75dp"
android:gravity="center"
android:text="Shopping List"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="35sp"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false" />
</LinearLayout>
<TextView
android:id="#+id/txtItemDes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Search From An Item In The List:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/inputAppointName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter An Item Name" >
<requestFocus />
</EditText>
<Button
android:id="#+id/btnAddItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add"/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="56px"
android:orientation="horizontal" >
<TextView
android:id="#+id/txtPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total Price:"
android:textAppearance="?android:attr/textAppearanceMedium" />
*************ERROR*************
<EditText
android:id="#+id/totalListPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="number"></EditText>
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
It should be
R.id.totalListPrice
not
android.R.id.totalListPrice
Import local R class in your java file instead the R class of android framework
import <your package name>.R;
You don't need the "android" in front of the R. Instead, import the generated R file from your own package (you should be able to delete the import android.R line and replace it with your R import easily).
It should just be R.id.totalListPrice.
Related
hello there today I tried to make a relative layout for android but I m getting errors in R.java:
"Unexpected end of declaration" 8 times
I passed a hour on it and I still didnt find anything
here is the code (I m sure its about xml not java code)
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="#+id/playScrollView1">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:id="#+id/playRelativeLayout1">
<TextView
android:layout_height="wrap_content"
android:text="Convert to binary"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="fill_parent"
android:id="#+id/TextView1"
android:layout_alignParentTop="true"/>
<Button
android:layout_height="wrap_content"
android:text="0"
android:layout_width="wrap_content"
android:layout_below="#id/TextView1"
android:id="#+id/1"/>
<Button
android:layout_height="wrap_content"
android:text="0"
android:layout_width="wrap_content"
android:id="#+id/2"
android:layout_below="#id/1"/>
</RelativeLayout>
</ScrollView>
thanks in advance!
You need to use
<Button
android:layout_height="wrap_content"
android:text="0"
android:layout_width="wrap_content"
android:layout_below="#+id/TextView1" // missing+
android:id="#+id/button1"/> // change id to button1 not 1
Same for other button
http://developer.android.com/guide/topics/ui/declaring-layout.html
Any View object may have an integer ID associated with it, to uniquely identify the View within the tree. When the application is compiled, this ID is referenced as an integer, but the ID is typically assigned in the layout XML file as a string, in the id attribute.
Example :
android:id="#+id/my_button"
The at-symbol (#) at the beginning of the string indicates that the XML parser should parse and expand the rest of the ID string and identify it as an ID resource. The plus-symbol (+) means that this is a new resource name that must be created and added to our resources (in the R.java file)
For relative layout
http://developer.android.com/guide/topics/ui/layout/relative.html
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/playScrollView1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:id="#+id/playRelativeLayout1">
<TextView
android:layout_height="wrap_content"
android:text="Convert to binary"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="fill_parent"
android:id="#+id/TextView1"
android:layout_alignParentTop="true"/>
<Button
android:layout_height="wrap_content"
android:text="0"
android:layout_width="wrap_content"
android:layout_below="#+id/TextView1"
android:id="#+id/button1"/>
<Button
android:layout_height="wrap_content"
android:text="0"
android:layout_width="wrap_content"
android:id="#+id/2"
android:layout_below="#+id/button1"/>
</RelativeLayout>
</ScrollView>
Its just naming convention
Just change your id name of Buttons #+id/1 to #+id/One
it shall not be Numeric only it shall not start with Numeric
coz it will generate Field in R.java
like the following
public static final int 1=0x7f0a0032;
public static final int 2=0x7f0a0033;
As per convention it is WRONG a big WRONG
So Your code shall go like this
<Button
android:id="#+id/One"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/TextView1"
android:text="0" />
<Button
android:id="#+id/Two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/One"
android:text="0" />
OR completely
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/playScrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="#+id/playRelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="20dp"
android:paddingRight="20dp" >
<TextView
android:id="#+id/TextView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Convert to binary"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/One"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/TextView1"
android:text="0" />
<Button
android:id="#+id/Two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/One"
android:text="0" />
</RelativeLayout>
</ScrollView>
Hoping this is a simple one
I want two textviews on the same line aligned to the left.
Currently my layout is as shown:
I want this layout(note i modified the diagram on paint):
here is the xml i currently have:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/txtSetItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:layout_weight="2"
android:textAppearance="?android:attr/textAppearanceLarge"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"/>
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusableInTouchMode="false"
android:focusable="false" />
<TextView
android:id="#+id/txtSetAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false" />
<TextView
android:id="#+id/txtSetPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"/>
hmm your layout shouldn't compile-- there is no attribute android:layout_width on the TextView objects.
To fix this, remove layout_weight and set layout_width to wrap_content
Anyways, here:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="this"
/>
</LinearLayout>
Change your first TextView's weight to 0 (or to something smaller than 1). You're giving them equal weight and LinearLayout places them in a way that gives them equal width, which isn't what you want.
Don't use weight.
Use wrap_content on the layout_width.
This will get you what you are looking for:
<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"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This"
android:paddingLeft="10dp" />
</LinearLayout>
one way to go about this would be to use relative layout .
You can change how "close" or "far" apart the two textviews will be using changing the android:layout_marginLeft property on textView2.
The following property:
android:layout_alignBaseline="#+id/textView1"
makes sure that tv2 is aligned with tv1.
<?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/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="26dp"
android:text="TextView" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView1"
android:layout_alignBottom="#+id/textView1"
android:layout_marginLeft="32dp"
android:layout_toRightOf="#+id/textView1"
android:text="TextView" />
</RelativeLayout>
In this case, my xml file shows a certain layout while at runtime another one is generated. I am incredibly confused as to why the layout shown at runtime is completely different from the one given. here is the code for the xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="#+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
</LinearLayout>
<Button
android:id="#+id/signUp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sign up!" />
</LinearLayout>
This displays Insert name here on both a label and button. The button is correctly bound and pressing it does do something, however I cannot type in the textfield at runtime.
This one looks fine. Double check if you are changing any property with your java code.
Try to use this one instead. I have removed the useless LinearLayout and try to make the EditText clickable.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/username"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/signUp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Sign up!" />
</LinearLayout>
I have in, an android project, in res/layout folder a balloon_overlay.xml file. I recieve an error that the resource file cannot be resolved or is not a field. I do have android.R correctly referenced in my activity file where I use the resource file but I can't seem to find out what the problem is or how to fix it. Any help will be appreciated. Thanks
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/balloon_main_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/balloon_overlay_bg_selector"
android:minWidth="200dip"
android:orientation="horizontal"
android:paddingBottom="25dip"
android:paddingLeft="10dip"
android:paddingRight="0dip"
android:paddingTop="0dip" >
<LinearLayout
android:id="#id/balloon_inner_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dip"
android:layout_weight="1" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:minHeight="40dip"
android:orientation="vertical" >
<TextView
android:id="#+id/balloon_item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="balloon_item_title"
android:textColor="#android:color/primary_text_light"
android:textSize="16dip" />
<TextView
android:id="#+id/balloon_item_snippet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="balloon_item_snippet"
android:textColor="#android:color/secondary_text_light"
android:textSize="12dip" />
</LinearLayout>
<ImageView
android:id="#+id/balloon_disclosure"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical"
android:paddingTop="3dip"
android:src="#drawable/balloon_disclosure"
android:visibility="gone" />
</LinearLayout>
<ImageView
android:id="#id/balloon_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="10dip"
android:paddingLeft="10dip"
android:paddingRight="5dip"
android:paddingTop="5dip"
android:src="#drawable/balloon_overlay_close" />
</LinearLayout>
Do not use android.R but your own R class (which is automatically generated in the gen folder and contains the constants for your resources. In Eclipse, pressing Strg+Shift+O should organize your imports correctly.
I am trying to display two texts in a layout such that they are displayed one after the other but the following code is pasting the strings right above each other. What should I do. I tried to exchange fill_parent & wrap_content between the two textviews but it is worthless
<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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="false"
android:layout_centerVertical="false"
android:text="#string/hello_world"
tools:context=".StartingPoint" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="false"
android:layout_centerVertical="false"
android:text="#string/testing"
tools:context=".StartingPoint" />
You have to put the property below in the second TextView and set on the below property the id of the first TextView
change your two TextView by this two:
<TextView
android:id="#+id/TextView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="false"
android:layout_centerVertical="false"
android:text="#string/hello_world"
tools:context=".StartingPoint" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/TextView1"
android:layout_centerHorizontal="false"
android:layout_centerVertical="false"
android:text="#string/testing"
tools:context=".StartingPoint" />
But you need to read the documentation at android developers first, because that is very basic.
Hope to help :)
Use LinearLayout as the parent layout and set its orientation to horizontal. This will work.
You have to tell the RelativeLayout how it has to place the different views.
Documentation: http://developer.android.com/guide/topics/ui/layout/relative.html
It could be simpler to use a LinearLayout.
If you need RelativeLayout menas,
<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" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="FirstText"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="secondText"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Else you need LinearLayout means,
<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:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FirstText"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="secondText"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
In the Case of Relative layout you need the textview is one by one means use this parameter, android:layout_below="#+id/textView1" else you are using LinearLayout means use this parameter android:orientation="vertical"