How do I use button to switch pages in android studio? - java

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);
}
});
}

Related

How to use radio buttons for math operation in android

[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

Why is my xmlFile not getting displayed when I start my app?

so this is my code, which implements a Chronometer to my App:
import android.content.Context;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Chronometer mChronometer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.isBaselineAligned();
mChronometer = new Chronometer(this);
// Set the initial value
mChronometer.setText("00:10");
layout.addView(mChronometer);
Button startButton = new Button(this);
startButton.setText("Start");
startButton.setOnClickListener(mStartListener);
layout.addView(startButton);
Button stopButton = new Button(this);
stopButton.setText("Stop");
stopButton.setOnClickListener(mStopListener);
layout.addView(stopButton);
Button resetButton = new Button(this);
resetButton.setText("Reset");
resetButton.setOnClickListener(mResetListener);
layout.addView(resetButton);
setContentView(layout);
}
private void showElapsedTime() {
long elapsedMillis = SystemClock.elapsedRealtime() - mChronometer.getBase();
Toast.makeText(MainActivity.this, "Elapsed milliseconds: " + elapsedMillis,
Toast.LENGTH_SHORT).show();
}
View.OnClickListener mStartListener = new View.OnClickListener() {
public void onClick(View v) {
int stoppedMilliseconds = 0;
String chronoText = mChronometer.getText().toString();
String array[] = chronoText.split(":");
if (array.length == 2) {
stoppedMilliseconds = Integer.parseInt(array[0]) * 60 * 1000
+ Integer.parseInt(array[1]) * 1000;
} else if (array.length == 3) {
stoppedMilliseconds = Integer.parseInt(array[0]) * 60 * 60 * 1000
+ Integer.parseInt(array[1]) * 60 * 1000
+ Integer.parseInt(array[2]) * 1000;
}
mChronometer.setBase(SystemClock.elapsedRealtime() - stoppedMilliseconds);
mChronometer.start();
}
};
View.OnClickListener mStopListener = new View.OnClickListener() {
public void onClick(View v) {
mChronometer.stop();
showElapsedTime();
}
};
View.OnClickListener mResetListener = new View.OnClickListener() {
public void onClick(View v) {
mChronometer.setBase(SystemClock.elapsedRealtime());
showElapsedTime();
}
};
}
The problem is though, that when I start my App, everything I did in my xml file, won't be displayed on the App, everything I can see is the Chronometer and its Buttons. Did I overwrite my xml file in the java file or something like that?
In my xml file I am using a ScrollView as the rootView, if that matters in any way.
Thank you in advance,
Julian.
Hello First of all in your class you are adding (R.layout.activity_main) to your view and at the end of the on create method again you are adding a layout to your view like this:
setContentView(layout);
so it override your view.... that's why your layout is not visible..... remove that dynamic view or by creating a layout in your xml file add that layout to that view so it will work....... hope it will help....;

ontouchlistener isn't working

My image is moving weirdly and pausing at random moments while I need it to be continuous. It stops and then works. Please post if you can make it more efficient. Also look if there is a way to shorten the code.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
public int x;
public float g =10.0f;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView iconss = (ImageView) findViewById(R.id.craft);
final Button button1 = (Button) findViewById(R.id.left);
final TextView txt1 = (TextView)findViewById(R.id.fail);
final Button button2 = (Button) findViewById(R.id.Right);
button2.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
g = g + 10f;
x = 1;
final CharSequence work;
work = "it is working";
txt1.setText(work);
iconss.setTranslationX(g);
}
});
button2.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v,MotionEvent event) {
g = g + 10f;
x = 1;
final CharSequence work;
work = "it is working";
txt1.setText(work);
iconss.setTranslationX(g);
return false;
}
});
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
g = g - 10f;
x = 1;
final CharSequence work;
work = "it is working";
txt1.setText(work);
iconss.setTranslationX(g);
}
});
button1.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v,MotionEvent event) {
g = g - 10f;
x = 1;
final CharSequence work;
work = "it is working";
txt1.setText(work);
iconss.setTranslationX(g);
return false;
}
});
}
}
The problem is ambiguous between ClickEvent and TouchEvent.
Your should remove TouchListener and replace them by Animation
Calculate the target position the image will move to
Set transition time
Start animation when button is clicked

Multiple OnTouchListeners on Buttons and also Buttons with OnClickListeners in Android Studio

I am trying to create a max bench press calculator for a project for school. I am very new to coding and can not figure out why this is not working. The plus and menus weight buttons never work. I did have it working before with an OnClickListener and OnLongClickListener. I just want the OnTouchListeners to keep adding weight as long as the button is held down.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements OnClickListener {
private Button plusreps;
private Button menusreps;
private Button Calculate;
private Button plus;
private Button menus;
private TextView textView;
private TextView textView2;
private TextView textView3;
int reps;
int Maxint;
double Max;
double weight;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
textView2 = (TextView) findViewById(R.id.textView2);
textView3 = (TextView) findViewById(R.id.textView3);
plusreps = (Button) findViewById(R.id.button5);
menusreps = (Button) findViewById(R.id.button4);
menus = (Button) findViewById(R.id.button3);
plus = (Button) findViewById(R.id.button2);
Calculate = (Button) findViewById(R.id.button);
Calculate.setOnClickListener(this);
reps = 0;
weight = 100;
textView.setText("" + String.valueOf(weight));
textView2.setText("" + String.valueOf(reps));
plus.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent mEvent1) {
if (mEvent1.getAction() == MotionEvent.ACTION_DOWN)
startIncreasingWeight();
else if (mEvent1.getAction() == MotionEvent.ACTION_UP)
stopIncreasingWeight();
return false;
}
private void stopIncreasingWeight() {
textView.setText("" + String.valueOf(weight));
}
private void startIncreasingWeight() {
weight = weight++;
textView.setText("" + String.valueOf(weight));
}
});
menus.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent mEvent) {
if (mEvent.getAction() == MotionEvent.ACTION_DOWN)
startDecreasingWeight();
else if (mEvent.getAction() == MotionEvent.ACTION_UP)
stopDecreasingWeight();
return false;
}
private void stopDecreasingWeight() {
textView.setText("" + String.valueOf(weight));
}
private void startDecreasingWeight() {
weight = weight--;
textView.setText("" + String.valueOf(weight));
}
});
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.button4:
if (reps > 1){
textView2.setText("" + String.valueOf(reps - 1));
reps = reps - 1;
}
else {
textView2.setText("" + String.valueOf(reps));
}
break;
case R.id.button5:
textView2.setText("" + String.valueOf(reps + 1));
reps = reps + 1;
break;
case R.id.button:
Max = reps * weight * 0.0333 + weight;
int Maxint = (int) Math.round(Max);
textView3.setText("Your max is: " + String.valueOf(Maxint));
break;
}
}}
put this:
plusreps.setOnClickListener(this);
menusreps.setOnClickListener(this);

Attempted GCF app for Android

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();
}
});
}

Categories

Resources