I put two spinners in the same activity, one for City and the other for Town. When the user chooses a City, the Town spinner should populate with items according to the City that was chosen.
My problem is, the color of the background and the text is always different from the first one, however they have the same style and attributes. I couldn't find any logical solution and I didn't find any suggestion in web.
Do you have any idea about the reason or the solution?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="20dp"
android:onClick="pickDate"
android:text="Select date" />
<TextView
android:id="@+id/viewDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Distribution date"
android:textAlignment="center" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="The governorate"
android:textAlignment="center" />
<Spinner
android:id="@+id/static_spinner"
style="@style/Widget.AppCompat.Light.Spinner.DropDown.ActionBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="The district"
android:textAlignment="center" />
<Spinner
android:id="@+id/district_spinner"
style="@style/Widget.AppCompat.Light.Spinner.DropDown.ActionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textView15"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:text="Place ID" />
<EditText
android:id="@+id/plcID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="" />
<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:onClick="next"
android:text="Next" />
</LinearLayout>
</LinearLayout>
MainInfoActivity file
public class MainInfoActivity extends Activity {
TextView textView, plcID;
Spinner staticSpinner;
Spinner dynamicSpinner;
Spinner districtSpinner;
CharSequence[] arrayDistrict;
ArrayAdapter<CharSequence> districtAdapter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_info_activity);
textView = (TextView) findViewById(R.id.viewDate);
plcID = (TextView) findViewById(R.id.plcID);
//Drop down lists
staticSpinner = (Spinner) findViewById(R.id.static_spinner);
// Create an ArrayAdapter using the string array and a default spinner
final ArrayAdapter<CharSequence> staticAdapter = ArrayAdapter.createFromResource(
this, R.array.governorate_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
staticAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
staticSpinner.setAdapter(staticAdapter);
districtSpinner = (Spinner) findViewById(R.id.district_spinner) ;
districtAdapter = ArrayAdapter.createFromResource(getApplicationContext(),
R.array.Anbar, android.R.layout.simple_spinner_item);
districtAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
staticSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view1, int i, long l) {
districtAdapter = ArrayAdapter.createFromResource(getApplicationContext(),
R.array.Anbar, android.R.layout.simple_spinner_item);
districtAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
districtSpinner.setAdapter(districtAdapter);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
}
I recreated your code and it works fine for me.
The only difference I can find meaningful are in the lines below:
ArrayAdapter<CharSequence> staticAdapter = ArrayAdapter
.createFromResource(this, R.array.governorate_array,
android.R.layout.simple_spinner_item);
and
districtAdapter = ArrayAdapter
.createFromResource(getApplicationContext(),
R.array.Anbar,
android.R.layout.simple_spinner_item);
instead of using getApplicationContext() can you try to use this as you did in the staticAdapter?
I once faced something similar where my application had a theme different from my activity and the widgets looked slightly different as well.
UPDATE
I would like to add some more info about the reason why I suggested the change above.
Using the right Context is linked to another one of those behaviors. While the framework will not complain and will return a perfectly good view hierarchy from a LayoutInflater created with the application context, the themes and styles from your app will not be considered in the process. This is because Activity is the only Context on which the themes defined in your manifest are actually attached. Any other instance will use the system default theme to inflate your views, leading to a display output you probably didn’t expect.
Quote from this link: https://possiblemobile.com/2013/06/context/
By default, drop-down views are inflated against the theme of the {@link Context} passed to the adapter's constructor. More.......
ArrayAdapter source code: https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/widget/ArrayAdapter.java#461