Hide text of Android spinner - android

I have a spinner with a background image. But when I add array adapter to the spinner, the text is shown on the background image. I want to hide this text. There is also a TextView. My requirement is, when I click the image, the spinner should pop up and when I selects an item, the TextView gets updated with the selected item.

Spinner spin = (Spinner) d.findViewById(R.id.searchCriteria);
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// hide selection text
((TextView)view).setText(null);
// if you want you can change background here
}
public void onNothingSelected(AdapterView<?> arg0) {}
});

Create ghost_text.xml in your layouts folder with this text:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/transparent" />
Then use that file in your adapter like this:
ArrayAdapter<String> your_adapter = new ArrayAdapter<String>(this,
R.layout.ghost_text, your_list);
your_spinner.setAdapter(your_adapter);
You should also be able to use this technique with other resource-based adapters.

Probably you can define a xml-layout file, without any textview in it, and supply it to the adapter for the spinner.
e.g. empty_spinner_item.xml
<ImageView xmlns:android="http..."
android:layout_width=".."
/>
and then use any adapter:
spinner.setAdapter(new SimpleAdapter(this, data, R.layout.empty_spinner_item, ... ));

Related

Text Color Changes to White when select item from searchable spinner

I am using custom searchable spinner in my android application. In one of my activity, I am using two searchable spinners. one for Citys and one for area locations. On selection of 1st Spinner item, I am Changing the adapter of the 2nd spinner to show Area location of that respective City's. but when I select an item of the 2nd spinner, Text Color of the selected item of spinner changes to White. How to stop it.
I have Attached ScreenShots and Code below.
Before Selecting Any Item
After Selecting Items Of Both Spinner
Activity.java
citySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
// Create an ArrayAdapter using the string array and a default spinner layout
if(citySpinner.getItemAtPosition(i).equals("Mumbai"))
{
adapterArea = ArrayAdapter.createFromResource(getApplicationContext(),
R.array.mumbai, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapterArea.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
areaSpinner.setAdapter(adapterArea);
}
else if(citySpinner.getItemAtPosition(i).equals("Delhi"))
{
adapterArea = ArrayAdapter.createFromResource(getApplicationContext(),
R.array.delhi, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapterArea.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
areaSpinner.setAdapter(adapterArea);
}
else if(citySpinner.getItemAtPosition(i).equals("Thane"))
{
adapterArea = ArrayAdapter.createFromResource(getApplicationContext(),
R.array.thane, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapterArea.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
areaSpinner.setAdapter(adapterArea);
}
else if(citySpinner.getItemAtPosition(i).equals("Select City"))
{
buttonAdd.setEnabled(false);
buttonAdd.setVisibility(View.GONE);
adapterArea = ArrayAdapter.createFromResource(getApplicationContext(),
R.array.blank, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapterArea.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
areaSpinner.setAdapter(adapterArea);
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
areaSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if(areaSpinner.getItemAtPosition(i).equals("Select Area")|| areaSpinner.getItemAtPosition(i).equals("Select City First!"))
{
buttonAdd.setEnabled(false);
buttonAdd.setVisibility(View.GONE);
}
else
{
buttonAdd.setEnabled(true);
buttonAdd.setVisibility(View.VISIBLE);
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
Activity.xml
<com.toptoche.searchablespinnerlibrary.SearchableSpinner
android:id="#+id/spinnerCity"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_marginTop="24dp"
android:entries="#array/city_name"
app:hintText="Select City"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView3" />
<com.toptoche.searchablespinnerlibrary.SearchableSpinner
android:id="#+id/spinnerArea"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_marginTop="32dp"
app:hintText="Select Area"
app:layout_constraintEnd_toEndOf="#+id/spinnerCity"
app:layout_constraintStart_toStartOf="#+id/spinnerCity"
app:layout_constraintTop_toBottomOf="#+id/spinnerCity" />
Change your create adapter code.
Try this,
ArrayAdapter adapterArea=new ArrayAdapter(getBaseContext(),android.R.layout.simple_spinner_dropdown_item,getResources().getStringArray(R.array.mumbai));
areaSpinner.setAdapter(adapterArea);
In the first spinner the layout set from the library by default but in the second spinner, you are setting it programmatically which is android made and which colors are dependable on a theme of your project so my suggestion for you to change the layout with a customized one.

Spinner, strings can be seen in the background

I use to change the difficulty from my game a Spinner Item and use the strings "Leicht","Mittel","Schwer" then i took as background an drawable file, but when i start the application i can see in the background the strings i used for the spinner.. can i hide the strings? that i see just the drawable background?
schwierigkeitsgradAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, new String[] {" Leicht"," Mittel"," Schwer"});
schwierigkeitsgradAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
schwierigkeitsgrad.setAdapter(schwierigkeitsgradAdapter);
and the xml code:
<Spinner
android:id="#+id/schwierigkeitsgrad"
android:layout_gravity="center_horizontal"
android:layout_marginTop="360dp"
android:background="#drawable/btnschwierigkeit"
android:textAlignment="center"
android:layout_width="220dp"
android:layout_height="90dp"
android:gravity="center_vertical|start"
android:foregroundGravity="center_vertical"
android:spinnerMode="dialog" />
try this to hide spinner text during selection:
Spinner spinner = (Spinner)findViewById(R.id.your_spinner);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// hide selection text
((TextView)view).setText(null);
// if you want you can change background here
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
To show spinner items use:
String[] test=new String[]{"easy", "medium", "heavy"};
ArrayAdapter<String> adapter= new ArrayAdapter<String>(yourActivity.this,android.R.layout.simple_spinner_item, test);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
schwierigkeitsgrad.setAdapter(adapter);

Setting spinner text color programmatically lags, is slow, is wrong color for split second

TLDR: My spinner displays the wrong color for a split second.
I have a problem with my spinner. Whenever I run the app, if the activity is not cached in memory, it sometimes lags. The text is a default color (like black) before I can set it to the right color. It looks really unprofessional.
Video:
Please watch this screen recording to see this in action:
https://drive.google.com/file/d/0By2AG5yaBEhMRnRsbVBDU251STQ/view
How it looks for one split-second while loading the page:
How it looks after the lag time (and how it is supposed to look from the start):
Code:
public class MyActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
Spinner spinner = (Spinner) findViewById(R.id.spinner);
//Get rid of the normal toolbar's title, because the spinner is replacing the title.
getSupportActionBar().setDisplayShowTitleEnabled(false);
//Set the choices on the spinner by setting the adapter.
spinner.setAdapter(new SpinnerAdapter(toolbar.getContext(), new String[]{"Overview", "Story", "Specifications", "Poll", "Video"}, accentColor, backgroundColor));
//Set the listener for when each option is clicked.
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
//Change the selected item's text color
((TextView) view).setTextColor(backgroundColor);
}
#Override
public void onNothingSelected(AdapterView<?> parent)
{
}
});
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/ColorPrimary"
android:elevation="4dp">
<Spinner
android:id="#+id/spinner"
app:popupTheme="#style/AppTheme.PopupOverlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</android.support.v7.widget.Toolbar>
What I was doing wrong:
Before, I was following the advice of this answer, and setting the text color in the onItemSelected method, but that method is only called automatically after the UI is done, and you cannot call onItemSelected directly from your code. That was causing a lag. (But it is still needed for when you choose an item from the drop down list - see my solution to this question.)
Solution:
The strategy is to obtain the "Selected" view and set its text color before onCreate finishes. When I tested it in the debugger, no UI is shown during the onCreate method, so this is guaranteed to work.
I just had to add this code after the call to setAdapter(...):
//Set the text color of the Spinner's selected view (not a drop down list view)
spinner.setSelection(0, true);
View v = spinner.getSelectedView();
((TextView)v).setTextColor(backgroundColor);
The key point is to call spinner.setSelection(0, true) with the true parameter. Otherwise, if you just call spinner.setSelection(0), the View v would be null. I found out about this thanks to this answer.
Complete method:
Here is the complete method. NOTE: The code in onItemSelected still needs to be there! Because otherwise, every time you select an item from the drop down list, it will have the wrong color.
#Override
protected void onCreate(Bundle savedInstanceState)
{
Spinner spinner = (Spinner) findViewById(R.id.spinner);
//Get rid of the normal toolbar's title, because the spinner is replacing the title.
getSupportActionBar().setDisplayShowTitleEnabled(false);
//Set the choices on the spinner by setting the adapter.
spinner.setAdapter(new SpinnerAdapter(toolbar.getContext(), new String[]{"Overview", "Story", "Specifications", "Poll", "Video"}, accentColor, backgroundColor));
//Set the text color of the Spinner's selected view (not a drop down list view)
spinner.setSelection(0, true);
View v = spinner.getSelectedView();
((TextView)v).setTextColor(backgroundColor);
//Set the listener for when each option is clicked.
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
//Change the selected item's text color
((TextView) view).setTextColor(backgroundColor);
}
#Override
public void onNothingSelected(AdapterView<?> parent)
{
}
});
}
For more info on the source code of the setSelection methods, see the AbsSpinner.java code here: https://android.googlesource.com/platform/frameworks/base/+/jb-release/core/java/android/widget/AbsSpinner.java
And here is Spinner.java in case it helps: https://android.googlesource.com/platform/frameworks/base/+/jb-release/core/java/android/widget/Spinner.java
If somebody else needs to get separate text color, design, etc. for spinner's selected item and drop-down items you can accomplish it with next code
val adapter = ArrayAdapter(
context,
R.layout.custom_spinner_title_item,
tabsArray.map { it.name }.toTypedArray(),
)
spinner.adapter = adapter
adapter.setDropDownViewResource(android.R.layout.simple_list_item_1)
where
R.layout.custom_spinner_title_item - design file of your selected item
android.R.layout.simple_list_item_1 - design file of drop down items
As a result, there is no more need in AdapterView.OnItemSelectedListener hack

Spinner does not show selected value

I have implemented the spinner by populating the array list through database.I can get and show the array list in my spinner array adapter but if I select the item in spinner it does not shown in spinner?What I had mistake here?
Here is my code,
Spinner spinner1 = (Spinner) findViewById(R.id.prospin);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, providerlist);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
I get the selected item string by using this,
Spinner provid = (Spinner)findViewById(R.id.prospin);
String provider =provid.getSelectedItem().toString();
Can anyone help me out pls!!!
This answer may be a little stupid but try it. It worked for me.
Check the background color of your spinner!
And if it`s white change it
Enjoy it!
I got same problem and solved by adding notifyDataSetChanged() after binding data in Spinner.
First of all I have bind adapter with Blank ArrayList then getting List of Items from Server and added to that List but forgot to notifyDataSetChanged() after updated List.
just add adapter.notifyDataSetChanged(); after updating list.
Hope it will helpful.
Problem:
Spinner displays neither the default nor selected item value. But drop-down menu items are show when selected.
Cause:
Background and text color both being white!!!
Solutions:
xml(Preferable):
Write a custom layout for a spiner item and use it instead of the default,android.R.layout.simple_spinner_item.
How to change spinner text size and text color?
Code(Less reliable):
your_spinner_instance.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
((TextView) view).setTextColor(Color.RED);
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
Android needs some major update or maybe dart and flutter should take over...
Thanks Catluc
Use wrap_content for the height of Spinner.
Probably it does not have enough height to show text.
Well this also happens if the context is not properly given. I was using getApplicationContext() where as it needs getBaseContext().
if you have a custom adapter you should change the TextView text color
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView view = (TextView) super.getView(position, convertView, parent);
view.setTextColor(Color.parseColor("#000000"));
return view;
}
and if you don't have a custom adapter you should just change spinner background
I was using hardcoded value for width and height. After changing to wrap_content it works.
For me the problem is that I was using getApplicationContext(). When I replace getApplicationContext() with this it works fine.
ArrayAdapter<*> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, documentsCategories);
use android:spinnerMode="dropdown" attribute in your declared Spinner element's xml
I just had this problem and after trying all of the solutions listed found out that the issue was that I had set the spinner layout_width to 60dp.
Changed it to fill_parent and problem solved.
This happens occasionally, I guess it's a bug but there's a workaround which is funny but just do it,
In the property bar of the spinner set the background to a different color,
In your .xml where you have your spinner, set:
android:background="#android:drawable/spinner_background_material"
run the code it will generate an error saying the background is private
delete the line of the android:background in the .xml and run the code again,your spinner should work fine showing all the values, if it doesn't take the extra step of setting background tint to a different color preferably black.
Run it again.
I had the same problem, and none of the solutions were working for me. Finally found the problem was that the spinner was being populated from firestore db via an async task.
Rearranged the code so that the adapter is set after the async task completion, and voila!!
add background color white and the details will display
for example
<Spinner
android:id="#+id/size_spinner"
style="#style/FormTextStyle"
android:drawSelectorOnTop="true"
android:layout_marginStart="#dimen/default_gap"
android:background="#android:color/white"
/>
Check these links:
http://www.technotalkative.com/android-spinner-example/
http://www.mkyong.com/android/android-spinner-drop-down-list-example/
Else, try to store it on selecting the item:
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapter, View v,
int position, long id) {
// On selecting a spinner item
String item = adapter.getItemAtPosition(position).toString();
// Showing selected spinner item
Toast.makeText(getApplicationContext(),
"Selected Country : " + item, Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Try this.
final ArrayList<String> providerlist= new ArrayList<String>();
Spinner spinner1 = (Spinner) findViewById(R.id.prospin);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, providerlist);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String item = providerlist.get(position);
// Showing selected spinner item
Toast.makeText(this,
"Selected Country : " + item, Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Generally spinners gets selected by default when they are used ,so try to set blank,or any other data to first position that is zero position, you will get exact position of you selected item .
The issue from what I found was with the style sheet.Use this
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="windowNoTitle">false</item>
<item name="windowActionBar">true</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
For the xml layout use this
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fitsSystemWindows="true"
android:paddingBottom="5dp"
style="#style/AppTheme">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:paddingLeft="24dp"
android:paddingRight="24dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Spinner"
android:layout_marginTop="10dp"
android:textColor="#color/colorBlack"/>
<Spinner
android:id="#+id/Spinner"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:backgroundTint="#color/colorPrimary"
android:textColorHint="#05ab9a"
android:padding="15dp"
style="#style/Base.Widget.AppCompat.Spinner.Underlined"
tools:targetApi="lollipop" />
</LinearLayout>
</ScrollView>
And finally for the class
String [] NUMBERS= {"3 ","6 ","13 "};
Spinner s_spinner = (Spinner) findViewById(R.id.Spinner);
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_dropdown_item_1line, NUMBERS);
// Specify the layout to use when the list of choices appears
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
s_spinner.setAdapter(spinnerAdapter );
This answer may be a little stupid but if you do the same mistake then try...
set values to your ArrayList first and then assign that arrayList to spinner.
I declared a global arrayList and set it to spinner first and then add values to it from another method... at that time i faced the same problem. Otherwise you can do notifyDataSetChanged() to your arrayList.
try this code==>
ArrayAdapter<String> stateNameAdaptor = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, stateNameList);
stateNameAdaptor.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnState.setAdapter(stateNameAdaptor);
Wierd. I had the same problem. What I did to make it work :
1. Add some initial data to the list ( ex.--- please select - -)
2. Load the rest of data and add it to the list
3. Call adapter.notifyDatasetChaged()
In my case the list is populated from firestore before supplied to the adapter construction. The list is already populated and is displayed in spinner. but the size of the list was returned zero. So I only can select the item from the list, but there is no display in the spinner text and unable to call the onitemselected part. I previously made a callback of the querysnapshot and from the snapshot I populated the list. it caused only adding the items to the spinner but cannot be selected. I resolved this by taking a callback of the list instead of the querysnapshot from a repository class and after this my list is populated well and its size is correct and the spinner works fine..my code is attached here.. hope this helps someone...
//interface
public interface SprRbaCallback{
void onSprRbaCallback(List<String> list);
}
//Repository class
public void getRBA(final SprRbaCallback sprRbaCallback){
rbaCollection.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
#Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
for(DocumentSnapshot snapshot : queryDocumentSnapshots){
String item = (String) snapshot.get("dealerName");
assert item != null;
Log.d(TAG, item);
list.add(item);
}
Log.d(TAG, String.valueOf(list.size()));
sprRbaCallback.onSprRbaCallback(list);
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.d(TAG, e.toString());
}
});
}
// calling method..
private void addItemsToRBASpinner() {
firebaseRepository.getRBA(new FirebaseRepository.SprRbaCallback() {
#Override
public void onSprRbaCallback(List<String> list) {
int size = list.size();
Log.d(TAG + "listsize", String.valueOf(size));
ArrayAdapter<String> sprAdaptSelectRBA = new ArrayAdapter<String>(CreateAccountActivity.this,
R.layout.spinneritem, list);
sprAdaptSelectRBA.setDropDownViewResource(R.layout.rbaspinner_dropdown);
spr_rbaSelect.setAdapter(sprAdaptSelectRBA);
sprAdaptSelectRBA.notifyDataSetChanged();
spr_rbaSelect.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.d("NOBY", "ENTERED ON ITEMSELETED");
txt_selectedRBA = (String) parent.getItemAtPosition(position);
Toast.makeText(parent.getContext(), "Selected RBA: " + txt_selectedRBA, Toast.LENGTH_LONG).show();
Log.d("NOBY", txt_selectedRBA);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Log.d("NOBY", "ENTERED NOTHING SELECTED");
txt_selectedRBA = "Dixon";
}
});
}
});
}
This code works fine for me...hope this help someone...:)
In my case I was using ViewModel to take data from repository which further takes data from room database. I was querying a list of string and then observing that to fill the spinner.
My mistake: I was making an adapter outside the ViewModel. Though I was getting the list inside the viewmodel.
My old code :
val machineNameList: MutableList<String> = mutableListOf()
createMachineViewModel.allMachineNames.observe(viewLifecycleOwner, Observer {
machineNameList.addAll(it)
}
val spinnerAdapter = ArrayAdapter(
requireActivity(),
androidx.appcompat.R.layout.support_simple_spinner_dropdown_item,
machineNameList
)
spinnerAdapter.notifyDataSetChanged()
binding.spnMachine.adapter = spinnerAdapter
How did I solved this: Just inflate the spinner inside ViewModel only. Using Logs I was able to find that outside viewmodel the list is still empty. So you have to do like this :
val machineNameList: MutableList<String> = mutableListOf()
createMachineViewModel.allMachineNames.observe(viewLifecycleOwner, Observer {
val spinnerAdapter = ArrayAdapter(
requireActivity(),
androidx.appcompat.R.layout.support_simple_spinner_dropdown_item,
machineNameList
)
machineNameList.addAll(it)
Timber.d("All items are : $machineNameList")
spinnerAdapter.notifyDataSetChanged()
binding.spnMachine.adapter = spinnerAdapter
})
Changing the width and height to wrap_content worked for me. Surprisingly, the given width of some fixed value (60dp in my case) was working in normal mode, but it was showing blank for the landscape mode.
You should use the first Spinner to get the values.
Try the following code:
String provider = spinner1.getSelectedItem().toString();
What you are doing is getting the default value of the spinner.
Try This code
ArrayAdapter<String> arrayAdapte=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,states);
arrayAdapte.setDropDownViewResource(android.R.layout.simple_list_item_1 );
spinnerState.setAdapter(arrayAdapte);
String data=spinnerState.getSelectedItem().toString(); // this is select particular item from list;
int position=spinnerState.getSelectedItemPosition(); // this return position of data selected in list;
out_marginLeft="50dp"
android:layout_marginTop="50dp"
android:layout_marginRight="50dp"
android:gravity="center"
android:orientation="vertical">
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/txtProgress"
android:layout_centerHorizontal="true"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:progress="50"
android:progressTint="#android:color/black" />
<TextView
android:id="#+id/txtProgress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Progress"
android:textColor="#android:color/black" />
</LinearLayout>
Java

ListView elements are not visible by default

Below is my code and image. I am populating the listview with the arraylist in runOnUIThread() in onpostexecute() which has values gotten from the remote server in the doInBackground(). But the thing is elements are visible only when the focus is on particular item. I have been trying with different things to set the elements visible, but all have gone vain. Can someone please suggest me how to get the items visible. Note: I can't extend the ListActivity, as I have another class that needs to be extended which is the subclass of an activity.
runOnUiThread(new Runnable() {
public void run() {
//Updating parsed json data to Listview
ListView listView = (ListView)findViewById(R.id.listsubcategory);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1, subCategoryList);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String selectedSubcategory = subCategoryList.get(position);
Toast.makeText(getApplicationContext(), "You clicked on "+selectedSubcategory, Toast.LENGTH_SHORT).show();
}
});
}
});
The problem is the styling of the list items. In normal state your items have white background and white text color, therefore you can't see them. When the state changes to focused the colors change. You can easily solve the problem by using your custom list item instead of the system's android.R.layout.simple_list_item_1.
Define a layout for the item, it can be something like this:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/my_item_background"
android:textColor="#color/my_text_color"
/>
Now, if the item's layout is res/layout/my_list_item.xml, create the adapter this way:
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getContext(),
R.layout.my_list_item, subCategoryList);

Categories

Resources