I'm trying to add a new food with some add ons, basically everytime the add field button is press it will create two edittext which is the name and price of the add on, there will be a scenario whereby users wants to delete the edittext name and price. I've successfully created two edittexts also setting them with the IDs that i'm giving them in integer. But I'm having problem with deleting the two edittext with a button whenever i click the delete field button.
// Add On
addOnLayout.removeAllViews();
addFields.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AddOnCounters++;
etFoodName = new EditText(addfoodAddOn.this);
etFoodName.setId(ETFoodID);
etFoodName.setHint("Enter your AddOn Name");
etPrice = new EditText(addfoodAddOn.this);
etPrice.setId(ETPriceID);
etPrice.setHint("Enter your AddOn Price");
Log.d("-------","-------------------what is edittext name id " + ETFoodID);
Log.d("-------","-------------------what is edittext price id " + ETPriceID);
ETFoodID++;
ETPriceID++;
addOnLayout.addView(etFoodName);
addOnLayout.addView(etPrice);
}
});
removeFields.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(AddOnCounters>=1 && ETFoodID > 0 && ETPriceID > 10){
addOnLayout.removeViewAt(ETFoodID);
addOnLayout.removeViewAt(ETPriceID);
ETFoodID--;
ETPriceID--;
}
}
});
I'm expecting the result to be whenever i click the delete field button, it will delete the very bottom last two edittexts which is the name and price.
Replace your listener
removeFields.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(AddOnCounters>=1 && ETFoodID > 0 && ETPriceID > 10){
View v1 = addOnLayout.findViewById(ETFoodID);
View v2 = addOnLayout.findViewById(ETPriceID);
addOnLayout.removeView(v1);
addOnLayout.removeView(v2);
ETFoodID--;
ETPriceID--;
}
}
});
After the button click just do this:
addFields.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
etPrice.setText("")
etFoodName.setText("");
}
}
Remove it from its parent.
removeFields.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
removeFromParent(etFoodName);
removeFromParent(etPrice);
}
});
static void removeFromParent(View view) {
((ViewGroup)view.getParent()).removeView(view);
}
Related
i've this issue made me crazy. When retrieved JSON data i will use this my adapter:
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
if (holder instanceof OriginalViewHolder) {
final DomandaQuizTipo1 domandaQuiz1 = items.get(position);
OriginalViewHolder vItem = (OriginalViewHolder) holder;
/* Getting each answer from array of answer */
Risposta1 risposta1 = domandaQuiz1.getRisposte().get(0);
Risposta1 risposta2 = domandaQuiz1.getRisposte().get(1);
Risposta1 risposta3 = domandaQuiz1.getRisposte().get(2);
/* Get and set text of question */
vItem.domanda.setTypeface(typefaceRegular);
vItem.domanda.setText(domandaQuiz1.getDomanda());
/* Get and set text of answer 1 */
vItem.risposta1.setText(risposta1.getRisposta());
vItem.risposta1.setTextOn(risposta1.getRisposta());
vItem.risposta1.setTextOff(risposta1.getRisposta());
/* Get and set text of answer 2 */
vItem.risposta2.setText(risposta2.getRisposta());
vItem.risposta2.setTextOn(risposta2.getRisposta());
vItem.risposta2.setTextOff(risposta2.getRisposta());
/* Get and set text of answer 3 */
vItem.risposta3.setText(risposta3.getRisposta());
vItem.risposta3.setTextOn(risposta3.getRisposta());
vItem.risposta3.setTextOff(risposta3.getRisposta());
/* When i click the first answer uncheck 2nd and 3rd */
vItem.risposta1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity.punteggiDomandeQuiz1[position] = items.get(position).getRisposte().get(0).getPunteggio();
MainActivity.UpdatePunteggioTotale();
vItem.risposta2.setChecked(false);
vItem.risposta3.setChecked(false);
}
});
/* When i click the second answer uncheck 1st and 3rd */
vItem.risposta2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity.punteggiDomandeQuiz1[position] = items.get(position).getRisposte().get(1).getPunteggio();
MainActivity.UpdatePunteggioTotale();
Log.d("adapterposition", "POS" + position);
vItem.risposta1.setChecked(false);
vItem.risposta3.setChecked(false);
}
});
/* When i click the third answer uncheck 1st and 2nd */
vItem.risposta3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity.punteggiDomandeQuiz1[position] = items.get(position).getRisposte().get(2).getPunteggio();
MainActivity.UpdatePunteggioTotale();
vItem.risposta1.setChecked(false);
vItem.risposta2.setChecked(false);
}
});
} else {
((ProgressViewHolder) holder).progressBar.setIndeterminate(true);
}
}
My issue is this: in a recyclerview of each question that contains 3 answers to choose, when i click on one of answer (ex. first toggle button) is checked correctly but when i scroll recycler view i see other question with answer (ex. first toggle button) checked.
How can i avoid this issue? Take a look from this gif i putted below.
Check gif this please to understand
You should add a field like a selected (local not server) and check that's is selected and change item ui
if(vItem.selected==1)
vItem.risposta1.setChecked(true);
else
vItem.risposta1.setChecked(false);
if(vItem.selected==2)
vItem.risposta2.setChecked(true);
else
vItem.risposta2.setChecked(false);
if(vItem.selected==3)
vItem.risposta3.setChecked(true);
else
vItem.risposta3.setChecked(false);
vItem.risposta1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity.punteggiDomandeQuiz1[position].selected=1;
notifyItemChange(adapterPosition);
}
});
vItem.risposta2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity.punteggiDomandeQuiz1[position].selected=2;
notifyItemChange(adapterPosition);
}
});
vItem.risposta3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity.punteggiDomandeQuiz1[position].selected=3;
notifyItemChange(adapterPosition);
}
});
You have to set the status of each toggle button in onBindViewHolder.
Something like:
vItem.risposta1.setText(risposta1.getRisposta());
//...
vItem.risposta1.setChecked(risposta1.isChecked());
Also when you update the data in the OnClickListener you have to notify the adapter:
adapter.notifyDataSetChanged();
I am using listView to change the value
here is my code. But It's not going to stop. when Listview last index no. suppose 10 then, it's crash. how do i stop next button when listView end.
btnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtOne.setText(letter[lastView+1]);
txtTwo.setText(letterDetails[lastView+1]);
txtThree.setText(letterB[lastView+1]);
lastView++;
if (lastView == letter.length){
lastView=0;
Toast.makeText(getApplicationContext(), lastView+" ", Toast.LENGTH_SHORT).show();
}
}
});
btnPrevious.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtOne.setText(letter[lastView-1]);
txtTwo.setText(letterDetails[lastView-1]);
txtThree.setText(letterB[lastView-1]);
lastView--;
}
});
Basically letter.length return size of array. Suppose you have 10 items in array then the index will be 0-9.The reason behind your app is crashing is due to array out of bound error.
What you have to do is just add this line in your if condition.
Code
if (lastView == (letter.length-1))
I'm a beginner in Android Studio, and I want to make a C button in calc with 2 functions.
How do I do that on one tap the C button erases only one number, and on hold erase all numbers in TextView?
findViewById(R.id.btnClear).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtScreen.setText(""); // Clear the screen
// Reset all the states and flags
lastNumeric = false;
stateError = false;
lastDot = false;
You can set an onClickListener and onLongClickListener to achieve this.
cButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String currentText = textView.getText().toString();
if(currentText.length >= 2){
currentText = currentText.substring(0, currentText.length - 2);
}else{
currentText = "";
}
textView.setText(currentText);
}
});
cButton.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
textView.setText("");
return true;
}
});
Have you looked in to the onClick() and onLongPress() methods?
cancelButton.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
myTextView.setText("");
return true;
}
});
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
removeOneCharacter();
}
});
private void removeOneCharacter()
{
String textViewValue = myTextView.getText().toString();
if (textViewValue != null && textViewValue.length >= 2)
{
myTextView.setText(textViewValue.substring(0, textViewValue.length() - 2);
}
}
The onClick() method removes on char from the TextView at a time. The onClick() method however removes the entire String from the TextView but only considering if there is already 2 or more characters to prevent an Exception from occurring due to the upper-bound of the substring.
An improvement here could be to add another if function that checks the character length within the Long press and performs a clear if there is only one character remaining.
What I want is when I click the "x" button of the second item, it will remove the second item and then make the "+" of the first item visible. Here is what I try but not work, the "+" button is not showed.
holder.ivDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View view = recyclerView.getChildAt(position - 1);
holder.ivAdd = (ImageView) view.findViewById(R.id.img_add_items);
holder.ivAdd.setVisibility(View.VISIBLE);
list.remove(position);
notifyDataSetChanged();
}
});
Try this code,
holder.ivDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View view = recyclerView.getChildItemId(position - 1);
holder.ivAdd = (ImageView) view.findViewById(R.id.img_add_items);
holder.ivAdd.setVisibility(View.VISIBLE);
list.remove(position);
notifyDataSetChanged();
}
});
I hope that will help you for solving your problem.
Im new to android and Im wondering if there is a standard method which is used to accomplish the retrieval of all the views' positions with in a GridLayout.
I'm dynamically adding and removing views from a GridLayout like this
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addButton.setVisibility(View.INVISIBLE);
Button newButton = new Button(LayoutAnimationsByDefault.this);
gridContainer.getColumnCount();
newButton.setText(String.valueOf("position#"+"huh"));
newButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
gridContainer.removeView(v);
addButton.setVisibility(View.VISIBLE);
}
});
gridContainer.addView(newButton, Math.min(-1, gridContainer.getChildCount()));
}
});
I've also tried this..
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addButton.setVisibility(View.INVISIBLE);
Button newButton = new Button(LayoutAnimationsByDefault.this);
Object tag =v.getTag();
if(tag!=null)
{
int position = (Integer)tag;
Log.v("onClick", "Position: " + position);
newButton.setTag(position);
newButton.setText(String.valueOf("position#"+position));
TextView text1 = (TextView) newButton;
String message = "positionis" + position + text1.getText().toString();
Toast.makeText(LayoutAnimationsByDefault.this, message, Toast.LENGTH_LONG).show();
}
newButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
gridContainer.removeView(v);
addButton.setVisibility(View.VISIBLE);
}
});
gridContainer.addView(newButton, Math.min(-1, gridContainer.getChildCount()));
gridContainer.getColumnCount();
}
This creates a new button,but with nothing inside, not text or anything
Im trying to figure out how to update and retrieve the updated position of each view in the gridlayout, each time an item is added, removed re-arranged in the GridLayout.
By position I don't mean x,y coordinates, but the 0,1,2,3,4 position as seen with in listviews.
I've tried getColumnCount(); getChildAt(); ActionListener() ActionPerformed() etc.
Any help, pointers, hints?Thanks!!