I'm currently trying to create a cash register application. The user can press buttons to add products to their receipt.
The buttons should have a static text inside that tells users what the product is and how much it costs.
The button should also have a dynamic text called amount: 0x. That when pressed iterates + 1 so it shows amount: 1x.
How do I go about this? I'm pretty new to android so how can I have 1 button with one part static and 1 part dynamic text?
EDIT:
Thanks for all your answers so far but i'm not looking for a way to iterate the amount. By using setText I also overwrite the product and the cost of the product.
So I want a way to only update the amount when a button is pressed. Instead of using:
button.settext("Pepsi: 1,50" + amount + "x");
Is there a way or should I just use setText?
int counter = 0;
button.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
counter++;
button.setText("Amount" + counter + "x");
}
});
set a counter, listen for the click event for the button. Now inside click event increment the counter and set the new text to the button.
Related
I need to add 5 Input fields (EditText) dynamically one by one on button click and want to take values from them and store them into database using Room Persistence with MVVM.
Here I'm adding the view dynamically
private void addEditTextView() {
View inputView = getLayoutInflater().inflate(R.layout.row_edit_text, null, false);
EditText val1 = inputView.findViewById(R.id.input_value_1);
binding.layoutList.addView(inputView);
}
Any suggestion would be very helpful.
Thank you in advance.
Add view based on child count
private void addEditTextView() {
if (binding.layoutList.getChildCount() <= 5) {
View inputView = getLayoutInflater().inflate(R.layout.row_edit_text, null, false);
EditText val1 = inputView.findViewById(R.id.input_value_1);
binding.layoutList.addView(inputView);
}
}
"When I clicked Add button it is adding input field one by one, this code is working but I just want to limit for 5 fields not more not less and take values from them."
If you want to add exactly 5 fields on button click I recommend designing a fragment with the 5 fields in place, then when the button is clicked, inflate the fragment into your parent view. Then code the fragment appropriately with the data you're working with.
Then if you wanted, you could deflate the fragment on button click to clear the view or add some other way to clear the fragment when you want. Much easier than what you're doing currently in my own opinion.
You might as well include a submit button in your fragment assuming this is some kind of form.
You can simply define an integer and increase it every time you add the EditText but you should check if your integer is less than 5 everytime the method is called.
Example
private void addEditTextView() {
int count = 0;
if (count < 5){
View inputView = getLayoutInflater().inflate(R.layout.row_edit_text, null, false);
EditText val1 = inputView.findViewById(R.id.input_value_1);
binding.layoutList.addView(inputView);
count++;
}
}
I want to record the number of times a certain button is clicked in the android studio and then display it and the time it was clicked when another button is pressed.
take one variable for each button and assign 0 value then each button click that variable value increment.
var i=0
mBtnClick=findViewById(R.id.count)
mBtnClick?.setOnClickListener {
textview?.text = ""+i++
}
create one integer variable and every button click increment that variable like below:
int i=0;
buttonname.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View view)
{
i++;
}
});
textview.setText(i)
So I have three labels and I added a mouse clicked listener on them. If I clicked the label, the value inside the label will change. Now, I want to add a key press listener. When I press letter a for example, I want my label1 to change also its value. I already made a keyeventlistener for that but it doesn't do what I want.
private void secondKeyPressed(java.awt.event.KeyEvent evt) {
if(evt.getKeyCode()==KeyEvent.VK_A){
int number = Integer.parseInt(second.getText());
number = number + 1;
String number1 = String.valueOf(number);
first.setText(number1);
}
}
My program has 3 buttons so when I start my program, the first button is highlighted so I guess, that's the place where I should do an event listener? Is there anyway that I can start adding key event listener on the very start of my program?
This question is related to one I posted earlier. Views removed using removeView() are there when I next open the Activity (Android)
Background: When a user logs into my app they are taken from the login activity to the mainpage activity. The mainpage has a TableLayout that contains dynamically generated buttons. However if the user logs out and back in again, all of these buttons are repeated so I am trying to find out how best to remove these buttons after they are generated. In my previous post it was suggested I remove the buttons at the very start of the main page activity, before the new ones are drawn, so this is what I am trying to implement.
However when I call getChildCount() on this layout it does not always return the correct answer.
So far, here is the code that is run at the start of the main page activity:
TableLayout tableLayout = (TableLayout)findViewById(R.id.MainPageTableTitle);
//removeSectionButtons(tableLayout); this is where i am trying to remove the buttons
System.out.println("there are oncreate " + tableLayout.getChildCount());
drawButtons(tableLayout);
System.out.println("there are ondraw " + tableLayout.getChildCount());
The first print line returns 0 and the second print line always returns the correct answer (number of buttons drawn including all of the repeated ones). But I am not sure why getChildCount() returns the wrong answer the first time. If anyone can explain I would be incredibly grateful
My drawButtons() method is as follows (it draws two buttons per row):
public void drawButtons(TableLayout tableLayout){
//get the number of buttons
int noOfButtons = mySectionTableHandler.getSectionDetails().size();
//calculate the number of rows needed (there are 2 columns)
//set flag to say if buttons are odd as it affects how many are drawn
int noOfRows;
boolean evenNoOfButtons;
if(noOfButtons % 2 == 0){
//even no of buttons
noOfRows = noOfButtons/2;
evenNoOfButtons = true;
} else {
//odd no of buttons
noOfRows = (noOfButtons+1)/2;
evenNoOfButtons = false;
}
//counter to give each button a unique id
int counter = 1;
for(int i = 0; i<noOfRows;i++){
TableRow newRow = new TableRow(this);
Button a = new Button(MainPageActivity.this);
a.setId(counter);
sectionButtons.put(counter, a);
counter++;
newRow.addView(a);
//if there are even buttons OR if there are an odd no
//of buttons but this isn't the last row then add
//second button to row
if(evenNoOfButtons || (!evenNoOfButtons && (noOfRows-1!=i))){
Button b = new Button(MainPageActivity.this);
b.setId(counter);
sectionButtons.put(counter, b);
counter++;
newRow.addView(b);
}
tableLayout.addView(newRow);
}
}
This was my bad, turns out I was re-adding data to the sqlite database each time the user logged in without wiping previous details.
So my code was generating a button for every field in the database as it should have been.
I am working on an android server-client application. There is a connection between the android application and the server.
Based on the first message from the server, a number of buttons is created and displayed on the screen. This number usually is between 1 and 10.
I don't want to initialise 10 buttons in my activity, as maybe, in the future, the number of buttons will increase to 20.
This is the way I initialise buttons and show them on the screen:
actionButtons = server.getActionButtons();
TableLayout buttonLayout =
(TableLayout) rootView.findViewById(R.id.tblLayoutButtons);
for(int i=0; i< actionButtons.length; i++)
{
btnAction.setWidth(100);
btnAction.setHeight(50);
btnAction.setTag(actionButtons[i]);
btnAction.setText(actionButtons[i].getName());
btnAction.setOnClickListener(btnActionClick);
buttonLayout.addView(btnAction);
}
This all works well. But my problem is that the server sends statusupdates for the buttons, every 3 seconds. Each button stands for a light, that can be on or off. A button that is 'ON' should have another background than a button that is 'OFF'. The buttons should be updated every time an update from the server is received.
How could this be done?
I would make it like this:
public void createButtons() {
actionButtons = server.getActionButtons();
TableLayout buttonLayout = (TableLayout) rootView.findViewById(R.id.tblLayoutButtons);
for(int i=0; i< actionButtons.length; i++)
{
btnAction.setWidth(100);
btnAction.setHeight(50);
btnAction.setTag(actionButtons[i]);
btnAction.setText(actionButtons[i].getName());
btnAction.setOnClickListener(btnActionClick);
btnAction.setId(i);
buttonLayout.addView(btnAction);
}
}
public void updateButton() {
//Get the ID of the button to toggle from the server and get the related view
ToggleButton buttonToToggle = (ToggleButton) findViewById(Integer.parseInt(server.getMessage()));
if (buttonToToggle.isChecked()) {
buttonToToggle.setChecked(false);
buttonToToggle.setBackground(R.drawable.offImage)
}
else {
buttonToToggle.setChecked(true);
buttonToToggle.setBackground(R.drawable.onImage)
}
}
This has the advantage, that you just have to send the ID of the Button and it gets toggled.
Alternatively you could send the binary value off all buttons, so you have to send also just a few bytes... In this case, you can assign the buttons their value in a for loop, almost like you create them.
Hope I helped ;)
If you want to change status of an already created Button then while creating them at run time assign them unique Id's using setID(int) method from the View class.
So for your button1 you can,
button1.setId(100);
and when you want to change thngs you can fetch the ID for the button and change it appropriately.
if(toChangeButton.getID() == 100){
//Change button1
}
Hope this helps.
Try to use the listview to add the buttons.When you get the data from the server,just update the adapter of the listview.
Hope this helps.