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
Related
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();
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);
I have an application that populates a ListView with user input data from another Activity ; however, whenever I enter that data into the second Activity then return to the ListView activity, I see it populated with the values I entered. But, if I return to the second activity again, enter the values, and go back to the ListView activity, I see the new values updated but the old ones have disappeared.
Now, I don't know if I'm not updating the listview correctly or if I need to use SQLite databases to store this data or shared preferences in order to update the listview to new data while containing the previous information.
Thanks for any help.
ListActivity.java
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.personalproject.peter.timerapp.TestingForAlarmData.TestAlarm;
import java.util.ArrayList;
import java.util.List;
public class ListOfAlarms extends ActionBarActivity {
private static final int RESULT = 1000;
List<TestAlarm> alarms = new ArrayList<>();
String title;
int totalTime;
ListView listOfAlarms;
ArrayAdapter<TestAlarm> alarmArrayAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_of_alarms);
final TextView emptyViewForList = (TextView) findViewById(R.id.emptyTextViewForList);
listOfAlarms = (ListView) findViewById(R.id.listView);
alarmArrayAdapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1, alarms);
listOfAlarms.setAdapter(alarmArrayAdapter);
// if(listOfAlarms.getCount() <= 0){
// emptyViewForList.setText("No Alarms Currently Available");
// listOfAlarms.setEmptyView(emptyViewForList);
// }
listOfAlarms.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
alarms.get(position);
Intent clockDownActivity = new Intent(ListOfAlarms.this, CountDownAct.class);
clockDownActivity.putExtra("Title", title);
clockDownActivity.putExtra("totalTime", totalTime);
startActivity(clockDownActivity);
}
});
}
#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_list_of_alarms, 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);
}
public void goToFillOut(View view) {
Intent goingToFillOut = new Intent(this, Test.class);
startActivityForResult(goingToFillOut, RESULT);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == RESULT && resultCode == RESULT_OK){
title = data.getStringExtra("title");
totalTime = data.getIntExtra("totalTime", 0);
alarms.add(new TestAlarm(title, totalTime));
alarmArrayAdapter.notifyDataSetChanged();
}
}
}
SecondActivity
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Test extends ActionBarActivity {
private static final String LOGTAG = "Test.class";
private static final long timeInterval = 1000;
private Button complete;
private EditText titleEditText;
private EditText hourEditText;
private EditText minuteEditText;
private EditText secondEditText;
public static int hour;
public static int minute;
public static int second;
public static String title;
public int actualTimeFiniliazedInMilliSeconds;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
titleEditText = (EditText) findViewById(R.id.titleEditText);
hourEditText = (EditText) findViewById(R.id.hourEditText);
minuteEditText = (EditText) findViewById(R.id.minuteEditText);
secondEditText = (EditText) findViewById(R.id.secondEditText);
complete = (Button) findViewById(R.id.completeButton);
}
#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);
}
public void saveTimer(View view) {
if(titleEditText.getText().toString().isEmpty() || hourEditText.getText().toString().isEmpty()
|| minuteEditText.getText().toString().isEmpty() || secondEditText.getText().toString().isEmpty()) {
Toast.makeText(this, "Oops you forgot one", Toast.LENGTH_LONG).show();
return;
}
// complete.setVisibility(View.GONE);
title = titleEditText.getText().toString();
hour = Integer.parseInt(hourEditText.getText().toString().trim());
minute = Integer.parseInt(minuteEditText.getText().toString().trim());
second = Integer.parseInt(secondEditText.getText().toString().trim());
hour *= 3600000;
minute *= 60000;
second *= 1000;
actualTimeFiniliazedInMilliSeconds = hour + minute + second;
Intent intent = new Intent(Test.this, ListOfAlarms.class);
intent.putExtra("title", title);
intent.putExtra("totalTime", actualTimeFiniliazedInMilliSeconds);
setResult(RESULT_OK, intent);
finish();
}
}
Alarm.java
public class TestAlarm {
public String title;
public int totalTime;
public TestAlarm (String title, int totalTime) {
this.title = title;
this.totalTime = totalTime;
}
#Override
public String toString() {
return title;
}
}
I am new to android and I am just dipping my toes in with is. I want to have number picker define what the color of text is. This is the code for the number picker so far.
package nathan.nathan;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.NumberPicker;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
TextView numberView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
numberView = (TextView)findViewById(R.id.numberview);
NumberPicker numberPicker = (NumberPicker) findViewById(R.id.numberPicker);
numberPicker.setMaxValue(100);
numberPicker.setMinValue(0);
numberPicker.setWrapSelectorWheel(true);
numberPicker.setOnValueChangedListener( new NumberPicker.
OnValueChangeListener() {
#Override
public void onValueChange(NumberPicker picker, int
oldVal, int newVal) {
numberView.setText("I am "+
newVal);
}
});
}
#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);
}
}
I'm just confused on what to do. Help would be much appreciated! :)
It's up to you how you'd like to map your integer from 0 to 100 to a color, but here's one way to do it:
numberPicker.setOnValueChangedListener(
new NumberPicker.OnValueChangeListener() {
#Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
numberView.setText("I am " + newVal);
int color;
if (newVal < 30) {
color = Color.parseColor("#ff0000");
} else if (newVal < 60) {
color = Color.parseColor("#00ff00");
} else {
color = Color.parseColor("#0000ff");
}
numberView.setTextColor(color);
}
});
See the TextView.setTextColor() documentation.
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.