package com.example.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class MainActivity extends AppCompatActivity {
    ListView list;
    String[] mobileArray = {"Android", "IPhone", "WindowsMobile", "Blackberry",
            "WebOS", "Ubuntu", "Windows7", "Max OS X"};
    EditText edit_query;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        edit_query=findViewById(R.id.edit_query);
        list = findViewById(R.id.list);
        final ArrayList<String> arrayList=new ArrayList<String>(Arrays.asList(mobileArray));
       findViewById(R.id.buttonPanel).setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               if(!edit_query.getText().toString().isEmpty()){
                if(arrayList.contains(edit_query.getText().toString())){
                    Toast.makeText(MainActivity.this,"Already it is in list",Toast.LENGTH_LONG).show();
                }else{
                    arrayList.add(edit_query.getText().toString());
                    ArrayAdapter adapter = new ArrayAdapter<String>(MainActivity.this,
                            android.R.layout.simple_list_item_1, Collections.synchronizedList(arrayList));
                    list.setAdapter(adapter);
                }
               }
           }
       });



        ArrayAdapter adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, Collections.synchronizedList(arrayList));
        list.setAdapter(adapter);
    }

}
