Question about CheckBox in Java in Android Studio - android

I am creating a project in Android Studio, in which there is a small home screen with a button. When a user clicks this button on the home screen, the application must go to another screen, which contains several checkboxes.
However, I am having two problems:
The code accesses the main page. But when you click on a button to go to another page, the application leaves the screen instead of going to another page. I already put the name of the method that is on another page, which I want you to execute, in the onclick attribute but it still doesn't work.
I would like that after a user clicks on a button that is on the home screen, opens a screen that contains several checkboxes (this screen is already made). Each check box contains a value. When a user clicks on a checkbox, I created a variable that adds up.
When the user clicks on the checkboxes the user wants, and after the user clicks the send button, the sum of the checkbox values ​​will appear in a textView.
This is the checkbox code:
int result = 0;
private CheckBox blue;
private CheckBox red;
private CheckBox yellow;
private CheckBox green;
private CheckBox black;
private CheckBox white;
private CheckBox orange;
private CheckBox purple;
private CheckBox gray;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.id.interfacegraphicalname);
blue = findViewById(R.id.blue);
red = findViewById(R.id.red);
yellow = findViewById(R.id.yellow);
green = findViewById(R.id.green);
black = findViewById(R.id.black);
white = findViewById(R.id.white);
orange = findViewById(R.id.orange);
purple = findViewById(R.id.purple);
gray = findViewById(R.id.gray);
testFinalResult(result);
}
public void check() {
if (blue.isChecked()) {
result += 30;
} else if (red.isChecked()) {
result += 30;
} else if (green.isChecked()) {
result += 30;
} else ig (yellow.isChecked()) {
result += 20;
} else if (black.isChecked()) {
result += 20;
} else if (white.isChecked()) {
result +=20;
} else if (orange.isChecked()) {
result += 10;
} else if (purple.isChecked()) {
result += 10;
} else if (gray.isChecked()) {
result +=10;
}
int testResult = check();
public void finalTestResult(int testResult) {
if (testResult <= 60) {
finalResultText.setText("low value");
} else if (testResult <= 120) {
finalResultText.setText("medium value");
} else if (testResult<= 180) {
finalResultText.setText("high value");
}
}
}
But as the code contains one more error that when you click a button on the home screen, the application is not going to the next screen for some reason, I would like to show you all the code (which is too much big to insert here on Stack OverFlow) for you to check out what is going wrong, that I'm not able to run the code normally at all, ok?
Please, could you check?
I've been working on this project for a long time, and I'm not getting the code to run normally on the smartphone.
The code link is:
https://github.com/AvaianoFC1/OficialTestProject

/Checked out your code on github here is my initial findings
hopefully it will help you resolve your issue in your Calc.java class you need to setcontentview to entries layout instead of referring back to main layout*/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
setContentView(R.layout.entries);
you are referring back to main activity layout which only has the layout for one button, so your button is working but its taking you back to activity main - you need to display entries layout and get all your values from that layout and then move ahead accordingly
another error from my inital lookup that i noticed is this
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/holo_blue_bright"
android:onClick="sendMessage2"
android:text="OPEN PAGE 2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.647"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.237" />
android:onClick="sendMessage2" -> you are calling the sendMessage2 function which you have not defined you need to call the openPage2 function
also you do not need the openPage1 and openPage2 in your main activity class
Let me know if this helps you these are the few errors that are flatout - also i would recommend adding a snackbar or a toast message on button click before implementing the functionality just to make sure thats its working as intended

Related

VISIBLE EditText goes to INVISBLE on orientation change

I have a spinner that has numbers from 1 - 10. If the user selects anything above 1, a certain EditText that is INVISBLE becomes VISIBLE, this is what I want. But when I change it from portrait to landscape, it goes back to INVISIBLE.
This is where the changing happens
btn_Calc.setOnClickListener(new View.OnClickListener() {
#Override
if (split > 1) {
et_APP.setVisibility(View.VISIBLE);
tv_app.setVisibility(View.VISIBLE);
vis = 1;
}
else{
et_APP.setVisibility(View.INVISIBLE);
tv_app.setVisibility(View.INVISIBLE);
vis = 2;
}
});
And I tried to do onSaveInstanceState like this
if(savedInstanceState != null) {
int isVis = savedInstanceState.getInt("vis", 2);
if(isVis == 1){
et_APP.setVisibility(View.VISIBLE);
tv_app.setVisibility(View.VISIBLE);
}
else{
et_APP.setVisibility(View.INVISIBLE);
tv_app.setVisibility(View.INVISIBLE);
}
}
public void onSaveInstanceState(Bundle savedInstanceState){
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putInt("getVisible", vis);
}
Sorry for poor formatting, I cut out unrelated code.
It works that if the selected item in the spinner is greater than 1 and I click the calc button, it shows the fields I want but when I change orientation they dissapear again. Any ideas on how I can get to stay visible when I change the orientation?
The names of your keys don't match between save and load.
Saving:
savedInstanceState.putInt("getVisible", vis);
Restoring:
int isVis = savedInstanceState.getInt("vis", 2);
The layout is being recreated if you don't want Android to do anything add the following line:`android:configChanges="keyboardHidden|orientation|screenSize"' to the activity in the manifest.

100 Buttons and only 1 Active

I have one activity and here i have 100 buttons, i want that when i press Button 1 then press another Button the Button 1 should get unpressed.
i know i can make this with
if(Button1.isPressed()) {
Button2.setPressed(false);
Button3.setPressed(false);
Button4.setPressed(false);
Button5.setPressed(false);
Button6.setPressed(false);
Button7.setPressed(false);
Button8.setPressed(false);
......................... }
else { do nothing }
.... BUT!
it's too much code
Coders will kill me or will just laugh on me.
any ideas?
maybe there is a way to unpress the all buttons from the activity?
Not the prettiest solution ever, but you could make an OnClickListener like this:
View.OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
ViewGroup parent = (ViewGroup) v.getParent();
for (int i = 0; i < parent.getChildCount(); i++) {
View current = parent.getChildAt(i);
if (current != v && current instanceof Button) {
((Button) current).setPressed(false);
}
}
((Button) v).setPressed(true);
}
}
and attach it to all of your buttons.
Then, whenever a button is clicked, it will iterate over all views that are in the same layout (or actually, view group) as the clicked button, and, for any of those views that are buttons except for the clicked button, it will call setPressed(false).
Note that this only works out of the box if all the buttons are in the same layout. If they are in nested layouts, you will have to adapt it a little.
Off topic: What do you need 100 buttons for? That's a lot of buttons. You may want to redesign your user interface
Ok so instead of looping through all the buttons on over and over again when one button is pressed, you can just store a variable which stores the button number of the button that was last pressed. Now, when the second button is pressed, disable the button that was pressed earlier, you get its index from the saved variable, enable the button that was pressed and store its index in the variable.
Heres an example pseudo code to give you and idea:
int buttonLastPressed = 0;
void onButtonClick(Button buttonPressed){
if(buttonLastPressed != 0){
disableButton(buttonLastPressed);
enableButton(buttonPressed);
buttonLastPressed = buttonPressed.getIndex()
}
}
Saves you from looping through each and every button to disable it.
Define id of button 1 to 100
When press button occurs save it's id in some member variable like previous_pressed
Before updating a previous_pressed value find and unpress previous pressed button like this
Button previous_pressed_button = (Button) findViewById(previous_pressed);
Now you have the previous pressed button, So upress it or whatever.

swithcing to next textview

Ok im making app and it have 15 button's and 6 textview's.I want when I press first button to change value of first textview(value is "") to something (number one for example).But problem is when i press second button if first textview is already set to some value to set set second textview to second value.
If you need something else ask in comments (sorry for bad English)
here is what I was doing(this is under onclick)when i press second button
if(textview1.equals("1")){
textview2.setText("2");}
else if (textview1.equals("")){
textview1.setText("2");
}
It sounds like you wish to show last 6 buttons pressed.
Store all pressed buttons in a List (i.e. LinkedList) of size 6. Initially, it will be empty.
Then whenever any button is pressed do two things:
1) add it to the List and delete old elements if size exceeds six.
2) set button values from List.
Second step can be achieved like this:
// all TextViews should be in a list
private List<TextView> textViews;
// declare as field
private List<String> memoryQueue = new ArrayList<String>();
public void update() {
//set fields for the pressed buttons
for (int i=0; i<6 && i<memoryQueue.size(); i++) {
String text = memoryQueue.get(i);
textViews.get(i).setText(text);
}
// set empty fields
for (int i = memoryQueue.size(); i<6; i++) {
textViews.get(i).setText("");
}
}
This code snippet assumes that you store your TextViews in a List.
And Easiest way to keep track of last six button:
public void buttonPressed(Button button) {
//get text based on your button
String text = button.getText();
if (memoryQueue.contains(text)) {
return;
}
memoryQueue.add(text);
if (memoryQueue.size() > 6) {
memoryQueue.remove(0);
}
}
Since you're concerned with the text inside of your text view, you should be using the object's getText method:
if( textview1.getText().equals("1") ){ // Edited
textview2.setText("2");
} else if (textview1.getText().equals("")){ //Edited
textview1.setText("2");
}
At first, you have to get the String text from TextView using getText() method then you can compare that String with another String. Now, change your condition as follows...
if(textview1.getText().toString().equals("1")){
textview2.setText("2");}
else if (textview1.getText().toString().equals("")){
textview1.setText("2");
}

Change Text in a same Layout

I'm Making simple app for project
That App contains lot of text so i want,
"when a button is pressed, text should Change in same layout"
like PowerPoint slide.
I want change text only not scroll.
Now i made my app, have lots of Windows or Layouts.
It is not looking good, too much layout in simple app so please help me .
Thanks in advance
Doing this is very easy, I will quickly walk you through the Algorithm:
Set a class level variable called as FLAG initialize it to 1.
Let us assume that FLAG = 1 will represent the first slide. FLAG = 2 the second slide and so on.
Now in your button click you can use a switch case or an if else condition, based on the value of the flag display the relevant text in textview.
Once done, increment the flag, for the next set of sentence(s).
Class level:
int FLAG = 1;
onCreate:
Initialize your textView:
TextView mtv = (TextView)findViewById(R.id.yourid);
Set a button click listener:
private View.OnClickListener slides = new View.OnClickListener() {
#Override
public void onClick(View v) {
if(FLAG ==1)
mtv.setText("First slide");
else if(FLAG ==2)
mtv.setText("Second Slide");
//and so on...
FLAG = FLAG+1;//increment flag
}
};

Android multiple button click

Android multiple button click
I have a button and I want on the button's first click i display a thing, and on it's second i display another etc ..
I have a button and want it to have 11 clicks .. on the first click Num.settext("First");
on second click Num.settext("Second");
etc .. until the tenth click .. then on the 11th click its's Num.settext("0"); and it resets from the begning ..
like ..
1,2,3,4,5,6,7,8,9,10,11(0) 1,2,3,4,5,6,7,8,9,10,11(0)
Wouldn't be easier if you store the click number in a variable?
For instance:
//...
int clickNumber = 0;
//...
public void onClick() {
if(clickNumber > 10) {//reset variable
clickNumber = 0;
}
if(clickNumber == 0) {
Num.setText("First");
clickNumber++;
}
else if(clickNumber == 1) {
Num.setText("Second");
clickNumber++;
}
//...
}
//...
Maintain a field called cycle and an array of texts.
Then on click:
Num.setText(texts[cycle]);
cycle=(cycle + 1)%texts.length;

Categories

Resources