Android digital clock textview timepicker using buttons - java

I'm newbie Java programmer and newbie Android app developer... and i'm trying to make a simple 24h digital clock acting like a timepicker. I don't want to use the standard TimePicker widget in this case. App should work on Android 2.1+ also.
My clock i supposed look like this 23:59. When the user clicks on the clock rightmost field, buttons ranging from 0 to 9 (placed in the same Fragment) should update this rightmost field. The field should also be highlighted. I accomplished this with
view.setBackgroundResource(R.color.solid_grey);
Other fields should be updated in the same way, with some logic to avoid invalid values of course. Highlighting should be removed from first touched field when user touch another field.
My crappy solution to the problem:
What i did was to make five TextViews, one for each number and one for the colon. I have attached onTouch listeners to the changeable fields in the clock and onClick listeners for the buttons. Then i have some more or less complicated code with viewholders and tagging buttons with viewholder and what not to get all of this to work.
There MUST be a better way to do this! Don't you think?
First i tried to have a single TextView and just check which index in the string representing the clock in the textview, that was clicked. But this didn't work very good with highlighting. The index was also hard to compute with precision since i could not come up with a better idea than to use
(int) event.getX();
inside the OnTouchListener for the clock TextView.
Any ideas on how to accomplish this in the simplest possible way? If not, i have to stick with the butt-ugly hard to maintain code i made (no i won't post it here). :S

Ok, i'll post my own bulky solution here. It might not be pretty but it is working. Feel free to modify it to your liking. Keep in mind that I'm a newbie from Sweden. :P
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- The two most significant hex sets the transparency value -->
<color name="timefield_highlight_color">#FF8F8F8F</color>
<color name="timefield_no_highlight_color">#FF000000</color>
</resources>
timepicker_digital_24h.xml
Change the #dimen-stuff to your liking. Include this in your layout, using xml include-tag, wherever you want. DON'T change the id:s of the views.
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="#+id/layout_timepicker_digital_24h"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/vertical_margin" >
<TextView
android:id="#+id/textview_time_set_colon_divider"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:maxLength="1"
android:singleLine="true"
android:text=""
android:textSize="#dimen/alarm_time_huge_textsize"
tools:ignore="SpUsage" />
<TextView
android:id="#+id/textview_time_set_hour_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/textview_time_set_colon_divider"
android:maxLength="1"
android:singleLine="true"
android:text=""
android:textSize="#dimen/alarm_time_huge_textsize"
tools:ignore="SpUsage" />
<TextView
android:id="#+id/textview_time_set_hour_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/textview_time_set_hour_right"
android:maxLength="1"
android:singleLine="true"
android:text=""
android:textSize="#dimen/alarm_time_huge_textsize"
tools:ignore="SpUsage" />
<TextView
android:id="#+id/textview_time_set_minute_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/textview_time_set_colon_divider"
android:maxLength="1"
android:singleLine="true"
android:text=""
android:textSize="#dimen/alarm_time_huge_textsize"
tools:ignore="SpUsage" />
<TextView
android:id="#+id/textview_time_set_minute_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/textview_time_set_minute_left"
android:maxLength="1"
android:singleLine="true"
android:text=""
android:textSize="#dimen/alarm_time_huge_textsize"
tools:ignore="SpUsage" />
</RelativeLayout>
<LinearLayout
android:id="#+id/layout_time_buttons_row_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/horizontal_margin_small"
android:layout_marginRight="#dimen/horizontal_margin_small" >
<Button
android:id="#+id/button_1_time_set"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="#dimen/button_set_time_margin"
android:layout_weight="1"
android:text="1"
android:textSize="#dimen/button_set_time_textsize"
tools:ignore="HardcodedText" />
<Button
android:id="#+id/button_2_time_set"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="#dimen/button_set_time_margin"
android:layout_weight="1"
android:text="2"
android:textSize="#dimen/button_set_time_textsize"
tools:ignore="HardcodedText" />
<Button
android:id="#+id/button_3_time_set"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="#dimen/button_set_time_margin"
android:layout_weight="1"
android:text="3"
android:textSize="#dimen/button_set_time_textsize"
tools:ignore="HardcodedText" />
</LinearLayout>
<LinearLayout
android:id="#+id/layout_time_buttons_row_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/horizontal_margin_small"
android:layout_marginRight="#dimen/horizontal_margin_small" >
<Button
android:id="#+id/button_4_time_set"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="#dimen/button_set_time_margin"
android:layout_weight="1"
android:text="4"
android:textSize="#dimen/button_set_time_textsize"
tools:ignore="HardcodedText" />
<Button
android:id="#+id/button_5_time_set"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="#dimen/button_set_time_margin"
android:layout_weight="1"
android:text="5"
android:textSize="#dimen/button_set_time_textsize"
tools:ignore="HardcodedText" />
<Button
android:id="#+id/button_6_time_set"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="#dimen/button_set_time_margin"
android:layout_weight="1"
android:text="6"
android:textSize="#dimen/button_set_time_textsize"
tools:ignore="HardcodedText" />
</LinearLayout>
<LinearLayout
android:id="#+id/layout_time_buttons_row_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/horizontal_margin_small"
android:layout_marginRight="#dimen/horizontal_margin_small" >
<Button
android:id="#+id/button_7_time_set"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="#dimen/button_set_time_margin"
android:layout_weight="1"
android:text="7"
android:textSize="#dimen/button_set_time_textsize"
tools:ignore="HardcodedText" />
<Button
android:id="#+id/button_8_time_set"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="#dimen/button_set_time_margin"
android:layout_weight="1"
android:text="8"
android:textSize="#dimen/button_set_time_textsize"
tools:ignore="HardcodedText" />
<Button
android:id="#+id/button_9_time_set"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="#dimen/button_set_time_margin"
android:layout_weight="1"
android:text="9"
android:textSize="#dimen/button_set_time_textsize"
tools:ignore="HardcodedText" />
</LinearLayout>
<Button
android:id="#+id/button_0_time_set"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/vertical_margin_large"
android:layout_marginLeft="#dimen/horizontal_margin"
android:layout_marginRight="#dimen/horizontal_margin"
android:layout_marginTop="#dimen/button_set_time_margin"
android:text="0"
android:textSize="#dimen/button_set_time_textsize"
tools:ignore="HardcodedText" />
</merge>
Enums.java
package com.example.example.timepicker;
public class Enums {
public static enum TimeField {
HOUR_LEFT, HOUR_RIGHT, MINUTE_LEFT, MINUTE_RIGHT, NONE;
public TimeField nextReal() {
TimeField fields[] = TimeField.values();
int ordinal = this.ordinal(); // incoming field index
switch(ordinal) {
case 0: // HOUR_LEFT
ordinal = 1;
break;
case 1: // HOUR_RIGHT
ordinal = 2;
break;
case 2: // MINUTE_LEFT
ordinal = 3;
break;
case 3: // MINUTE_RIGHT
ordinal = 0;
break;
case 4: // NONE
ordinal = 0;
}
return fields[ordinal];
}
}
}
TimePickerDigital24h.java
(There is a toast-string in this code that you have to define in your strings.xml.)
package com.example.example.timepicker;
import android.app.Activity;
import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.example.example.R;
import com.example.example.timepicker.Enums.TimeField;
public class TimePickerDigital24h implements OnClickListener, OnTouchListener {
private TimeField mTimeFieldToSet = TimeField.HOUR_LEFT;
private TextView mTextViewHourLeft, mTextViewHourRight, mTextViewMinuteLeft, mTextViewMinuteRight;
private Context mContext;
public TimePickerDigital24h (Context context, String hourOfDay, String minute) {
mContext = context;
mTextViewHourLeft = (TextView) ((Activity) context).findViewById(R.id.textview_time_set_hour_left);
mTextViewHourLeft.setText(hourOfDay.substring(0, 1));
mTextViewHourRight = (TextView) ((Activity) context).findViewById(R.id.textview_time_set_hour_right);
mTextViewHourRight.setText(hourOfDay.substring(1, 2));
mTextViewMinuteLeft = (TextView) ((Activity) context).findViewById(R.id.textview_time_set_minute_left);
mTextViewMinuteLeft.setText(minute.substring(0, 1));
mTextViewMinuteRight = (TextView) ((Activity) context).findViewById(R.id.textview_time_set_minute_right);
mTextViewMinuteRight.setText(minute.substring(1, 2));
mTextViewHourLeft.setOnTouchListener(this);
mTextViewHourRight.setOnTouchListener(this);
mTextViewMinuteLeft.setOnTouchListener(this);
mTextViewMinuteRight.setOnTouchListener(this);
this.setTimeFieldHighlight(TimeField.HOUR_LEFT);
Button button0 = (Button) ((Activity) context).findViewById(R.id.button_0_time_set);
button0.setOnClickListener(this);
Button button1 = (Button) ((Activity) context).findViewById(R.id.button_1_time_set);
button1.setOnClickListener(this);
Button button2 = (Button) ((Activity) context).findViewById(R.id.button_2_time_set);
button2.setOnClickListener(this);
Button button3 = (Button) ((Activity) context).findViewById(R.id.button_3_time_set);
button3.setOnClickListener(this);
Button button4 = (Button) ((Activity) context).findViewById(R.id.button_4_time_set);
button4.setOnClickListener(this);
Button button5 = (Button) ((Activity) context).findViewById(R.id.button_5_time_set);
button5.setOnClickListener(this);
Button button6 = (Button) ((Activity) context).findViewById(R.id.button_6_time_set);
button6.setOnClickListener(this);
Button button7 = (Button) ((Activity) context).findViewById(R.id.button_7_time_set);
button7.setOnClickListener(this);
Button button8 = (Button) ((Activity) context).findViewById(R.id.button_8_time_set);
button8.setOnClickListener(this);
Button button9 = (Button) ((Activity) context).findViewById(R.id.button_9_time_set);
button9.setOnClickListener(this);
}
#Override
public boolean onTouch(View view, MotionEvent event) {
switch(view.getId()) {
case R.id.textview_time_set_hour_left:
mTimeFieldToSet = TimeField.HOUR_LEFT;
this.setTimeFieldHighlight(mTimeFieldToSet);
return true;
case R.id.textview_time_set_hour_right:
mTimeFieldToSet = TimeField.HOUR_RIGHT;
this.setTimeFieldHighlight(mTimeFieldToSet);
return true;
case R.id.textview_time_set_minute_left:
mTimeFieldToSet = TimeField.MINUTE_LEFT;
this.setTimeFieldHighlight(mTimeFieldToSet);
return true;
case R.id.textview_time_set_minute_right:
mTimeFieldToSet = TimeField.MINUTE_RIGHT;
this.setTimeFieldHighlight(mTimeFieldToSet);
return true;
}
return false;
}
#Override
public void onClick(View view) {
int valueToSet = 0;
switch(view.getId()) {
case R.id.button_0_time_set:
valueToSet = 0;
break;
case R.id.button_1_time_set:
valueToSet = 1;
break;
case R.id.button_2_time_set:
valueToSet = 2;
break;
case R.id.button_3_time_set:
valueToSet = 3;
break;
case R.id.button_4_time_set:
valueToSet = 4;
break;
case R.id.button_5_time_set:
valueToSet = 5;
break;
case R.id.button_6_time_set:
valueToSet = 6;
break;
case R.id.button_7_time_set:
valueToSet = 7;
break;
case R.id.button_8_time_set:
valueToSet = 8;
break;
case R.id.button_9_time_set:
valueToSet = 9;
break;
}
try {
this.setTimeField(valueToSet);
} catch (UnsupportedOperationException e) {
Toast.makeText(mContext, mContext.getString(R.string.toast_time_set_error), Toast.LENGTH_LONG).show();
//e.printStackTrace();
}
}
// Setter for timefields in the clock time display. Also highlights the correct field.
private void setTimeField (int valueToSet) throws UnsupportedOperationException {
int hourLeft = Integer.parseInt(mTextViewHourLeft.getText().toString());
int hourRight = Integer.parseInt(mTextViewHourRight.getText().toString());
UnsupportedOperationException exception = new UnsupportedOperationException("Input value invalid for this clock field");
setTimeFieldHighlight(mTimeFieldToSet.nextReal());
switch(mTimeFieldToSet) {
case HOUR_LEFT:
if (valueToSet <= 1) {
mTextViewHourLeft.setText("" + valueToSet);
mTimeFieldToSet = TimeField.HOUR_RIGHT;
break;
} else if (valueToSet <= 2 && hourRight <= 3) {
mTextViewHourLeft.setText("" + valueToSet);
mTimeFieldToSet = TimeField.HOUR_RIGHT;
break;
} else if (valueToSet <= 2 && hourRight >= 4) {
mTextViewHourRight.setText("3");
mTextViewHourLeft.setText("" + valueToSet);
mTimeFieldToSet = TimeField.HOUR_RIGHT;
break;
} else {
setTimeFieldHighlight(mTimeFieldToSet);
throw exception;
}
case HOUR_RIGHT:
if (valueToSet <= 3) {
mTextViewHourRight.setText("" + valueToSet);
mTimeFieldToSet = TimeField.MINUTE_LEFT;
break;
} else if (valueToSet > 3 && hourLeft <= 1) {
mTextViewHourRight.setText("" + valueToSet);
mTimeFieldToSet = TimeField.MINUTE_LEFT;
break;
} else if (valueToSet > 3 && hourLeft >= 2) {
mTextViewHourLeft.setText("1");
mTextViewHourRight.setText("" + valueToSet);
mTimeFieldToSet = TimeField.MINUTE_LEFT;
break;
} else {
setTimeFieldHighlight(mTimeFieldToSet);
throw exception;
}
case MINUTE_LEFT:
if (valueToSet <= 5) {
mTextViewMinuteLeft.setText("" + valueToSet);
mTimeFieldToSet = TimeField.MINUTE_RIGHT;
break;
} else {
setTimeFieldHighlight(mTimeFieldToSet);
throw exception;
}
case MINUTE_RIGHT:
mTextViewMinuteRight.setText("" + valueToSet);
mTimeFieldToSet = TimeField.HOUR_LEFT;
break;
case NONE:
}
}
// Highlighting of the fields in the clock display
private void setTimeFieldHighlight(TimeField field) {
switch(field) {
case HOUR_LEFT:
mTextViewHourLeft.setBackgroundResource(R.color.timefield_highlight_color);
mTextViewHourRight.setBackgroundResource(R.color.timefield_no_highlight_color);
mTextViewMinuteLeft.setBackgroundResource(R.color.timefield_no_highlight_color);
mTextViewMinuteRight.setBackgroundResource(R.color.timefield_no_highlight_color);
break;
case HOUR_RIGHT:
mTextViewHourLeft.setBackgroundResource(R.color.timefield_no_highlight_color);
mTextViewHourRight.setBackgroundResource(R.color.timefield_highlight_color);
mTextViewMinuteLeft.setBackgroundResource(R.color.timefield_no_highlight_color);
mTextViewMinuteRight.setBackgroundResource(R.color.timefield_no_highlight_color);
break;
case MINUTE_LEFT:
mTextViewHourLeft.setBackgroundResource(R.color.timefield_no_highlight_color);
mTextViewHourRight.setBackgroundResource(R.color.timefield_no_highlight_color);
mTextViewMinuteLeft.setBackgroundResource(R.color.timefield_highlight_color);
mTextViewMinuteRight.setBackgroundResource(R.color.timefield_no_highlight_color);
break;
case MINUTE_RIGHT:
mTextViewHourLeft.setBackgroundResource(R.color.timefield_no_highlight_color);
mTextViewHourRight.setBackgroundResource(R.color.timefield_no_highlight_color);
mTextViewMinuteLeft.setBackgroundResource(R.color.timefield_no_highlight_color);
mTextViewMinuteRight.setBackgroundResource(R.color.timefield_highlight_color);
break;
case NONE:
mTextViewHourLeft.setBackgroundResource(R.color.timefield_no_highlight_color);
mTextViewHourRight.setBackgroundResource(R.color.timefield_no_highlight_color);
mTextViewMinuteLeft.setBackgroundResource(R.color.timefield_no_highlight_color);
mTextViewMinuteRight.setBackgroundResource(R.color.timefield_no_highlight_color);
}
}
public String getHourOfDay() {
String hourOfDay = mTextViewHourLeft.getText().toString()
+ mTextViewHourRight.getText().toString();
return hourOfDay;
}
public String getMinute() {
String minute = mTextViewMinuteLeft.getText().toString()
+ mTextViewMinuteRight.getText().toString();
return minute;
}
}
**Put this code in your Fragment's OnActivityCreated method to instantiate the TimePickerDigital24h object: **
mTimePicker = new TimePickerDigital24h(getActivity(), "23", "59");
You can read back the time set by the user this way:
mTimePicker.getHourOfDay();
mTimePicker.getMinute();
I'm sure the code could be better in many ways. If you guys know how to do this in a much simpler way, please let me know. / TiredDude

Related

Disabling RadioGroup to avoid multiple counts of a score

I am trying to disable my RadioGroup so that the user cannot re-select and therefore my score count rises. I have seen an implementation here:
How to disable a RadioGroup until checkbox is checked
Albeit tried to go about it this way
My xml:
<RadioGroup
android:id="#+id/rg1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/sm_margin"
android:orientation="horizontal">
<RadioButton
android:id="#+id/pates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/sm_margin"
android:text="#string/pates"
android:onClick="onRadioButtonClicked" />
<RadioButton
android:id="#+id/chips"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/sm_margin"
android:text="#string/chips"
android:onClick="onRadioButtonClicked" />
<RadioButton
android:id="#+id/frites"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/sm_margin"
android:text="#string/frites"
android:onClick="onRadioButtonClicked" />
<RadioButton
android:id="#+id/crepes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/def_margin"
android:layout_marginLeft="#dimen/sm_margin"
android:text="#string/crepes"
android:onClick="onRadioButtonClicked" />
</RadioGroup>
</LinearLayout>
<TextView
android:id="#+id/question2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/def_margin"
android:background="#drawable/layout_bg"
android:fontFamily="#font/cambria"
android:padding="#dimen/tv_padding"
android:text="#string/question2" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginHorizontal="70dp"
android:adjustViewBounds="true"
android:src="#drawable/jc_fav" />
<RadioGroup
android:id="#+id/rg2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="#dimen/sm_margin"
android:orientation="vertical">
<RadioButton
android:id="#+id/mille_feuille"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/milleFeuille"
android:onClick="onRadioButtonClicked" />
<RadioButton
android:id="#+id/creme_chantilly"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/cremeChantilly"
android:onClick="onRadioButtonClicked" />
<RadioButton
android:id="#+id/opera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/opera"
android:onClick="onRadioButtonClicked" />
<RadioButton
android:id="#+id/creme_brulee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/def_margin"
android:text="#string/cremeBrulee"
android:onClick="onRadioButtonClicked" />
</RadioGroup>
Java:
int score = 0;
RadioGroup rgQ1;
RadioGroup rgQ2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rgQ1 = findViewById(R.id.rg1);
rgQ2 = findViewById(R.id.rg2);
}
public void onRadioButtonClicked(View view) {
// Is the button now checked? then assign into a boolean named 'checked'
boolean checked = ((RadioButton) view).isChecked();
// Question 1 logic
// Check correct answer is checked and update score
switch (view.getId()) {
case R.id.frites:
if (checked) score += 1;
Log.v("MainActivity", "score" + score);
break;
}
// Disable RadioButtons of rg1
for (int i = 0; i < rgQ1.getChildCount(); i++) {
(rgQ1.getChildAt(i)).setEnabled(false);
}
// Question 2 logic
// Check correct answer is checked and update score
switch (view.getId()) {
case R.id.mille_feuille:
if (checked) score += 1;
Log.v("MainActivity", "score" + score);
break;
}
// Disable RadioButtons of rg2
for (int i = 0; i < rgQ2.getChildCount(); i++) {
(rgQ2.getChildAt(i)).setEnabled(false);
}
}
}
It disables the first radiogroup when an item is selected and also the 2nd (before a user has selected for the 2nd radiogroup.
Any ideas?
You are disabling your second radio group in the same click event. Check which radio button group is selected.
Like this, Similarly check group two and disable radio button group two!
int selectedId = radioGroup.getCheckedRadioButtonId();
if(selectedId != null){
//Then disable radio button group one
}

Android - How do I keep a button clicked (Selected)?

I am trying to build a calculator with two TextView fields for the two numbers. I figured out how to input the numbers using an "in-app" number pad for the top number, Operand 1 [textView] (I know it would be easier using an EditText but this is for an assignment). I am having trouble switching to the second textView, Operand 2 [textView2].
When I am done input the number for textView, I want to switch to textView2 (using the plus, minus, mult, and/or div Buttons) and continue to enter in the numbers so I can use it for calculations.
Here is an Image of what my app looks like. Please ignore the stars, progress bar, and raido buttons as they are apart of the assignment but not relevent to the calculator.
Do you have any suggestion about how I can do this?
Android Code
package com.example.tristan.assn2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ArrayList<String> list1 = new ArrayList<String>();
ArrayList<String> list2 = new ArrayList<String>();
String operand1 = "";
String operand2 = "";
String oneS = "1";
String twoS = "2";
String threeS = "3";
String fourS = "4";
String fiveS = "5";
String sixS = "6";
String sevenS = "7";
String eightS = "8";
String nineS = "9";
String zeroS = "0";
String dotS = ".";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendMessage(View view)
{
Button clickedButton = (Button) view;
Button one = (Button) findViewById(R.id.button);
Button two = (Button) findViewById(R.id.button2);
Button three = (Button) findViewById(R.id.button3);
Button four = (Button) findViewById(R.id.button4);
Button five = (Button) findViewById(R.id.button5);
Button six = (Button) findViewById(R.id.button6);
Button seven = (Button) findViewById(R.id.button7);
Button eight = (Button) findViewById(R.id.button8);
Button nine = (Button) findViewById(R.id.button9);
Button zero = (Button) findViewById(R.id.button10);
Button dot = (Button) findViewById(R.id.button11);
Button clear = (Button) findViewById(R.id.button12);
Button plus = (Button) findViewById(R.id.button17);
Button minus = (Button) findViewById(R.id.button15);
Button mult = (Button) findViewById(R.id.button19);
Button div = (Button) findViewById(R.id.button21);
Button sr = (Button) findViewById(R.id.button16);
Button fac = (Button) findViewById(R.id.button20);
Button dd = (Button) findViewById(R.id.button22);
Button equal = (Button) findViewById(R.id.button23);
//------------------------------------------------------------------------------------------
TextView textView = (TextView) findViewById(R.id.textView);
TextView textView2 = (TextView) findViewById(R.id.textView2);
//OPERAND1
if(clickedButton == minus) {
operand1 = "-";
list1.add(operand1);
}
if (clickedButton == one) {
operand1 = oneS;
list1.add(operand1);
}
if (clickedButton == two) {
operand1 = twoS;
list1.add(operand1);
}
if (clickedButton == three) {
operand1 = threeS;
list1.add(operand1);
}
if (clickedButton == four) {
operand1 = fourS;
list1.add(operand1);
}
if (clickedButton == five) {
operand1 = fiveS;
list1.add(operand1);
}
if (clickedButton == six) {
operand1 = sixS;
list1.add(operand1);
}
if (clickedButton == seven) {
operand1 = sevenS;
list1.add(operand1);
}
if (clickedButton == eight) {
operand1 = eightS;
list1.add(operand1);
}
if (clickedButton == nine) {
operand1 = nineS;
list1.add(operand1);
}
if (clickedButton == zero) {
operand1 = zeroS;
list1.add(operand1);
}
if (clickedButton == dot) {
operand1 = dotS;
list1.add(operand1);
}
//Builds String from ArrayList
StringBuilder sb1 = new StringBuilder();
for (String s1 : list1) {
sb1.append(s1);
}
//STRING NUMBER
String output1 = sb1.toString();
textView.setText(output1);
//NEED TO CONVERT TO DOUBLE*****************
//IF plus, minus, mult, div is pressed, switch to Operand 2 and input numbers
if(clickedButton == plus || clickedButton == minus || clickedButton == mult || clickedButton == div) {
if (clickedButton == one) {
operand2 = oneS;
list2.add(operand2);
}
if (clickedButton == two) {
operand2 = twoS;
list2.add(operand2);
}
if (clickedButton == three) {
operand2 = threeS;
list2.add(operand2);
}
if (clickedButton == four) {
operand2 = fourS;
list2.add(operand2);
}
if (clickedButton == five) {
operand2 = fiveS;
list2.add(operand2);
}
if (clickedButton == six) {
operand2 = sixS;
list2.add(operand2);
}
if (clickedButton == seven) {
operand2 = sevenS;
list2.add(operand2);
}
if (clickedButton == eight) {
operand2 = eightS;
list2.add(operand2);
}
if (clickedButton == nine) {
operand2 = nineS;
list2.add(operand2);
}
if (clickedButton == zero) {
operand2 = zeroS;
list2.add(operand2);
}
if (clickedButton == dot) {
operand2 = dotS;
list2.add(operand2);
}
//Builds String from ArrayList
StringBuilder sb2 = new StringBuilder();
for (String s2 : list2) {
sb2.append(s2);
}
//STRING NUMBER
String output2 = sb2.toString();
textView2.setText(output2);
}
}
}
XML File
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.tristan.assn2.MainActivity">
<Button
android:text="/"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button21"
android:fontFamily="sans-serif-medium"
android:layout_alignBaseline="#+id/button18"
android:layout_alignBottom="#+id/button18"
android:layout_toRightOf="#+id/button18"
android:layout_alignRight="#+id/button15"
android:layout_alignEnd="#+id/button15"
android:onClick="sendMessage"/>
<Button
android:text="3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button3"
android:layout_alignBaseline="#+id/button"
android:layout_alignBottom="#+id/button"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="sendMessage"/>
<Button
android:text="2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button2"
android:layout_alignBaseline="#+id/button"
android:layout_alignBottom="#+id/button"
android:layout_toLeftOf="#+id/button3"
android:layout_toStartOf="#+id/button3"
android:onClick="sendMessage"/>
<Button
android:text="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:layout_marginBottom="61dp"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/button2"
android:layout_toStartOf="#+id/button2"
android:onClick="sendMessage"/>
<Button
android:text="4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button4"
android:layout_above="#+id/button2"
android:layout_alignLeft="#+id/button11"
android:layout_alignStart="#+id/button11"
android:onClick="sendMessage"/>
<Button
android:text="5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button2"
android:layout_toLeftOf="#+id/button3"
android:layout_toStartOf="#+id/button3"
android:id="#+id/button5"
android:onClick="sendMessage"/>
<Button
android:text="6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/button5"
android:layout_toRightOf="#+id/button2"
android:layout_toEndOf="#+id/button2"
android:id="#+id/button6"
android:onClick="sendMessage"/>
<Button
android:text="7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button7"
android:layout_above="#+id/button4"
android:layout_alignLeft="#+id/button4"
android:layout_alignStart="#+id/button4"
android:onClick="sendMessage"/>
<Button
android:text="8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/button7"
android:layout_toRightOf="#+id/button4"
android:layout_toEndOf="#+id/button4"
android:id="#+id/button8"
android:onClick="sendMessage"/>
<Button
android:text="9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button6"
android:layout_toRightOf="#+id/button5"
android:id="#+id/button9"
android:layout_alignTop="#+id/button8"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="sendMessage"/>
<Button
android:text="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button3"
android:layout_toLeftOf="#+id/button3"
android:layout_toStartOf="#+id/button3"
android:id="#+id/button10"
android:onClick="sendMessage"/>
<Button
android:text="."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button10"
android:layout_toLeftOf="#+id/button2"
android:id="#+id/button11"
android:layout_below="#+id/button5"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="87dp"
android:layout_marginStart="87dp"
android:layout_marginTop="47dp"
android:onClick="sendMessage"/>
<Button
android:text="-"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button15"
android:layout_alignTop="#+id/button7"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="41dp"
android:layout_marginStart="41dp"
android:layout_toLeftOf="#+id/button4"
android:layout_toStartOf="#+id/button4"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"
android:onClick="sendMessage"/>
<Button
android:text="+"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button17"
android:layout_alignBaseline="#+id/button15"
android:layout_alignBottom="#+id/button15"
android:layout_toStartOf="#+id/button4"
android:layout_toLeftOf="#+id/button4"
android:layout_marginRight="47dp"
android:layout_marginEnd="47dp"
android:onClick="sendMessage" />
<Button
android:text="x"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button18"
android:layout_below="#+id/button17"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="#+id/button17"
android:layout_alignEnd="#+id/button17"
android:fontFamily="sans-serif-medium"
android:onClick="sendMessage"/>
<Button
android:text="x"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button19"
android:layout_below="#+id/button17"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="#+id/button17"
android:layout_alignEnd="#+id/button17"
android:fontFamily="sans-serif-medium"
android:onClick="sendMessage"/>
<Button
android:text="sr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button16"
android:fontFamily="sans-serif-medium"
android:layout_below="#+id/button4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/button20"
android:layout_toStartOf="#+id/button20"
android:onClick="sendMessage"/>
<Button
android:text="!"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button20"
android:fontFamily="sans-serif-medium"
android:layout_below="#+id/button21"
android:layout_alignLeft="#+id/button15"
android:layout_alignStart="#+id/button15"
android:layout_alignRight="#+id/button21"
android:layout_alignEnd="#+id/button21"
android:onClick="sendMessage"/>
<Button
android:text="DD - DMS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button11"
android:layout_toStartOf="#+id/button"
android:id="#+id/button22"
android:layout_alignRight="#+id/button20"
android:layout_alignEnd="#+id/button20"
android:onClick="sendMessage"/>
<TextView
android:hint="Operand 1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/textView"
android:layout_toLeftOf="#+id/button5"
android:layout_toStartOf="#+id/button5" />
<TextView
android:hint="Operand 2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="13dp"
android:id="#+id/textView2"
android:layout_toLeftOf="#+id/button8"
android:layout_toStartOf="#+id/button8" />
<TextView
android:hint="Results"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/textView3"
android:layout_toRightOf="#+id/textView"
android:layout_toEndOf="#+id/textView" />
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/ratingBar"
android:isIndicator="false"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<ProgressBar
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progressBar"
android:layout_alignBottom="#+id/ratingBar"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="12dp"
android:layout_marginStart="12dp" />
<RadioButton
android:text="Use for Operand 1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioButton"
android:layout_below="#+id/button23"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<RadioButton
android:text="Use for Operand 2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/radioButton"
android:layout_alignLeft="#+id/radioButton"
android:layout_alignStart="#+id/radioButton"
android:id="#+id/radioButton2" />
<Button
android:text="="
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button23"
android:layout_alignTop="#+id/textView2"
android:layout_toRightOf="#+id/textView2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="sendMessage"/>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner"
android:layout_below="#+id/radioButton2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:text="C"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button12"
android:layout_alignBaseline="#+id/button10"
android:layout_alignBottom="#+id/button10"
android:layout_toRightOf="#+id/button8"
android:layout_toEndOf="#+id/button8"
android:onClick="sendMessage"/>
</RelativeLayout>
You have to listen the key clicks after the plus/minus/.. button clicks occurs. Use a flag. set the flag 1 when plus/minus/.. or textview2 gets clicked and change the textview to textview2 and list1 to list2. Do the vice versa when textview1 is clicked.
you can do something like this :
public class YourActivityName extends AppCompatActivity {
TextView operand1, operand2,result,tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dd);
operand1 = (TextView) findViewById(R.id.textView);
operand2 = (TextView) findViewById(R.id.textView2);
result= (TextView) findViewById(R.id.textView3);
tv=operand1;
}
public void sendMessage(View v) {
if (v.getId() == R.id.button19 || v.getId() == R.id.button15 || v.getId() == R.id.button17 || v.getId() == R.id.button21 || v.getId() == R.id.button18) {
tv=operand2;
}
if(v.getId()==R.id.button7){
tv.setText(tv.getText()+"7");
}
if(v.getId()==R.id.button8){
tv.setText(tv.getText()+"8");
}
if(v.getId()==R.id.button23){
int op1=Integer.parseInt(operand1.getText().toString());
int op2=Integer.parseInt(operand2.getText().toString());
op2+=op1;
result.setText(op2+"");
tv=operand1;
operand1.setText("");
operand2.setText("");
}
}
}
this is code only for having addition on pressing '=' button and for taking input only from button '7' and '8' you need to and functionality for other buttons and operations ..but it should solve your problem of changing textview for input...
you can also do by means of flags . as suggested by #Code-Apprentice ...
You should toggle a boolean flag whenever the user clicks an operator button. Then when the user clicks a number button, check this flag to determine which operand is being used.
In conjunction with this flag, you can also have a variable like TextView currentTextView which is assigned to refer to the currently used TextView. Then whenever the user clicks an operator button, assign this variable to the other TextView. This strategy will make it so you only need if statements in the OnClickListeners for the operator buttons. You won't need any if statements in the OnClickListeners for the number buttons.
p.s. Note that every number button already displays the numerical character. You can use this fact to remove nearly all of the if...else chain in your click listener.
p.p.s. You should consider writing different OnClickListeners for each button. With the previously mentioned strategy, you can have a single listener for all the number buttons, but I still suggest creating a different listener for each operator button.

can't use findViewById id cannot resolved

I can't figured it out in my project.
I already save the .xml, but the findViewById cannot find my Id. All other findViewById are fine, but the error is on the Apple one. I tried to rename the id, but it didn't work. Here is my code
package com.flaternity.fasterbrain;
import android.annotation.SuppressLint;
import com.flaternity.fasterbrain.*;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
#SuppressLint("NewApi")
#SuppressWarnings("deprecation")
public class Game extends ActionBarActivity {
TextView Timer, TEST, Score, GAMEOVER, yourScore, TimerText, TIMEUP;
Button Left, Right, BACKBTN;
ImageView Apple;
int RanSoal, whichTrue = 0, EngSoal;
static int score=0;
static int curGame;//0=Math 1=English
String[] EngTest={
"I __ sick",//0
"I __ Happy yerterday",
"Books __ available in BookStore",
"He __ Games yesterday",
"Minions __ Bananas",
"She __ Books",
"How __ water do you have?",//6
"your __ is Nice",
"__ love Game",
"I am __ than you",
};
String[] Mathtest ={
"2x2=__",//1 [0]
"4:__=4",
"4+5=__",
"5X5=__",
"5X__=45",//5 [4]
"35-5=__",
"5:5=__",
"2-5=__",
"2X(-5)=__",
"2+__=1",//10[9]
"5-5=__",
"2+1=__",
"4:__=8",
"4:2=__",
"3-2=__",//15[14]
"9X4=__",
"10:__=80",
"9X__=36",
"8X4=__",
"3X3=__"//20[19]
};
CountDownTimer timer = new CountDownTimer(3000,1000){
#Override
public void onTick(long millisUntilFinished) {
Timer.setText(""+millisUntilFinished / 1000);
}
#Override
public void onFinish() {
TimeUP();
Timer.setText("0");
}
};
private void MakeSoalMath(){
RanSoal = (int)(Math.random()*19);
TEST.setText(""+Mathtest[RanSoal]);
switch(RanSoal){
case 0:
Left.setText("4");//True
Right.setText("8");
whichTrue = 0;
break;
case 1:
Left.setText("1");//True
Right.setText("4");
whichTrue = 0;
break;
case 2:
Left.setText("9");//True
Right.setText("20");
whichTrue = 0;
break;
case 3:
Left.setText("25");//True
Right.setText("10");
whichTrue = 0;
break;
case 4:
Left.setText("9");//True
Right.setText("5");
whichTrue = 0;
break;
case 5:
Left.setText("30");//True
Right.setText("7");
whichTrue = 0;
break;
case 6:
Left.setText("1");//True
Right.setText("25");
whichTrue = 0;
break;
case 7:
Left.setText("-3");//True
Right.setText("3");
whichTrue = 0;
break;
case 8:
Left.setText("-10");//True
Right.setText("10");
whichTrue = 0;
break;
case 9:
Left.setText("-1");//True
Right.setText("1");
whichTrue = 0;
break;
case 10:
Left.setText("10");
Right.setText("0");//True
whichTrue = 1;
break;
case 11:
Left.setText("-3");
Right.setText("3");//True
whichTrue = 1;
break;
case 12:
Left.setText("2");
Right.setText("1/2");//True
whichTrue = 1;
break;
case 13:
Left.setText("8");
Right.setText("2");//True
whichTrue = 1;
break;
case 14:
Left.setText("3");
Right.setText("1");//True
whichTrue = 1;
break;
case 15:
Left.setText("5");
Right.setText("36");//True
whichTrue = 1;
break;
case 16:
Left.setText("8");
Right.setText("1/8");//True
whichTrue = 1;
break;
case 17:
Left.setText("27");
Right.setText("4");//True
whichTrue = 1;
break;
case 18:
Left.setText("12");
Right.setText("32");//True
whichTrue = 1;
break;
case 19:
Left.setText("6");
Right.setText("9");//True
whichTrue = 1;
break;
}
}
private void Benar(){
if(curGame == 0){
MakeSoalMath();
}
score++;
Score.setText(""+score);
timer.cancel();
timer.start();
}
private void Salah(){
BACKBTN.setVisibility(View.VISIBLE);
yourScore.setVisibility(View.VISIBLE);
GAMEOVER.setVisibility(View.VISIBLE);
Timer.setVisibility(View.INVISIBLE);
TEST.setVisibility(View.INVISIBLE);
Score.setVisibility(View.INVISIBLE);
Left.setVisibility(View.INVISIBLE);
Right.setVisibility(View.INVISIBLE);
TimerText.setVisibility(View.INVISIBLE);
yourScore.setText("your score:" + score);
timer.cancel();
}
private void TimeUP(){
BACKBTN.setVisibility(View.VISIBLE);
yourScore.setVisibility(View.VISIBLE);
TIMEUP.setVisibility(View.VISIBLE);
Timer.setVisibility(View.INVISIBLE);
TEST.setVisibility(View.INVISIBLE);
Score.setVisibility(View.INVISIBLE);
Left.setVisibility(View.INVISIBLE);
Right.setVisibility(View.INVISIBLE);
TimerText.setVisibility(View.INVISIBLE);
yourScore.setText("your score:" + score);
timer.cancel();
}
private void masukGame(){
Timer=(TextView)findViewById(R.id.Timer);
TEST=(TextView)findViewById(R.id.TEST);
Score=(TextView)findViewById(R.id.Score);
Left=(Button)findViewById(R.id.LeftAnswer);
Right=(Button)findViewById(R.id.RightAnswer);
BACKBTN=(Button)findViewById(R.id.BACKTom);
GAMEOVER=(TextView)findViewById(R.id.GAMEOVER);
yourScore=(TextView)findViewById(R.id.yourScore);
TimerText=(TextView)findViewById(R.id.TimeText);
TIMEUP=(TextView)findViewById(R.id.TIMEUP);
Apple=(ImageView)findViewById(R.id.Apple);
BACKBTN.setVisibility(View.INVISIBLE);
GAMEOVER.setVisibility(View.INVISIBLE);
yourScore.setVisibility(View.INVISIBLE);
TIMEUP.setVisibility(View.INVISIBLE);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
masukGame();
timer.start();
Right.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(whichTrue == 1){
Benar();
}else{
Salah();
}
}
});
Left.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(whichTrue == 0){
Benar();
}else{
Salah();
}
}
});
BACKBTN.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
Intent myIA = new Intent(Game.this, MainMenu.class);
Game.this.startActivity(myIA);
}
});
//Math
if(curGame==0){
MakeSoalMath();
}else{//ENGLISH
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.game, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Here is 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.flaternity.fasterbrain.Game" >
<TextView
android:id="#+id/Timer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:textSize="28sp"
android:textColor="#000000"
/>
<TextView
android:id="#+id/TEST"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="50dp"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:textSize="28sp"
android:textColor="#000000"
/>
<TextView
android:id="#+id/Score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal = "true"
android:textColor="#000000"
android:textSize="28sp"
android:text="0"
/>
<TextView
android:id="#+id/TimeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:textSize="28sp"
android:textColor="#000000"
android:text="Time Left:" />
<Button
android:id="#+id/LeftAnswer"
android:layout_width="140dp"
android:layout_height="150dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="80dp"
android:textColor="#000000"
android:textSize="32sp"
/>
<Button
android:id="#+id/RightAnswer"
android:layout_width="140dp"
android:layout_height="150dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="80dp"
android:layout_alignParentRight="true"
android:textColor="#000000"
android:textSize="32sp"
/>
<TextView
android:id="#+id/GAMEOVER"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:textColor="#000000"
android:textSize="40sp"
android:text="GAME OVER"
/>
<TextView
android:id="#+id/TIMEUP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:textColor="#000000"
android:textSize="40sp"
android:text="TIME IS UP"
/>
<Button
android:id="#+id/BACKTom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:layout_alignParentBottom="true"
android:layout_marginBottom="5dp"
android:textColor="#000000"
android:textSize="20sp"
android:text="Back"
/>
<TextView
android:id="#+id/yourScore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textColor="#000000"
android:textSize="28sp"
android:text="your score :"
/>
<ImageView
android:id="#+id/Apple"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal ="true"
android:layout_marginTop="40dp"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:src="#drawable/Apple" />
</RelativeLayout>
The Error is on Apple=(ImageView)findViewById(R.id.Apple);
but somehow I noticed the difference before the Problem and After the Problem. Before the problem, when I modify th R.java it says error so I have to restore. But After the problem when I modify R.java it says no Error, I was able to save the change. Does my R.java not generate? I do the reserach but I cant fix the problem. Everybody said the manifest, but I dont know what to change. What happned? How to fix this?

Android - Errors when trying to change activities

For some reason this came up when trying to switch activities. I've had no problem running the same kind of code with other activities in this project. I'll post the code and see if anyone can figure it out ;(
I can't get my error to format right in this, so here's the error log
http://textuploader.com/oe6g
Here's my different activities.
The Problem occurs when I click on the button in my New Game Activity Layout, and that button runs the confirm(View view)
NewGameActivity.java
public class NewGameActivity extends Activity {
EditText title;
TextView team1, team2;
SqliteHelper db;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_newgame);
//ListViews
team1 = (TextView) findViewById(R.id.textView4);
team2 = (TextView) findViewById(R.id.textView5);
title = (EditText) findViewById(R.id.editText1);
db = new SqliteHelper(this.getApplicationContext());
populateListViews();
registerClickCallback();
}
/*
*
* Database Functions
*
*/
/*
*
* ListView Stuff
*
*/
private void populateListViews() {
// THIS HERE WILL POPULATE BOTH TEAM LIST VIEWS
//Create list of items
Cursor cursor = db.getTeams();
ArrayList<String> values = new ArrayList<String>();
if (cursor != null && cursor.getCount() != 0) {
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
values.add(cursor.getString(cursor.getColumnIndex("Team_Names")));
cursor.moveToNext();
}
}
//Build Adapter
ArrayAdapter<String> t1adapter = new ArrayAdapter<String>(
this, // Context
R.layout.teamlistviews, // Layout to use
values); // Items to be displayed
ArrayAdapter<String> t2adapter = new ArrayAdapter<String>(
this, // Context
R.layout.teamlistviews, // Layout to use
values);
//Configure the List View
ListView t1list = (ListView) findViewById(R.id.listView1);
t1list.setAdapter(t1adapter);
ListView t2list = (ListView) findViewById(R.id.listView2);
t2list.setAdapter(t2adapter);
}
private void registerClickCallback() {
//This uses the List View and adds a listener to check for clicks/taps on different
//list view items. It will then display a message telling you which one you have selected.
ListView t1list = (ListView) findViewById(R.id.listView1);
ListView t2list = (ListView) findViewById(R.id.listView2);
t1list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View viewClicked,
int position, long id) {
//team 1
TextView textView = (TextView) viewClicked;
//Changing team name
TextView t1 = (TextView) findViewById(R.id.textView4);
String team1 = textView.getText().toString();
t1.setText(team1);
//Toast message
String message = "You Selected " + textView.getText().toString() + " for Team 1";
Toast.makeText(NewGameActivity.this, message, Toast.LENGTH_SHORT).show();
}
});
t2list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View viewClicked,
int position, long id) {
//team 2
TextView textView = (TextView) viewClicked;
//Changing Team name
TextView t2 = (TextView) findViewById(R.id.textView5);
String team2 = textView.getText().toString();
t2.setText(team2);
//Toast message
String message = "You Selected " + textView.getText().toString() + " for Team 2";
Toast.makeText(NewGameActivity.this, message, Toast.LENGTH_SHORT).show();
}
});
}
public void confirm(View view) {
String t1, t2, gTitle;
t1 = team1.toString();
t2 = team2.toString();
gTitle = title.toString();
Intent intent = new Intent(this, CourtActivity.class);
intent.putExtra("TEAM1", t1);
intent.putExtra("TEAM2", t2);
intent.putExtra("GAME_TITLE", gTitle);
startActivity(intent);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.new_game, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
CourtActivity.java
package com.example.statapalpha;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
import android.widget.TextView;
import android.widget.Toast;
import java.lang.Math;
import java.util.ArrayList;
// Court Screen
public class CourtActivity extends Activity implements OnMenuItemClickListener{
// Opens database
SqliteHelper db;
ArrayList<String> homePlayersIn = new ArrayList<String>();
ArrayList<String> awayPlayersIn = new ArrayList<String>();
ArrayList<String> homePlayersBench = new ArrayList<String>();
ArrayList<String> awayPlayersBench = new ArrayList<String>();
String team1, team2, team1n, team2n;
String player = "0"; // Player number for current play
String action = ""; // Action text for current play
position position = new position(); // Position for current play
int playNumber = 0;
private PopupMenu popupMenu;
boolean isHome = false;
int playerButton = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_court);
db = new SqliteHelper(this.getApplicationContext());
//Get Team 1 and 2 and Game Title
convertStrings();
// Gets players
getPlayers();
}
public void convertStrings() {
Intent mIntent = getIntent();
team1n = mIntent.getStringExtra("TEAM1");
team2n = mIntent.getStringExtra("TEAM2");
}
// Populates arrays with player numbers
void getPlayers() {
homePlayersBench.add("12");
homePlayersBench.add("10");
homePlayersBench.add("32");
homePlayersBench.add("16");
homePlayersBench.add("13");
homePlayersBench.add("19");
homePlayersIn.add("13");
homePlayersIn.add("14");
homePlayersIn.add("15");
homePlayersIn.add("16");
homePlayersIn.add("17");
awayPlayersBench.add("21");
awayPlayersBench.add("22");
awayPlayersBench.add("23");
awayPlayersBench.add("24");
awayPlayersBench.add("25");
awayPlayersBench.add("26");
awayPlayersIn.add("31");
awayPlayersIn.add("32");
awayPlayersIn.add("33");
awayPlayersIn.add("34");
awayPlayersIn.add("35");
}
// Stores x and y coordinate
public class position {
public int x;
public int y;
}
// Sets current player when a player button is clicked
public void setPlayer(View v) {
Button b = (Button)v;
player = b.getText().toString();
playerButton = b.getId();
switch(b.getId()) {
case R.id.p1:
case R.id.p2:
case R.id.p3:
case R.id.p4:
case R.id.p5: isHome = true; break;
default: isHome = false;
break;
}
}
// Sets string Action to whatever action the user taps
// then records play to database.
public void setAction(View v) {
Button b = (Button)v;
String toastAction = "";
String message = "";
String team = "";
switch(b.getId()) {
case R.id.fgMade: action = "F" + goal(position) + "H"; toastAction = "made " + goal(position) + " point shot";
break;
case R.id.fgMissed: action = "F" + goal(position) + "M"; toastAction = "missed " + goal(position) + " point shot";
break;
case R.id.ftMade: action = "FTH"; toastAction = "made freethrow";
break;
case R.id.ftMissed: action = "FTM"; toastAction = "missed freethrow";
break;
case R.id.rebound: action = "RB"; toastAction = "rebound";
break;
case R.id.assist: action = "AST"; toastAction = "assist";
break;
case R.id.block: action = "BL"; toastAction = "block";
break;
case R.id.steal: action = "STL"; toastAction = "steal";
break;
case R.id.turnover: action = "TO"; toastAction = "turnover";
break;
case R.id.sub: action = "SUB"; toastAction = "substitution";
break;
case R.id.foul: action = "FC"; toastAction = "commited foul";
break;
case R.id.undoPlay: undoPlay(v);
break;
}
if (action == "SUB") {
popupMenu = new PopupMenu(this.getBaseContext(), v);
Menu menu = popupMenu.getMenu();
if (isHome == true) {
for (String number : homePlayersBench) {
menu.add(number);
}
}
else {
for (String number : awayPlayersBench) {
menu.add(number);
}
}
popupMenu.setOnMenuItemClickListener(this);
popupMenu.show();
return;
}
else {
message = "Player " + player + " " + toastAction + " at (" + Integer.toString(position.x) + ", " + Integer.toString(position.y) + ")";
}
if (isHome == true)
team = team1;
else
team = team2;
playNumber++;
db.recordPlay(Integer.parseInt(player), team, action, position, playNumber);
Toast.makeText(CourtActivity.this, message, Toast.LENGTH_SHORT).show();
refreshPlayers();
}
// Gets tap position and saves it to 'position'
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
position.x = (int)event.getX(0);
position.y = (int)event.getY(0);
}
return super.onTouchEvent(event);
}
int goal(position position) {
int points=3;
if ((Math.sqrt(Math.pow((position.x - 1082), 2) + Math.pow((position.y - 453), 2)) < 270) || (Math.sqrt(Math.pow((position.x - 193), 2) + Math.pow((position.y - 453), 2)) < 270)) points = 2;
return points;
}
public void undoPlay(View v) {
db.undoPlay(Integer.toString(playNumber));
}
#Override
public boolean onMenuItemClick(MenuItem item) {
Button button = (Button)findViewById(playerButton);
button.setText(item.getTitle());
refreshPlayers();
return false;
}
}
activity_court.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"
android:background="#drawable/bg2court"
tools:context="${relativePackage}.${activityClass}" xmlns:android1="http://schemas.android.com/apk/res/android">
<Button
android:id="#+id/editPlays"
android:layout_width="190dp"
android:layout_height="75dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:onClick="editPlays"
android:text="#string/edit_plays"
/>
<Button
android:id="#+id/undoPlay"
android:layout_width="190dp"
android:layout_height="75dp"
android:layout_alignParentRight="true"
android:layout_below="#+id/editPlays"
android:onClick="undoPlay"
android:text="#string/undo" />
<Button
android:id="#+id/ftMissed"
android:layout_width="190dp"
android:layout_height="85dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:onClick="setAction"
android:text="#string/miss" />
<Button
android:id="#+id/ftMade"
android:layout_width="190dp"
android:layout_height="85dp"
android:layout_above="#+id/ftMissed"
android:layout_alignParentRight="true"
android:onClick="setAction"
android:text="#string/made" />
<Button
android:id="#+id/fgMade"
android:layout_width="190dp"
android:layout_height="85dp"
android:layout_above="#+id/fgMissed"
android:layout_alignParentRight="true"
android:onClick="setAction"
android:text="#string/made" />
<Button
android:id="#+id/fgMissed"
android:layout_width="190dp"
android:layout_height="85dp"
android:layout_above="#+id/ftMade"
android:layout_alignParentRight="true"
android:layout_marginBottom="52dp"
android:onClick="setAction"
android:text="#string/miss" />
<TextView
android:id="#+id/fgText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/fgMade"
android:layout_alignLeft="#+id/playerStats"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="#string/field_goal"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="38sp" />
<Button
android:id="#+id/playerStats"
android:layout_width="190dp"
android:layout_height="75dp"
android:layout_alignParentRight="true"
android:layout_below="#+id/undoPlay"
android:onClick="redoPlay"
android:text="#string/playerStats" />
<TextView
android:id="#+id/ftText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/ftMade"
android:layout_alignRight="#+id/fgText"
android1:layout_alignLeft="#+id/ftMade"
android:text="#string/free_throw"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="34sp" />
<Button
android:id="#+id/foul"
android:layout_width="190dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/ftMade"
android:onClick="setAction"
android:color= "#FF0079FF"
android:text="#string/foul" />
<Button
android:id="#+id/steal"
android:layout_width="190dp"
android:layout_height="80dp"
android:layout_above="#+id/turnover"
android:layout_alignParentLeft="true"
android:onClick="setAction"
android:text="#string/steal" />
<Button
android:id="#+id/block"
android:layout_width="190dp"
android:layout_height="80dp"
android:layout_above="#+id/steal"
android:layout_alignParentLeft="true"
android:onClick="setAction"
android:text="#string/block" />
<Button
android:id="#+id/assist"
android:layout_width="190dp"
android:layout_height="80dp"
android:layout_above="#+id/block"
android:layout_alignParentLeft="true"
android:onClick="setAction"
android:text="#string/assist" />
<Button
android:id="#+id/rebound"
android:layout_width="190dp"
android:layout_height="80dp"
android:layout_above="#+id/assist"
android:layout_alignParentLeft="true"
android:onClick="setAction"
android:text="#string/rebound" />
<Button
android:id="#+id/p1"
android:layout_width="80dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/rebound"
android:onClick="setPlayer"
android:text="#string/p1" />
<Button
android:id="#+id/p2"
android:layout_width="85dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/p1"
android:onClick="setPlayer"
android:text="#string/p2" />
<Button
android:id="#+id/p3"
android:layout_width="85dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/p2"
android:onClick="setPlayer"
android:text="#string/p3" />
<Button
android:id="#+id/p4"
android:layout_width="85dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/p3"
android:onClick="setPlayer"
android:text="#string/p4" />
<Button
android:id="#+id/p5"
android:layout_width="85dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/p4"
android:onClick="setPlayer"
android:text="#string/p5" />
<Button
android:id="#+id/p10"
android:layout_width="85dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/editPlays"
android:onClick="setPlayer"
android:text="#string/p10" />
<Button
android:id="#+id/p9"
android:layout_width="85dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/p10"
android:onClick="setPlayer"
android:text="#string/p9" />
<Button
android:id="#+id/p8"
android:layout_width="85dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/p9"
android:onClick="setPlayer"
android:text="#string/p8" />
<Button
android:id="#+id/p7"
android:layout_width="85dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/p8"
android:onClick="setPlayer"
android:text="#string/p7" />
<Button
android:id="#+id/p6"
android:layout_width="85dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/p7"
android:onClick="setPlayer"
android:text="#string/p6" />
<Button
android:id="#+id/sub"
android:layout_width="190dp"
android:layout_height="80dp"
android:layout_above="#+id/foul"
android:layout_toLeftOf="#+id/p1"
android:onClick="setAction"
android:text="#string/sub" />
<Button
android:id="#+id/turnover"
android:layout_width="190dp"
android:layout_height="80dp"
android:layout_above="#+id/sub"
android:layout_toLeftOf="#+id/p1"
android:onClick="setAction"
android:text="#string/turnover" />
<TextView
android:id="#+id/points"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/court"
android:layout_alignRight="#+id/rebound"
android:text="#string/points"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/fouls"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/undoPlay"
android:layout_toLeftOf="#+id/p1"
android:text="#string/fouls"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/p1p"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/points"
android:layout_alignRight="#+id/p1"
android:layout_marginRight="35dp"
android:text="#string/p1p"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/p1f"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/fouls"
android:layout_alignRight="#+id/p1"
android:layout_marginRight="35dp"
android:text="#string/p1f"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/p2p"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/points"
android:layout_alignRight="#+id/p2"
android:layout_marginRight="35dp"
android:text="#string/p2p"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/p2f"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/fouls"
android:layout_alignRight="#+id/p2"
android:layout_marginRight="35dp"
android:text="#string/p2f"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/p3p"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/points"
android:layout_alignRight="#+id/p3"
android:layout_marginRight="35dp"
android:text="#string/p3p"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/p3f"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/fouls"
android:layout_alignRight="#+id/p3"
android:layout_marginRight="35dp"
android:text="#string/p3f"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/p4p"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/points"
android:layout_alignRight="#+id/p4"
android:layout_marginRight="35dp"
android:text="#string/p4p"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/p4f"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/fouls"
android:layout_alignRight="#+id/p4"
android:layout_marginRight="35dp"
android:text="#string/p4f"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/p5p"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/points"
android:layout_alignRight="#+id/p5"
android:layout_marginRight="35dp"
android:text="#string/p5p"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/p5f"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/fouls"
android:layout_alignRight="#+id/p5"
android:layout_marginRight="35dp"
android:text="#string/p5f"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/p6p"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/points"
android:layout_alignRight="#+id/p6"
android:layout_marginRight="35dp"
android:text="#string/p6p"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/p6f"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/fouls"
android:layout_alignRight="#+id/p6"
android:layout_marginRight="35dp"
android:text="#string/p6f"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/p7p"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/points"
android:layout_alignRight="#+id/p7"
android:layout_marginRight="35dp"
android:text="#string/p7p"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/p7f"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/fouls"
android:layout_alignRight="#+id/p7"
android:layout_marginRight="35dp"
android:text="#string/p7f"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/p8p"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/points"
android:layout_alignRight="#+id/p8"
android:layout_marginRight="35dp"
android:text="#string/p8p"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/p8f"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/fouls"
android:layout_alignRight="#+id/p8"
android:layout_marginRight="35dp"
android:text="#string/p8f"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/p9p"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/points"
android:layout_alignRight="#+id/p9"
android:layout_marginRight="35dp"
android:text="#string/p9p"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/p9f"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/fouls"
android:layout_alignRight="#+id/p9"
android:layout_marginRight="35dp"
android:text="#string/p9f"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/p10p"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/points"
android:layout_alignRight="#+id/p10"
android:layout_marginRight="35dp"
android:text="#string/p10p"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/p10f"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/fouls"
android:layout_alignRight="#+id/p10"
android:layout_marginRight="35dp"
android:text="#string/p10f"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="#+id/court"
android:layout_width="900dp"
android:layout_height="600dp"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/p10"
android:contentDescription="#string/undo"
android:src="#drawable/court" />
</RelativeLayout>
I mean it tells me the error occurs in the onCreate of the CourtActivity, however I can't see anything that's wrong. This is part of my school project, I'm only concerned with the things that are causing me errors currently, not too worried about anything else.
Note:This is a group project. The courtactivity screen is the only one I've not had a part in making, so I'm not sure exactly what went into it. However now I'm trying to put everything together and running into this.
Anything helps! Thanks!
The error is happening while attempting to load an image specified on the Layout file, as reported by the following part of the log:
...: Caused by: java.lang.OutOfMemoryError
...: E/AndroidRuntime(2137): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
...: E/AndroidRuntime(2137): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:503)
I bet if you remove the line android:background="#drawable/bg2court" from the root container (RelativeLayout) in the file activity_court.xml the error will be gone.
If that is the case I would suggest you to try a smaller (lower resolution) version of that image.
While looking for a replacement, keep an eye on the heap memory allocation messages on logcat so you will know how much progress you are making.
Loading high resolution images on Android is usually a tricky process because of the following reasons:
Bitmap files loaded in memory have to be decompressed so they can be rendered, making them a lot larger then their original .JPG and .PNG files. To calculate how many bytes of memory will be allocated to load a given image, just follow this simple formula: width (pixels) x height (pixels) x number bytes per pixel (3 for 24 bit images). Example: a bitmap with the size of 2048x2048 pixels takes exactly 12mb of memory;
Bitmaps are loaded into the heap space which happens to be a pretty small portion of memory given by the OS to your app when it starts. The size of the heap space usually ranges from 16 to 64mb depending on the device.
Finally, I would suggest one of the following approaches:
Replace the large background image by a lower resolution version that can be scaled up to fill the background space (at the cost of quality of corse);
Load bitmaps programmatically. There are BitmapFactory methods that can downsample images on the fly, during the load process. More on this here;
Use a 3rd party library to do it for you. Picasso is an excelent choice.
The error is coming from inflating the layout. It's an OutOfMemoryError. Do you have some really huge bitmap or more likely a circular dependency? Can you upload the xml file R.layout.activity_court?
I had this error no long ago but got some help from the guys in here
here is what I did
if your background / image don't need transpart stuff or like keep in JPG format as it will be smaller. this should sort your Out of Memory issue temp. but when you have more image coming up you will still have problem (Out of Memory) coming up again
hope this can help you for the time been

Calculator error in java (classes)

I'm trying to make a simple calculator app for android and i'm stuck now. I was planning to make 2 calculators (one basic and the other more advanced, similar to the one built in Windows). The idea was to make it in 3 different classes.
Two classes would contain the "main" code (buttons, layout) and the last one should contain the function which im planning to call (like add, multiply). I managed to finish both calculators in one class but i don't know how to make them call functions from the other class.
PROBLEM: I managed to create the basic calculator and it can call functions from the other class but if i want to add more calculations one after another it wont work right. What I mean is this: Let's say my first number is 1 and my second is 3. It prints the result 4 but if i click add again and put 2 it prints the result 5 instead of 6. Somehow it stores the second variable into the first one and i cannot figure out why :(
Can anyone help me?
Thanks in advance
enter code here
package com.example.simcalc;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainInterface extends Activity implements OnClickListener {
static EditText disp;
static TextView txt1, txt2;
SimFunctions sm;
TextView tx1;
Button btDot, bt1, bt2, bt3, bt4, bt5, bt6, bt7 ,bt8, bt9, bt0, btPlus, btMinus, btDivide, btMult,btEquals, btC;
float num2 =0 , res;
float num1 = 0;
String saveNumber = "";
static char sim ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_interface);
disp = (EditText) findViewById(R.id.enterNumber);
bt1 = (Button) findViewById(R.id.one);
bt2 = (Button) findViewById(R.id.two);
bt3 = (Button) findViewById(R.id.three);
bt4 = (Button) findViewById(R.id.four);
bt5 = (Button) findViewById(R.id.five);
bt6 = (Button) findViewById(R.id.six);
bt7 = (Button) findViewById(R.id.seven);
bt8 = (Button) findViewById(R.id.eight);
bt9 = (Button) findViewById(R.id.nine);
bt0 = (Button) findViewById(R.id.zero);
btPlus = (Button) findViewById(R.id.add);
btMinus = (Button) findViewById(R.id.sub);
btMult = (Button) findViewById(R.id.mult);
btDivide = (Button) findViewById(R.id.div);
btC = (Button) findViewById (R.id.can);
btDot = (Button) findViewById(R.id.decimal);
btEquals = (Button) findViewById(R.id.equal);
txt1 = (TextView) findViewById(R.id.textView1);
txt2 = (TextView) findViewById(R.id.textView2);
bt1.setOnClickListener(this);
bt2.setOnClickListener(this);
bt3.setOnClickListener(this);
bt4.setOnClickListener(this);
bt5.setOnClickListener(this);
bt6.setOnClickListener(this);
bt7.setOnClickListener(this);
bt8.setOnClickListener(this);
bt9.setOnClickListener(this);
bt0.setOnClickListener(this);
btPlus.setOnClickListener(this);
btMinus.setOnClickListener(this);
btDivide.setOnClickListener(this);
btMult.setOnClickListener(this);
btC.setOnClickListener(this);
btDot.setOnClickListener(this);
btEquals.setOnClickListener(this);
disp.setText("0");
sm = new SimFunctions();
}
#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_interface, menu);
return true;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.one:
saveNumber += "1";
disp.setText(saveNumber);
break;
case R.id.two:
saveNumber += "2";
disp.setText(saveNumber);
break;
case R.id.three:
saveNumber += "3";
disp.setText(saveNumber);
break;
case R.id.four:
saveNumber += "4";
disp.setText(saveNumber);
break;
case R.id.five:
saveNumber += "5";
disp.setText(saveNumber);
break;
case R.id.six:
saveNumber += "6";
disp.setText(saveNumber);
break;
case R.id.seven:
saveNumber += "7";
disp.setText(saveNumber);
break;
case R.id.eight:
saveNumber += "8";
disp.setText(saveNumber);
break;
case R.id.nine:
saveNumber += "9";
disp.setText(saveNumber);
break;
case R.id.zero:
saveNumber += "0";
disp.setText(saveNumber);
break;
case R.id.decimal:
if (saveNumber.contains(".")){
break;
}
else{
saveNumber += ".";
disp.setText(saveNumber);
break;
}
case R.id.mult:
sim = '*';
if (saveNumber != ""){
num1 = Float.parseFloat(saveNumber);
saveNumber = "";
break;}
else{
break;}
case R.id.add:
sim = '+';
if (saveNumber != ""){
num1 = Float.parseFloat(saveNumber);
saveNumber = "";
break;}
else{
break;}
case R.id.div:
sim = '/';
if (saveNumber != ""){
num1 = Float.parseFloat(saveNumber);
saveNumber = "";
break;}
else{
break;}
case R.id.sub:
sim = '-';
if (saveNumber != ""){
num1 = Float.parseFloat(saveNumber);
saveNumber = "";
break;}
else{
break;}
case R.id.equal:
sm.equals(num1, saveNumber, sim);
break;
case R.id.can:
saveNumber = "";
disp.setText(saveNumber);
break;
}
}
}
package com.example.simcalc;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class SimFunctions extends MainInterface {
public void equals(float num1, String saveNumber, char simbol){
String ds = "";
if ( saveNumber != "") {
try{
num2 = Float.parseFloat(saveNumber);}
catch(NumberFormatException e){
e.printStackTrace();
}
}
else {
num2 = 0;}
switch (simbol){
case ('*'):
res = num1 * num2;
num1 = res;
ds = Float.toString(res);
disp.setText(ds);
saveNumber = "";
ds = "";
break;
case ('+'):
ds = Float.toString(num1);
txt1.setText(ds);
ds = Float.toString(num2);
txt2.setText(ds);
res = num1 + num2;
num1 = res;
ds = Float.toString(res);
disp.setText(ds);
saveNumber = "";
ds = "";
break;
case ('/'):
res = num1 / num2;
num1 = res;
ds = Float.toString(res);
disp.setText(ds);
saveNumber = "";`enter code here`
ds = "";
break;
enter code here
case ('-'):
res = num1 - num2;
num1 = res;
ds = Float.toString(res);
disp.setText(ds);
saveNumber = "";
ds = "";
break;
}
}
}
<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:background="#color/testBlack"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainInterface" >
<EditText
android:id="#+id/enterNumber"
android:layout_width="match_parent"
android:layout_height="110dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#color/scoreColor"
android:inputType="numberDecimal" />
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/linearLayout2"
android:layout_alignParentBottom="true"
android:layout_marginBottom="86dp"
android:gravity="center"
android:orientation="horizontal"
android:paddingTop="20dp" >
<Button
android:id="#+id/seven"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:text="7" />
<Button
android:id="#+id/eight"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:text="8" />
<Button
android:id="#+id/nine"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:text="9" />
<Button
android:id="#+id/div"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:text="/" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/linearLayout1"
android:layout_alignLeft="#+id/enterNumber"
android:gravity="center"
android:orientation="horizontal"
android:paddingTop="20dp" >
<Button
android:id="#+id/four"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:text="4" />
<Button
android:id="#+id/five"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:text="5" />
<Button
android:id="#+id/six"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:text="6" />
<Button
android:id="#+id/mult"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:text="*" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/linearLayout2"
android:layout_below="#+id/enterNumber"
android:layout_marginTop="20dp"
android:gravity="center"
android:orientation="horizontal"
android:paddingTop="20dp" >
<Button
android:id="#+id/one"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:text="1" />
<Button
android:id="#+id/two"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:text="2" />
<Button
android:id="#+id/three"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:text="3" />
<Button
android:id="#+id/sub"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:text="-" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/linearLayout1"
android:layout_alignParentBottom="true"
android:gravity="center"
android:orientation="horizontal"
android:paddingTop="20dp" >
<Button
android:id="#+id/can"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:text="c" />
<Button
android:id="#+id/zero"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:text="0" />
<Button
android:id="#+id/equal"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:text="=" />
<Button
android:id="#+id/add"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:text="+" />
<Button
android:id="#+id/decimal"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:text="." />
</LinearLayout>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/linearLayout3"
android:layout_alignParentLeft="true"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/linearLayout3"
android:layout_toRightOf="#+id/textView1"
android:text="TextView" />
</RelativeLayout>
You are putting the result of the operation into the global var 'num1'.
The next time you press '+' you overwrite that value with the previous saveNumber value.
So your logic is like this:
1 -> savenumber = 1
+ -> num1 = 1
3 -> savenumber = 3
= -> num2 = savenumber
-> res = num1 + num2
-> num1 = res
+ -> num1 = savenumber = 3
2 -> savenumber = 2
= -> num2 = savenumber
-> res = num1 + num2
= 3 + 2

Categories

Resources