My question is simple, how would you go about to set a Spinner/dropdown list in the Action bar (in place of the title)?
I know there are other similar questions on here but all the answers I could find were very old and used deprecated code or linked to some guide in the Android docs that isn't there anymore.
try with this one
call this from activity on create method and implement AdapterView.OnItemSelectedListener also.
String[] weeks = {"Sunday", "Monday", "Tuesday", "Wednesday", "thursday","Sunday"}
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.custom_app_bar_spinner);
Spinner spinner = (Spinner) findViewById(R.id.spinnerMethodInterviews);
Spinner spinnerSecond = (Spinner) findViewById(R.id.spinnerMethodSecond);
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter(this,
R.layout.custom_title, android.R.id.text1, weeks);
spinnerAdapter.setDropDownViewResource(R.layout.cutom_spinner);
spinner.setAdapter(spinnerAdapter);
spinnerSecond.setAdapter(spinnerAdapter);
spinner.setOnItemSelectedListener(this);
spinnerSecond.setOnItemSelectedListener(this);
main_Activity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.constraint.ConstraintLayout
android:id="@+id/view_top"
android:layout_width="0dp"
android:layout_height="50dp"
app:layout_constraintTop_toTopOf="parent"
android:background="@color/colorAccent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<Spinner
android:id="@+id/spinnerMethodInterviews"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:layout_constraintStart_toStartOf="parent"
android:spinnerMode="dropdown"/>
<Spinner
android:id="@+id/spinnerMethodSecond"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:layout_constraintEnd_toEndOf="parent"
android:spinnerMode="dropdown"/>
</android.support.constraint.ConstraintLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@+id/view_top"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
>
</android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>
custom_spinner.xml
<CheckedTextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:ellipsize="marquee"
android:singleLine="true"
android:textColor="@android:color/white" />
custom_title.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@android:id/text1"
style="?attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:ellipsize="marquee"
android:singleLine="true"
android:textColor="@android:color/white" />
Getting an error:
ActionBar actionBar = getSupportActionBar();
, the variable is requiring a ActionBar yet it is receiving the appcompat version so they are incompatible.i will update my code if you want
custom_app_bar_spinner is main_Activity.xml, and implement AdapterView.OnItemSelectedListene also in your activity.
when you extend appcompact activity then also implement this listener
let me update if you face any issue