Blog purpose for android basic example for android app developer. any query please my contact

Saturday, 2 December 2023

how to create simple to do list in android studio example

 how to create simple to do list in android studio example


Today learn how to create simple to do list in android studio example follow this Step in blog.

1.Main Activity.java:

import java.util.ArrayList;

import android.R.string;

import android.os.Bundle;

import android.app.Activity;

import android.view.KeyEvent;

import android.view.Menu;

import android.view.View;

import android.widget.ArrayAdapter;

import android.widget.EditText;

import android.widget.ListView;

 

public class MainActivity extends Activity {

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        ListView lv = (ListView) findViewById(R.id.ListView);

        final EditText text = (EditText) findViewById(R.id.edittext);

 

        final ArrayList<String> do_list = new ArrayList<String>();

        final ArrayAdapter<String> adapter;

 

        adapter = new ArrayAdapter<String>(this,

                android.R.layout.simple_list_item_1, do_list);

 

        lv.setAdapter(adapter);

 

        text.setOnKeyListener(new View.OnKeyListener() {

 

            @Override

            public boolean onKey(View v, int keyCode, KeyEvent event) {

                // TODO Auto-generated method stub

                if (event.getAction() == KeyEvent.ACTION_DOWN) {

                    if ((keyCode == KeyEvent.KEYCODE_DPAD_CENTER)

                            || (keyCode == KeyEvent.KEYCODE_ENTER)) {

                        do_list.add(0, text.getText().toString());

                        adapter.notifyDataSetChanged();

                        text.setText("");

                        return true;

                    }

                }

                return false;

            }

        });

    }

 

    @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, menu);

        return true;

    }

 

}


2. DolistitemView.java:



import android.content.Context;

import android.content.res.Resources;

import android.graphics.Canvas;

import android.graphics.Paint;

import android.util.AttributeSet;

import android.widget.TextView;

 

public class dolistitemview extends TextView {

 

    private Paint marginPaint;

    private Paint linePaint;

    private int paperColor;

    private float margin;

 

    public dolistitemview(Context context, AttributeSet attrs, int defStyle) {

        super(context, attrs, defStyle);

        // TODO Auto-generated constructor stub

        init();

    }

 

    private void init() {

 

        Resources myResources = getResources();

 

        marginPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

        marginPaint.setColor(myResources.getColor(R.color.notepad_margin));

        linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);

        linePaint.setColor(myResources.getColor(R.color.notepad_lines));

        paperColor = myResources.getColor(R.color.notepad_paper);

        margin = myResources.getDimension(R.dimen.notepad_margin);

    }

 

    @Override

    public void onDraw(Canvas canvas) {

        // Use the base TextView to render the text.

        canvas.drawColor(paperColor);

 

        //drawing lines

        canvas.drawLine(0, 0, 0, getMeasuredHeight(), linePaint);

        canvas.drawLine(0, getMeasuredHeight(), getMeasuredWidth(),

                getMeasuredHeight(), linePaint);

        //the margin line

        canvas.drawLine(margin, 0, margin, getMeasuredHeight(), marginPaint);

 

        canvas.save();

        canvas.translate(margin, 0);

        super.onDraw(canvas);

        canvas.restore();

    }

}



3.Main.xml:


<LinearLayout 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:orientation="vertical"

    tools:context=".MainActivity" >

 

    <EditText

        android:id="@+id/edittext"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content" />

 

    <ListView

        android:id="@+id/ListView"

        android:layout_width="match_parent"

        android:layout_height="wrap_content" />

 

</LinearLayout>




4. Dolistitem.xml:


<?xml version="1.0" encoding="utf-8"?>

 

<com.example.to_do_list.dolistitemview xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:fadingEdge="vertical"

    android:padding="10dp"

    android:scrollbars="vertical"

    android:textColor="@color/notepad_text" />

No comments:

Post a Comment