I'm looking for something similar to a drop down menu but when you click on it a block of text drops instead of a list of clickable items. Then you should be able to click to close it again when finished. My app is an informational app so, I want to make a list of several of these within a glossary in my app.
Any ideas??
you should use Spinner in android:
Here is the example :
Spinner mSipnner = (Spinner)findViewById(R.id.spin_beneficiary_targetcode);
List<String> categories2 = new ArrayList<String>();
categories2.add("Choose code");
categories2.add("001");
categories2.add("002");
categories2.add("003");
categories2.add("004");
categories2.add("005");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories2);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
////Spinner item selected
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String targetCode_item = parent.getItemAtPosition(position).toString();
// Showing selected spinner item
//Toast.makeText(parent.getContext(), "Selected: " + category_item, Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
// attaching data adapter to spinner
mSpinner.setAdapter(dataAdapter);
Related
I created some fragments in an Activity class as tabs where you can switch through and inside these I have various Spinner. The data I'm getting from a SQL-database with JSONParser and the Spinner gets created in the fragment classes as follow:
spinnerMaterial = (Spinner) view.findViewById(R.id.Spinner_material);
List<String> lables_material = new ArrayList<String>();
for (int i = 0; i < materialList.size(); i++) {
lables_material.add(materialList.get(i).getMaterial());
}
// Creating adapter for spinner
ArrayAdapter<String> spinnerAdapter_material = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_item, lables_material);
// Drop down layout style - list view with radio button
spinnerAdapter_material
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinnerMaterial.setAdapter(spinnerAdapter_material);
// spinner item select listener
spinnerMaterial.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view,
int position, long id) {
Object item = adapterView.getItemAtPosition(position);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
// TODO Auto-generated method stub
}
});
Actually everything is working. I'm getting all the data and the Spinner are poping up. BUT not in a direct way. So I need to switch between the tabs (fragments) and then go back that the Spinner is showing the data. Strange fact is that when I try to debug and go over some breakpoints the Spinner pops up immediately (without switching tabs).
Do you have an idea what the problem could be?
I made a listview and a spinner with 3 options. I want that spinner to connect with Listview on other intent.
The spinner is on a Edit page so whenever I click on 1 item on the list I get to the edit page.
I want the first option on the spinner to make the selected item on the list green the second will make it yellow and then third will make it red. How do I do it?
public class EditList extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
Spinner spinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_list);
final Button button=(Button)findViewById(R.id.save);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(EditList.this,Vans.class);
startActivity(intent);
}
});
// Spinner element
spinner = (Spinner) findViewById(R.id.spinner);
// Spinner click listener
spinner.setOnItemSelectedListener(EditList.this);
// Spinner Drop down elements
List<String> categories = new ArrayList<>();
categories.add("רכב עובד");
categories.add("יש תקלה");
categories.add("רכב במוסך");
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
}
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String work = parent.getItemAtPosition(position).toString();
if (position==0){
View listViewChildAt = Vans.listView.getChildAt(position);
listViewChildAt.setBackgroundColor(Color.GREEN);
}
Vans.listView.setBackgroundColor(position);
spinner.setSelection(position);
String takala = parent.getItemAtPosition(position).toString();
if (position==1){
View listViewChildAt = Vans.listView.getChildAt(position);
listViewChildAt.setBackgroundColor(Color.YELLOW);
}
String garage = parent.getItemAtPosition(position).toString();
if (position==2){
View listViewChildAt = Vans.listView.getChildAt(position);
listViewChildAt.setBackgroundColor(Color.RED);
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
I am new in android ,I want work like, In Spinner if i select any other items form array,that item should get selected including with some text,For example,if My spinner having 10 items,if i select 6th item that item should display like "Your Item Is: Item6",This should be display in my spinner as first item,After select of items form the array,Please help me ,,Thanks in advance..
String[] categories = {"Select one","Automobile","Computers","Education"};
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
then onItemSelected Listerner Do :-
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
categories[0] = item;
ArrayAdapter<String> newdataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
spinner.setAdapter(newdataAdapter);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
ArrayAdapter arrayAdapter = null;
String[] list=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//you can replace the below list with your list
list = {"Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune","Pluto"};
final Spinner spinner = (Spinner) findViewById(R.id.spinner);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner.setAdapter(arrayAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
spinner.setPrompt("Your selected item is:"+list[position]);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
I have used android custom spinner.
My custom spinner look like bellow
My List data is like
List<String> list = new ArrayList<String>();
list.add("001-00434016457-Md. Mokhhlesur Rahman");
list.add("001-00434016724-Mohammad Nazim Ullah");
list.add("001-00434016725-Mohsin Kabir");
list.add("001-00434016881-Maruf Pervaz Khan");
list.add("001-00434017171-Md.Nazmul Hasan");
My Spinner Adapter and setting data in spinner is like bellow
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
My problem is spinner's item text is large like "001-00434016881-Maruf Pervaz"
I want to select only id like 001-00434016881.
I don't want to get full text of the spinner item selection. How can I remove name part like -Maruf Pervaz Khan from 001-00434016881-Maruf Pervaz on item selection from spinner.
Please Help me
Try this. If the row item maintains same pattern as 001-00434016881-Md.Some Name
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, adapter.getItem(position).substring(0,adapter.getItem(position).lastIndexOf("-")), Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
Spinner item initially text value should be empty so i added empty vaue in spinner item but it shows the first spinner item is empty .please rectify my mistake which i have done
Required
Initially spinner should be empty
when selected spinner it should show only two items
spinner.java
LoginStatus_List = new ArrayList<String>();
LoginStatus_List.add("");
LoginStatus_List.add("Approve");
LoginStatus_List.add("Reject");
ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item,LoginStatus_List);
dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnrLognStatus.setAdapter(dataAdapter1);
spnrLognStatus.setSelection(0);
You have to create a costum SpinnerAdapter to achieve this.
Try this
You can set the first spinneritem empty: " ". But in this simple solution the user is still able to select the displayed empty spinneritem.
You can add a kind of "title" into your spinner and ignore it if the user select it. It's not exactly an empty line but the title will act as an hint to guide the user.
private static final String TITLE ="Select the action";
...
ArrayList<String> LoginStatus_List = new ArrayList<String>();
LoginStatus_List.add(TITLE);
LoginStatus_List.add("Approve");
LoginStatus_List.add("Reject");
ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item,LoginStatus_List);
dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnrLognStatus.setAdapter(dataAdapter1);
spnrLognStatus.setSelection(0);
spnrLognStatus.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String selection = (String)spnrLognStatus.getSelectedItem();
if(TITLE.equalsIgnoreCase(selection)){
// Ignore the selected title
}else{
// Do want you want here
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});