Add user input into a ListView on button click - android

I am trying to make a TO DO LIST. I have a EditText, Button, and ListView. On button click I want to add, what I typed into the EditText into a ListView.
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<EditText
android:id="#+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter task"
android:textSize="24dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/addTaskBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Add Task"
android:layout_below="#+id/editText"
android:layout_alignParentLeft="true" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" Task"
android:id="#+id/header"
android:layout_below="#+id/addTaskBtn"
android:background="#5e5e5e"
android:textColor="#FFFFFF"
android:textSize="14dp"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/list"
android:layout_below="#+id/header"
android:layout_centerHorizontal="true" />
Main_ToDoList.java
package com.example.todolist;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
public class Main_ToDoList extends Activity implements OnClickListener
{
private Button btnAdd;
private EditText et;
private ListView lv;
ArrayList<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
btnAdd = (Button)findViewById(R.id.addTaskBtn);
btnAdd.setOnClickListener(this);
et = (EditText)findViewById(R.id.editText);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, list);
lv.setAdapter(adapter);
}
public void onClick(View v)
{
String input = et.getText().toString();
if(input.length() > 0)
{
list.add(input);
adapter.notifyDataSetChanged();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main__to_do_list, menu);
return true;
}
}
The code doesn't work, new to android developing, and just trying to create a simple To Do List. Thanks for your help!

This should do it.
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
btnAdd = (Button)findViewById(R.id.addTaskBtn);
btnAdd.setOnClickListener(this);
et = (EditText)findViewById(R.id.editText);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, list);
// set the lv variable to your list in the xml
lv=(ListView)findViewById(R.id.list);
lv.setAdapter(adapter);
}
public void onClick(View v)
{
String input = et.getText().toString();
if(input.length() > 0)
{
// add string to the adapter, not the listview
adapter.add(input);
// no need to call adapter.notifyDataSetChanged(); as it is done by the adapter.add() method
}
}

you need to find your listview and then set the adapter:
lv=(ListView)findViewById(android.R.id.yourlistview);
lv.setAdapter(adapter);

Related

Using a nested if to make spinner string conditional selections with a bottun using intent

I am a beginner and I want the user to select three fields from a spinner, and using conditional statements to test all the three fields with an if condition and the and operator to decide which activity to be started using a button and intent.
activity level_menu
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/choose_txt"
android:textSize="30sp"
android:textStyle="bold"
android:layout_gravity="center"
android:textColor="#ff000000"
android:layout_marginBottom="30dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/prog"
android:textSize="32sp"
android:textStyle="bold"
android:textColor="#ff000000"
/>
<Spinner
android:id="#+id/program_spinner"
android:layout_width="match_parent"
android:layout_height="40dp"
android:entries="#array/program_list"
android:prompt="#string/programm_prompt"
android:layout_marginBottom="30sp"
android:backgroundTint="#e9ffc1"
android:background="#e9ffc1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/lev"
android:textSize="32sp"
android:textStyle="bold"
android:textColor="#ff000000"
/>
<Spinner
android:id="#+id/level_spinner"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginBottom="30sp"
android:backgroundTint="#e9ffc1"
android:background="#e9ffc1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sem"
android:textSize="32sp"
android:textStyle="bold"
android:textColor="#ff000000"/>
<Spinner
android:id="#+id/sem_spinner"
android:layout_width="match_parent"
android:layout_height="40dp"
android:backgroundTint="#e9ffc1"
android:background="#e9ffc1"
android:layout_marginBottom="20dp"></Spinner>
<Button
android:id="#+id/btnEnter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/enter"
android:textSize="25sp"
android:textStyle="bold"
android:textColor="#ff000000"
/>
level_menu.java
package inc.zibit.com.conware;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
public class Level_menu extends ActionBarActivity implements OnItemSelectedListener {
private Spinner program_spinner;
private Spinner level_spinner;
private Spinner sem_spinner;
private Button btnEnter;
ArrayAdapter<String> program_adapter;
List<String> program_list = new ArrayList<String>(Arrays.asList(getResources().getStringArray(R.array.program_list)));
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_level_menu);
addItemsOnSpinner2();
addItemsOnSpinner3();
addListenerOnButton();
// addListenerOnSpinnerItemSelection();
program_spinner = (Spinner) findViewById(R.id.program_spinner);
level_spinner = (Spinner) findViewById(R.id.level_spinner);
sem_spinner = (Spinner) findViewById(R.id.sem_spinner);
program_spinner.setOnItemSelectedListener(this);
level_spinner.setOnItemSelectedListener(this);
sem_spinner.setOnItemSelectedListener(this);
program_adapter=new ArrayAdapter<String>(getBaseContext(),android.R.layout.simple_spinner_item, program_list);
program_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
program_spinner.setAdapter(program_adapter);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (!(program_spinner.getSelectedItem().toString().trim().equals("Bsc. Computer Science"))) {
if (level_spinner.getSelectedItem().toString().trim().equals("Level 100")) {
if (sem_spinner.getSelectedItem().toString().trim().equals("1st Semester")) {
startActivity(new Intent(Level_menu.this, CompScience.class));
}
}
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
// add items into spinner dynamically
public void addItemsOnSpinner2() {
level_spinner = (Spinner)findViewById(R.id.level_spinner);
List<String> list = new ArrayList<String>();
list.add("Level 100");
list.add("Level 200");
list.add("Level 300");
list.add("Level 400");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
level_spinner.setAdapter(dataAdapter);
}
public void addItemsOnSpinner3() {
sem_spinner = (Spinner)findViewById(R.id.sem_spinner);
List<String> list = new ArrayList<String>();
list.add("1st Semester");
list.add("2nd Semester");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sem_spinner.setAdapter(dataAdapter);
}
public void addListenerOnButton() {
btnEnter = (Button) findViewById(R.id.btnEnter);
btnEnter.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if ( v.getId()==R.id.btnEnter){
Intent intent = new Intent(Level_menu.this,CompScience.class);
startActivity(intent);
}
}
});
}
}
When i run it crashes.. what am I not doing right?
Without any logs provided, my best guess would be that NullPointerException causes your crash inside onItemSelected() callback. If any of those 3 spinners have nothing selected, you will get NPE.
I suggest you first make sure that spinner.getSelectedItem() is not null, before comparing strings.

Editing ListView Dynamically in Android

Suppose I've already made a list using ListView. The list consists of colour names.
Now I take one EditText text field and one Button widget. I enter the text(colour name) and press Button. The colour name entered should be added to the list. Can anyone help me with the code?
MainActivity class
package com.example.asd;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends Activity
{
private ListView list;
private EditText et;
private ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter= new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1);
adapter.add("white");
adapter.add("black");
adapter.add("read");
list=(ListView)findViewById(R.id.listView1);
list.setAdapter(adapter);
et=(EditText)findViewById(R.id.editText1);
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
adapter.add(et.getText().toString());
}
});
}
}
layout
<RelativeLayout 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"
android:background="#android:color/black">
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="25dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText1"
android:layout_below="#+id/editText1"
android:text="Button" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:cacheColorHint="#00000000"
android:layout_below="#+id/button1" >
</ListView>
</RelativeLayout>
I think you are looking for this...
public class Colors extends Activity {
private EditText et1;
private Button but1;
private ListView list;
private ArrayList<String> colornames=new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 = (EditText)findViewById(R.id.et1);
but1 = (Button)findViewById(R.id.but1);
list = (ListView)findViewById(R.id.list1);
but1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
colornames.add(et1.getText().toString());
ArrayAdapter<String> adapter=new ArrayAdapter<String>(Colors.this, android.R.layout.simple_list_item_1,colornames);
list.setAdapter(adapter);
}
});
}
And the output looks like this

Change color of items in spinner

I want to change the color of items in spinner to say 3 different colors and all the items should be added dynamically.
I am adding items in the spinner through list.
Suppose i have 2 lists and i want to merge both list items in single spinner item but
both should be of different colors.
My xml file is :
<?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" >`
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="#66CCFF"
android:gravity="center"
android:text="abcd"
android:textColor="#android:color/white"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/tvMaterial"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/material"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="#+id/spinMaterial"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/tvWeight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Weight"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/etWeight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="Enter Weight"
>
</EditText>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/bAddCat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Add" />
<Button
android:id="#+id/bCancelCat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel" />
</LinearLayout>
</LinearLayout>
and Java class is
package com.androidui;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class AddCategory extends Activity {
TextView tv1;
String cat = null;
Spinner spin;
Button add;
Button cancel;
EditText etWt;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.add_category);
tv1 = (TextView)findViewById(R.id.textView1);
add = (Button)findViewById(R.id.bAddCat);
cancel = (Button)findViewById(R.id.bCancelCat);
etWt = (EditText)findViewById(R.id.etWeight);
Bundle extras = getIntent().getExtras();
if (extras != null) {
cat = extras.getString("name");
tv1.setText(cat);
}
addItemsOnSpinner();
//Onclick listners to buttons
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(AddCategory.this,
"Material : "+ spin.getSelectedItem().toString() + "\nWeight : " + etWt.getText().toString(),
Toast.LENGTH_LONG).show();
}
});
cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
public void addItemsOnSpinner() {
spin = (Spinner) findViewById(R.id.spinMaterial);
List<String> list = new ArrayList<String>();
list.add("Material 1");
list.add("Material 2");
list.add("Material 3");
list.add("Material 4");
list.add("Material 5");
list.add("Material 6");
//Second List
List<String> list2 = new ArrayList<String>();
list2.add("Mat 7");
list2.add("Mat 8");
list2.add("Mat 9");
//Combined List
List<String> listCombine = new ArrayList<String>();
listCombine.addAll(list);
listCombine.addAll(list2);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, listCombine);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(dataAdapter);
}
}
Please suggest a method.
Thanx
You can try SpannableString for coloring your text http://developer.android.com/reference/android/text/SpannableString.html

Want to add item in a list in android at runtime

<?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" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/speed"
android:inputType="number"></EditText>"
<TextView
android:id="#+id/TextViewSpeed"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text ="Speed"
android:layout_below="#+id/speed"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="AddValue"
android:id="#+id/AddValue"
>
</Button>
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ListView"
android:layout_below="#+id/AddValue"
>
</ListView>
</RelativeLayout>
This is my layout code. I wanted to add a text data from EditText to the ListView which is on same page. How to write a code to add text to the list when I click on the AddValue Button.
Thanks in advance.
use this code
addbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String val = edittext.getText().toString();
list.add(val);
((ArrayAdapter<Object>) listView.getAdapter()).notifyDataSetChanged();
}
});
Below snippet wil help you.
package org.sample;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
public class SampleActivity extends Activity
{
private Button add;
private EditText speedText;
private ArrayAdapter<String> adapter;
private ListView AddValue;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1);
AddValue=(ListView)findViewById(R.id.AddValue);
AddValue.setAdapter(adapter);
add = (Button) findViewById(R.id.add);
speedText = (EditText) findViewById(R.id.speed);
add.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
if (speedText.getText().toString().length() != 0)
{
adapter.add(speedText.getText().toString());
adapter.notifyDataSetChanged();
}
}
});
}
}

Android 2.2 requestFocus() and spinners

I'm having problems using s.requestFocus() when s is a spinner. Is there a special treatment to get it to work when it's a spinner ?
Thanks
try this code..
spinner.setFocusable(true);
spinner.setFocusableInTouchMode(true);
I'm guessing you aren't literally just looking to "give focus" to the spinner. When you give focus to an EditText, the keyboard pops up, so you may be expecting the spinner selection to "open up" on focus - but it doesn't work that way (don't ask me why). Use s.performClick() to do this - it will act just as if the user clicked on the spinner control.
use this
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class dynamic_spinner_main extends Activity {
private Spinner m_myDynamicSpinner;
private EditText m_addItemText;
private ArrayAdapter<CharSequence> m_adapterForSpinner;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_spinner);
///////////////////////////////////////////////////////////////
//grab our UI elements so we can manipulate them (in the case of the Spinner)
// or add listeners to them (in the case of the buttons)
m_myDynamicSpinner = (Spinner)findViewById(R.id.dynamicSpinner);
m_addItemText = (EditText)findViewById(R.id.newSpinnerItemText);
Button addButton = (Button)findViewById(R.id.AddBtn);
Button clearButton = (Button)findViewById(R.id.ClearBtn);
////////////////////////////////////////////////////////////////
//create an arrayAdapter an assign it to the spinner
m_adapterForSpinner = new ArrayAdapter(this, android.R.layout.simple_spinner_item);
m_adapterForSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
m_myDynamicSpinner.setAdapter(m_adapterForSpinner);
m_adapterForSpinner.add("gr");
m_myDynamicSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
Intent mIntent=new Intent(dynamic_spinner_main.this,sampleLocalization.class);
mIntent.putExtra("lang", m_myDynamicSpinner.getItemIdAtPosition(position));
System.out.println("Spinner value...."+m_myDynamicSpinner.getSelectedItem().toString());
startActivity(mIntent);
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
////////////////////////////////////////////////////////////////
//add listener for addButton
addButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
addNewSpinnerItem();
}
});
////////////////////////////////////////////////////////////////
//add listener for addButton
clearButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
clearSpinnerItems();
}
});
}
private void addNewSpinnerItem() {
CharSequence textHolder = "" + m_addItemText.getText();
m_adapterForSpinner.add(textHolder);
}
private void clearSpinnerItems() {
m_adapterForSpinner.clear();
m_adapterForSpinner.add("dummy item");
}
}
main_spinner.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:layout_height="wrap_content"
android:layout_margin="4px"
android:id="#+id/newSpinnerItemText"
android:layout_width="fill_parent"></EditText>
<Button android:layout_height="wrap_content"
android:id="#+id/AddBtn"
android:layout_margin="4px"
android:layout_width="fill_parent"
android:text="Add To Spinner"></Button>
<Button android:layout_height="wrap_content"
android:id="#+id/ClearBtn"
android:layout_margin="4px"
android:layout_width="fill_parent"
android:text="Clear Spinner Items"></Button>
<Spinner android:layout_height="wrap_content"
android:id="#+id/dynamicSpinner"
android:layout_margin="4px"
android:layout_width="fill_parent"></Spinner>
</LinearLayout>
Spinner isn't focusable by default (source). So you have to make it focusable either in xml
<Spinner
android:id="#+id/spinner"
android:focusable="true"
android:layout_width="wrap_conten"
android:layout_height="wrap_content" />
or in code
findViewById(R.id.spinner).setFocusable(true);

Categories

Resources