Program description:
This is a ListView program with options menu, and context menu.
This activity has a button, and edit text. User will enter the item in edit text and clicks button to add element to list view dynamically.
This program contains an options menu with send, add, and remove buttons.
On clicking send button all list view items will be sent to next activity. Next activity will display all the elements in toast messages. This is done by using putExtras.
On clicking add button in menu items, it will check if edit text is having any element to add to list view. If there is any it will be added to list view, else a toast message.
On clicking remove button in menu items, it will remove 0th element from list view.
Note: I have not added any check if list view is empty or not, so if you click remove from menu and if list view is empty, it will crash your program.
This program also contains a context menu to be displayed when ever user press and hold on any list view items, and asks user if he wants to delete that item from list view. On clicking remove it will remove that item from list view.
First activity
package com.techpalle.b15_listviewassignment;xml file for first activity
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;
public class MainActivity extends Activity {
EditText et;
Button b;
ListView lv;
ArrayListal;
ArrayAdapteraa;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.editText1);
b = (Button) findViewById(R.id.button1);
lv = (ListView) findViewById(R.id.listView1);
al = new ArrayList();
aa = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, al);
lv.setAdapter(aa);
registerForContextMenu(lv);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
al.add(0, et.getText().toString());
aa.notifyDataSetChanged();
et.setText("");
}
});
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.myconmenu, menu);
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo ac = (AdapterContextMenuInfo)
item.getMenuInfo();
int id = ac.position;
al.remove(id);
aa.notifyDataSetChanged();
return super.onContextItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.item1: //send
Intent in = new Intent(getApplicationContext(),
SecondScreen.class);
in.putStringArrayListExtra("items", al);
startActivity(in);
break;
case R.id.item2: //add
if(et.getText().toString().equals("")){
Toast.makeText(getApplicationContext(),
"plz try again", 0).show();
}else{
al.add(et.getText().toString());
aa.notifyDataSetChanged();
et.setText("");
}
break;
case R.id.item3: //remove
al.remove(0);
aa.notifyDataSetChanged();
break;
}
return super.onOptionsItemSelected(item);
}
}
File name : activity_main.xml
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:hint="enter item to add"
android:ems="10" >
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:layout_marginLeft="53dp"
android:text="Add to ListView" />
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/button1"
android:layout_marginTop="22dp" >
Second activity
package com.techpalle.b15_listviewassignment;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
public class SecondScreen extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Intent in = getIntent();
ArrayListal = in.getStringArrayListExtra("items");
for(String item:al){
Toast.makeText(getApplicationContext(), item, 0).show();
}
}
}
xml file for second activity
File name : second.xml
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to second screen"
android:textAppearance="?android:attr/textAppearanceLarge" />
menu file for options menu
File name : main.xml
menu file for context menu
File name : main.xml
Download complete code : Click to download
Tags : android, listview, context menu for list items, listview context menu, options menu, pass list view elements to second activity.
No comments:
Post a Comment