Counter not incrementing for radio button in android - java

am using an if statement to increment the answer for a quiz app in android. i think for loop or while loop should work berrter. But i hae also tested that . am now getting it well.
Below is my code. Thank you
package newton.quizapplication;
import android.app.Activity;
import android.database.CursorJoiner;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MyActivity extends Activity {
private RadioGroup radioGroup1;
private RadioGroup radioGroup2;
private RadioGroup radioGroup3;
private RadioButton radioButton1;
private RadioButton radioButton2;
private RadioButton radioButton3;
private Button btnSubmit;
private int Ans = 0;
private TextView Total;
// private int wrong = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
String All;
Total = (TextView)findViewById(R.id.Score);
addListenerOnButton();
}
public void addListenerOnButton() {
radioGroup1 = (RadioGroup) findViewById(R.id.rg1);
radioGroup2 = (RadioGroup) findViewById(R.id.rg2);
radioGroup2 = (RadioGroup) findViewById(R.id.rg3);
btnSubmit = (Button) findViewById(R.id.submit);
btnSubmit.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// get selected radio button from radioGroup
int selectedId1 = radioGroup1.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioButton1 = (RadioButton) findViewById(selectedId1);
// get selected radio button from radioGroup
int selectedId2 = radioGroup1.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioButton2 = (RadioButton) findViewById(selectedId2);
// get selected radio button from radioGroup
int selectedId3 = radioGroup1.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioButton3 = (RadioButton) findViewById(selectedId3);
if (radioButton1.getText().equals("Hyper Text Markup Language")) {
Ans++;
}
else if (radioButton2.getText().equals("The World Wide Web Consortium")) {
// Toast.makeText(MyActivity.this, "Incorrect Answer", Toast.LENGTH_SHORT).show();
Ans++;
}
else if (radioButton2.getText().equals("Specific places within a particular page")) {
Ans++;
}
Total.setText("You Got " + Ans + " Answer correct out of 3");
Ans =0;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

You should change your conditions to :
if (radioButton1.getText().equals("Hyper Text Markup Language")) {
Ans++;
}
if (radioButton2.getText().equals("The World Wide Web Consortium")) {
// Toast.makeText(MyActivity.this, "Incorrect Answer", Toast.LENGTH_SHORT).show();
Ans++;
}
if (radioButton3.getText().equals("Specific places within a particular page")) {
Ans++;
}
In your original code of if (...) ... else if (...) ... else if (...) ..., only one condition can be met (since the first time a condition evaluates to true, no other conditions would be checked), so Ans can only be 0 or 1.
In addition, I'm assuming your last if should check radioButon3.

Related

Android Studio Java code

I'm trying to replace a part of the text after clicking the bottom but I can't make it work. When I click the bottom without entering a name the message should say "Hello Username", and if I enter a name the "Username" should be replace by the name. Here is the code so far. Thanks
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
public class HelloAndroid extends AppCompatActivity implements OnClickListener {
private EditText name;
private TextView display;
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello_android);
name = (EditText) findViewById(R.id.name);
display = (TextView) findViewById(R.id.display);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_hello_android, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onClick(View view) {
if (button == view) {
String message = "Hello, Username " + name.getText().toString() + "!";
if (button == view) {
String username = "Hello " + name.getText().toString() + "";
Toast toast = Toast.makeText(this, message, Toast.LENGTH_SHORT);
toast.show();
display.setText(message);
}
}
}
}
You are enable to do this because may be you are not getting click here . Here is my code change it .
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class HelloAndroid extends AppCompatActivity implements View.OnClickListener {
private EditText name;
private TextView display;
private Button button;
String message ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dummy);
name = (EditText) findViewById(R.id.name);
display = (TextView) findViewById(R.id.display);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.button :
if (name.getText().toString().isEmpty())
{
message = "Hello username" ;
}
else {
message = "Hello "+name.getText().toString() ;
}
Toast toast = Toast.makeText(this, message, Toast.LENGTH_SHORT);
toast.show();
display.setText(message);
break;
}
}
}

Android Studio unable to resolve method in class

I am trying to make a basic connect 3 app as part of a course challenge.
The problem I am having is that I am unable to access the methods inside the class Players in order to set which buttons have been pressed by which players: Cannot resolve method 'noughtsPlayer'.
I created an instance of Players on the onCreate method and declared it at the top of the script. That instance itself is recognised but for some reason the contained methods, noughtsPlayer and crossesPlayer are not.
MainActivity.java
package com.example.richardcurteis.connect3;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
public boolean noughtsTurn;
ArrayList board;
String players;
public void receiveClick(View view) {
String buttonPressed = (String) view.getTag();
board.remove(buttonPressed);
if (view instanceof Button) {
Button B = (Button) view;
B.setText(noughtsTurn ? players.noughtsPlayer() : players.crossesPlayer());
}
}
class Players {
public String noughtsPlayer() { return "O"; }
public String crossesPlayer() { return "X"; }
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
boolean noughtsTurn = true;
board = new ArrayList();
for (int x = 0; x < getBoardSize(); x++) {
String y = String.valueOf(x);
board.add(y);
}
Players players = new Players();
}
public int getBoardSize() {
int buttonCount = 0;
TableLayout tableLayout = (TableLayout) findViewById(R.id.tableLayout);
for (int rowIndex = 0; rowIndex < tableLayout.getChildCount(); rowIndex++) {
View tableLayoutChild = tableLayout.getChildAt(rowIndex);
if (tableLayoutChild instanceof TableRow) {
for (int i = 0; i < ((ViewGroup) tableLayoutChild).getChildCount(); i++) {
View view = ((ViewGroup) tableLayoutChild).getChildAt(i);
if (view instanceof Button) {
buttonCount++;
}
}
}
}
return buttonCount;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
You have global variable String players and local variable Players players.
This is problem.
And class String doesn't have those methods.
To solve this problem change String players; to Players players;
and Players players = new Players(); to players = new Players();

How to swap the position of two buttons in android?

I am creating a a puzzle match game where in order to match two buttons together you must swap the position of them. for example, i want to swap the position of button1 with the position that button2 is in. Anybody have any suggestions?
Here is my code below:
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import java.util.ArrayList;
import java.util.Collections;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class puzzle extends ActionBarActivity {
private TextView moveCounter;
private TextView feedbackText;
private Button[] buttons;
private Boolean bad_move=false;
// private static final Integer[] goal = new Integer[] {0,1,2,3,4,5,6,7,8};
// private String [][] puz = new String [3][3];
Button b [][];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_puzzle);
swap();
}
private void setBoard(){
b = new Button[3][3];
b[0][0]= (Button).findViewById(R.id.button1);
b[0][1]= (Button).findViewById(R.id.button2);
b[0][2] = (Button).FindById(R.id.button3);
b[1][0] = (Button).FindBdyId(R.id.button4);
b[1][1] = (Button).FindById(R.id.button5);
b[1][2] = (Button).FindById(R.id.button6);
b[2][0] = (Button).FindById(R.id.button7);
b[2][1] = (Button).FindById(R.id.button8);
b[2][2] = (Button).FindById(R.id.button9);
}
private void swap() {
b.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_puzzle, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
If you are using API level greater than 11, you can use getX() and getY() to get coordinates of a button.
And setX() and setY() to set a button in a particular coordinate.
for example,
// getting coordinates of button
float posX = button.getX();
float posY = button.getY();
// setting button2 in the coordinates that we got above
button2.setX(posX);
button2.setY(posY);
The way I've gotten this work is similar to your approach you had originally posted. Try giving this a shot:
Button btnOne = (Button) findViewById(R.id.buttonOne);
Button btnTwo = (Button) findViewById(R.id.buttonTwo);
float posOneX = btnOne.getX();
float posOneY = btnOne.getY();
float posTwoX = btnTwo.getX();
float posTwoY = btnTwo.getY();
btnOne.setX(posTwoX);
btnOne.setY(posTwoY);
btnTwo.setX(posOneX);
btnTwo.setY(posOneY);

Creating a series of radio groups in Android Eclipse

Brilliant stuff Umesh, thanks! You've taught me a lot there!
I've one more question if you don't mind.
I would now like to choose one radio button from each radiogroup (for Example, Chinese, Level 1, Guess the City) and then have the message at the top read "You chose Chinese, Level 1, Guess the City". How can I write this in the Java script. so far I have:
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private RadioGroup radioGroup1;
private RadioGroup radioGroup2;
private RadioGroup radioGroup3;
private RadioButton chinese, indian, russian;
private Button button;
private TextView textView;
public String string_first_radiogroup;
public String string_second_radiogroup;
public String string_third_radiogroup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup1 = (RadioGroup) findViewById(R.id.radio1);// your radio group1
radioGroup2 = (RadioGroup) findViewById(R.id.radio2);// your radio group2
radioGroup3 = (RadioGroup) findViewById(R.id.radio3);// your radio group3
radioGroup1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// find which radio button is selected
if(checkedId == R.id.chinese) {
string_first_radiogroup="Chinese"; // String updated on radio check
} else if(checkedId == R.id.indian) {
string_first_radiogroup="Indian"; // String updated on radio check
} else {
string_first_radiogroup="Russian"; // String updated on radio check
}
}
});
chinese = (RadioButton) findViewById(R.id.chinese);
indian = (RadioButton) findViewById(R.id.indian);
russian = (RadioButton) findViewById(R.id.russian);
textView = (TextView) findViewById(R.id.text);
radioGroup1.check(R.id.chinese);// setting Chinese selected by default
string_first_radiogroup="Chinese"; //setting "Chinese" by default in string also
radioGroup2.check(R.id.difficulty1);
string_second_radiogroup="Level 1 (Starter)";
button = (Button)findViewById(R.id.choose);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
textView.setText("You chose " + string_first_radiogroup + " option");
textView.setText("You chose " + string_second_radiogroup + " option");
}
});
}
}
Do the same in onCreate() method for radioGroup2 and radioGroup3 .
Reduce your code by Declaring a public string And update it in radioGroup.onCheckedChangeListener
Where you are using toast
On button click just display that string .
No need to check in button clickListener.
For 3 radio groups use 3 strings.
public String string_first_radiogroup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup1 = (RadioGroup) findViewById(R.id.radio1);// your radio group1
radioGroup2 = (RadioGroup) findViewById(R.id.radio2);// your radio group2
radioGroup3 = (RadioGroup) findViewById(R.id.radio3);// your radio group3
radioGroup1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// find which radio button is selected
if(checkedId == R.id.chinese) {
Toast.makeText(getApplicationContext(), "Choice: Chinese",
Toast.LENGTH_SHORT).show();
string_first_radiogroup="Chinese"; // String updated on radio check
} else if(checkedId == R.id.indian) {
Toast.makeText(getApplicationContext(), "Choice: Indian",
Toast.LENGTH_SHORT).show();
string_first_radiogroup="Indian"; // String updated on radio check
} else {
Toast.makeText(getApplicationContext(), "Choice: Russian",
Toast.LENGTH_SHORT).show();
string_first_radiogroup="Russian"; // String updated on radio check
}
}
});
chinese = (RadioButton) findViewById(R.id.chinese);
indian = (RadioButton) findViewById(R.id.indian);
russian = (RadioButton) findViewById(R.id.russian);
textView = (TextView) findViewById(R.id.text);
radioGroup.check(R.id.chinese);// setting Chinese slected by default
string_first_radiogroup="Chinese"; //setting "Chinese" by default in string also
button = (Button)findViewById(R.id.choose);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
textView.setText("You chose " + string_first_radiogroup + " option");
}
});
}

Converting a double to a string for a radio button

I have not been able to figure out but what im trying to do is convert a couple double variables in my application to a string. Im not exactly sure how to do that in my current code. I keep getting a "cannot invoke isChecked() on the primitive type double". I wasnt able to find any tutorial explaining how to do this so forgive me. Here is my code, any tips would be appreciated :
package net.androidbootcamp.zipcarrentalapp;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity
{
private RadioGroup radioGroupId;
private RadioButton radioButton;
private Button button;
final RadioButton Compact = (RadioButton)findViewById(R.id.radCompact);
final RadioButton MidSize = (RadioButton)findViewById(R.id.radMidSize);
final RadioButton Luxury = (RadioButton)findViewById(R.id.radLuxury);
final TextView result = (TextView) findViewById(R.id.txtResult);
double radCompact = 59.99;
double radMidSize = 65.99;
double radLuxury = 89.99;
int days = 0;
double computeValue;
public void operation(int days)
{
if(days <= 10)
{
if(radCompact.isChecked())
{
computeValue = radCompact * days;
txtResult.setText("Cost:" + Double.toString(computeValue));
}
else if (radMidSize.isChecked())
{
computeValue = radMidSize * days;
txtResult.setText("Cost: " + Double.toString(computeValue));
}
else if (radLuxury.isChecked())
{
computeValue = radLuxury * days;
txtResult.setText("Cost:" + Double.toString(computeValue));
}
}
}
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings)
{
return true;
}
return super.onOptionsItemSelected(item);
}
}
Your code is a little bit off. You have three buttons,
final RadioButton Compact = (RadioButton)findViewById(R.id.radCompact);
final RadioButton MidSize = (RadioButton)findViewById(R.id.radMidSize);
final RadioButton Luxury = (RadioButton)findViewById(R.id.radLuxury);
You call isChecked() on them. For example, this
if(radCompact.isChecked())
should be
if (Compact.isChecked())
and then
else if (MidSize.isChecked())
and finally
else if (Luxury.isChecked())
Edit And,
final TextView result = (TextView) findViewById(R.id.txtResult);
should be
final TextView txtResult = (TextView) findViewById(R.id.txtResult);
You could try to typecast the variable to the Double object.
Something like:
((Double) computeValue).toString

Categories