Skip to content

Instantly share code, notes, and snippets.

@erayakartuna
Last active January 22, 2019 19:05
Show Gist options
  • Save erayakartuna/2173d8915b434f86255448bfc1131a4e to your computer and use it in GitHub Desktop.
Save erayakartuna/2173d8915b434f86255448bfc1131a4e to your computer and use it in GitHub Desktop.
Android Material Design Widget Spinner
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Spinner
android:id="@+id/subject_spinner"
android:layout_width="1dp"
android:layout_height="1dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp"
android:background="#000000"
android:focusable="true"
android:focusableInTouchMode="true"
android:spinnerMode="dialog"
android:visibility="gone" />
<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_subject"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/TextLabel"
app:errorTextAppearance="@style/error_appearance">
<EditText
android:id="@+id/input_subject"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/subject_label"
android:inputType="textEmailSubject"
android:maxLength="55"
android:textColor="@color/editText" />
</android.support.design.widget.TextInputLayout>
</RelativeLayout>
<string-array name="subjects">
<item>S1</item>
<item>S2</item>
<item>S3</item>
<item>SN..</item>
</string-array>
<string name="subject_label">Subject</string>
final Spinner spinnerSubject = (Spinner) findViewById(R.id.subject_spinner);
final EditText subject_input = (EditText) findViewById(R.id.input_subject);
final String[] subjets = getResources().getStringArray(R.array.subjects);
dataAdapterForSpinner = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,subjets);
dataAdapterForSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerSubject.setAdapter(dataAdapterForSpinner);
subject_input.setKeyListener(null);
subject_input.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
spinnerSubject.setVisibility(View.VISIBLE);
spinnerSubject.performClick();
}
});
subject_input.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
if(b){
spinnerSubject.setVisibility(View.VISIBLE);
spinnerSubject.performClick();
}
else{
spinnerSubject.setVisibility(View.GONE);
}
}
});
spinnerSubject.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,int position, long id) {
subject_input.setText(spinnerSubject.getSelectedItem().toString()); //this is taking the first value of the spinner by default.
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
subject_input.setText("");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment