Lab 8
Lab 8
SEL-448
Lab Journal: 8
Task No.1:
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.879" />
</androidx.constraintlayout.widget.ConstraintLayout>
Java File:
package com.example.lab8;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements
AdapterView.OnItemSelectedListener {
String[] arr = { "Paries,France", "PA,United States","Parana,Brazil",
"Padua,Italy", "Pasadena,CA,United States"};
String [] arr1 = {"EE", "CS", "BA", "CE"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AutoCompleteTextView autocomplete = findViewById(R.id.autoCompleteTextView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this,android.R.layout.select_dialog_item, arr);
autocomplete.setThreshold(2);
autocomplete.setAdapter(adapter);
Spinner spin = findViewById(R.id.spinner);
spin.setOnItemSelectedListener(this);
ArrayAdapter<String> sa = new ArrayAdapter<String>
(this,android.R.layout.simple_spinner_item, arr1);
sa.setDropDownViewResource(android.R.layout.simple_spinner_item);
spin.setAdapter(sa);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String str = autocomplete.getText().toString();
Toast.makeText(MainActivity.this,str, Toast.LENGTH_SHORT).show();
}
}); }
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(this,arr1[i], Toast.LENGTH_SHORT).show();}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}}
Output:
Conclusion:
In this lab, we learned about autocomplete text view and spinners in android applications. The main
objective of this lab is how to implement autocomplete text view and spinners in android applications and
also apply threshold in autocomplete text view. All the tasks were completed successfully.