I am using Motorola Xoom with WiFi for application development purpose.I have tried to implement code to create speedometer using accelerometer but the only problem I am facing is that JAVA compiler is deprecating "Sensorlistener". I need some help to fix it. My using following codes for JAVA and .xml layout
JAVA
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.Context;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;
public class Speedometer extends Activity {
Handler handler = new Handler();
SensorManager sensorManager;
TextView myTextView;
float appliedAcceleration = 0;
float currentAcceleration = 0;
float velocity = 0;
Date lastUpdate;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
myTextView = (TextView)findViewById(R.id.myTextView);
lastUpdate = new Date(System.currentTimeMillis());
sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
sensorManager.registerListener(sensorListener, SensorManager.SENSOR_ACCELEROMETER, SensorManager.SENSOR_DELAY_FASTEST);
Timer updateTimer = new Timer("velocityUpdate");
updateTimer.scheduleAtFixedRate(new TimerTask() {
public void run() {
updateGUI();
}
}, 0, 1000);
}
private void updateGUI() {
// Convert from meters per second to miles per hour.
final double mph = (Math.round(100*velocity / 1.6 * 3.6))/100;
// Update the GUI
handler.post(new Runnable() {
public void run() {
myTextView.setText(String.valueOf(mph) + "mph");
}
});
}
private void updateVelocity() {
// Calculate how long this acceleration has been applied.
Date timeNow = new Date(System.currentTimeMillis());
long timeDelta = timeNow.getTime()-lastUpdate.getTime();
lastUpdate.setTime(timeNow.getTime());
// Calculate the change in velocity at the
// current acceleration since the last update.
float deltaVelocity = appliedAcceleration * (timeDelta/1000);
appliedAcceleration = currentAcceleration;
// Add the velocity change to the current velocity.
velocity += deltaVelocity;
}
private final SensorListener sensorListener = new SensorListener() {
double calibration = Double.NaN;
public void onSensorChanged(int sensor, float[] values) {
double x = values[SensorManager.DATA_X];
double y = values[SensorManager.DATA_Y];
double z = values[SensorManager.DATA_Z];
double a = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2));
if (calibration == Double.NaN)
calibration = a;
else {
updateVelocity();
currentAcceleration = (float)a;
}
}
public void onAccuracyChanged(int sensor, int accuracy) {}
};
}
.XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#+id/myTextView"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textStyle="bold"
android:textSize="40sp"
android:text="CENTER"
android:editable="false"
android:singleLine="true"
android:layout_margin="10px"/>
/>
</LinearLayout>
I will be grateful for helpful suggestions.
The documentation spells it out, just use SensorEventListener instead.
In your xml file you wrote twice /> for closing the TextView tag
android:layout_margin="10px"/>
/>
</LinearLayout>
just delete one of them.
Related
I am in the dawn of my Android programming adventure and have just become able to communicate between on-screen views. So the next step for me is to successfully pull text from a TextView (set by a Dialog) and use a Start Button to run a Timer based on the user selected Dialog (default to the current minute value of the clock).
Here is what you see on screen.
A TextView that displays the selection from the Dialog.
A Selector Button that launches the Dialog TimePicker Dialog and resets the Start Button.
A Start Button that (should) read the TextView, disable itself, and begin a CountDownTimer based on a Long extracted from the TextView string.
A debug TextView that displays to me what is actually seen by the system.
The entire activity consists of a single Java File with two Classes declared and of course an XML. Every time I click my start button, despite the Debug TextView showing that I properly extracted the Long value for the seconds the timer instantly completes. I can see from my debug TextView that when I select say.. 08:26, the pSecondsLeft=26 as it should.. but the timer still doesn't count down from 26. I can't see my mistake.
Here is the XML first.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<TextView android:id="#+id/timeDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Time will appear here after being selected"
android:textSize="30sp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button android:id="#+id/pickTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Change the time"/>
<Button android:id="#+id/startTimer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Start the time"
/>
</LinearLayout>
<TextView android:id="#+id/timeRemaining"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="30sp"
android:text="Time Remaining"
/>
</LinearLayout>
And here is my Main Activity.
package com.stembo.android.botskooltimepickertutorial;
import java.util.Calendar;
import java.util.StringTokenizer;
import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
public class TimePickerActivity extends Activity {
/** Private members of the class */
private TextView displayTime;
private Button pickTime;
private Button startTimer;
private TextView timeRemaining;
private int pMinutesLeft;
private int pSecondsLeft;
/** This integer will uniquely define the
* dialog to be used for displaying time picker.*/
static final int TIME_DIALOG_ID = 0;
/** Callback received when the user "picks" a time in the dialog */
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int minLeft, int secLeft) {
pMinutesLeft = minLeft;
pSecondsLeft = secLeft;
updateDisplay();
displayToast();
}
};
/** Updates the time in the TextView */
private void updateDisplay() {
displayTime.setText(
new StringBuilder()
.append(pad(pMinutesLeft)).append(":")
.append(pad(pSecondsLeft)));
}
/** Displays a notification when the time is updated */
private void displayToast() {
Toast.makeText(this, new StringBuilder().append("Time choosen is ")
.append(displayTime.getText()), Toast.LENGTH_SHORT).show();
}
/** Add padding to numbers less than ten */
private static String pad(int c) {
if (c >= 10)
return String.valueOf(c);
else
return "0" + String.valueOf(c);
}
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/** Capture our View elements */
displayTime = (TextView) findViewById(R.id.timeDisplay);
pickTime = (Button) findViewById(R.id.pickTime);
startTimer = (Button) findViewById(R.id.startTimer);
timeRemaining = (TextView) findViewById(R.id.timeRemaining);
/** Listener for click event of the pick button */
pickTime.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startTimer.setEnabled(true);
showDialog(TIME_DIALOG_ID);
}
});
/**Listener for click event of the start button */
startTimer.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
startTimer.setEnabled(false);
StringTokenizer st = new StringTokenizer(displayTime.getText().toString(), ":");
while (st.hasMoreElements()){
st.nextElement();
long pSecondsTimer = Long.parseLong(st.nextToken());
}
timeRemaining.setText(displayTime.getText()+" Token="+ pSecondsLeft);
long oneSecondInterval = 1000;
MyCount counter = new MyCount(pSecondsLeft, oneSecondInterval);
counter.start();
}
});
/** Get the current time */
final Calendar cal = Calendar.getInstance();
pMinutesLeft = cal.get(Calendar.HOUR_OF_DAY);
pSecondsLeft = cal.get(Calendar.MINUTE);
/** Display the current time in the TextView */
updateDisplay();
}
/** Create a new dialog for time picker */
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_DIALOG_ID:
return new TimePickerDialog(this,
mTimeSetListener, pMinutesLeft, pSecondsLeft, true);
}
return null;
}
public class MyCount extends CountDownTimer {
public MyCount(long pSecondsLeft, long countDownInterval){
super(pSecondsLeft, countDownInterval);
}
#Override
public void onTick(long pSecondsTimer){
displayTime.setText("Time remaining: " + pSecondsLeft);
}
#Override
public void onFinish(){
displayTime.setText("Countdown Complete!");
}
}
}
Here is the Start Button code which I am having trouble with, its in the Main Activity but may be easier to see excluded.
/**Listener for click event of the start button */
startTimer.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
startTimer.setEnabled(false);
StringTokenizer st = new StringTokenizer(displayTime.getText().toString(), ":");
while (st.hasMoreElements()){
st.nextElement();
long pSecondsTimer = Long.parseLong(st.nextToken());
}
timeRemaining.setText(displayTime.getText()+" Token="+ pSecondsLeft);
long oneSecondInterval = 1000;
MyCount counter = new MyCount(pSecondsLeft, oneSecondInterval);
counter.start();
}
});
It appears there is a misunderstanding about the values you get back from TimePickerDialog.OnTimeSetListener. It gives you hours and minutes, but you're expecting minutes and seconds. Same for the values coming out of the Calendar you're using on onCreate.
That said, if you are still trying to use a TimePickerDialog to get minutes and seconds with a full understanding that you're reinterpreting the values, you will need to multiply the number of "seconds" you receive from the picker by 1000 to get a unit in milliseconds that you can feed to the CountDownTimer.
MyCount counter = new MyCount(pSecondsLeft * 1000, oneSecondInterval);
I think it's because you're not subtracting from the pSecondsLeft variable on each tick
public class MyCount extends CountDownTimer {
public MyCount(long pSecondsLeft, long countDownInterval){
super(pSecondsLeft, countDownInterval);
}
#Override
public void onTick(long pSecondsTimer){
displayTime.setText("Time remaining: " + pSecondsLeft);
pSecondsLeft --;
}
#Override
public void onFinish(){
displayTime.setText("Countdown Complete!");
}
}
Hey guys I have been trying to figure this out and have looked over a number of questions here but can't seem to find the answer to my problem. I am making an app that displays dinners at random from an array. I would like these dinners to be clickable and take the user to a web page but I have no idea how to make that happen so at the moment I have just added the link below the dinner which looks pretty ugly.
Here is the class that contains the recipes:
package me.oak.dinnertime;
import java.util.Random;
public class CookBook {
public String[] mfood =
{
"Chicago Deep Dish Pizza \n \n http://www.taste.com.au/recipes/28896/chicago+deep+dish+pizza?ref=collections,pizza-recipes",
"Spaghetti Bolognese \n \n http://www.bbcgoodfood.com/recipes/1502640/the-best-spaghetti-bolognese",
"Bourbon Chicken \n \n http://www.food.com/recipe/bourbon-chicken-45809",
};
public String getFood() {
String food = "";
//Randomly select a dinner
Random randomGenerator = new Random(); //Construct a new Random number generator
int randomNumber = randomGenerator.nextInt(mfood.length);
//Convert random number to text
food = mfood[randomNumber];
return food;
}
}
And here is the main activity:
package me.oak.dinnertime;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class DinnerTimeActivity extends Activity {
private CookBook mCookBook = new CookBook();
private ColourWheel mColourWheel = new ColourWheel();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dinner_time);
final TextView dinnerLabel = (TextView) findViewById(R.id.DinnerTextView);
final Button showDinnerButton = (Button) findViewById(R.id.showDinnerButton);
final RelativeLayout relativelayout = (RelativeLayout) findViewById(R.id.relativeLayout);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View view) {
String food = mCookBook.getFood();
//Update the label with the dinner
dinnerLabel.setText(food);
int colour = mColourWheel.getColour();
relativelayout.setBackgroundColor(colour);
showDinnerButton.setTextColor(colour);
}
};
showDinnerButton.setOnClickListener(listener);
}
}
And here is the XML file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".DinnerTimeActivity"
android:background="#ff51b46d"
android:id="#+id/relativeLayout">
<TextView android:text="What's for dinner?" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textColor="#80ffffff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/DinnerTextView"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="24sp"
android:textColor="#android:color/white"
android:text="Click the button to find out!"
android:autoLink="web" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dinner Time"
android:id="#+id/showDinnerButton"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#android:color/white"
android:textColor="#ff51b46d" />
</RelativeLayout>
Sorry to give you so much, I just hope someone can help me out.
To use the LinkMovementMethod, try following:
change your array list content from:
Chicago Deep Dish Pizza \n \n http://www.taste.com.au/recipes/28896/chicago+deep+dish+pizza?ref=collections,pizza-recipes
to:
Chicago Deep Dish Pizza
And when set this text to your TextView, do it as:
(Updated: remove the underline and change text color, source: Remove underline from links in TextView - Android)
Spannable s = (Spannable) Html.fromHtml(foodString);
for (URLSpan u: s.getSpans(0, s.length(), URLSpan.class)) {
s.setSpan(new UnderlineSpan() {
public void updateDrawState(TextPaint tp) {
//remove the underline
tp.setUnderlineText(false);
//set text color
tp.setColor(getResources().getColor(R.color.orange));
}
}, s.getSpanStart(u), s.getSpanEnd(u), 0);
}
dinnerLabel.setText(s);
dinnerLabel.setMovementMethod(LinkMovementMethod.getInstance());
Also remove the
android:autoLink="web"
in the xml.
As I have tested, "Chicago Deep Dish Pizza" will appear as a clickable link in the testview.
just use something like this in your string file and refrence it in the textview
<string name="links">Google shalom is a boyGoogle 2 </string>
you can also use this in an array in the strings file
to make it actually work do this
terms = findViewById(R.id.terms);
terms.setMovementMethod(LinkMovementMethod.getInstance());
I answered this on Hackforums for you.
Store the links in a separate array make sure the indexes of the links line up with the location in the other array. So:
public class CookBook {
public String[] mfood =
{
"Chicago Deep Dish Pizza",
"Spaghetti Bolognese",
"Bourbon Chicken",
};
public String[] mLinks =
{
"http://www.taste.com.au/recipes/28896/chicago+deep+dish+pizza?ref=collections,pizza-recipes",
"http://www.bbcgoodfood.com/recipes/1502640/the-best-spaghetti-bolognese",
"http://www.food.com/recipe/bourbon-chicken-45809",
};
public int getRandomFoodIndex() {
//Randomly select a dinner
Random randomGenerator = new Random(); //Construct a new Random number generator
int randomNumber = randomGenerator.nextInt(mfood.length);
//Convert random number to text
return randomNumber;
}
public String getFood(int index) {
return mfood[index];
}
public String getLink(int index) {
return mLinks[index];
}
}
and then
public class DinnerTimeActivity extends Activity {
private CookBook mCookBook = new CookBook();
private ColourWheel mColourWheel = new ColourWheel();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dinner_time);
final TextView dinnerLabel = (TextView) findViewById(R.id.DinnerTextView);
final Button showDinnerButton = (Button) findViewById(R.id.showDinnerButton);
final RelativeLayout relativelayout = (RelativeLayout) findViewById(R.id.relativeLayout);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View view) {
int index = mCookBook.getRandomFoodIndex();
String food = mCookBook.getFood(index);
String link = mCookBook.getLink(index);
//Update the label with the dinner
dinnerLabel.setText(food);
/** open link with the link variable */
int colour = mColourWheel.getColour();
relativelayout.setBackgroundColor(colour);
showDinnerButton.setTextColor(colour);
}
};
showDinnerButton.setOnClickListener(listener);
}
}
I'm trying to create an application that figures planetary weight. In the first view the user inputs their weight and it has an enter button, in the second view is a list of radio buttons and the select button. I want it to multiply their weight by the planet's force. To do this I needed the weight variable for both activity_main.xml and planets.xml. At first I had it in only planets, but now that it's in main as well, the if else statements are erroring.
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/askwtTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="17dp"
android:layout_marginTop="19dp"
android:text="#string/askwt" />
<EditText
android:id="#+id/inputwtEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/askwtTextView"
android:layout_below="#+id/askwtTextView"
android:layout_marginTop="26dp"
android:ems="10"
android:inputType="numberDecimal" />
<Button
android:id="#+id/enterButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/inputwtEditText"
android:layout_below="#+id/inputwtEditText"
android:layout_marginTop="38dp"
android:onClick="buttonclick"
android:text="#string/enter" />
</RelativeLayout>
planets.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/planetTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/planet" />
<TextView
android:id="#+id/textViewform2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/mercuryRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/mercury" />
<RadioButton
android:id="#+id/venusRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/venus" />
<RadioButton
android:id="#+id/earthRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/earth" />
<RadioButton
android:id="#+id/marsRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/mars" />
<RadioButton
android:id="#+id/jupiterRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/jupiter" />
<RadioButton
android:id="#+id/saturnRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/saturn" />
<RadioButton
android:id="#+id/uranusRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/uranus" />
<RadioButton
android:id="#+id/neptuneRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/neptune" />
<RadioButton
android:id="#+id/plutoRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/pluto" />
</RadioGroup>
<Button
android:id="#+id/selectButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="buttonclick2"
android:text="#string/select" />
<TextView
android:id="#+id/textViewform2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
JAVA:
package com.deitel.planetaryweight;
import android.os.Bundle;
import android.app.Activity;
import android.widget.*;
import android.view.View;
import android.view.Menu;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.EditText;
import android.widget.TextView;
import java.text.DecimalFormat;
public class MainActivity extends Activity {
//Global variable
double weight;
private Button enter; // creates a button
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
enter = (Button) findViewById(R.id.enterButton);
//Start with first screen
setContentView(R.layout.activity_main);
}
//buttonclick for form 1
public void buttonclick(View view){
//creates an editext and assigns the resource id of the xml edittext.
EditText wtentry = (EditText)findViewById(R.id.inputwtEditText);
//Receives the input from the edittext, converts it to a double (number).
weight = Double.parseDouble(wtentry.getText().toString());
//switch views to screen 2
setContentView(R.layout.planets);
//change the value of the textview on screen 2 to the calculation value
TextView t2 = (TextView)findViewById(R.id.textViewform2);
t2.setText(Double.toString(weight));
}
//buttonclick for form 2!
public void buttonclick2(View view){
setContentView(R.layout.planets);
RadioButton mercury = (RadioButton) findViewById(R.id.mercuryRadio);
RadioButton venus = (RadioButton) findViewById(R.id.venusRadio);
RadioButton earth = (RadioButton) findViewById(R.id.earthRadio);
RadioButton mars = (RadioButton) findViewById(R.id.marsRadio);
RadioButton jupiter = (RadioButton) findViewById(R.id.jupiterRadio);
RadioButton saturn = (RadioButton) findViewById(R.id.saturnRadio);
RadioButton uranus = (RadioButton) findViewById(R.id.uranusRadio);
RadioButton neptune = (RadioButton) findViewById(R.id.neptuneRadio);
RadioButton pluto = (RadioButton) findViewById(R.id.plutoRadio);
//Makes a variable for the entered amount
Double mercurypf;
Double venuspf;
Double earthpf;
Double marspf;
Double jupiterpf;
Double saturnpf;
Double uranuspf;
Double neptunepf;
Double plutopf;
Double weight;
// constants
final double mercuryforce = 0.38;
final double venusforce = 0.91;
final double earthforce = 1.00;
final double marsforce = 0.38;
final double jupiterforce = 2.34;
final double saturnforce = 1.06;
final double uranusforce = .92;
final double neptuneforce = 1.19;
final double plutoforce = 0.06;
// Code used to determine which planet RadioButton is checked:
if(mercury.isChecked())
{
mercurypf = mercuryforce * weight;
}
else
{
mercurypf = 0.00;
}
if(venus.isChecked())
{
venuspf = venusforce * weight;
}
else
{
venuspf = 0.00;
}
if(earth.isChecked())
{
earthpf = earthforce * weight;
}
else
{
earthpf = 0.00;
}
if(mars.isChecked())
{
marspf = marsforce * weight;
}
else
{
marspf = 0.00;
}
if(jupiter.isChecked())
{
jupiterpf =jupiterforce * weight;
}
else
{
jupiterpf = 0.00;
}
if(saturn.isChecked())
{
saturnpf = saturnforce * weight;
}
else
{
saturnpf = 0.00;
}
if(uranus.isChecked())
{
uranuspf = uranusforce * weight;
}
else
{
uranuspf = 0.00;
}
if(neptune.isChecked())
{
neptunepf = neptuneforce * weight;
}
else
{
neptunepf = 0.00;
}
if(pluto.isChecked())
{
plutopf = plutoforce * weight;
}
else
{
plutopf = 0.00;
}
}
}
One way to do it is:
Create a new class called say Constants and declare this variable as static there. Now you can access this variable from all you activities.
public class Constants {
public static double weight;
}
Access it from all your activities as
Constants.weight = 2.5;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
enter = (Button) findViewById(R.id.enterButton);
}
You should be setting the content view BEFORE you find any view.
Further, you don't need any changes in the way you've defined your variables, given weight is a private variable in your MainActivity class, and you're shifting views in the same activity. The variable is storing the value of the editText BEFORE you change the view so it won't lose the value in the same class even if you change the contentView. As far as I can see, the problem is that you're not setting the enter button right (the change I've suggested above should work).
In addition to the rest, there's some structural and design issues you need to resolve. Take a look at the docs for information on passing information back and forth to and from Activities. setContentView() shouldn't be called more than once in an Activity unless you have a good reason to.
public class MainActivity extends Activity {
/* You should get used to declaring everything with the correct visibility. Good practice is to make everything private and use public mutator methods */
//Global variable
private double weight;
private Button enter; // creates a button
// Views
private EditText wtEntry;
private TextView txtForm2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Start with first screen
setContentView(R.layout.activity_main);
enter = (Button) findViewById(R.id.enterButton);
//creates an editext and assigns the resource id of the xml edittext.
wtEntry = (EditText)findViewById(R.id.inputwtEditText);
txtForm2 = (TextView)findViewById(R.id.textViewform2);
}
// Button clicks shouldn't do anything but perform clicky actions. Leave field initialization, view creation, etc to the Activity.
//buttonclick for form 1
public void buttonclick(View view){
//Receives the input from the edittext, converts it to a double (number).
weight = Double.parseDouble(wtEntry.getText().toString());
//change the value of the textview on screen 2 to the calculation value
t2.setText(Double.toString(weight));
// If you want a new layout, it's best to start a new activity.
// It looks like you want to get information back, so use startActivityForResult().
// setContentView(R.layout.planets);
Intent dataIntent = new Intent(this, PlanetChooser.class);
dataIntent.putExtra("com.yourpackage.identifier.DATA_WEIGHT", weight);
startActivityForResult(dataIntent, Activity.RESULT_OK);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Check that the resultCode is the same as we started the activity with
if(resultCode == Activity.RESULT_OK){
// get the double from the Intent, using the same string name (package prefixed)
// or a default value if it didn't get set.
double resultWeight = data.getDoubleExtra("com.yourpackage.identifier.RESULT_WEIGHT", 0.0);
// Now do something with resultWeight
}
}
}
// PlanetChooser.class
public class PlanetChooser extends Activity {
// constants, usually denoted by uppercase and declared static and final
public static final double MERCURYFORCE = 0.38;
public static final double VENUSFORCE = 0.91;
public static final double EARTHFORCE = 1.00;
public static final double MARSFORCE = 0.38;
public static final double JUPITERFORCE = 2.34;
public static final double SATURNFORCE = 1.06;
public static final double URANUSFORCE = 0.92;
public static final double NEPTUNEFORCE = 1.19;
public static final double PLUTOFORCE = 0.06;
private RadioButton mercury, venus, earth, mars, jupiter, saturn, uranus, neptune, pluto;
// No need to use the Double object as opposed to the primitive unless you have good reason
private double mercurypf, venuspf, earthpf, marspf, jupiterpf, saturnpf, uranuspf, neptunepf, plutopf, weight;
// One variable will suffice, it seems.
private double resultForce;
public void onCreate(Bundle s){
super.onCreate(s);
setContentView(R.layout.planets);
mercury = (RadioButton) findViewById(R.id.mercuryRadio);
venus = (RadioButton) findViewById(R.id.venusRadio);
earth = (RadioButton) findViewById(R.id.earthRadio);
mars = (RadioButton) findViewById(R.id.marsRadio);
jupiter = (RadioButton) findViewById(R.id.jupiterRadio);
saturn = (RadioButton) findViewById(R.id.saturnRadio);
uranus = (RadioButton) findViewById(R.id.uranusRadio);
neptune = (RadioButton) findViewById(R.id.neptuneRadio);
pluto = (RadioButton) findViewById(R.id.plutoRadio);
}
public void buttonclick2(View view){
/*
It looks to me here you're looking to see which box is checked, and set a value based on
that planet. Since instance variables (in this case mercurypf, jupiterpf, etc) are initialized
to the default value (0), there's no need to set them manually.
*/
// Code used to determine which planet RadioButton is checked:
if(mercury.isChecked()) {
resultForce = MERCURYFORCE * weight;
}
if(venus.isChecked()){
resultForce = VENUSFORCE * weight;
}
if(earth.isChecked()){
resultForce = EARTHFORCE * weight;
}
if(mars.isChecked()){
resultForce = MARSFORCE * weight;
}
if(jupiter.isChecked()){
resultForce =JUPITERFORCE * weight;
}
if(saturn.isChecked()){
resultForce = SATURNFORCE * weight;
}
if(uranus.isChecked()){
resultForce = URANUSFORCE * weight;
}
if(neptune.isChecked()){
resultForce = NEPTUNEFORCE * weight;
}
if(pluto.isChecked()){
resultForce = PLUTOFORCE * weight;
}
// Create a new data Intent to pass back to the calling activity, set the result code,
// and manually finish() this activity.
Intent dataIntent = new Intent(this);
dataIntent.putDoubleExtra("com.yourpackage.identifier.RESULT_DATA", resultForce);
setResult(Activity.RESULT_OK, dataIntent);
finish();
}
}
Use Application or static variable to save the value globally. see the example
Create a new class that extends Application.
public class Globals extends Application{
private int data=200;
public int getData(){
return this.data;
}
public void setData(int d){
this.data=d;
}
}
Second:
Add the class to the AndroidManifest file as an attribute of <application> tag:
<application android:name=".Globals" />
Then you can access your global data from any Activity by calling getApplication()
Globals g = (Globals)getApplication();
int data=g.getData();
Static Variable Example
class Global{
public static int global=1;
}
in your activity simply use it with its class name
int a;
a=Global.global;
I used this code for adding a clock to my app:
<DigitalClock
android:id="#+id/digitalclock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:textSize = "30sp"
/>
The problem is that it shows also seconds..there is a simple and fast way for hide those? I need just hours and minutes in hh:mm format instead of hh:mm:ss! any suggestions? Thanks!
Found the answer here, for anyone else looking for a working answer, here it is:
Clone/copy DigitalClock.java from android source
Change format strings within new CustomDigitalClock
package com.example;
import android.content.Context;
import android.content.res.Resources;
import android.database.ContentObserver;
import android.os.Handler;
import android.os.SystemClock;
import android.provider.Settings;
import android.text.format.DateFormat;
import android.util.AttributeSet;
import android.widget.TextView;
import java.util.Calendar;
/**
* You have to make a clone of the file DigitalClock.java to use in your application, modify in the following manner:-
* private final static String m12 = "h:mm aa";
* private final static String m24 = "k:mm";
*/
public class CustomDigitalClock extends TextView {
Calendar mCalendar;
private final static String m12 = "h:mm aa";
private final static String m24 = "k:mm";
private FormatChangeObserver mFormatChangeObserver;
private Runnable mTicker;
private Handler mHandler;
private boolean mTickerStopped = false;
String mFormat;
public CustomDigitalClock(Context context) {
super(context);
initClock(context);
}
public CustomDigitalClock(Context context, AttributeSet attrs) {
super(context, attrs);
initClock(context);
}
private void initClock(Context context) {
Resources r = context.getResources();
if (mCalendar == null) {
mCalendar = Calendar.getInstance();
}
mFormatChangeObserver = new FormatChangeObserver();
getContext().getContentResolver().registerContentObserver(
Settings.System.CONTENT_URI, true, mFormatChangeObserver);
setFormat();
}
#Override
protected void onAttachedToWindow() {
mTickerStopped = false;
super.onAttachedToWindow();
mHandler = new Handler();
/**
* requests a tick on the next hard-second boundary
*/
mTicker = new Runnable() {
public void run() {
if (mTickerStopped) return;
mCalendar.setTimeInMillis(System.currentTimeMillis());
setText(DateFormat.format(mFormat, mCalendar));
invalidate();
long now = SystemClock.uptimeMillis();
long next = now + (1000 - now % 1000);
mHandler.postAtTime(mTicker, next);
}
};
mTicker.run();
}
#Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
mTickerStopped = true;
}
/**
* Pulls 12/24 mode from system settings
*/
private boolean get24HourMode() {
return android.text.format.DateFormat.is24HourFormat(getContext());
}
private void setFormat() {
if (get24HourMode()) {
mFormat = m24;
} else {
mFormat = m12;
}
}
private class FormatChangeObserver extends ContentObserver {
public FormatChangeObserver() {
super(new Handler());
}
#Override
public void onChange(boolean selfChange) {
setFormat();
}
}
}
Reference custom class within in layout xml
<com.example.CustomDigitalClock
android:id="#+id/fragment_clock_digital"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DigitalClock" />
Load CustomDigitalClock within activity/fragment
CustomDigitalClock dc = (CustomDigitalClock)
mFragmentView.findViewById(R.id.fragment_clock_digital);
The DigitalClock Javadoc states:
Class Overview
Like AnalogClock, but digital. Shows seconds. FIXME: implement
separate views for hours/minutes/seconds, so proportional fonts don't
shake rendering
Judging by the FIXME, the ability to hide portions of DigitalClock might be implemented eventually. I didn't find anything currently in the Javadoc or source code that would do what you want it to.
Unless you want to write your own class that extends DigitalClock (or your own clock implementation altogether), you could just cover the seconds portion of the DigitalClock with another element if it would serve your purpose.
Scope of the project
When a user touches the Android screen with two fingers, draw a "Frame" at each touch location with a "cursor" for each frame. Each frame is a custom slider that the cursor will move up and down. All the way up will be 100%, middle will be 0% and all the way down will be -100%. This will be used to control small motors, similar to tank turning, each touch controls a separate motor (sending signals over bluetooth). After a two touch and everything is drawn, I want to be able to lift off either finger, BUT keep the cursor at what ever location it was last at, while the other finger is free to move its cursor. When the last finger is lifted off, everything "hides" and resets to 0%.
Functionality Wanted
On two finger touch, draw separate .pngs under the touch location
After the frames and cursors are drawn, keep track of where they are relative to the frame to determine the percentage.
If a finger is lifted off, keep that fingers cursor at last known location, but the other finger can move it's cursor. Also if the finger is put back down it should be able to move its cursor again.
If both fingers are lifted off of the screen, hide everything and reset percentages to 0%
Functionality Obtained
I can draw the frames and cursors on multitouch
Positions and percentages work fine
Cursors do move properly
What doesn't work
I am unsure if I should have one custom class that handles both touch event or if i should have 2 instances of the custom class each handling their own touch events (I have tried both, the only way i get any "real" functionality is with 1 custom class handling both touch events, the other way doesn't work as intended)
When I only have 1 custom class, It works great, but I have it "hide" everything if both fingers are not on the screen, and sometimes android registers that I have lifted a finger off the screen and this causes me a lot of issues when the frames hide then re appear in a different location
When I use 2 custom classes I touch each custom class would have its own touch event, and i wouldn't have to worry about multitouch if i split the classes evenly between the screen. This was not the case, still need to deal with multitouch
Can someone explain to me how android handles their touch events. from what I have done, it seems if i lay down finger 1, the finger 2, the first finger will register a "ACTION_DOWN" and the second will register a "ACTION_POINTER_2_DOWN", BUT if i life off my first finger, my second finger is "demoted" and now all of the events my second finger registers does not related to "ACTION_POINTER_2" and instead will be "ACTION_DOWN, ACTION_UP, etc". Is this correct?
TouchUI.java
package com.robota.android;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ImageView;
public class TouchUI extends ImageView {
public static final String LEFT_TOUCHUI = "com.robota.android:id/leftTouchUI";
public static final String RIGHT_TOUCHUI = "com.robota.android:id/rightTouchUI";
private String whoAmI = new String();
private MyPoints framePts = new MyPoints();
private MyPoints cursorPts = new MyPoints();
private Bitmap frame;
private Bitmap cursor;
private int frameWidth;
private int frameHeight;
private int cursorHeight;
private boolean pointerDown = false;
private int dy;
public TouchUI(final Context context, final AttributeSet as){
super(context, as);
Log.d("TouchUI", getResources().getResourceName(this.getId()));
whoAmI = new String(getResources().getResourceName(this.getId()));
if(whoAmI.equals(LEFT_TOUCHUI)){
frame = BitmapFactory.decodeResource(getResources(), R.drawable.tank_left);
}else if(whoAmI.equals(RIGHT_TOUCHUI)){
frame = BitmapFactory.decodeResource(getResources(), R.drawable.tank_right);
}
cursor = BitmapFactory.decodeResource(getResources(), R.drawable.cursor);
frameWidth = frame.getWidth();
frameHeight = frame.getHeight();
cursorHeight = cursor.getHeight();
}
public void determinePointers(int x, int y){
framePts.setOrigin(x-frameWidth/2, y-frameHeight/2);
cursorPts.setOrigin(x-frameWidth/2, y-frameHeight/2);
}
#Override
public boolean onTouchEvent(MotionEvent e){
int x = 0;
int y = 0;
Log.d("TouchUI", ">>>>> " + whoAmI);
if(e.getAction() == MotionEvent.ACTION_DOWN){
determinePointers(x,y);
pointerDown = true;
}else if(e.getAction() == MotionEvent.ACTION_UP){
pointerDown = false;
}else if(e.getAction() == MotionEvent.ACTION_MOVE){
dy = (int)e.getY()-framePts.getY();
if(dy <= 0){
dy=0;
}else if(dy+cursorHeight/2 >= frameHeight){
dy=frameHeight;
}
sendMotorSpeed(dy);
}
return true;
}
public void sendMotorSpeed(int dy){
float motor = dy;
motor-=frameHeight;
motor*=-1;
motor = (motor/frameHeight)*255;
PacketController.updateMotorSpeeds(whoAmI, (int)motor);
}
public void onDraw(Canvas canvas){
if(pointerDown){//twoDown){
canvas.drawBitmap(frame, framePts.getX(), framePts.getY(), null);
canvas.drawBitmap(cursor, cursorPts.getX(), (cursorPts.getY()+dy), null);
}
invalidate();
}
private class MyPoints{
private int x = -100;
private int y = -100;
private int deltaY = 0;;
public MyPoints(){
this.x = 0;
this.y = 0;
}
public int getX(){
return this.x;
}
public int getY(){
return this.y;
}
public void setOrigin(int x, int y){
this.x = x;
this.y = y;
}
public int getDeltaY(){
return deltaY;
}
public void setDeltaY(int newY){
deltaY = (newY-y);
Log.d("TouchUI", "DY: " + deltaY);
}
}
}
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parentLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.robota.android.TouchUI xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/leftTouchUI"
android:background="#0000"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_weight="1">
</com.robota.android.TouchUI>
<com.robota.android.TouchUI xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rightTouchUI"
android:background="#0000"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_weight="1">
</com.robota.android.TouchUI>
</LinearLayout>
RobotController.java (Main Activity Class)
package com.robota.android;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
public class RobotController extends Activity {
// Tag used to keep track of class in the Log
private static final String TAG = "robotController_new";
// Boolean to debugging
private static final boolean D = true;
// Intent request codes
private static final int DISCONNECT_DEVICE = 1;
private static final int CONNECT_DEVICE = 2;
private static final int REQUEST_ENABLE_BT = 3;
// Handler Codes
public static final int MESSAGE_READ = 1;
public static final int MESSAGE_WRITE = 2;
// Local Bluetooth Adapter
private BluetoothAdapter bluetoothAdapter = null;
// Bluetooth Discovery and Datahandler
private BluetoothComm btComm = null;
// Debug's TextView, this is where strings will be written to display
private TextView tv;
private ScrollView sv;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
if(D) Log.d(TAG, "++ON CREATE++");
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(bluetoothAdapter == null){
if(D) Log.d(TAG, "NO BLUETOOTH DEVICE");
Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_SHORT).show();
finish();
return;
}
PacketController.controller = this;
}
public void onStart(){
super.onStart();
if(D) Log.d(TAG, "++ON START++");
if(!bluetoothAdapter.isEnabled()){
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
}else{
// Start BluetoothComm
if(btComm == null){
setupComm();
}
}
}
/**
* Creates new Bluetooth Communication
*/
private void setupComm(){
if(D) Log.d(TAG, "+++setupComm+++");
btComm = new BluetoothComm(this, handler);
}
private void connectDevice(Intent data){
if(D) Log.d(TAG, "+++connectDevice+++");
String addr = data.getExtras()
.getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
BluetoothDevice device = bluetoothAdapter.getRemoteDevice(addr);
if(D) Log.d(TAG,"REMOTE ADDR: "+ addr);
btComm.connect(device);
}
private void disconnectDevice(){
if(D) Log.d(TAG, "---disconnectDevice---");
if(btComm.getState() == btComm.STATE_CONNECTED){
btComm.disconnect();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
//super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent serverIntent = null;
switch(item.getItemId()){
case R.id.insecure_connect_scan:
// Launch the DeviceListActivity to see devices and do scan
serverIntent = new Intent(this, DeviceListActivity.class);
try{
startActivityForResult(serverIntent, CONNECT_DEVICE);
}catch(ActivityNotFoundException activityNotFound){
Log.e(TAG, "Could not start DeviceListActivity(Insecure)");
}
return true;
}
return false;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
switch(requestCode){
case CONNECT_DEVICE:
if(resultCode == Activity.RESULT_OK){
connectDevice(data);
}
break;
case DISCONNECT_DEVICE:
if(resultCode == Activity.RESULT_OK){
disconnectDevice();
}
break;
}
}
public Handler getHandler(){
return this.handler;
}
public BluetoothComm getBtComm(){
return this.btComm;
}
// The Handler that gets information back from the BluetoothChatService
private final Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
if(D) Log.d(TAG, "check message");
switch (msg.what) {
case MESSAGE_READ:
if(D) Log.d(TAG, "trying to read message");
byte[] readBuf = (byte[]) msg.obj;
// construct a string from the valid bytes in the buffer
String readMessage = new String(readBuf, 0, msg.arg1);
if(D) Log.d(TAG, "bytes: " + readBuf + " arg1: " + msg.arg1 + " Message: " + readMessage);
tv.append(readMessage);
break;
case MESSAGE_WRITE:
if(D) Log.d(TAG, "trying to send message");
String sendMessage = new String(String.valueOf(msg.obj));
}
}
};
}
Any other classes not listed I didn't believe needed to be, but if they are needed please let me know.
Any help is much appreciated
You're going to need to save the pointerId's of each point and compare them to the new Id's given with each MotionEvent. It's slightly tricky to explain, so I'll point you to this ADB Post that explains it much better than I could. Long story short? Multitouch can be tricksy, but it's not as bad as it looks at first glance.