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
Related
Honestly I dont know how to proceed. I looked up some feeds on this website and got a bit confused. The imageview button should be invisibly dragged at the specific location of an other image and as long as my pressed thumb relocates to the other image, a activity should be called. In an earlier version, the activity was functional but not while moving the button while it is hold down with my thumb. Hopefully someone knows an answer for this (for me very challenging question.
#Override
public boolean onTouchEvent(MotionEvent event) {
if (imageRect == null) {
imageRect = new Rect();
imageView2.getGlobalVisibleRect(imageRect);
}
int x = (int) event.getX();
int y = (int) event.getY();
if (imageRect.contains(x, y)) {
openActivity();
}
return super.onTouchEvent(event);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
detectorCompat = new GestureDetectorCompat(this, new GestureListener());
imageViewPlayButton = findViewById(R.id.PlayButton_Image_White_Base);
imageView2 = findViewById(R.id.register_Icon_Base);
imageView3 = findViewById(R.id.navigation_Background_Image_Register);
imageView4 = findViewById(R.id.Login_Icon_Base);
imageViewPlayButton.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
imageView2.setVisibility(View.VISIBLE);
imageView3.setVisibility(View.VISIBLE);
imageView4.setVisibility(View.VISIBLE);
} else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
imageView2.setVisibility(View.INVISIBLE);
imageView3.setVisibility(View.INVISIBLE);
imageView4.setVisibility(View.INVISIBLE);
}
return true;
}
});
}
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GestureDetectorCompat;
import android.annotation.SuppressLint;
import android.app.RemoteInput;
import android.content.Intent;
import android.graphics.Rect;
import android.media.effect.Effect;
import android.os.Bundle;
import android.os.Handler;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private ImageView imageViewPlayButton, imageView2, imageView3, imageView4;
private Rect imageRect;
#SuppressLint("ClickableViewAccessibility")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
detectorCompat = new GestureDetectorCompat(this, new GestureListener());
imageViewPlayButton = findViewById(R.id.PlayButton_Image_White_Base);
imageView2 = findViewById(R.id.register_Icon_Base);
imageView3 = findViewById(R.id.navigation_Background_Image_Register);
imageView4 = findViewById(R.id.Login_Icon_Base);
imageViewPlayButton.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
imageView2.setVisibility(View.VISIBLE);
imageView3.setVisibility(View.VISIBLE);
imageView4.setVisibility(View.VISIBLE);
} else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
imageView2.setVisibility(View.INVISIBLE);
imageView3.setVisibility(View.INVISIBLE);
imageView4.setVisibility(View.INVISIBLE);
}
return false;
}
});
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (imageRect == null) {
imageRect = new Rect();
imageView2.getGlobalVisibleRect(imageRect);
}
int x = (int) event.getX();
int y = (int) event.getY();
if (imageRect.contains(x, y)) {
openActivity();
}
return true;
}
private void openActivity() {
Intent intent = new Intent(this, Register.class);
startActivity(intent);
overridePendingTransition(0, 0);
}
}
first of all pardon my English, but I think it's understandable.
So in this app is a counter and I need it to do sound when it reaches for example 30 seconds.
note: this is my first question here so if I broke any rules or I could have asked better way, let me know please
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;
public class MainActivity extends AppCompatActivity {
private Button mStartButton;
private Button mPauseButton;
private Button mResetButton;
private Chronometer mChronometer;
private long lastPause;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mStartButton = (Button) findViewById(R.id.start_button);
mPauseButton = (Button) findViewById(R.id.pause_button);
mResetButton = (Button) findViewById(R.id.reset_button);
mChronometer = (Chronometer) findViewById(R.id.chronometer);
mStartButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (lastPause != 0){
mChronometer.setBase(mChronometer.getBase() + SystemClock.elapsedRealtime() - lastPause);
}
else{
mChronometer.setBase(SystemClock.elapsedRealtime());
}
mChronometer.start();
mStartButton.setEnabled(false);
mPauseButton.setEnabled(true);
}
});
mPauseButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
lastPause = SystemClock.elapsedRealtime();
mChronometer.stop();
mPauseButton.setEnabled(false);
mStartButton.setEnabled(true);
}
});
mResetButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mChronometer.stop();
mChronometer.setBase(SystemClock.elapsedRealtime());
lastPause = 0;
mStartButton.setEnabled(true);
mPauseButton.setEnabled(false);
}
});
}
}
Define global integer variable as counter and count it in Chronometer's OnChronometerTickListener, and when it reach for example 30 then play sound and reset your counter:
int c = -1; // define global
chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
#Override
public void onChronometerTick(Chronometer chronometer) {
c++;
if(c == 30) {
c = 0;
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.ding);
mp.start();
}
}
});
[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 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);
}
});
}
Good afternoon,
I am looking to assign a generic variable that can be changed throughout the app and passed into CountDownTimer as my long
package com.rcd.learningactivities;
import java.text.DecimalFormat;
import java.util.concurrent.TimeUnit;
import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
protected CountDownTimer cd;
private long countTime = 0; // Variable for CountDownTimer
private Button lastPressedButton;
private Button red;
private Button blue;
private TextView blueTimer;
private TextView redTimer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
blueTimer = (TextView) findViewById(R.id.blueTimer);
blue = (Button) findViewById(R.id.button1);
redTimer = (TextView) findViewById(R.id.redTimer);
red = (Button) findViewById(R.id.button2);
cd = new CountDownTimer(countTime, 1000) { //<--- trying to use countTime here
public void onTick(long millisUntilFinished) {
DecimalFormat dfmin = new DecimalFormat("0");
DecimalFormat dfsec = new DecimalFormat("00");
double seconds = TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished)%60;
double min = TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished);
if (lastPressedButton == blue){
blueTimer.setText(String.valueOf(dfmin.format(min)) + ":" + String.valueOf(dfsec.format(seconds)));
}
else if (lastPressedButton == red) {
redTimer.setText(String.valueOf(dfmin.format(min)) + ":" + String.valueOf(dfsec.format(seconds)));
}
}
public void onFinish() {
if (lastPressedButton == blue){
blueTimer.setText("5:00");
}
else if (lastPressedButton == red){
redTimer.setText("5:00");
}
}
};
blue.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
lastPressedButton = blue;
countTime = 300000; // <-- setting countTime here
if (blueTimer.getText().toString().contains("5:00")){
cd.start();
} else {
cd.cancel();
cd.onFinish();
}
}
});
red.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
lastPressedButton = red;
countTime = 250000;
if (redTimer.getText().toString().contains("5:00")){
cd.start();
} else {
cd.cancel();
cd.onFinish();
}
}
});
}
#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;
}
}
For whatever reason, it stays with the initially declared number 0 (or if i change it to say 300000 then its always 300000) and its not changing the countTime below in my onClick.
Any help is greatly appreciated. If it can be accompanied by an explanation, that would be great!
Thanks everyone in advance!
EDIT: Im guessing its a scope issue that im overlooking??
EDIT2: If it is a scope issue, im a little confused as to how im able to reset the "lastPressedButton" variable set just about it, but not the countTime.
Java has pass by value semantics, not pass by reference, the value of countTime at construction is passed to CountDownTimer, CDT does not see any updates to countTime after it has been instantiated.