newdeveloper newdeveloper - 4 months ago 107
Android Question

how to set spinner text in center and set divider also

I want to set a spinner text center and set a divider also. There is a problem in set text at the center and set divider. Also the spinner items and spinner values are coming from a server json.

This is my code:

XML:

<Spinner
android:id="@+id/txtSpinner"
android:layout_width="fill_parent"
style="@android:style/Widget.Holo.Light.Spinner"
android:dropDownVerticalOffset="28dp"
android:gravity="center_horizontal"
android:textAlignment="center"
android:layout_height="45dp"
/>


Java code:

private void getSpinnerData() {
final ProgressDialog loading = ProgressDialog.show(this, "Loading Data", "Please wait...", false, false);
loading.setCancelable(true);
StringRequest stringRequest = new StringRequest(spinner_url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
loading.dismiss();
JSONObject j_obj = null;
try {
j_obj = new JSONObject(response);
result = j_obj.getJSONArray("data");
getdata(result);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void getdata(JSONArray j) {
for (int i = 0; i < j.length(); i++) {
try {
JSONObject json = j.getJSONObject(i);
spinnerlist.add(json.getString("category"));
} catch (JSONException e) {
e.printStackTrace();
}
}
//Setting adapter to show the items in the spinner
txtSpinner.setAdapter(new ArrayAdapter<String>(TextActivity.this, android.R.layout.simple_spinner_dropdown_item, spinnerlist));

}


current output

enter image description here

What I want

this

Answer

my answer is here its working.....

create new Style

<style name="ThemeName" parent="@android:style/Widget.Holo.Light.Spinner">
    <item name="android:dropDownListViewStyle">@style/mySpinnerStyle</item>
</style>
<style name="mySpinnerStyle" parent="@style/Widget.AppCompat.Spinner.Underlined">
    <item name="android:divider">#000</item>
    <item name="android:dividerHeight">1dp</item>
</style>