I'm creating a fragment that contains number pickers and cancels okay buttons. Whenever I click the first spinner in the main activity, the spinner will pop out the fragment that contains number pickers. I select the number pickers values using getValue() method, but how to pass this value back to the spinner in main activity. I try to use getposition() method from spinner, but it won't works, because I didn't use setAdapter() method assign values to my spinner, I just want my spinner show the only one value retrieved from the fragment number pickers.
OnCreate function
s = (Spinner) findViewById(R.id.spinner1);
s.setOnTouchListener(onSpinnerTouchListener);
Below is my onSpinnerTouchListener:
LayoutInflater li = LayoutInflater.from(TaxCalc.this);
View promptView = li.inflate(R.layout.frag_numpicker, null);
AlertDialog.Builder builder = new AlertDialog.Builder(TaxCalc.this);
builder.setView(promptView);
builder.setTitle("Choose Amount of People");
builder.setPositiveButton("OK",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
int value = mNumberPicker.getValue();
});
builder.setNegativeButton("NO",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
alertDialog.cancel();
}
});
mNumberPicker = (NumberPicker) promptView.findViewById(R.id.number_picker);
array_spinner = new String[30];
for (i = 0; i < 30 ; i++){
array_spinner[i] = Integer.toString(i+1);
}
mNumberPicker.setMaxValue(array_spinner.length-1);
mNumberPicker.setMinValue(0);
mNumberPicker.setWrapSelectorWheel(false);
mNumberPicker.setDisplayedValues(array_spinner);
alertDialog = builder.create();
alertDialog.show();
alertDialog.setCanceledOnTouchOutside(false);
Related
hello I am making a bmi calculator i want it to work based on 3 variables, weight height and gender.
The gender in on a spinner which lets the user choose:
final Spinner spinner = view.findViewById(R.id.gender);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this.getActivity(),R.array.gender, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
The problem is the spinner uses a whole new method for it to work:
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String gendertext = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), gendertext,Toast.LENGTH_SHORT).show();
}
i want to take that variable gendertext and use it on this part of code which is inside the onCreateView
edit_height = (EditText) view.findViewById(R.id.edit_height);
edit_weight = (EditText) view.findViewById(R.id.edit_weight);
button_calculate_bmi = (Button) view.findViewById(R.id.button_calculate_bmi);
text_results = (TextView) view.findViewById(R.id.text_results);
button_calculate_bmi.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
String results = "Results:";
float height = 0;
float weight = 0;
try {
height = Float.parseFloat(edit_height.getText().toString());
weight = Float.parseFloat(edit_weight.getText().toString());
} catch (Exception ex) {
Context context = getActivity();
CharSequence text = "You forgot to enter something";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
height = height/100;
float bmi = weight / (height * height);
text_results.setText(results+bmi);
}
});
return view;
}
thank you in advance!
You can create the genderText variable outside of all methods in the Activity and initialize it to the first item in your R.array.gender. When the value in spinner is changed, onItemSelected is called and you get the updated value which you can set to the variable genderText.
Then in the button OnClickListener you can use the genderText variable.
I'm Developing a game so I want to get player name using an AlertDialog. But I don't Know certain number of players, it's Variable between 2 to 16!
I've added an spinner to ask about the NumberOfPlayers and a Button to Show AlertDialog then I tried to add certain number of EditText Using for loop. It doesn't have error but when I run application on phone, I just have Ok and Cancel Buttons. I couldn't resolve problem and become appreciate if someone help me.
This is My AlertDialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Opponents:");
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
final EditText[] input = new EditText[NumberOfPlayers];
for (int aux=0;aux==NumberOfPlayers;aux++) {
input[aux].setInputType(InputType.TYPE_CLASS_TEXT);
layout.addView(input[aux]);
}
builder.setView(layout); // this is a set method, not add
// Set up the buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
for (int aux=0;aux==NumberOfPlayers;aux++){
//PlayersTXT[aux].setText(input[aux].getText().toString());
}
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
Just change your code with below one and you will get dynamic edittext in alert dialog:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Opponents:");
LinearLayout layout = new LinearLayout(this);
layout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
layout.setOrientation(LinearLayout.VERTICAL);
final EditText[] input = new EditText[NumberOfPlayers];
for (int aux=0;aux<NumberOfPlayers;aux++) {
input[aux] = new EditText(MainActivity.this);
input[aux].setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
input[aux].setInputType(InputType.TYPE_CLASS_TEXT);
layout.addView(input[aux]);
}
builder.setView(layout); // this is a set method, not add
// Set up the buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
for (int aux=0;aux==NumberOfPlayers;aux++){
//PlayersTXT[aux].setText(input[aux].getText().toString());
}
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
Let me highlight problems in your code are:
you are using "aux==NumberOfPlayers" in "for loop" which is wrong. it should be "aux < NumberOfPlayers"
you are not initializing edittext in "for loop" such as "input[aux] = new EditText(MainActivity.this);"
you are not giving height and width for both linear layout as well edittext after initializing such as ".setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));"
I have this project App demo video that needs 6 types of images to be added in a horizontal scroll view each will have dialog fragment.
How can i find the position of images in horizontal scroll view and delete view when slide down.
final String[] AddCustomitems = {"Blink single message", "Blink double message", "Message", "Scroll", "Split", "Temp"}; //list of items that can be added in layout
int[] customviewsDrawable = new int[]{R.drawable.custom_blink, R.drawable.custom_blink_double, R.drawable.custom_message, R.drawable.custom_scroll, R.drawable.custom_split_double, R.drawable.temp};
public void CustomAnimationList(final Activity activity) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//set the title for alert dialog
builder.setTitle("Dot Matrix Add view");
//set items to alert dialog. i.e. our array , which will be shown as list view in alert dialog
builder.setItems(AddCustomitems, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
// setting the button text to the selected item from the list
AddItem(item);
}
});
//Creating CANCEL button in alert dialog, to dismiss the dialog box when nothing is selected
builder.setCancelable(false)
.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
//When clicked on CANCEL button the dalog will be dismissed
dialog.dismiss();
}
});
// Creating alert dialog
AlertDialog alert = builder.create();
//Showing alert dialog
alert.show();
}
void AddItem(final int itemNum) {
if (countItem < 9) {
final float scale = getResources().getDisplayMetrics().density;
int dpWidthInPx = (int) (175 * scale); //rescalling views
int dpHeightInPx = (int) (250 * scale); //rescalling views
final LinearLayout sv = (LinearLayout) findViewById(R.id.horizontalLayout);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(dpWidthInPx, dpHeightInPx);
layoutParams.setMargins(10, 0, 0, 10); //adding margin
final ImageView iv = new ImageView(this);
iv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alertView(itemNum); //this will send itemNum to switch case for corresponding dialog fragment.
}
});
iv.setLayoutParams(layoutParams);
iv.setBackgroundResource(customviewsDrawable[itemNum]);
sv.addView(iv);
countItem++;
} else {
alertItem(); //alert message if items are more then 8
}
System.out.println("count items :" + countItem);
}
I have implemented this by using ItemTouchHelper class.I have also added some features
1)Custom multiple dialog fragment added in Horizontal Scroll View.
2)Drag and drop feature to move items or delete item when swiped up or down
3)List the Order of items when Button is pressed
4)Alert dialog if items are more then the 8 elements.
I have committed the code on github for anyone to try out.[https://github.com/parmarravi/HorizontalRecyclerView ]
I have ListView that is populated using a CursorAdapter. In each ListView row there are too textviews and a button. An AlerDialog is supposed to pop on button click.The AlertDiaog itself is filled with a cursor.
the problem is that when i try to make and alert dialog builder it needs an argument that i don't know how to get it. the code that i"m posting here has a syntax error that i don't know how to fix it.
i tried to move the ItemClickListener to original activity class but the button has null value when i try to assign it to an object.
this is the bindView of the CursorAdapter:
public void bindView(final View view, Context context, Cursor cursor) {
//ListView list= (ListView) view.findViewById(R.id.words);
Button addButton=(Button)view.findViewById(R.id.add_btn);
addButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view1) {
//Dictionary d=new Dictionary();
AlertDialog.Builder builder=new AlertDialog.Builder(Dictionary.this); // the error
final Cursor boxes=Dictionary.getCursor();
builder.setCursor(boxes, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Dictionary.getCursor().moveToPosition(i);
String text = boxes.getString(boxes.getColumnIndex("_id"));
Log.d("select", "onClick: "+text);
}
}, "name");
builder.show();
}
});
TextView english=(TextView) view.findViewById(R.id.english);
TextView farsi=(TextView) view.findViewById(R.id.farsi);
english.setText(cursor.getString(1));
farsi.setText(cursor.getString(2));
}
I've come across an issue which I've never had before. I have a Fragment containing a button. This button (ViewBookingButton) shows a popup dialog. When the dialog opens I would like to set a string in an EditText (AllBooking). Unfortunately it crashes and shows NullPointerExecption. What is going wrong? Here is my code:
ViewBookingButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
AlertDialog.Builder ViewBookingHelpBuilder = new AlertDialog.Builder(getActivity());
ViewBookingHelpBuilder.setTitle("view booking:");
LayoutInflater inflater = getActivity().getLayoutInflater();
View DialogLayout = inflater.inflate(R.layout.view_booking, null);
ViewBookingHelpBuilder.setView(DialogLayout);
TextView AllBooking = (TextView)view.findViewById(R.id.AllBooking);
AllBooking.setText("Hello World");//WHEN I REMOVE THIS THE DIALOG WORKS
ViewBookingHelpBuilder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which)
{
}
});
AlertDialog helpDialog = ViewBookingHelpBuilder.create();
helpDialog.show();
}
});
Change this
TextView AllBooking = (TextView)view.findViewById(R.id.AllBooking);
to
TextView AllBooking = (TextView)DialogLayout.findViewById(R.id.AllBooking);
TextView belongs to the inflated layout and `findViewById looks for the view in the current view hierarchy.