Professional Documents
Culture Documents
activity_list_view.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"
>
<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="25dp"
android:textAlignment="center"
android:padding="15dp"
android:textColor="#F44336"
android:text="Hello Vijay"
android:textStyle="bold"
android:textSize="25dp"
android:gravity="center_horizontal" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:padding="2dp"
android:id="@+id/listview"
/>
</LinearLayout>
ListView.java
package com.example.helloworld;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
TextView textview;
android.widget.ListView listView;
ArrayList arraylist;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
textview = findViewById(R.id.textview);
listView = findViewById(R.id.listview);
listView.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int
position, long id) {
switch (position)
{
case 0:
textview.setBackgroundColor(Color.RED);
break;
case 1:
textview.setBackgroundColor(Color.GREEN);
break;
case 2:
textview.setBackgroundColor(Color.YELLOW);
break;
case 3:
textview.setBackgroundColor(Color.BLUE);
break;
case 4:
textview.setBackgroundColor(Color.MAGENTA);
break;
case 5:
textview.setBackgroundColor(Color.BLACK);
break;
}
}
});
}
}
Output
activity_spinner.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"
tools:context=".Spinner">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edit"
android:inputType="text"
android:layout_margin="8dp"
android:padding="8dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/add"
android:text="ADD"
android:layout_marginLeft="25dp"
android:layout_marginTop="10dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/remove"
android:text="REMOVE"
android:layout_marginLeft="75dp"
android:layout_marginTop="10dp"
/>
</LinearLayout>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spin"
android:layout_margin="15dp"
android:padding="5dp"
/>
</LinearLayout>
Spinner.java
package com.example.helloworld;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.ArrayList;
Button add,remove;
ArrayList<String> arrayList;
ArrayAdapter<String> arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner);
spinner = findViewById(R.id.spin);
editText = findViewById(R.id.edit);
add = findViewById(R.id.add);
remove = findViewById(R.id.remove);
spinner.setAdapter(arrayAdapter);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String item = editText.getText().toString().trim();
if(item =="" || (item.length()==0))
{
Toast.makeText(getApplicationContext(),"Not Found
Item",Toast.LENGTH_LONG).show();
return;
}
boolean result =
arrayList.add(editText.getText().toString());
if(result)
{
arrayAdapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext()," Added
Successfuly",Toast.LENGTH_LONG).show();
}else {
Toast.makeText(getApplicationContext()," Failed Not
Added ",Toast.LENGTH_LONG).show();
}
}
});
remove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String item = editText.getText().toString().trim();
if(item =="" || (item.length()==0))
{
Toast.makeText(getApplicationContext(),"Not Found
Item",Toast.LENGTH_LONG).show();
return;
}
boolean result =
arrayList.remove(editText.getText().toString());
if(result)
{
arrayAdapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext()," Remove
Successfuly",Toast.LENGTH_LONG).show();
}else {
Toast.makeText(getApplicationContext()," Failed Not
Removed ",Toast.LENGTH_LONG).show();
}
}});
spinner.setOnItemSelectedListener(new
AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext()," Selected item :"+
parent.getItemAtPosition(position).toString(),Toast. LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}