Last active
August 29, 2015 14:13
-
-
Save manugarri/da21628eec018438ba8a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package es.catmobil.tornofici.ui.views; | |
import android.content.Context; | |
import android.content.res.TypedArray; | |
import android.os.Build; | |
import android.text.Editable; | |
import android.text.TextWatcher; | |
import android.util.AttributeSet; | |
import android.util.Log; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.AdapterView; | |
import android.widget.ArrayAdapter; | |
import android.widget.EditText; | |
import android.widget.ListAdapter; | |
import android.widget.RelativeLayout; | |
import android.widget.Spinner; | |
import android.widget.SpinnerAdapter; | |
import android.widget.TextView; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Created by Bernat on 25/03/2014. | |
*/ | |
public class HintTitleSpinner extends RelativeLayout implements AdapterView.OnItemSelectedListener { | |
private TextView textView; | |
private Spinner spinner; | |
private float density; | |
private String hint; | |
private CustomSpinnerAdapter adapter; | |
public HintTitleSpinner(Context context) { | |
super(context); | |
init(context, null); | |
} | |
public HintTitleSpinner(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(context, attrs); | |
} | |
public HintTitleSpinner(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
init(context, attrs); | |
} | |
private void init(Context context, AttributeSet attrs) { | |
this.hint = getHint(attrs); | |
density = context.getResources().getDisplayMetrics().density; | |
isInEditMode(); | |
textView = new TextView(context); | |
textView.setVisibility(View.GONE); | |
textView.setText(hint); | |
spinner = new HintableSpinner(context); | |
spinner.setVisibility(View.VISIBLE); | |
spinner.setOnItemSelectedListener(this); | |
setItems(); | |
addTextView(textView); | |
addSpinner(spinner); | |
// postInvalidate(); | |
} | |
private String getHint(AttributeSet attrs) { | |
if (getContext() != null && attrs != null) { | |
int[] attributes = new int[]{android.R.attr.hint}; | |
TypedArray a = getContext().obtainStyledAttributes(attrs, attributes); | |
if (a != null) { | |
return a.getString(0); | |
} | |
} | |
return ""; | |
} | |
private void addTextView(TextView textView) { | |
LayoutParams paramsTextView = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); | |
paramsTextView.addRule(RelativeLayout.ALIGN_PARENT_LEFT); | |
paramsTextView.addRule(RelativeLayout.ALIGN_PARENT_TOP); | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { | |
paramsTextView.addRule(RelativeLayout.ALIGN_PARENT_START); | |
} | |
paramsTextView.setMargins((int) (16 * density), 0, 0, 0); | |
addView(textView, paramsTextView); | |
} | |
private void addSpinner(Spinner spinner) { | |
LayoutParams paramsSpinner = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); | |
paramsSpinner.addRule(RelativeLayout.ABOVE, textView.getId()); | |
paramsSpinner.addRule(RelativeLayout.ALIGN_PARENT_LEFT); | |
paramsSpinner.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { | |
paramsSpinner.addRule(RelativeLayout.ALIGN_PARENT_START); | |
paramsSpinner.addRule(RelativeLayout.ALIGN_PARENT_END); | |
} | |
paramsSpinner.setMargins(0, (int) (16 * density), 0, 0); | |
addView(spinner, paramsSpinner); | |
} | |
public TextView getTextView() { | |
return textView; | |
} | |
public Spinner getSpinner() { | |
return spinner; | |
} | |
private void setItems() { | |
setItems(new ArrayList<String>()); | |
} | |
public void setItems(List<String> items) { | |
adapter = new CustomSpinnerAdapter(getContext(), this.hint); | |
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); | |
for (String item : items) { | |
adapter.add(item); | |
} | |
spinner.setAdapter(adapter); | |
} | |
private void showTitle() { | |
textView.setVisibility(VISIBLE); | |
} | |
public String getText() { | |
return (String) spinner.getAdapter().getItem(spinner.getSelectedItemPosition()); | |
} | |
@Override | |
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { | |
if (adapter.isSelected()) { | |
showTitle(); | |
} | |
spinner.invalidate(); | |
adapter.notifyDataSetChanged(); | |
} | |
@Override | |
public void onNothingSelected(AdapterView<?> parent) { | |
} | |
private class CustomSpinnerAdapter extends ArrayAdapter<String> { | |
private String hint; | |
private boolean selected = false; | |
private TextView firstTextView; | |
public CustomSpinnerAdapter(Context context, String hint) { | |
super(context, android.R.layout.simple_spinner_dropdown_item); | |
this.hint = hint; | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
View v = super.getView(position, convertView, parent); | |
if (v != null) { | |
if (position == 0) { | |
firstTextView = (TextView) v.findViewById(android.R.id.text1); | |
if (spinner.getSelectedItemPosition() == 0 && !selected) { | |
firstTextView.setText(""); | |
firstTextView.setHint(hint); | |
} else { | |
firstTextView.setText(getItem(0)); | |
} | |
} | |
} | |
return v; | |
} | |
@Override | |
public View getDropDownView(int position, View convertView, ViewGroup parent) { | |
selected = true; | |
return super.getView(position, convertView, parent); | |
} | |
public boolean isSelected() { | |
return this.selected; | |
} | |
} | |
private class HintableSpinner extends Spinner { | |
private OnItemSelectedListener itemSelectedListener; | |
public HintableSpinner(Context context) { | |
super(context); | |
} | |
@Override | |
public void setSelection(int position) { | |
super.setSelection(position); | |
if (itemSelectedListener != null) { | |
itemSelectedListener.onItemSelected(this, null, position, getItemIdAtPosition(position)); | |
} | |
} | |
@Override | |
public void setOnItemSelectedListener(OnItemSelectedListener listener) { | |
this.itemSelectedListener = listener; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment