Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This class is used to change spinner view properties (text color, font, size etc) easily without setting adapter programmatically.
<android.support.v7.widget.AppCompatSpinner xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/labelSurveyNumber"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:entries="@array/label_form1_sno_array"
android:paddingTop="@dimen/standard_margin"
android:paddingBottom="@dimen/standard_margin"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
package com.locopixel.agri.ui.helper;
import android.database.DataSetObserver;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
/**
* Created by Qamar4P on 10/18/2017.
*/
public class SpinnerViewHelper {
/*
use it like new SpinnerViewHelper((Spinner)view.findViewById(R.id.labelSurveyNumber),(parent, v, position, id) -> UrduFontHelper.set(v));
*/
public SpinnerViewHelper(Spinner spinner, AdapterView.OnItemClickListener l) {
final SpinnerAdapter adapter = spinner.getAdapter();
spinner.setAdapter(new SpinnerAdapter() {
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View view = adapter.getDropDownView(position, convertView, parent);
l.onItemClick(null,view,position,getItemId(position));
return view;
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
adapter.registerDataSetObserver(observer);
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
adapter.unregisterDataSetObserver(observer);
}
@Override
public int getCount() {
return adapter.getCount();
}
@Override
public Object getItem(int position) {
return adapter.getItem(position);
}
@Override
public long getItemId(int position) {
return adapter.getItemId(position);
}
@Override
public boolean hasStableIds() {
return adapter.hasStableIds();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = adapter.getView(position, convertView, parent);
l.onItemClick(null,view,position,getItemId(position));
return view;
}
@Override
public int getItemViewType(int position) {
return adapter.getItemViewType(position);
}
@Override
public int getViewTypeCount() {
return adapter.getViewTypeCount();
}
@Override
public boolean isEmpty() {
return adapter.isEmpty();
}
});
}
}
package com.locopixel.agri.ui.helper;
import android.content.res.AssetManager;
import android.graphics.Typeface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.locopixel.agri.App;
/**
* Created by Qamar4P on 10/18/2017.
*/
public class UrduFontHelper {
private static Typeface font;
public static void set(View... views) {
if(font == null)
font = Typeface.createFromAsset(App.instance.getAssets(),"fonts/Jameel Noori Nastaleeq.ttf");
for (View view : views) {
if(view instanceof ViewGroup){
ViewGroup viewGroup = (ViewGroup) view;
if(viewGroup.getChildCount() > 0)
set(getChildViews(viewGroup));
}else if(view instanceof TextView){
((TextView)view).setTypeface(font);
}
}
}
private static View[] getChildViews(ViewGroup viewGroup) {
View[] views = new View[viewGroup.getChildCount()];
for (int i = 0; i < viewGroup.getChildCount(); i++) {
views[i] = viewGroup.getChildAt(i);
}
return views;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.