<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
tools:context=".MainActivity3"
android:id="@+id/main0"
>
<Spinner
android:id="@+id/spinner0"
android:layout_width="250dp"
android:layout_height="50dp"
android:background="@drawable/asdf0"
android:layout_alignParentEnd="true"
android:layout_centerInParent="true"
android:layout_marginEnd="20dp" />
<TextView
android:id="@+id/textview0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_toStartOf="@id/spinner0"
android:layout_marginEnd="25dp"
android:text="TEXT" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.4"
android:text="spinner"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners
android:radius="10dp"/>
<stroke
android:width="1dp"
android:color="@color/black" /> <!-- μ -->
</shape>
package com.example.spinner;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView textView;
Spinner spinner;
String[] items = {"AAAA", "SSSS", "DDDD", "QQQQ", "PPPP"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textview0);
spinner = findViewById(R.id.spinner0);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, items); //(MainActivity, simple_spinner_item λ μ΄μμ, items λ°°μ΄)
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) { //(adapter, view κ°μ²΄, view indexκ°, idκ°)
textView.setText(items[position]);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
textView.setText(" ");
}
});
}
}
μμ±ν μ 체 μμ€μ½λ github : Android_Study/spinner/