In: Computer Science
android studio
Reproduce the same action as this using a ListView.
<resources> <string-array name="pizzas"> <item>Ham and Pineapple</item> <item>Supreme</item> <item>Seafood</item> <item>Italian</item> <item>Meat Lovers</item> </string-array> </resources>
Following is the layout XML File
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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=".MainActivity"> <Spinner android:id="@+id/spinner" android:layout_width="368dp" android:layout_height="wrap_content" tools:layout_editor_absoluteX="8dp" tools:layout_editor_absoluteY="42dp" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" tools:layout_editor_absoluteX="163dp" tools:layout_editor_absoluteY="145dp" /> </android.support.constraint.ConstraintLayout>
Following is the JAVA main File.
package com.example.wincrap.myapplication; import android.support.v7.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 { private Spinner spinner; public TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); spinner = (Spinner)findViewById(R.id.spinner); textView = (TextView) findViewById(R.id.textView); ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.pizzas, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); spinner.setOnItemSelectedListener( new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { textView.setText(String.valueOf(parent.getItemAtPosition(position).toString())); } @Override public void onNothingSelected(AdapterView<?> parent) { } } ); setContentView(R.layout.activity_main); } }
For the above program, following are the files
android_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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=".MainActivity"> <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="fill_parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout>
MainActivity.java
package com.example.wincrap.myapplication; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.listView); ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.pizzas, android.R.layout.simple_list_item_1); //System.out.println(R.array.pizzas) adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); listView.setAdapter(adapter); listView.setOnItemClickListener( new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(MainActivity.this, parent.getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show(); } } ); } }
pizzas.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="pizzas"> <item>Ham and Pineapple</item> <item>Supreme</item> <item>Seafood</item> <item>Italian</item> <item>Meat Lovers</item> </string-array> </resources>
The location of the files would be the same as it was
before.
Following are the ScreenShots of the running app on my phone.
That was a nice
question to answer
Friend, If you have any doubts in understanding do let me know in
the comment section. I will be happy to help you further.
Please like if you think effort deserves a like.
Thanks