R.id.myEditText error // todolist attempt - android

Hey guys,
i bought an android dev book from WROX (Android 2 application development) and on chapter 2 they have an average task list project that i can't seem to get working.
I copied the exact line of code and tried tweaking them to get it to work but the following line remains an error no matter what i do.
final EditText myEditText = (EditText) findViewById(R.id.myEditText);
the error is the R.id.myEditText, i've added it to the R.java file but no luck it doesn't want to work.
the java file for it is the following.
package com.paad.todolist;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
public class ToDoList extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView myListView = (ListView) findViewById(R.id.myListView);
final EditText myEditText = (EditText) findViewById(R.id.myEditText);
final ArrayList<String> todoItems = new ArrayList<String>();
final ArrayAdapter<String> aa;
aa = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, todoItems);
myListView.setAdapter(aa);
myEditText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keycode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN)
if (keycode == KeyEvent.KEYCODE_DPAD_CENTER) {
todoItems.add(0, myEditText.getText().toString());
aa.notifyDataSetChanged();
myEditText.setText("");
return true;
}
return false;
}
});
}
}
the XML file is the follwing
<?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"
>
<TextView
android:id="#+id/myTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
<ListView
android:id="#+id/myListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Any ideas?

You dont update your R.java file directly, this is generated automatically when your project is built. The issue is that you have not added the EditText to your layout:
<EditText
android:id="#+id/myEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>

If you change
android:id="#+id/myTextView"
to
android:id="#+id/myEditText"
it should work.
What's happening under the hood? When you define an element with a specific id in XML file, Android tools (Eclipse probably in your case) will regenerate R.java file.
R.java should never be edited by hand, think of it as a source file that represents your XML elements in a type safe way which you can access in Java code.

Change your xml file as below and then try:
<?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" > <EditText android:id="#+id/myEditText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="#string/hello" /> <ListView android:id="#+id/myListView" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>

Related

How to know which button user clicked and its text and id in Android Studio?

Through out the whole android application I want to capture button click, radio button clicks, link click etc... basically a user interaction in whole android application. Is there any common method to detect which element user click and its values.?
Try using onCickListener() on the buttons.
In Kotlin:
Add id to button in .xml files with android:id="#+id/nameOfButton". Every button needs an unique name.
In .kt file, use setOnClickListener with the id to set up the action when user click the button.
If there are several buttons, just follow step 1 and 2.
Example:
step 1
<Button
android:id="#+id/buttonSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save" />
step 2
buttonSave.setOnclickListenter {
//TODO: your code goes here
}
Its very simple you just need to get the text and id of button from the onclick method. Here is java and xml code for it:
XML:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:onclick="getid"
android:text="Save" />
JAVA:
public void getid(View v){
int id = v.getId();
String text = v.getText();
}
As #Muthukumar Lenin asked here is listview in xml and java
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:id="#+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
tools:context=".MainActivity">
<TextView
android:id="#+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="italic"
android:textColor="#5f65ff"
android:padding="5dp"
android:text="Choose is the best football player?"/>
<ListView
android:id="#+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/textview" />
</RelativeLayout>
JAVA:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listview);
final TextView textView = (TextView) findViewById(R.id.textview);
String[] players = new String[] {"CR7", "Messi", "Hazard", "Neymar"};
List<String> Players_list = new ArrayList<String>(Arrays.asList(players));
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Players_list);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedItem = (String) parent.getItemAtPosition(position);
textView.setText("The best football player is : " + selectedItem);
}
});
}
}
Some of these code are from tutorialspoint and some are edited by me.

Android project display error on R.id.button

I surdenly noticed that My project starts throwing error anytime I try to access a resources that is a button. It underlines R.id.button. I dont understand why. I even deleted the last xml that I created but problem persist.
This is an example of my 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:background="#drawable/layoutborder"
android:orientation="vertical" >
<TextView
android:id="#+id/chat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="#string/stepone"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/wine" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:src="#drawable/ai" />
<Button
android:id="#+id/drugdetails"
style="#style/smallButtonStyleBlackpearl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:text="#string/nextbut" />
</LinearLayout>
My Java code
package com.example.rhemahealthcare;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.actionbarsherlock.app.SherlockActivity;
import com.example.rhemahealthcare.R;
public class SteponeActivity extends SherlockActivity{
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.steponeactivity);
final Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
// TODO Auto-generated method stub
Intent intent = new Intent(SteponeActivity.this,SteptwoActivity.class);
startActivity(intent);
}
});
}
}
i think you change any button1 id buy clicking right click and choose edit id. this option changes all the ids with that name in all the layouts.
As #Aleks G gussed it right in the comment, you don't have any button with id as button1 in your xml file. You've mentioned it :
final Button button = (Button)findViewById(R.id.button1);
Use the appropriate ID or put one in your layout file.
I have figured out the problem. My button ids were automatically change to button1 so they did not reference their previous ids that I gave to them. Thanks alot

Simple TodoList Application

I'm trying to create a simple to-do list application from a book on Android development. When I try to run it, I get a "this application has stopped working" message. I have absolutely no clue why, except for the fact that it has something to do with a fatal error at setContentView(R.layout.main). Can someone help me fix it?
package com.test.tadalist;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
public class TadaList extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ListView listView = (ListView)findViewById(R.id.listView);
final EditText editText = (EditText)findViewById(R.id.editText);
final ArrayList<String> todoItems = new ArrayList<String>();
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, todoItems);
listView.setAdapter(adapter);
editText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent keyEvent) {
if (keyCode == KeyEvent.KEYCODE_ENTER)
{
todoItems.add(0, editText.getText().toString());
adapter.notifyDataSetChanged();
editText.setText("");
return true;
}
return false;
}
});
}
}
layout/main.xml:
<?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">
<EditText
android:id="#+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Entry"
/>
<ListView
android:id="#+id/listView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
You're missing a lot in your layout 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">
<EditText
android:id="#+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Entry"
/>
<ListView
android:id="#+id/listView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
A stack trace would help, but I imagine it's likely because you don't have a container for your two views. Try enclosing your two views within something like a LinearLayout.

Android: View crashes when I have two EditText boxes with #+id defined in the xml

Another newbie question here. I'm trying to create a form which takes several text input fields from the user, however my view keeps crashing/failing to load with the following error reported in the log:
Window already focused, ignoring focus gain of:com.android.internal.view.IInputMethodClient$Stub$Proxy#628a9148
This is what my xml looks like (this crashes)
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">"
<LinearLayout
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_gravity="top">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Please enter contact details\n\nName:"/>
<EditText
android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:maxLength="30"
android:maxLines="1"
android:hint="#string/compose_name"></EditText>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="\nSurname:"/>
<EditText
android:id="#+id/surname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:layout_below="#id/name"
android:maxLength="30"
android:maxLines="1"
android:hint="#string/compose_surname"></EditText>-->
<Button
android:id="#+id/new_contact_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/submit" />
</LinearLayout>
</ScrollView>
However if I remove just the line:
android:id="#+id/surname"
It works (well at least it loads the view, of course I can't access the content in the EditText field without creating an id for it).
The view also fails if I add #+id tags to both the TextView fields (but works if I add only one).
What's going on here? I thought you should be able to label multiple view fields in your UI (all the examples in my book let me do this)?
I'm using NetBeans to develop on.
EDIT: Adding Javacode:
package org.me.savingsdepositrecord;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;
public class NewContact extends Activity
implements OnClickListener, View.OnKeyListener {
//private EditText nameField;
//private String name;
private Button submitButton;
EditText nameField = null;
EditText surnameField = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_contact);
nameField = (EditText) findViewById(R.id.name);
nameField.setOnKeyListener(this);
surnameField = (EditText) findViewById(R.id.surname);
submitButton = (Button) findViewById(R.id.new_contact_button);
// Set up click listeners for all the buttons
submitButton.setOnClickListener(this);
}
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
String name = nameField.getText().toString();
String surname = surnameField.getText().toString();
// TODO: Save Nickname setting (strNicknameToSave)
return true;
}
return false;
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.new_contact_button:
Intent i = new Intent(this, MainActivity.class);
finish();
startActivity(i);
break;
// More buttons go here (if any) ...
}
}
}
Yes, you can label multiple fields. It might be a good to take out the android:layout_below attribute since that isn't supported in LinearLayout.
Otherwise, your Java code might be the culprit. Can you post it as well?
Edit: I also tested your code and found no errors, although I had to remove the '-->' and the #string references.

Android Simple ToDo list app not rendering correctly

Am following the tutorial here on creating a simple todo list app:
http://www.mydroid.apnafundaz.com/2010/07/todolist-application-in-android-step-by-step-guide-with-screen-shots/
Have completed and have no errors, however my app doesn't appear like in that example, rather it looks like this - basically the EditText appears, but there's no ListView with previous tasks...
As there's no errors, it's rather hard to work out where I'm going wrong, but as my partner's also doing the same tutorial and has come up with exactly the same result, I am thinking there could be an issue with the tut - any advice much appreciated!
Just in case, here's my java file:
package com.example.helloworld;
import java.util.ArrayList;
import android.R.string;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
public class HelloWorld extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Inflate your view
setContentView(R.layout.main);
// Get references to UI widgets
ListView myListView = (ListView)findViewById(R.id.myListView);
final EditText myEditText = (EditText)findViewById(R.id.myEditText);
// Create the array list of todo items
final ArrayList<String> todoItems = new ArrayList<String>();
// Create the array adaptor to bind the array to the listview
final ArrayAdapter<String> aa;
aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
todoItems);
// Bind the array adapter to the listview
myListView.setAdapter(aa);
myEditText.setOnKeyListener(new OnKeyListener(){
public boolean onKey(View v, int keyCode, KeyEvent event){
if(event.getAction() == KeyEvent.ACTION_DOWN)
if(keyCode == KeyEvent.KEYCODE_DPAD_CENTER)
{
todoItems.add(0,myEditText.getText().toString());
aa.notifyDataSetChanged();
myEditText.setText("");
return true;
}
return false;
}
}
);
}
}
And here's my main.xml:
<?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"
>
<EditText
android:id="#+id/myEditText"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="New to Do Item"
/>
<ListView
android:id="#+id/myListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Do one small change, in your EditText you are setting android:layout_height="fill_parent". So you cannot see List view. Just change it to wrap_content
<EditText
android:id="#+id/myEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="New to Do Item"
/>
KEYCODE_DPAD_CENTER is an odd choice of keycode... are you actually pressing this to add the edittext contents to the list? It's an odd choice cos some phones don't have directional pads. Try changing it for KEYCODE_ENTER.
On the first start, your list should be empty, because you have no previous todos defined. Have you tried to add a todo with the edittext? After that you should see it in the listview.

Categories

Resources