Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make custom Android string Picker that fits inside another view?

I have been researching Android Pickers as described here.

There are pickers for time, date, and number fields but, much to my surprise, I discovered there is no generic picker for basic strings.

I have found several resources discussing how to re-purpose the NumberPicker to use strings as follows:

Android picker widget for arbitrary strings

and

https://github.com/hotchemi/StringPicker/blob/master/library/src/main/java/hotchemi/stringpicker/StringPicker.java

These look promising, but my end goal is to have my string picker fit within a panel on my main view instead of popping over the view. I need to do this because the top of my view has an object that has gets modified based on what is chosen in the picker and hence needs to be displayed in real-time to show how different values for a given property would affect its look.

Is there any way to do this?

like image 989
gonzobrains Avatar asked Oct 30 '15 17:10

gonzobrains


1 Answers

You just need to use NumberPicker without any library you can implement it I also had the same problem and I found a solution for it. I used NumberPicker like this and it works very well.

In picker_layout add number picker and show that layout on animation on main layout.

And in your class use it ...

<NumberPicker
     android:id="@+id/numberPicker1"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical" /> 


numberPicker1= (NumberPicker) findViewById(R.id.numberPicker1);
final String genders[] = { "unassessed", "Skipped", "Incorrect", "Correct", "1 mark" };

numberPicker1.setMinValue(0);
numberPicker1.setMaxValue(genders.length - 1);
numberPicker1.setDisplayedValues(genders);
numberPicker1.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);

NumberPicker.OnValueChangeListener myValChangedListener = new NumberPicker.OnValueChangeListener() {
 @Override
 public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
     btnBar.setText("Value: " + genders[newVal]);
 }
};

numberPicker1.setOnValueChangedListener(myValChangedListener);
like image 170
Rajesh Gauswami Avatar answered Oct 22 '22 11:10

Rajesh Gauswami