MainActivity.java
--------------------
package com.sam.web.spinner;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
String[] countryNames;
private Spinner spinner;
private TextView textView;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
countryNames=getResources().getStringArray(R.array.country_names);
spinner=(Spinner) findViewById(R.id.spinnerId);
textView=(TextView) findViewById(R.id.textViewID);
button=(Button) findViewById(R.id.buttonId);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,R.layout.sample_view,R.id.textViewsampleId,countryNames);
spinner.setAdapter(adapter);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String value=spinner.getSelectedItem().toString();
textView.setText(value);
}
});
}
}
=========================================================================
activity_main.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"
android:background="#78f4ca"
android:padding="10dp"
android:gravity="center|top"
tools:context="com.sam.web.spinner.MainActivity">
<Spinner
android:id="@+id/spinnerId"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Spinner>
<Button
android:id="@+id/buttonId"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="print"
android:textSize="20sp"
android:textStyle="bold"
/>
<TextView
android:id="@+id/textViewID"
android:layout_marginTop="20dp"
android:textStyle="bold"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
=====================================================================================
string.xml
------------
<resources>
<string name="app_name">Spinner</string>
<string-array name="country_names">
<item>Australia</item>
<item>Bangladesh</item>
<item>Brazil</item>
<item>HongKong</item>
<item>India</item>
<item>Italy</item>
<item>Laos</item>
<item>NewZialand</item>
<item>Pakistan</item>
<item>Russia</item>
<item>UK</item>
</string-array>
</resources>
======================================================================================
sample_view.xml
-----------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textViewsampleId"
android:paddingLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
/>
</LinearLayout>
thistimebd Bangladesh Live online newsportal, education, Lifestyle, Health, Photography, gif image etc.
Make your own name or company name website | contact: thistimebd24@gmail.com
Copyright © 2020-2024 News Portal in Bangladesh - THISTIMEBD.COM. ALL Rights Reserved.