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.
Related
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.
i try make spinner and button with clickHandler
this is my nyoba.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
public class nyoba extends Activity {
String[] nama_hari = {"Senin", "Selasa", "Rabu","Kamis","Jum'at","Sabtu","Minggu"};
/** Called when the activity is first created. */
Spinner spinner1;
Button btnSubmit;
TextView textView1;
String tes;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnSubmit = (Button)findViewById(R.id.btnSubmit);
textView1 = (TextView)findViewById(R.id.textView1);
spinner1 = (Spinner)findViewById(R.id.spinner1);
ArrayAdapter<String> aa = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, nama_hari);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(aa);
}
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
tes = nama_hari[arg2];
}
public void clickHandler(View view){
switch (view.getId()){
case R.id.btnSubmit:
textView1.setText(tes);
break;
}
}
}
and this is my main.xml file
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:background="#ffffff">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dip"
>
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clickHandler"
android:text="Submit" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</RelativeLayout>
</ScrollView>
but when i run it to emulator,and i choose 1 option in spinner and click the button , the TextView doesn't show up the word from spinner which i choose
(for example, i choose "senin" at spinner, and click the button, but the text "senin" doesn't appear in the TextView )
i check DDMS and there are no error.....
is the code wrong?
can you help me please which one is wrong?
Thank anyway....
I think you don't set the selection listener on your spinner.
But actually you can just retrieve the spinner's selected position with getSelectedItemPosition in the click handler of your button:
Try this:
public void clickHandler(View view) {
switch (view.getId()) {
case R.id.btnSubmit:
textView1.setText(nama_hari[spinner1.getSelectedItemPosition()]);
break;
}
}
In my application I want to change the list view text color. In this case I am using XML file for list view. Is it possible? If yes then give the example.
gender.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/topelg"
>
<ListView
android:id="#+id/android:list"
android:layout_marginTop="60dip"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#000000"
/>
</LinearLayout>
Egender.java
package com.Elgifto;
import android.app.ListActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
public class Egender extends ListActivity{
Button b1;
ListView lv;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gender);
// b1=(Button) findViewById(R.id.butt1);
b1=new Button(this);
b1.setText("Done");
//b1.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
lv=(ListView) findViewById(android.R.id.list);
lv.addHeaderView(b1);
b1.setOnClickListener(new OnClickListener(){
public void onClick(View v)
{
// To send a result, simply call setResult() before your
// activity is finished.
finish();
}
});
lv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_single_choice, GENDER));
lv.setBackgroundColor(Color.BLACK);
lv.setItemsCanFocus(false);
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lv.setTextFilterEnabled(true);
}
private static final String[] GENDER = new String[] {
"Male","Female"
};
}
If you would like to change color of all ListView items, instead of passing default android.R.layout.simple_list_item_single_choice to ArrayAdapter you should pass custom list item XML, which has different TextColor attribute.
For example, created custom_list_item.xml under folder Layout:
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text1"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorSingle"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:textColor="#FF0000"
/>
Then passed it to adapter:
new ArrayAdapter<String>(this, R.layout.custom_list_item, stringList)
I've got list, were all items are red.
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>
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.