First off I need to tell you that I am new to java I have only been using it for a bout 3 weeks. I have am putting to gather a small app, this page (activity) has a bitmap image on it with 12 buttons each button is to invoke a different math equation, I have that bit working sofar. But have 3 problems, 1, the results TextView R.id.resultstextbox named = resultsText, dos not clear when a new button is pressed. I have tried declaring the TextView object as a global but get a runtime error. 2, the results are rounded and I need it to be 3 decimal places. 3, the buttons when placed over the bitmap image in graphical layout are all in the correct place, and this is also good when running in the emulator, but when testing on my phone the buttons have all moved, how do I fix this? I would appreciate any help I can get for this problem ? thanks
layout file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
android:paddingLeft="5dp"
android:paddingRight="5dp" >
<ImageView
android:id="@+id/ohms_wheel_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="175dp"
android:layout_marginTop="5dp"
android:contentDescription="@string/ohms_wheel_image"
android:src="@drawable/ohmslawwheel" />
<Button
android:id="@+id/button7"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/ohms_wheel_image"
android:layout_marginRight="14dp"
android:layout_toLeftOf="@+id/button6"
android:text="Button" />
<Button
android:id="@+id/button9"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button4"
android:layout_alignBottom="@+id/button4"
android:layout_alignLeft="@+id/ohms_wheel_image"
android:text="Button" />
<Button
android:id="@+id/button10"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/button3"
android:text="Button" />
<Button
android:id="@+id/button11"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button3"
android:layout_alignLeft="@+id/entervoltstexlable"
android:text="Button"
android:textColor="@android:color/transparent" />
<Button
android:id="@+id/button8"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button7"
android:layout_alignLeft="@+id/entervoltstexlable"
android:text="Button" />
<TextView
android:id="@+id/resultstextbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/enterampslable"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/button5"
android:layout_marginBottom="34dp"
android:clickable="false"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white" />
<TextView
android:id="@+id/displaytextbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/entervoltstexlable"
android:layout_alignLeft="@+id/entervoltstexlable"
android:layout_marginBottom="10dp"
android:layout_toLeftOf="@+id/button4"
android:clickable="false"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white" />
<Button
android:id="@+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/button2"
android:layout_marginTop="14dp"
android:text="Button" />
<Button
android:id="@+id/button6"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button7"
android:layout_alignBottom="@+id/button7"
android:layout_alignRight="@+id/button1"
android:text="Button" />
<Button
android:id="@+id/button4"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button3"
android:layout_below="@+id/button3"
android:text="Button" />
<Button
android:id="@+id/button5"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button6"
android:layout_toRightOf="@+id/button6"
android:text="Button" />
<Button
android:id="@+id/button12"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_toRightOf="@+id/button11"
android:text="Button" />
<TextView
android:id="@+id/entervoltstexlable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/enterampslable"
android:layout_alignLeft="@+id/ohms_wheel_image"
android:layout_alignParentEnd="false"
android:layout_alignParentStart="false"
android:layout_marginBottom="15dp"
android:layout_marginLeft="14dp"
android:clickable="false"
android:text="@string/volts"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white" />
<TextView
android:id="@+id/enterampslable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/resultstextbox"
android:layout_alignLeft="@+id/entervoltstexlable"
android:layout_marginBottom="14dp"
android:clickable="false"
android:text="@string/amps"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_alignBaseline="@+id/enterampslable"
android:layout_alignBottom="@+id/enterampslable"
android:layout_alignLeft="@+id/enterampslable"
android:layout_alignParentRight="false"
android:layout_alignRight="@+id/ohms_wheel_image"
android:layout_marginLeft="240dp"
android:ems="10"
android:height="25dp"
android:inputType="number"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_alignBaseline="@+id/entervoltstexlable"
android:layout_alignBottom="@+id/entervoltstexlable"
android:layout_alignLeft="@+id/entervoltstexlable"
android:layout_alignRight="@+id/editText2"
android:layout_marginLeft="240dp"
android:ems="10"
android:gravity="center_vertical"
android:height="25dp"
android:inputType="number"
android:textAppearance="?android:attr/textAppearanceSmall" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/ohms_wheel_image"
android:layout_marginLeft="16dp"
android:layout_marginTop="14dp"
android:layout_toRightOf="@+id/button12"
android:background="@android:color/transparent"
android:text="@string/ohmsButton" />
<Button
android:id="@+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/button5"
android:layout_alignTop="@+id/button1"
android:layout_marginTop="22dp"
android:background="@null"
android:text="@string/ohmsButton"
android:textColor="@android:color/transparent" />
</RelativeLayout>
Activity
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Ohms_Law extends Activity implements OnClickListener{
long lg1 = 0;
long lg2 = 0;
long ans;
EditText input1;
EditText input2;
int btnNo;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.ohms_wheel_page);
//Find and define labels and display text box's
//TextView resultsText = (TextView) findViewById(R.id.resultstextbox);
input2 = (EditText) findViewById(R.id.editText2);
//EditText input2 = (EditText) findViewById(R.id.editText2);
input1 = (EditText) findViewById(R.id.editText1);
input2.addTextChangedListener(new TextWatcher() {
TextView resultsText = (TextView) findViewById(R.id.resultstextbox);
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start,int before, int count) {
//get user data
String strValue1 = input1.getText().toString();
String strValue2 = input2.getText().toString();
try {
//convert to long
lg1 = Long.parseLong(strValue1);
// lg1 = Long.parseLong(getText(s));
lg2 = Long.parseLong(strValue2);
}
catch (NumberFormatException nfe)
{
System.out.println("NumberFormatException: " + nfe.getMessage());
}
//Select which buttin pressed
switch(btnNo)
{
case 1:
//test has anything been enterd
if(lg1 > 0 && lg2 > 0)
{
//do math
ans = lg1 / lg2;
}
else
{
//Disply results
resultsText.setText("");
}
break;
case 2:
//test has anything been enterd
if(lg1 > 0 && lg2 > 0)
{
//do math
ans = lg1 * lg2;
}
else
{
//Disply results
resultsText.setText("");
}
break;
}
//results convert toString
Long.toString(ans);
String str = null;
str = str.valueOf(ans);
//Disply results
resultsText.setText("Results = " + str + " Volts");
}
});
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
TextView voltsText =(TextView) findViewById(R.id.entervoltstexlable);
TextView ampsText = (TextView) findViewById(R.id.enterampslable);
TextView displayText = (TextView) findViewById(R.id.displaytextbox);
TextView resultsText2 = (TextView) findViewById(R.id.resultstextbox);
switch(v.getId()){
case R.id.button1:
//Button id No
btnNo = 1;
//change Calc Didplay
displayText.setText("Power / Current = Volts");
//chang display data
voltsText.setText("Enter Power in Watts");
//change Calc Didplay
//chang didplay text
ampsText.setText("Enter Amps.");
//clear results
resultsText2.setText("");
//Clear input text
input1.setText("");
input2.setText("");
//Set focust to first edittext box
input1.requestFocus();
// math P/A (lg1 / lg2) = voltys
break;
case R.id.button2:
//Button id No
btnNo = 2;
//change Calc Didplay
displayText.setText("Amps x ohms = Volts");
//chang display data
voltsText.setText("Emter Amps");
//chang didplay text
ampsText.setText("Enter Resistance in ohms.");
//clear results
resultsText2.setText("");
//Clear input text
input1.setText("");
input2.setText("");
//Set focust to first edittext box
input1.requestFocus();
//Math A*R = volts
break;
}
}
}