Android | ToDoList Acitvity

Total Files : 1)Todolist.java 2) todolist.xml 3)string.xml

1)ToDoList.java

package myapplication.example1.com.hackonlycom;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;

import java.util.ArrayList;

import static android.view.KeyEvent.KEYCODE_ENTER;

public class ToDoList extends Activity {
    EditText myEditText;
    ListView myListView;


    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.todolist);
        initialize();
        //To create arraylist of to do items        final    ArrayList todoItems=new ArrayList();

        final ArrayAdapter aa=new ArrayAdapter(this,android.R.layout.simple_list_item_1,todoItems);

        //To Bind the adapter to llstview        myListView.setAdapter(aa);
        myEditText.setOnKeyListener(new View.OnKeyListener() {
            @Override            public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {

                if(keyEvent.getAction()==KeyEvent.ACTION_DOWN)//If any key pressed go for another if or returns false                    if((keyCode==keyEvent.KEYCODE_DPAD_CENTER)||(keyCode==KEYCODE_ENTER)){
                    //Key code constant: Directional Pad Center key. May also be synthesized from trackball motions.                        todoItems.add(0,myEditText.getText().toString());//0 is index                        aa.notifyDataSetChanged();//change update to listview                        myEditText.setText("");
                        return  true;
                }
                return false;//Bydefault onKey() returns false            }
        });

    }
    void initialize(){
    myEditText=findViewById(R.id.et1);
    myListView=findViewById(R.id.lv1);
    }
}


2. todolist.xml

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">
<EditText    android:id="@+id/et1"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:hint="@string/addItemHint"    android:contentDescription="@string/addItemContentDescription"/>
    <ListView        android:id="@+id/lv1"        android:layout_width="match_parent"        android:layout_height="wrap_content"></ListView>

</LinearLayout>



3)string.xml

<string name="app_name">ToDoList</string>
<string name="addItemHint">New To Do Item</string>
<string name="addItemContentDescription">New To Do Item</string>
Reactions:

0 comments:

Post a Comment