How to display number pickers in pop up window
Solution 1:
I've tried using AlertDialog instead of PopupWindow. It's working fine on my side, give it a try if it's what you are looking for.
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.NumberPicker;
public class MainActivity extends AppCompatActivity {
final String month[] = {"jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"};
final String sub[] ={"Airlaw","criminal","Income tax","Direct tax","Customs", "Defence & Security Forces" , "Disinvestment" , "Education" , "Election" ,
"Electricity & Energy" , "Environment, Wildlife & Animal", "Exchange Control & FDI " , "Excise"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageButton button = (ImageButton) findViewById(R.id.imageButton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openDialog();
}
});
}
/**
* This will construct An {@link AlertDialog} with some custom views.
*/
private void openDialog() {
//Inflating a LinearLayout dynamically to add TextInputLayout
//This will be added in AlertDialog
final LinearLayout linearLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.view_number_dialog, null);
NumberPicker numberpicker = (NumberPicker) linearLayout.findViewById(R.id.numberPicker1);
NumberPicker numberPicker1 = (NumberPicker) linearLayout.findViewById(R.id.numberPicker2);
NumberPicker numberPicker2 = (NumberPicker) linearLayout.findViewById(R.id.numberPicker3);
numberpicker.setMinValue(1950);
numberpicker.setMaxValue(2018);
numberpicker.setValue(2017);
numberPicker1.setMinValue(0);
numberPicker1.setMaxValue(month.length - 1);
numberPicker1.setDisplayedValues(month);
numberPicker1.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
numberPicker2.setMinValue(0);
numberPicker2.setMaxValue(sub.length - 1);
numberPicker2.setDisplayedValues(sub);
numberPicker2.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
//Finally building an AlertDialog
final AlertDialog builder = new AlertDialog.Builder(this)
.setPositiveButton("Submit", null)
.setNegativeButton("Cancel", null)
.setView(linearLayout)
.setCancelable(false)
.create();
builder.show();
//Setting up OnClickListener on positive button of AlertDialog
builder.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Code on submit
}
});
}
}
view_number_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="260dp"
android:orientation="horizontal"
android:padding="14dp">
<NumberPicker
android:id="@+id/numberPicker1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<NumberPicker
android:id="@+id/numberPicker2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="14dp"
android:layout_marginRight="14dp"
android:layout_weight="1" />
<NumberPicker
android:id="@+id/numberPicker3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
It'll output as following...