Android ListView with Adding Dynamically Data and Search Functionality

Posted by

Hello everyone and welcome to easytechsolutions.info so our today’s post is about working on listview in android and dynamically add values on button click in list and also search functionality.

ListView in android is a layout view in which you can show your data in a list format like one after another and it has a inbuilt property of scrolling too so that whenever your data is more than to be fit inside the screen of app you can scroll to see rest of the data .

So let’s get started with a simple android app in which we have a edittext and a add button so that whenever you type any data in it and tap add button that data will directly added into the listview .

Steps you need to follow:-

Step 1:- Open up your Android Studio and make a new project name it whatever you want  and select empty activity.

Step 2:- Open up your activity_main.xml and copy paste below code in it:-

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_marginTop="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginBottom="16dp"
    android:layout_marginRight="16dp"
    tools:context="com.edittextlistviewexample.MainActivity">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/search1"
        android:hint="Search...."
        android:paddingLeft="5dp"
        android:ems="15"
        >
    </EditText>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

    <EditText
        android:id="@+id/edit1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="Enter Text To Add In ListView"/>

    <Button
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.3"
        android:text="Submit"
        android:fontFamily="sans-serif-condensed"
        android:textStyle="bold"
        android:background="#ff00"
        android:textColor="#fff"/>
        </LinearLayout>

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listview1"
        android:layout_marginTop="16dp"/>

</LinearLayout>

Step 3:- Open up your MainActivity.java class and copy paste below code in it:-

MainActivity.java

package com.edittextlistviewexample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    //Defining EditText and Button Variables:-
    EditText editText;
    Button button;
    ListView listView;
    EditText search;
    String name;
    //public String cs;

    //Defining ArrayAdapterList Variable:-
     ArrayList<String> list = new ArrayList<String>();
     ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //FindViewById EditText , Button and ListView:-
         editText = (EditText) findViewById(R.id.edit1);
         button = (Button) findViewById(R.id.button1);
         listView = (ListView) findViewById(R.id.listview1);
         search = (EditText) findViewById(R.id.search1);

        //Setting SearchView to give Search Options:-


        //Making adapter instance and pass 3 parameters inside it(Context , layoutView and list):-
        adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, (List<String>) list);
        listView.setAdapter(adapter);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                name = editText.getText().toString();
                if(name.length() > 0 )
                {
                    list.add(name);
                    adapter.notifyDataSetChanged();
                }
                editText.setText("");
            }
        });

        //Setting AddTextChangeListner on EditText:-
        search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            adapter.getFilter().filter(s.toString());

            }

            @Override
            public void afterTextChanged(Editable s) {


            }
        });


    }
}

That’s it just save all and run your app you will get to see a screen with search bar at top , simple edittext and right of it there is a add button too now just write any data inside edittext and tap on add button it will add that data to your listview in that screen below edittext and there is also search functionality so data you can directly search specific data inside your listview.

our facebook page :-https://www.facebook.com/easytechsolutions.info/
LIKE | SHARE | SUPPORT

Leave a Reply

Your email address will not be published. Required fields are marked *