I am new to Android and am trying to create a very basic app that calculates and displays the GCF of two numbers entered by the user. Here is a copy of my GCF.java:
package com.example.GCF;
import java.util.Arrays;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class GCF extends Activity {
private TextView mAnswer;
private EditText mA, mB;
private Button ok;
private String A, B;
private int iA, iB;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mA = (EditText) findViewById(R.id.entry);
mB = (EditText) findViewById(R.id.entry1);
ok = (Button) findViewById(R.id.ok);
mAnswer = (TextView) findViewById(R.id.answer1);
ok.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
A = mA.getText().toString();
B = mB.getText().toString();
}
});
// the String to int conversion happens here
iA = Integer.parseInt(A.trim());
iB = Integer.parseInt(B.trim());
while (iA != iB) {
int[] nums={ iA, iB, Math.abs(iA-iB) };
Arrays.sort(nums);
iA=nums[0];
iB=nums[1];
}
updateDisplay();
}
private void updateDisplay() {
mAnswer.setText(
new StringBuilder().append(iA));
}
}
Any Suggestions? Thank you!
You probably want this ( for your onCreate function )
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mA = (EditText) findViewById(R.id.entry);
mB = (EditText) findViewById(R.id.entry1);
ok = (Button) findViewById(R.id.ok);
mAnswer = (TextView) findViewById(R.id.answer1);
ok.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
A = mA.getText().toString();
B = mB.getText().toString();
// the String to int conversion happens here
iA = Integer.parseInt(A.trim());
iB = Integer.parseInt(B.trim());
while (iA != iB) {
int[] nums={ iA, iB, Math.abs(iA-iB) };
Arrays.sort(nums);
iA=nums[0];
iB=nums[1];
}
updateDisplay();
}
});
}
Related
[Android newbie] Help required in adding 4 math operations as options to a radion button.
Also I need to perform respective options on selecting radio button option.
package com.sivaneshsg.wallet;
import android.support.annotation.IdRes;
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.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.view.View.OnClickListener;
public class MainActivity extends AppCompatActivity {
int cashamount = 0;
int cardamount = 0;
int walletamount;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText et = (EditText) findViewById(R.id.inputamount);
final RadioButton card1 = (RadioButton) findViewById(R.id.card1);
final RadioButton cash1 = (RadioButton) findViewById(R.id.cash1);
final RadioButton card2 = (RadioButton) findViewById(R.id.card2);
final RadioButton cash2 = (RadioButton) findViewById(R.id.cash2);
final RadioGroup rg =(RadioGroup) findViewById(R.id.rgroup);
final TextView t1 = (TextView) findViewById(R.id.amountcard);
final TextView t2 = (TextView) findViewById(R.id.amountcash);
final TextView t3 = (TextView) findViewById(R.id.amountwallet);
Button but = (Button) findViewById(R.id.button);
final int amount = Integer.parseInt(et.getText().toString());
cash1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
cash2.setChecked(false);
card1.setChecked(false);
card2.setChecked(false);
cashamount = cashamount + amount;
}
});
card1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
cash2.setChecked(false);
cash1.setChecked(false);
card2.setChecked(false);
cardamount=cardamount+amount;
}
});
cash2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
cash1.setChecked(false);
card1.setChecked(false);
card2.setChecked(false);
cashamount = cashamount - amount;
}
});
card2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
cash2.setChecked(false);
cash1.setChecked(false);
card2.setChecked(false);
cardamount=cardamount-amount;
}
});
but.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
t1.setText("Amount in Card : RS. " + cardamount);
t2.setText("Amount in Cash : Rs. " + cashamount);
walletamount = cardamount + cashamount;
t3.setText("Total Amount in Wallet : RS. " + walletamount);
}
});
}
}
your amount variable taking value at onCreate() method which is initially 0
I'm trying to make an APK that saves passwords with the site using two different ArrayLists. This way, I can get the right indexnumber of the site and get the password based on this indexnumber. In the beginning of MainActivity, I add two random Strings to the ArrayLists, so that I don't have to work with empty ArrayLists, but this is utterly useless I think.
The problem is I can only view the last site-password I have put in. Previous combinations are "lost."
code:
MainActivity.java
package com.example.prive.passwordsafe;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
public ArrayList<String> passwordList = new ArrayList<>();
public ArrayList<String> siteList = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
passwordList.add("ejifjejfijeifjeijfiejifjeijfiejfijefie");
siteList.add("iejfijeifjiejfiejidvjijijeijivjiejvijeivjejv");
Button addButton = (Button) findViewById(R.id.addButton);
Button showButton = (Button) findViewById(R.id.showButton);
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
firstIntent();
}
});
showButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
secondIntent();
}
});
}
#Override
public void onResume(){
super.onResume();
add();
}
private void firstIntent() {
Intent intent = new Intent(MainActivity.this, addActivity.class);
intent.putStringArrayListExtra("passwordList", passwordList);
intent.putStringArrayListExtra("siteList", siteList);
startActivity(intent);
}
private void secondIntent() {
Intent intent = new Intent(MainActivity.this, showActivity.class);
intent.putStringArrayListExtra("passwordList", passwordList);
intent.putStringArrayListExtra("siteList", siteList);
startActivity(intent);
}
public void add(){
Bundle pickupData = getIntent().getExtras();
if(pickupData == null){
return;
}
String receivedPassword = pickupData.getString("Password");
String receivedSite;
receivedSite = pickupData.getString("Site");
passwordList.add(receivedPassword);
siteList.add(receivedSite);
}
}
addActivity.java
package com.example.prive.passwordsafe;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class addActivity extends AppCompatActivity {
public EditText siteInsert, passwordInsert;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_toevoeg);
siteInsert = (EditText) findViewById(R.id.siteInsert);
passwordInsert = (EditText) findViewById(R.id.passwordInsert);
siteInsert.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast msg = Toast.makeText(getBaseContext(), "site", Toast.LENGTH_LONG);
msg.show();
}
});
passwordInsert.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast msg = Toast.makeText(getBaseContext(), "password", Toast.LENGTH_LONG);
msg.show();
}
});
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String password = passwordInsert.getText().toString();
String site = siteInsert.getText().toString();
Intent intent = new Intent(addActivity.this, MainActivity.class);
intent.putExtra("Password", password);
intent.putExtra("Site", site);
startActivity(intent);
}
});
}
}
showActivity.java
package com.example.prive.passwordsafe;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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 showActivity extends AppCompatActivity {
public EditText editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_toon);
Button showButton = (Button) findViewById(R.id.showButton);
Button backButton = (Button) findViewById(R.id.backButton);
editText = (EditText) findViewById(R.id.editText);
final TextView textView = (TextView) findViewById(R.id.textView);
Bundle pickupData = getIntent().getExtras();
final ArrayList<String> passwordList = pickupData.getStringArrayList("passwordList");
final ArrayList<String> siteList = pickupData.getStringArrayList("siteList");
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast msg = Toast.makeText(getBaseContext(), "site", Toast.LENGTH_LONG);
msg.show();
}
});
if (passwordenList != null && siteList != null) {
showButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int numberOfPasswords = passwordenList.size();
for (int i = 0; i <= numberOfPasswords; i++) {
String password;
String temporary = editText.getText().toString();
if (temporary.equals(siteList.get(i))) {
password = passwordList.get(i);
textView.setText(password);
}else{
password = "wrong input";
textView.setText(password);
}
}
}
});
}else{
return;
}
backButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
}
}
When you exit an Activity, all the data on it is lost. You have to persist your ArrayList in SQLite or use SharedPreferences instead.
SharedPreferences: https://developer.android.com/reference/android/content/SharedPreferences.html
SQLite: https://developer.android.com/training/basics/data-storage/databases.html
i was making an android app and i need to use one integer value in 2 activities. I tried using this code but it didn't work.
//Integer Sender
Intent myIntent = new Intent(A.this, B.class);
myIntent.putExtra("MyIntNameGoesHere", intValue);
startActivity(myIntent);
//Integer receiver
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);
It says Cannot resolve symbol intValue and Cannot resolve symbol A and the same for B.
Here's the whole code.
MainActivity:
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int balance;
private SharedPreferences preferences;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Hide notification bar
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//Click counter
final TextView text = (TextView) findViewById(R.id.balance_text);
assert text != null;
// to retreuve the values from the shared preference
preferences = PreferenceManager.getDefaultSharedPreferences(this);
balance = preferences.getInt("balance", 0);
text.setText(balance + " $");
final ImageButton button = (ImageButton) findViewById(R.id.click_button);
assert button != null;
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
balance++;
text.setText("" + balance + " $");
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("balance", balance);
editor.apply();
}
});
final Button UpgradesButton = (Button) findViewById(R.id.upgrades_button);
assert UpgradesButton != null;
UpgradesButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, UpgradesActivity.class));
}
});
//Balance Integer Sender
}
}
UpgradesActivity:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
public class UpgradesActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upgrades);
//Hide notification bar
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
final Button e2u_button = (Button) findViewById(R.id.e2u_button);
assert e2u_button != null;
e2u_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
final Button back_button = (Button) findViewById(R.id.back_button);
assert back_button != null;
back_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(UpgradesActivity.this, MainActivity.class));
}
});
//TODO: Pass balance integer from MainActivity to here.
}
}
Error code:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
public class UpgradesActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upgrades);
//Receive balance from MainActivity
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("key_int", 0);
//Hide notification bar
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
final Button e2u_button = (Button) findViewById(R.id.e2u_button);
assert e2u_button != null;
e2u_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(balance >= 300){ //ERROR ON THIS LINE
}
}
});
final Button back_button = (Button) findViewById(R.id.back_button);
assert back_button != null;
back_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(UpgradesActivity.this, MainActivity.class));
}
});
//TODO: Pass balance integer from MainActivity to here.
}
}
ALL ABOVE IS ANSWERED! ---------------------------------------------------------
Now i have problems with this part of the code:
#Override
public void onClick(View v) {
if(balance >= 300){
balance -= 300;
}
if(balance < 300){
final TextView text = (TextView) findViewById(R.id.not_enough_money_text);
assert text != null;
text.setText("You do not have enough money.");
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
text.setText("");
}
}, 2000);
}
}
When i click the button it says i do not have enough money but i have over 300. Please help me.
I found out what the problem was but I'm not sure how to fix it. I need to send balance back to MainActivity. Can anyone help with that?
Send the data like this -
UpgradesButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, UpgradesActivity.class);
intent.putExtra("key_int", balance);
startActivity(intent);
}
});
And fetch it in onCreate() of UpgradesActivity -
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("key_int", 0);
You're using different key when sending and receiving the Int.
Change this line:
int intValue = mIntent.getIntExtra("intVariableName", 0);
To this:
int intValue = mIntent.getIntExtra("MyIntNameGoesHere", 0);
I am trying to use btn2 to switch my page from MainActivity to CalcPage.class.
Cant seem to figure out what I am doing wrong.
I have error lines underneath btn2, OnClickListener, Override, and View v
Here is my MainActivity.java
package edu.khershockolivetcollege.ballistic_calculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.content.Intent;
import java.text.DecimalFormat;
public class MainActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
Button btn1 = (Button)findViewById(R.id.calculate);
Button btn2 = (Button)findViewById(R.id.calculate);
final EditText et1 = (EditText)findViewById(R.id.muzzleText);
final EditText et2 = (EditText)findViewById(R.id.rangeText);
final TextView time = (TextView)findViewById(R.id.timeAnswer);
final TextView bulletdrop = (TextView)findViewById(R.id.dropAnswer);
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
DecimalFormat f = new DecimalFormat("##.00");
double x = new Integer(et1.getText().toString());
double y = new Integer(et2.getText().toString());
double timetotarget = y / x;
double grav = 9.81;
double timesquared = timetotarget * timetotarget;
double drop = grav * timesquared;
time.setText(" " + f.format(timetotarget) + " seconds");
bulletdrop.setText(" " + f.format(drop) + " meters");
}
btn2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(context, CalcPage.class);
startActivity(i);
}
});
}
}
It seems that you are setting your btn2 OnClickListener inside your btn1 OnClickListener. If you do it outside and close all your curly brackets it should work.
Like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
Button btn1 = (Button)findViewById(R.id.calculate);
Button btn2 = (Button)findViewById(R.id.calculate);
final EditText et1 = (EditText)findViewById(R.id.muzzleText);
final EditText et2 = (EditText)findViewById(R.id.rangeText);
final TextView time = (TextView)findViewById(R.id.timeAnswer);
final TextView bulletdrop = (TextView)findViewById(R.id.dropAnswer);
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
DecimalFormat f = new DecimalFormat("##.00");
double x = new Integer(et1.getText().toString());
double y = new Integer(et2.getText().toString());
double timetotarget = y / x;
double grav = 9.81;
double timesquared = timetotarget * timetotarget;
double drop = grav * timesquared;
time.setText(" " + f.format(timetotarget) + " seconds");
bulletdrop.setText(" " + f.format(drop) + " meters");
}
});
btn2.setOnClickListener(new OnClickListener() {
#Override
public void onClick (View v){
Intent i = new Intent(context, CalcPage.class);
startActivity(i);
}
});
}
Line 33, all debug logs are fine.
As u can see I even comented the method that is causing problems and it still crashesh.
MainActivity.java
package com.example.hillsmatrixinverser;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
Math matrix;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText Text1 = (EditText) findViewById(R.id.a) ;
final EditText Text2 = (EditText) findViewById(R.id.b) ;
final EditText Text3 = (EditText) findViewById(R.id.c) ;
final EditText Text4 = (EditText) findViewById(R.id.d) ;
// final EditText TextFinal = (EditText) findViewById(R.id.editText1) ;
final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//
Log.d("TEXT1", Text1.getText().toString());
Log.d("TEXT2", Text2.getText().toString());
Log.d("TEXT3", Text3.getText().toString());
Log.d("TEXT4", Text4.getText().toString());
matrix.GetValues(Text1.getText().toString(), Text2.getText().toString(),
Text3.getText().toString(), Text4.getText().toString());
// TextFinal.setText(matrix.Calculate());
}
});
}
}
class:
package com.example.hillsmatrixinverser;
import android.app.Application;
public class Math {
public int a;
public int b;
public int c;
public int d;
public void GetValues(String a1, String b1, String c1, String d1){
// Integer.parseInt(a1);
// Integer.parseInt(b1);
// Integer.parseInt(c1);
// Integer.parseInt(d1);
}
public String Calculate(){
return Integer.toString(a+b+c+d);
}
}
Line causing problems:
matrix.GetValues(Text1.getText().toString(), Text2.getText().toString(),
Text3.getText().toString(), Text4.getText().toString());
You never initialize Math matrix;
So when you're trying to call your GetValues method on your matrix object, it throws the NPE.