Android TextViews not Updating With New Text in onCreate Method of Activity - java

I am adding TextViews to a simple application, running some code to determine what text should show in each one, then displaying it based on what a user previously entered in a bundle.
The problem is no text is showing up at all!
I am running code in the onCreate method that determines what text should show based on passed in values from a previous activity in a bundle. The values seem to pass in without an error and the other Textviews in the activity reflect the changes, but not the income, expenses or incomeExpenses TextView.
Here is my code:
package androidbro.costoflivingcalculator;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import java.io.*;
import java.io.Console;
public class FinalGrade extends ActionBarActivity {
//Grade and message strings
public String myGrade;
public String finalMessage;
//Doubles for calculated values
public double rentMortgage;
public double utilities;
public double insurance;
public double phoneInternet;
public double food;
public double carPayment;
public double misc;
public double myIncome;
public double totalExpenses;
public double expensesToIncome;
//Calculate grade based on expenses to income ratio
public String getGrade(double expensesToIncome) {
String grade = "A";
if (expensesToIncome<=0.3) {
grade = "A";
} else if (expensesToIncome<=0.4){
grade = "B";
} else if (expensesToIncome<=0.6) {
grade = "C";
} else if (expensesToIncome<=0.8) {
grade = "D";
} else {
grade = "F";
}
return grade;
}
//Determine final message
public String getFinalMessage(String grade){
String finalMessage;
if (grade == "A"){
finalMessage = "You are living well within your means! You could probably afford to spend more. Either way, great work!";
} else if(grade == "B"){
finalMessage = "You are doing great! Your expenses are well below your income. There are a few areas to improve, but good work!";
} else if(grade == "C") {
finalMessage = "Not too bad, but not great either. You might be in trouble if you lost your income. Try to improve a little!";
} else if(grade == "D") {
finalMessage = "Uh oh! You are spending well over half the money you earn! Consider saving and investing more or it could be trouble.";
} else {
finalMessage = "Oh no! You are spending almost all your income! Save a little and live within your means or else you'll be broke!";
}
return finalMessage;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_final_grade);
//Income, expenses and bundles
Bundle b = getIntent().getExtras();
rentMortgage = b.getDouble("rentmortgage");
utilities = b.getDouble("utilities");
insurance = b.getDouble("insurance");
phoneInternet = b.getDouble("car");
food = b.getDouble("phone");
carPayment = b.getDouble("food");
misc = b.getDouble("misc");
myIncome = b.getDouble("myincome");
totalExpenses = rentMortgage+utilities+insurance+phoneInternet+food+carPayment+misc;
expensesToIncome = totalExpenses/myIncome;
//Set final message and grade values
myGrade = getGrade(expensesToIncome);
finalMessage = getFinalMessage(myGrade);
//TextViews for setting values
TextView gradeView = (TextView)findViewById(R.id.gradeView);
TextView messageView = (TextView)findViewById(R.id.messageView);
TextView income = (TextView)findViewById(R.id.income);
TextView expenses = (TextView)findViewById(R.id.expenses);
TextView incomeExpenses = (TextView)findViewById(R.id.incomeExpenses);
//Display final grade and message
gradeView.setText(myGrade);
messageView.setText(finalMessage);
income.setText(Double.toString(myIncome));
expenses.setText(Double.toString(totalExpenses));
incomeExpenses.setText(Double.toString(expensesToIncome));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_final_grade, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
//Display expenses, income and expenses to income percentage on final grade
}
And here is my XML:
<ScrollView
android:layout_width="fill_parent"
android:id="#+id/scrollView"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="ericleeconklin.costoflivingcalculator.FinalGrade">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your Grade:"
android:id="#+id/textView2"
android:textSize="40dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="A"
android:id="#+id/gradeView"
android:textSize="85dp"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="You are living well within your means! You could probably afford to spend more. Either way, great work!"
android:id="#+id/messageView"
android:layout_below="#+id/gradeView"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Monthly Income:"
android:id="#+id/yourIncome"
android:layout_below="#+id/messageView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:paddingTop="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/income"
android:paddingTop="20dp"
android:background="#ff77ff75"
android:width="180dp"
android:height="25dp"
android:layout_alignBottom="#+id/yourIncome"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:text="sdsdfsd"
android:textColor="#color/abc_background_cache_hint_selector_material_dark"
android:textIsSelectable="true"
android:textSize="23dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Expenses:"
android:id="#+id/yourExpenses"
android:paddingTop="20dp"
android:layout_below="#+id/income"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="#+id/yourIncome"
android:layout_alignEnd="#+id/yourIncome"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/incomeExpenses"
android:paddingTop="20dp"
android:background="#ff77ff75"
android:width="180dp"
android:height="25dp"
android:layout_alignBottom="#+id/textView4"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:text="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/expenses"
android:paddingTop="20dp"
android:background="#ff77ff75"
android:width="180dp"
android:height="25dp"
android:layout_alignBottom="#+id/yourExpenses"
android:layout_alignLeft="#+id/incomeExpenses"
android:layout_alignStart="#+id/incomeExpenses"
android:text="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Expenses/Income"
android:id="#+id/textView4"
android:paddingTop="20dp"
android:layout_below="#+id/yourExpenses"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/incomeExpenses"
android:layout_toStartOf="#+id/incomeExpenses"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Here are some tips:"
android:id="#+id/textView5"
android:paddingTop="350dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
</ScrollView>

Instead of this:
income.setText(Double.toString(myIncome));
expenses.setText(Double.toString(totalExpenses));
incomeExpenses.setText(Double.toString(expensesToIncome));
Do this:
income.setText(String.valueOf(myIncome);
expenses.setText(String.valueOf(totalExpenses));
incomeExpenses.setText(String.valueOf(expensesToIncome));
I have updated my answer
For passing double value between Activity:
Intent inent = new Intent(thisActivity.this, nextActivity.class);
Bundle b = new Bundle();
b.putDouble("myincome", myIncome); // myIncome is your double value here
intent.putExtras(b);
startActivity(intent);
To get value in next Activity:
Bundle b = getIntent().getExtras();
double myIncome = b.getDouble("myincome");
Check your procedure of passing double value, I have mentioned it above
Edited XML Layout:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/tv_BigHeading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="Your Grade:"
android:textSize="40sp" />
<TextView
android:id="#+id/tv_Grade"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_BigHeading"
android:layout_centerHorizontal="true"
android:text="A"
android:textSize="80sp"
android:textStyle="bold" />
<TextView
android:id="#+id/messageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_Grade"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="You are living well within your means! You could probably afford to spend more. Either way, great work!"
android:textSize="14sp" />
<TextView
android:id="#+id/yourIncome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/messageView"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="Monthly Income:"
android:textColor="#000000"
android:textSize="14sp" />
<TextView
android:id="#+id/incomeValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/messageView"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:layout_toRightOf="#+id/yourIncome"
android:background="#ff77ff75"
android:text="1000 $"
android:textColor="#000000"
android:textSize="14sp" />
<TextView
android:id="#+id/yourExpenses"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/yourIncome"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="20dp"
android:text="Expenses:"
android:textColor="#000000"
android:textSize="14sp" />
<TextView
android:id="#+id/expenseValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/incomeValue"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:layout_toRightOf="#+id/yourExpenses"
android:background="#ff77ff75"
android:text="1000 $"
android:textColor="#000000"
android:textSize="14sp" />
<TextView
android:id="#+id/Expenses_Income"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/yourExpenses"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="20dp"
android:text="Expenses/Income:"
android:textColor="#000000"
android:textSize="14sp" />
<TextView
android:id="#+id/Expenses_Income_Value"
android:layout_width="wrap_content"
android:layout_height="16dp"
android:layout_below="#+id/expenseValue"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:layout_toRightOf="#+id/Expenses_Income"
android:background="#ff77ff75"
android:text="1000 $"
android:textColor="#000000"
android:textSize="14sp" />
<TextView
android:id="#+id/tips"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/Expenses_Income"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="20dp"
android:text="Here are some tips:"
android:textColor="#000000"
android:textSize="16sp" />
</RelativeLayout>
</ScrollView>
This will solve your issue now, and use all my recommendations e.g: income.setText(String.valueOf(myIncome);
for setting the textview values

Related

How to cycle through letters in a multi touch interface?

I am creating an android application for an old style cell phone where you click numbers multiple times and cycle through letters on each click. Then, you commit that letter using a dedicated button.
So far I can cycle through the letters for a given digit (i.e. the number 2 cycles through abc) but cannot figure out how to commit a letter to a textview and move on to the next button press.
The code I have written is rather messy and I feel like it could be simplified quite a bit. I haven't been stuck like this in a while.
I will include the code I have so far. Any help is appreciated!
public class MainActivity extends AppCompatActivity {
Button one, two, three, four, five, six, seven, eight, nine, zero;
Button commit;
TextView input;
int clicks = 0;
String finalInput = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input = findViewById(R.id.input);
commit = findViewById(R.id.commit);
one = findViewById(R.id.one);
two = findViewById(R.id.abc);
three = findViewById(R.id.def);
four = findViewById(R.id.ghi);
five = findViewById(R.id.jkl);
six = findViewById(R.id.mno);
seven = findViewById(R.id.pqrs);
eight = findViewById(R.id.tuv);
nine = findViewById(R.id.wxyz);
zero = findViewById(R.id.zero);
two.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
clicks ++;
if(clicks == 1){
finalInput = input.getText().toString();
if(finalInput.length() != 0){
finalInput = finalInput.substring(0, finalInput.length() -1);
input.setText(finalInput);
}
input.setText(finalInput + "a");
}else if(clicks == 2){
finalInput = input.getText().toString();
finalInput = finalInput.substring(0, finalInput.length() -1);
input.setText(finalInput);
System.out.println(finalInput);
input.setText(finalInput + "b");
}else{
finalInput = input.getText().toString();
finalInput = finalInput.substring(0, finalInput.length() -1);
input.setText(finalInput);
input.setText(finalInput + "c");
clicks = 0;
}
}
});
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.243" />
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:gravity="center">
<Button
android:id="#+id/one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:textSize="20dp" />
<Button
android:id="#+id/abc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:text="2"
android:textSize="20dp" />
<Button
android:id="#+id/def"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:text="3"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:gravity="center">
<Button
android:id="#+id/ghi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:textSize="20dp" />
<Button
android:id="#+id/jkl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:text="5"
android:textSize="20dp" />
<Button
android:id="#+id/mno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:text="6"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:gravity="center">
<Button
android:id="#+id/pqrs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7"
android:textSize="20dp" />
<Button
android:id="#+id/tuv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:text="8"
android:textSize="20dp" />
<Button
android:id="#+id/wxyz"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:text="9"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:gravity="center">
<Button
android:id="#+id/zero"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/default_gap"
android:text="0"
android:textSize="20dp" />
<Button
android:id="#+id/commit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Commit" />
</LinearLayout>
</GridLayout>
</androidx.constraintlayout.widget.ConstraintLayout>[![enter image description here][2]][2]
[1]: https://i.stack.imgur.com/cnagW.jpg
[2]: https://i.stack.imgur.com/L99Ya.png

It does not total the value given by total=i1+i2+i3+i4+i5; t17.setText(String.valueOf(total));it always show 0 at t17

java
there is problem in total.I have already tried try and catch.It always
show 0 at t17.looks like it does not set the total value.
It does not total the value given by total=i1+i2+i3+i4+i5;
t17.setText(String.valueOf(total));it always show 0 at t17
package com.example.myproject1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
public class Bill extends AppCompatActivity {
TextView t,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14,t15,t16,t17;
Button b,b1;
int total;
int i1,i2,i3,i4,i5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bill);
t=findViewById(R.id.textView);
t1=findViewById(R.id.textView5);
t2=findViewById(R.id.textView10);
t3=findViewById(R.id.textView15);
t4=findViewById(R.id.textView6);
t5=findViewById(R.id.textView11);
t6=findViewById(R.id.textView16);
t7=findViewById(R.id.textView7);
t8=findViewById(R.id.textView12);
t9=findViewById(R.id.textView17);
t10=findViewById(R.id.textView8);
t11=findViewById(R.id.textView13);
t12=findViewById(R.id.textView18);
t13=findViewById(R.id.textView9);
t14=findViewById(R.id.textView14);
t15=findViewById(R.id.textView19);
t16=findViewById(R.id.textView20);
t17=findViewById(R.id.textView21);
b=findViewById(R.id.button22);
b1=findViewById(R.id.button23);
Intent obj2 = getIntent();
String r = obj2.getStringExtra("k");
t.setText(r);
String res = obj2.getStringExtra("key");
String res1 = obj2.getStringExtra("key2");
String res2 = obj2.getStringExtra("key3");
String res3 = obj2.getStringExtra("key4");
String res4 = obj2.getStringExtra("key5");
String res5 = obj2.getStringExtra("key6");
String res6 = obj2.getStringExtra("key7");
String res7 = obj2.getStringExtra("key8");
String res8 = obj2.getStringExtra("key9");
String res9 = obj2.getStringExtra("key10");
String res10 = obj2.getStringExtra("key11");
String res11 = obj2.getStringExtra("key12");
String res12 = obj2.getStringExtra("key13");
String res13 = obj2.getStringExtra("key14");
String res14 = obj2.getStringExtra("key15");
t1.setText(res);
t2.setText(res1);
t3.setText(res2);
t4.setText(res3);
t5.setText(res4);
t6.setText(res5);
t7.setText(res6);
t8.setText(res7);
t9.setText(res8);
t10.setText(res9);
t11.setText(res10);
t12.setText(res11);
t13.setText(res12);
t14.setText(res13);
t15.setText(res14);
try{
if (res2 != null) {
i1 = Integer.parseInt(res2);
} else {
i1 = 0;
}
if (res5 != null) {
i2 = Integer.parseInt(res5);
} else {
i2 = 0;
}
if (res8 != null) {
i3 = Integer.parseInt(res8);
} else {
i3 = 0;
}
if (res11 != null) {
i4 = Integer.parseInt(res11);
} else {
i4 = 0;
}
if (res14 != null) {
i5 = Integer.parseInt(res14);
} else {
i5 = 0;
}
}catch (NumberFormatException e){
}
t16.setText("Total Price:");
total=i1+i2+i3+i4+i5;
t17.setText(String.valueOf(total));
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent obj = new Intent(Bill.this, Menu.class);
startActivity(obj);
}
});
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent obj1 = new Intent(Bill.this, MainActivity.class);
startActivity(obj1);
}
});
}
}
XML
the xml of my code is given as below though it has nothing to do with
my coding part.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/bill4"
tools:context=".Bill">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="224dp"
android:layout_marginTop="5dp"
android:background="#92278F"
android:gravity="center"
android:text="user"
android:textColor="#0066FF"
android:textSize="24sp"
android:textStyle="bold"
app:fontFamily="#font/merienda_one"
tools:layout_editor_absoluteX="161dp"
tools:layout_editor_absoluteY="64dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:background="#CD81DB"
android:fontFamily="#font/joti_one"
android:text="Bill"
android:textColor="#111111"
android:textSize="#android:dimen/app_icon_size"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:layout_marginTop="60dp"
android:background="#92278F"
android:gravity="center"
android:paddingLeft="25dp"
android:paddingRight="25dp"
android:text="Item"
android:textColor="#0066FF"
android:textSize="24dp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="13dp"
android:layout_marginTop="60dp"
android:background="#92278F"
android:gravity="center"
android:paddingLeft="25dp"
android:paddingRight="25dp"
android:text="Quantity"
android:textColor="#0066FF"
android:textSize="24dp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="60dp"
android:background="#92278F"
android:gravity="center"
android:paddingLeft="25dp"
android:paddingRight="25dp"
android:text="Price"
android:textColor="#0066FF"
android:textSize="24dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:orientation="vertical">
<TextView
android:id="#+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#111111"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#111111"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#111111"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#111111"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView9"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#111111"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:orientation="vertical">
<TextView
android:id="#+id/textView10"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#111111"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#111111"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView12"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#111111"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView13"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#111111"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView14"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#111111"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="120dp"
android:orientation="vertical">
<TextView
android:id="#+id/textView15"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#111111"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView16"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#111111"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView17"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#111111"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView18"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#111111"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView19"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#111111"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/textView20"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginTop="20dp"
android:textColor="#111111"
android:text=""
android:textSize="14dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView21"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="200dp"
android:layout_marginTop="20dp"
android:textColor="#111111"
android:textSize="14dp"
android:textStyle="bold"
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/button22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginTop="20dp"
android:background="#92278F"
android:textColor="#0066FF"
android:textStyle="bold"
app:fontFamily="#font/merienda_one"
android:text="Cancel" />
<Button
android:id="#+id/button23"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="165dp"
android:layout_marginTop="20dp"
android:background="#92278F"
android:textColor="#0066FF"
android:textStyle="bold"
app:fontFamily="#font/merienda_one"
android:text="Order" />
</LinearLayout>
</LinearLayout>
I have used Sharedpreferences instead of intent however it is possible
from both.
Data recieving class
public class class_name extends AppCompatActivity {
SharedPreferences sharedPref;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bill);
sharedPref=this.getSharedPreferences("calis",Context.MODE_PRIVATE);
String res = sharedPref.getString("k1",null);
String res1 = sharedPref.getString("k2",null);
String res2 = sharedPref.getString("k3",null);
Integer res15 = sharedPref.getInt("k4",0);
Integer vres15 = sharedPref.getInt("k5",0);
int total2 = res15+vres15;
t17.setText(String.valueOf(total2));
}
}
Data sending class
public class class_name2 extends Fragment {
SharedPreferences sp;
sp=this.getActivity().getSharedPreferences("calis", Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sp.edit();
editor.putString("k1",value1);
editor.putInt("k4", value2);
editor.commit();
}

can't find reason why I can't read from editText

In the code below, when the 'submit' button is pushed, I'm trying to get the values from three editTexts. After I get the String values, I want to convert them to int values and check whether they are in the specified range.
However I am having trouble getting the values from editText. I'm not sure if this is the problem or if there is another even bigger flaw with my code.
When I enter the three editTexts and press the 'submit' button, the app stops with the 'submit' button pressed down. It seems as if it's in some sort of loop - just a guess. So, the toast msg doesn't appear on my screen(Even after I changed my code with what #sunnyday mentioned). The app just seems stalled. It won't even go back to the previous activity.
I am just starting Android and any suggestions would be greatly appreciated.
Java Code:
package activities;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import com.example.chloe.myapplication.R;
public class PayActivity extends ActionBarActivity implements View.OnClickListener {
Button submitButton, flip;
Switch switchButton;
EditText et_totalPeople, et_payPeople, et_amount;
TextView tv_payPeople, tv_people;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pay_numofpeople);
submitButton = (Button) findViewById(R.id.submitButton);
switchButton = (Switch)findViewById(R.id.switchButton);
et_totalPeople= (EditText) findViewById(R.id.et_totalPeople);
et_payPeople= (EditText) findViewById(R.id.et_payPeople);
et_amount = (EditText) findViewById(R.id.et_amount);
switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked == true) {
et_payPeople.setEnabled(false);
et_payPeople.setClickable(false);
} else {
et_payPeople.setEnabled(true);
et_payPeople.setClickable(true);
}
}
});
submitButton.setOnClickListener(this);
}
public void onClick(View v) {
switch(v.getId()){
case R.id.submitButton:
int people = Integer.parseInt(et_totalPeople.getText().toString());
//tested with toast here.. didn't work, which means it can't even read from editText
Toast.makeText(getApplicationContext(), people, Toast.LENGTH_LONG).show();
while(people<1 || people>100) {
Toast.makeText(getApplicationContext(), "Enter values 1~100", Toast.LENGTH_LONG).show();
et_totalPeople.setSelectAllOnFocus(true);
people = Integer.parseInt(et_totalPeople.getText().toString());
}
if(switchButton.isChecked()==false) { /*Only certain people pay*/
int payer = Integer.parseInt(et_payPeople.getText().toString());
while(payer<1 || payer>100) {
Toast.makeText(getApplicationContext(), "Enter values 1~100", Toast.LENGTH_LONG).show();
et_payPeople.setSelectAllOnFocus(true);
payer = Integer.parseInt(et_payPeople.getText().toString());
}
}
int amount = Integer.parseInt(et_amount.getText().toString());
while(amount<1 || amount>10000000) {
Toast.makeText(getApplicationContext(), "Enter values 1~10,000,000", Toast.LENGTH_LONG).show();
et_amount.setSelectAllOnFocus(true);
amount = Integer.parseInt(et_amount.getText().toString());
}
/*now all three values are valid, continue*/
break;
}
}
}
xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/restaurant2_lighter"
android:layout_gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_gravity="center"
android:orientation="vertical">
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#6effc118"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Everyone pays: "
android:textSize="15dp"
android:textColor="#ffffff"/>
<Switch
android:id="#+id/switchButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="NO"
android:textOn="YES"
android:textSize="15dp"/>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#6effc118"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter\nthe number\nof people"
android:textSize="15dp"
android:textColor="#ffffffff"
android:layout_marginRight="40dp"/>
<EditText
android:id="#+id/et_totalPeople"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="ex) 1~99 "
android:layout_gravity="center"
android:background="#4be3cc86"
android:textColor="#ffffffff"
android:padding="10dp"
android:inputType="numberDecimal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" people"
android:textColor="#ffffffff"
android:layout_gravity="center"/>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#6effc118"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:id="#+id/tv_payPeople"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter\nthe number\nof people\nto pay"
android:textColor="#ffffffff"
android:layout_marginRight="40dp"
android:textSize="15dp"/>
<EditText
android:id="#+id/et_payPeople"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="ex) 1~99 "
android:layout_gravity="center"
android:background="#4be3cc86"
android:textColor="#ffffffff"
android:padding="10dp"
android:inputType="numberDecimal" />
<TextView
android:id="#+id/tv_people"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" people"
android:textColor="#ffffffff"
android:layout_gravity="center"/>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#6effc118"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter\nthe total\namount"
android:textSize="15dp"
android:textColor="#ffffffff"
android:layout_marginRight="40dp"/>
<EditText
android:id="#+id/et_amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="ex) 50000 "
android:layout_gravity="center"
android:background="#4be3cc86"
android:textColor="#ffffffff"
android:padding="10dp"
android:inputType="numberDecimal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" won"
android:textColor="#ffffffff"
android:layout_gravity="center"/>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#6effc118"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<Button
android:id="#+id/submitButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="submit"
android:padding="10dp"
android:textSize="20dp"
android:layout_marginTop="30dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Thank you for your time :)
You get stuck in an endless loop in your validations as soon as a wrong value is entered. Lets say I enter 101 for people then you end up in this loop as soon as you hit the submit button.
while(people<1 || people>100) {
Toast.makeText(getApplicationContext(), "Enter values 1~100", Toast.LENGTH_LONG).show();
et_totalPeople.setSelectAllOnFocus(true);
people = Integer.parseInt(et_totalPeople.getText().toString());
}
The reason is, that your while loop runs in the UI thread of your app and therefore you cannot enter a different value, because for that to be possible the edit text field would need the UI thread for itself.
To solve this, do something like this (in pseudo code):
onclick() {
if (people < 1 || people > 100) {
showToast();
return;
}
if (other-validation-condition == false) {
showOtherToast();
return;
}
if (yet-another-validation-condition == false) {
showYetAnotherToast();
return;
}
// now, everything should be valid...
continueDoingStuffWithValidatedValues();
}
Toast.makeText(getApplicationContext(), people, Toast.LENGTH_LONG).show();
Must be :
Toast.makeText(getApplicationContext(), String.valueOf(people), Toast.LENGTH_LONG).show();
Toast.makeText(Context, int, long). At there, int is resource id.

How to use spinners in android?

I am new to android. I am trying to make simple unit converter. The layout contains EditText for input, two spinners (to and from), an ImageButton and EditText to display answer.
I want to add values to spinners and when these values are selected i want to perform action.
My xml code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".BitsBytes" >
<Spinner
android:id="#+id/SpinnerFrom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_alignRight="#+id/inputvalue"
android:layout_below="#+id/textView2"
android:entries="#array/bitsbytesfrom"
android:prompt="#string/from_prompt" />
<Spinner
android:id="#+id/SpinnerTo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView3"
android:layout_alignRight="#+id/SpinnerFrom"
android:layout_below="#+id/textView3"
android:entries="#array/bitsbytesto"
android:prompt="#string/to_prompt" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/SpinnerFrom"
android:layout_below="#+id/SpinnerFrom"
android:layout_marginTop="16dp"
android:text="To:"
android:textColor="#000000"
android:textSize="20dp" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="Value: "
android:textSize="20dp" />
<EditText
android:id="#+id/inputvalue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView6"
android:layout_alignBottom="#+id/textView6"
android:layout_toRightOf="#+id/textView6"
android:ems="10"
android:hint="Enter value"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView6"
android:layout_below="#+id/inputvalue"
android:layout_marginTop="16dp"
android:text="From:"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp"
android:text="Bits/Bytes Conversion"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="25dp" />
<ImageButton
android:id="#+id/convertButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/SpinnerTo"
android:layout_centerHorizontal="true"
android:background="#00000000"
android:src="#drawable/btnconvert"
android:clickable="true"/>
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/answer"
android:layout_centerHorizontal="true"
android:text="Answer"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/answer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="28dp"
android:ems="10"
android:inputType="numberDecimal" />
</RelativeLayout>
My java code:
package com.easy.convert;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;
public class BitsBytes extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.bitsbytes);
ImageButton ib = (ImageButton) findViewById(R.id.convertButton);
ib.setOnClickListener(new View.OnClickListener() {
EditText etValue = (EditText) findViewById(R.id.inputvalue);
Spinner spBitsBytesFrom = (Spinner) findViewById(R.id.SpinnerFrom);
Spinner spBitsBytesTo = (Spinner) findViewById(R.id.SpinnerTo);
String txtFromSpinner = spBitsBytesFrom.getSelectedItem().toString();
String txtToSpinner = spBitsBytesTo.getSelectedItem().toString();
TextView Answer = (TextView) findViewById(R.id.answer);
public void onClick(View v)
{
if (txtFromSpinner.equals("Bits") && txtToSpinner.equals("Bytes"))
{
Double value = Double.parseDouble(etValue.getText().toString());
Double answer = (double) 0;
answer = value/8;
String stringAnswer = Double.toString(answer);
Answer.setText(stringAnswer);
}
}
});
}
}
I am filling the spinners using string array. I am not able to figure out whats wrong in my code because there is no error in logcat.
Change this,
(txtFromSpinner.equals("Bits") && txtToSpinner.equals("Bytes"))
to
if((txtFromSpinner.getSelectedItem().toString().equals("Bits")) && (txtToSpinner.getSelectedItem().toString().equals("Bytes")))
this will get the text from the item selected in the spinner.

LogCat error Error opening trace file: no such file or directory

Sorry if this is a very noob question, but the code below is not giving an output and I the logcat view isn't clear so I can't tell how far it is getting when I run the emulator.
Have I got the radio buttons coded correctly?
Are the variable statements coded correctly?
Can you see any other errors?
Layout xml is as follows:
<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=".MainActivity" >
<EditText
android:id="#+id/editText4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText3"
android:layout_below="#+id/editText3"
android:layout_marginTop="27dp"
android:ems="10" />
<EditText
android:id="#+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText2"
android:layout_below="#+id/editText2"
android:layout_marginTop="19dp"
android:ems="10" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText1"
android:layout_below="#+id/editText1"
android:layout_marginTop="16dp"
android:ems="10" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="21dp"
android:ems="10" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editText1"
android:layout_toRightOf="#+id/editText1"
android:text="#string/pressu"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/editText3"
android:layout_toRightOf="#+id/editText2"
android:text="#string/volu"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editText3"
android:layout_alignLeft="#+id/textView2"
android:text="#string/molu"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editText4"
android:layout_alignLeft="#+id/textView3"
android:text="#string/tempu"
android:textAppearance="?android:attr/textAppearanceSmall" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText4"
android:layout_below="#+id/editText4"
android:layout_marginTop="35dp"
android:text="#string/calc" />
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/button1"
android:layout_marginRight="33dp" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/pressure" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/vol" />
<RadioButton
android:id="#+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/mol" />
<RadioButton
android:id="#+id/radio3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/temp" />
</RadioGroup>
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button1"
android:layout_below="#+id/button1"
android:layout_marginTop="22dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
The Java for the main activity is here
package com.minichanic.idealgas;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
private EditText pressure;
private EditText volume;
private EditText moles;
private EditText temperature;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pressure = (EditText) findViewById(R.id.editText1);
volume = (EditText) findViewById(R.id.editText2);
moles = (EditText) findViewById(R.id.editText3);
temperature= (EditText) findViewById(R.id.editText4);
}
public void onClick(View view) {
//set values to the input
float pressuren = Float.parseFloat(pressure.getText().toString());
float volumen = Float.parseFloat(volume.getText().toString());
float molesn = Float.parseFloat(moles.getText().toString());
float temperaturen = Float.parseFloat(temperature.getText().toString());
//change values to standard conditions if blank
if (pressuren == 0) {pressuren = 100000;
}
if (volumen == 0) {volumen = (float)0.0247;
}
if (molesn == 0) {molesn= 1;
}
if (temperaturen == 0) {temperaturen = 298;
}
//set input values as answers for set text later
float pressans = (float) pressuren;
float volans = (float) volumen;
float moleans = (float) molesn;
float temperaturans = (float) temperaturen;
//set calculation depending on radio button
switch (view.getId()) {
case R.id.radio0:
pressans = (float) (molesn * 8.31 * temperaturen ) / volumen;
break;
case R.id.radio1:
volans = (float) (molesn * 8.31 * temperaturen ) / pressuren;
break;
case R.id.radio2:
moleans = (float) ((float) (pressuren * volumen)/(8.31 * temperaturen));
break;
case R.id.radio3:
temperaturans = (float) ((float) (pressuren * volumen)/(8.31 * molesn));
break;
}
//print values in the EditText fields
pressure.setText(""+pressans);
volume.setText(""+volans);
moles.setText(""+moleans);
temperature.setText(""+temperaturans);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
The logCat i am getting for the package is:
Error opening trace file: no such file or directory
Any ideas will be greatly appreciated.
Its a system level error log and it does not affect execution of your application, so you don't have to worry about it.
Generally Its occurs Jelly Bean (android 4.1).And mainly its because of some SD-card issue.

Categories

Resources