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);
Related
Starting with line 34 I am getting error: class, interface, enum expected. This is a simple percent calculator app as my introduction to android studio. I have tried fix all all the formatting suggestion I read for other people getting the same error but I had no luck. I would appreciate any suggestions or ideas on how to fix that or also if you have any basic app ideas I would love to hear them.
package com.example.bheue.percenttutorial;
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.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView totalTextView;
EditText percentageText;
EditText numberText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
};
TextView totalTextView = (TextView) findViewById(R.id.totalText);
percentageText = (EditText) findViewById(R.id.percentageText);
numberText = (EditText) findViewById(R.id.numberText);
Button calcButton = (Button) findViewById(R.id.percentText);
calcButton.setOnClickListener(new view.OnClickListener())
#Override
public void onClick(view view){
float percentage = Float.parseFloat(percentageText.getText().toString());
float dec = percentage / 100;
float total = dec * Float.parseFloat(numberText.getText().toString());
totalTextView.setText(Float.toString(total));
}
#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);
}
This is what your code should be:
package com.example.bheue.percenttutorial;
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.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.uplication.hamza.helpinghands.R;
public class MainActivity extends AppCompatActivity {
TextView totalTextView;
EditText percentageText;
EditText numberText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final TextView totalTextView = (TextView) findViewById(R.id.totalText);
percentageText = (EditText) findViewById(R.id.percentageText);
numberText = (EditText) findViewById(R.id.numberText);
Button calcButton = (Button) findViewById(R.id.percentText);
calcButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
float percentage = Float.parseFloat(percentageText.getText().toString());
float dec = percentage / 100;
float total = dec * Float.parseFloat(numberText.getText().toString());
totalTextView.setText(Float.toString(total));
}
});
}
#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);
}
}
Did you copy this code from somewhere and had problems in pasting?
I can not figure out why this code output always show NaN. Is there anybody who can help me figure out where I did mistake?
package com.example.user.solar_calculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Context;
import android.content.Intent;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;
public class efficiency extends Activity {
Button button;
EditText efficiency;
EditText vol;
String e,v;
double e1,wat,v1, AC_Load_in_ampere_hour_calculation,dc;
double load,load_vol,Total_Daily_load,lol;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_efficiency);
initControls();
addListenerOnButton();
}
public void initControls() {
efficiency=(EditText)findViewById(R.id.editText1);
vol=(EditText)findViewById(R.id.editText2);
button=(Button)findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
e1=Double.parseDouble(efficiency.getText().toString()); // Make use of autoboxing. It's also easier to read.
} catch (NumberFormatException e) {
e1=1;
}
try {
v1=Double.parseDouble(vol.getText().toString());// Make use of autoboxing. It's also easier to read.
} catch (NumberFormatException e) {
v1=1;
}
Bundle extras = getIntent().getExtras();
if(extras !=null) {
String value = extras.getString("FOUR");
load=Double.parseDouble(value.toString());
}
wat=load_vol*(1+.1);
AC_Load_in_ampere_hour_calculation = (load)/( v1* (e1/100));
Total_Daily_load=AC_Load_in_ampere_hour_calculation+dc;
Intent i = new Intent(efficiency.this, calculation.class);
//Create the bundle
Bundle bundle = new Bundle();
//Add your data to bundle
bundle.putString("seven", String.valueOf(AC_Load_in_ampere_hour_calculation));
bundle.putString("eight", String.valueOf(Total_Daily_load));
bundle.putString("nine", String.valueOf(dc));
//Add the bundle to the intent
i.putExtras(bundle);
//Fire that second activity
startActivity(i);
}
});
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button);
}
#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 think the line who cause problem is AC_Load_in_ampere_hour_calculation = (load)/( v1* (e1/100));
Because if v1 = 0 it will be impossible.
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 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
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.