activity_main.xml
<TextView
android:id="@+id/txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#80f0"
android:gravity="center"
android:text="country"
android:textSize="30dp" />
<Spinner
android:id="@+id/sp01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp" />
MainActivity.java
public class MainActivity extends AppCompatActivity {
TextView textView;
Spinner spinner;
ArrayAdapter<String> adapter = null;
String[] items = {"korea", "china", "japan", "france"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.txt);
spinner = findViewById(R.id.sp01);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
}
---seperate data---
activity_main.xml
<TextView
android:id="@+id/txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#80f0"
android:gravity="center"
android:text="country"
android:textSize="30dp" />
<Spinner
android:id="@+id/sp01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp" />
country.xml
<string-array name="country">
<item>korea</item>
<item>china</item>
<item>japan</item>
<item>france</item>
</string-array>
MainActivity.java
public class MainActivity extends AppCompatActivity {
Spinner spinner;
ArrayAdapter<CharSequence> adapter = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = findViewById(R.id.sp01);
adapter = ArrayAdapter.createFromResource(this, R.array.country, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
}
'android studio' 카테고리의 다른 글
inflation (0) | 2019.08.11 |
---|---|
GridView (0) | 2019.08.11 |
AdapterView(ListView) (0) | 2019.08.10 |
view에 도형, 텍스트 그리기 (0) | 2019.08.10 |
Button, RadioButton, CheckBox, ImageView, ImageButton (0) | 2019.08.10 |