Android simple Interest Calculator - java

I'm starter programmer on Android environment. And I need your help.
Here is the what I'm trying to do
package com.codeherenow.sicalculator;
import com.codeherenow.sicalculator.R;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
public class SICalculatorActivity extends Activity {
private TextView PA;
private TextView Interest_Rate;
private TextView Years;
private EditText PA_bar;
private EditText IR_bar;
private SeekBar year_bar;
private Button calculate;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sicalculator);
PA = (TextView) findViewById(R.id.PA);
Interest_Rate = (TextView) findViewById(R.id.Interest_Rate);
Years= (TextView) findViewById(R.id.Years);
PA_bar= (EditText) findViewById(R.id.PA_bar);
IR_bar= (EditText) findViewById(R.id.IR_bar);
year_bar=(SeekBar) findViewById(R.id.year_bar);
calculate=(Button) findViewById(R.id.calculate);
calculate.setOnClickListener(new OnClickListener(){
public void onClick(View v){
}
}
}
}
My 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=".SICalculatorActivity" >
<TextView
android:id="#+id/Years"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/IR_bar"
android:layout_centerVertical="true"
android:text="2 Year(s)"
android:textSize="20sp" />
<SeekBar
android:id="#+id/year_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Years"
android:layout_below="#+id/Years"
android:layout_marginTop="21dp" />
<Button
android:id="#+id/calculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/year_bar"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/year_bar"
android:text="Calculate" />
<EditText
android:id="#+id/IR_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/Years"
android:layout_marginBottom="14dp"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/Interest_Rate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/IR_bar"
android:layout_alignLeft="#+id/IR_bar"
android:layout_marginBottom="15dp"
android:text="Interest Rate"
android:textSize="20sp" />
<TextView
android:id="#+id/PA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/PA_bar"
android:layout_alignParentTop="true"
android:layout_marginTop="14dp"
android:text="Principal Amount"
android:textSize="20sp" />
<EditText
android:id="#+id/PA_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Interest_rate"
android:layout_below="#+id/PA"
android:layout_marginTop="17dp"
android:ems="10"
android:inputType="number" />
</RelativeLayout>
Actually , I need help for not xml part. I need help for java coding part. Even if i'm trying to use onclicklistener , compiler always show error on coding page? Could someone help me?

Try this:
public class MainActivity extends Activity {
TextView PA;
TextView Interest_Rate;
TextView Years;
EditText PA_bar;
EditText IR_bar;
EditText year_bar;
Button calculate;
TextView t1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PA = (TextView) findViewById(R.id.PA);
Interest_Rate = (TextView) findViewById(R.id.Interest_Rate);
Years = (TextView) findViewById(R.id.Years);
PA_bar = (EditText) findViewById(R.id.PA_bar);
IR_bar = (EditText) findViewById(R.id.IR_bar);
year_bar = (EditText) findViewById(R.id.year_bar);
calculate = (Button) findViewById(R.id.calculate);
t1 = (TextView) findViewById(R.id.t1);
calculate.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
int num1 = Integer.parseInt(PA_bar.getText().toString());
int num2 = Integer.parseInt(IR_bar.getText().toString());
int num = Integer.parseInt(year_bar.getText().toString());
int si = (num1 * num2 * num) / 100;
t1.setText(Integer.toString(si));
}
});
}};

You have a syntactical error in your code. You are missing the closing parentheses and semi-colon.
calculate.setOnClickListener(new OnClickListener() {
public void onClick(View v){
}
}); //<--- Change is here.

Related

Error while using setOnclickListener and View

Here, the user has to input names and phone numbers of 3 contacts in this app and register. I have used sharedpreferences method to save the data input. But the setOnclickListener and View remains cannot resolve symbol. Kindly help as I am a beginner. Any help is appreciated.
This is the MainActivity.java file
package com.me.addcontact
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
Button btn1= (Button) findViewById(R.id.button1);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
EditText name1 = (EditText)findViewById(R.id.name1);
EditText num1 = (EditText)findViewById(R.id.num1);
EditText name2 = (EditText)findViewById(R.id.name2);
EditText num2 = (EditText)findViewById(R.id.num2);
EditText name3 = (EditText)findViewById(R.id.name3);
EditText num3 = (EditText)findViewById(R.id.num3);
editor = sharedPreferences.edit();
editor.putString("name1", name1.getText().toString());
editor.putInt("num1", Integer.parseInt(num1.getText().toString()));
editor.putString("name2", name2.getText().toString());
editor.putInt("num2", Integer.parseInt(num2.getText().toString()));
editor.putString("name3", name3.getText().toString());
editor.putInt("num3", Integer.parseInt(num3.getText().toString()));
editor.commit();
Toast.makeText(getApplicationContext(), "Details Saved", Toast.LENGTH_SHORT).show();
}});
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sharedPreferences = this.getSharedPreferences("com.me.addcontact", Context.MODE_PRIVATE);
}
}
This is the 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.ashmi.addcontact.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="70sp"
android:background="#cccccc"
android:id="#+id/buttonLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true">
<Button
android:layout_width="300sp"
android:layout_height="50sp"
android:text=" Register"
android:textColor="#bfdfdf"
android:textSize="15sp"
android:background="#003366"
android:id="#+id/button1"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/name1"
android:hint="Emergency Contact Name 1"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:layout_below="#+id/name1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/num1"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="Number" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_below="#+id/num1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="34dp"
android:id="#+id/name2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="Emergency Contact Name 2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:layout_below="#+id/name2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/num2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="Number" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_marginTop="34dp"
android:id="#+id/name3"
android:hint="Emergency Contact Name 3"
android:layout_below="#+id/num2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:layout_below="#+id/name3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/num3"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="Number" />
</RelativeLayout>
you must use setOnclickListener interface in onCreate method like this
package com.me.addcontact
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sharedPreferences = this.getSharedPreferences("com.me.addcontact", Context.MODE_PRIVATE);
Button btn1= (Button) findViewById(R.id.button1);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
EditText name1 = (EditText)findViewById(R.id.name1);
EditText num1 = (EditText)findViewById(R.id.num1);
EditText name2 = (EditText)findViewById(R.id.name2);
EditText num2 = (EditText)findViewById(R.id.num2);
EditText name3 = (EditText)findViewById(R.id.name3);
EditText num3 = (EditText)findViewById(R.id.num3);
editor = sharedPreferences.edit();
editor.putString("name1", name1.getText().toString());
editor.putInt("num1", Integer.parseInt(num1.getText().toString()));
editor.putString("name2", name2.getText().toString());
editor.putInt("num2", Integer.parseInt(num2.getText().toString()));
editor.putString("name3", name3.getText().toString());
editor.putInt("num3", Integer.parseInt(num3.getText().toString()));
editor.commit();
Toast.makeText(getApplicationContext(), "Details Saved", Toast.LENGTH_SHORT).show();
}
});
}
public class MainActivity extends AppCompatActivity {
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
Button btn1;
EditText name1, num1, name2, num2, name3, num3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1= (Button) findViewById(R.id.button1);
sharedPreferences = this.getSharedPreferences("com.me.addcontact", Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
name1 = (EditText)findViewById(R.id.name1);
num1 = (EditText)findViewById(R.id.num1);
name2 = (EditText)findViewById(R.id.name2);
num2 = (EditText)findViewById(R.id.num2);
name3 = (EditText)findViewById(R.id.name3);
num3 = (EditText)findViewById(R.id.num3);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
editor.putString("name1", name1.getText().toString());
editor.putInt("num1", Integer.parseInt(num1.getText().toString()));
editor.putString("name2", name2.getText().toString());
editor.putInt("num2", Integer.parseInt(num2.getText().toString()));
editor.putString("name3", name3.getText().toString());
editor.putInt("num3", Integer.parseInt(num3.getText().toString()));
editor.commit();
Toast.makeText(getApplicationContext(), "Details Saved", Toast.LENGTH_SHORT).show();
}});
}
}
I hope this will solve your error ..

Dynamically Remove EditText Field in LinearLayout

My problem is that once my editText fields are created, I don't know how to remove them one by one with the other button in my app. I don't really understand the android:id element as it relates to setID() in my java code. I guess my main question is, how do I figure out how to target dynamically created editText views with their id's if I don't know their id's. Here is my code, any help is really appreciated:
Java:
package com.zdowning.decisionmaker;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ScrollView;
import android.widget.Toast;
import java.lang.*;
import static com.zdowning.decisionmaker.R.layout.activity_main;
public class MainActivity extends AppCompatActivity {
public int numberOfLines = 3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(activity_main);
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getAnswer();
}
});
final Button add_button = (Button) findViewById(R.id.add_button);
add_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Add_Line();
}
});
final Button remove_button = (Button) findViewById(R.id.remove_button);
remove_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Remove_Line();
}
});
}
public void Add_Line() {
LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayoutDecisions);
// add edittext
EditText et = new EditText(this);
LinearLayout.LayoutParams p = new
LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
et.setLayoutParams(p);
et.setId(numberOfLines++);
et.setText("Enter Answer #" + numberOfLines++);
ll.addView(et);
numberOfLines++;
Toast.makeText(MainActivity.this, "1", Toast.LENGTH_LONG).show();
}
public void Remove_Line() {
}
public void getAnswer() {
String[] options = new String[numberOfLines];
EditText text = (EditText)findViewById(R.id.editText2);
options[0] = text.getText().toString();
text = (EditText)findViewById(R.id.editText3);
options[1] = text.getText().toString();
text = (EditText)findViewById(R.id.editText4);
options[2] = text.getText().toString();
int number = (int)(Math.random() * 3);
String answer = options[number];
TextView answerBox = (TextView)findViewById(R.id.textView7);
answerBox.setText(answer);
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
android:background="#d4cfcd">
tools:context="com.zdowning.decisionmaker.MainActivity">
<LinearLayout
android:id="#+id/linearLayoutMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical" >
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center"
android:hint="Enter Your Question Here"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"/>
<LinearLayout
android:id="#+id/linearLayoutDecisions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/editText3"
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center"
android:hint="Enter Answer #2" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center"
android:hint="Enter Answer #1" />
<EditText
android:id="#+id/editText4"
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center"
android:hint="Enter Answer #3" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<Button
android:id="#+id/add_button"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:text="+"
android:textColor="#android:color/background_light"
android:textSize="18sp"/>
<View
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" />
<Button
android:id="#+id/remove_button"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:text="-"
android:textColor="#android:color/background_light"
android:textSize="18sp"
android:layout_margin="10dp"/>
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/button"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:text="DECIDE!"
android:textColor="#android:color/background_light"
android:textSize="18sp"
android:layout_centerInParent="true" />
</RelativeLayout>
<TextView
android:id="#+id/textView7"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_margin="20dp"
android:textColor="#android:color/black"
android:textSize="36sp" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
There is no relationship between android:id and your setID.
Check this answer to unsderstand more about that.
Anyway, one approach to achieve to remove a generated line could be based on the children index of your LinearLayout like this:
private LinearLayout mEditTextContainer;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
mEditTextContainer = (LinearLayout)findViewById(R.id.linearLayoutDecisions);
...
}
public void addLine() {
// add edittext
EditText et = new EditText(this);
LinearLayout.LayoutParams p = new
LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
et.setLayoutParams(p);
et.setText("Enter Answer #" + (mEditTextContainer.getChildCount()+1));
mEditTextContainer.addView(et);
Toast.makeText(MainActivity.this, "1", Toast.LENGTH_LONG).show();
}
public void removeLine() {
mEditTextContainer.removeViewAt(mEditTextContainer.getChildCount()-1);
}

Android App crashed and showing a white blank Screen

Note: Sorry if my problem may be really easy to fix, I'm a total beginner in this, I literally started learning this just 2 days ago.
So anyway, I'm making this basic calculator app for androids. I built the .apk and sent it to my phone but it just shows a blank white screen and then it crashes. Here's the xml code and the java code:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="#drawable/numbs" >
<TextView
android:id="#+id/title"
android:text="Calculator"
android:textColor="#E11608"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="35sp"
android:layout_gravity="top|center"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#000000"
/>
<EditText
android:id="#+id/value1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:inputType="number"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:ems="10"
android:layout_gravity="center"
android:layout_marginBottom="80dp"
android:hint="Type in your next value"
/>
<EditText
android:id="#+id/value2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:inputType="number"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:ems="10"
android:layout_gravity="center"
android:layout_marginBottom="148dp"
android:hint="Type in your first value"
/>
<Button
android:id="#+id/add"
android:text="+"
android:layout_gravity="center|left"
android:onClick="onClick"
/>
<Button
android:id="#+id/minus"
android:text="-"
android:layout_gravity="center"
android:layout_marginRight="40dp"
android:onClick="onClick"
/>
<Button
android:id="#+id/divide"
android:text="÷"
android:layout_gravity="center"
android:layout_marginLeft="40dp"
android:onClick="onClick"
/>
<Button
android:id="#+id/multiply"
android:text="×"
android:layout_gravity="center|right"
android:onClick="onClick"
/>
<TextView
android:id="#+id/answer"
android:text="Your answer should display here"
android:textColor="#E11608"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:layout_gravity="bottom|center"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp"
android:background="#FFFFFF"
/>
</FrameLayout>
Java code:
package com.Drift.app;
import android.app.*;
import android.os.*;
import java.io.*;
import android.widget.*;
import android.view.View.*;
import android.view.*;
public class MainActivity extends Activity
{
int val1;
int val2;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
work();
}
public void work()
{
//create parameter for add button
Button add = (Button) findViewById(R.id.add);
Button multiply = (Button) findViewById(R.id.multiply);
Button minus = (Button) findViewById(R.id.minus);
Button divide = (Button) findViewById(R.id.divide);
TextView title = (TextView) findViewById(R.id. title);
final TextView answer = (TextView) findViewById(R.id. answer);
final EditText value1 = (EditText) findViewById(R.id. value1);
final EditText value2 = (EditText) findViewById(R.id. value2);
val1 = Integer.parseInt(value1.getText().toString());
final Integer val1 = new Integer(value1.getText().toString());
val2 = Integer.parseInt(value2.getText().toString());
final Integer val2 = new Integer(value2.getText().toString());
add.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
answer.setText(String.valueOf(val1 + val2));
}
});
multiply.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
answer.setText(String.valueOf(val1 * val2));
}
});
minus.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
answer.setText(String.valueOf(val1 - val2));
}
});
divide.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
answer.setText(String.valueOf(val1 / val2));
}
});
}
}
Sorry for not commenting much. Can someone please tell me what I did wrong or maybe propose a better code. P.s sorry again if nearly everything here is wrong but like I said, I'm a total beginner. Thanks.
Okay, thanks for the answers this is is what I ended up with
package com.Drift.app;
import android.app.*;
import android.os.*;
import java.io.*;
import android.widget.*;
import android.view.View.*;
import android.view.*;
public class MainActivity extends Activity
{
int val1;
int val2;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
work();
}
public void work()
{
//create parameter for add button
Button add = (Button) findViewById(R.id.add);
Button multiply = (Button) findViewById(R.id.multiply);
Button minus = (Button) findViewById(R.id.minus);
Button divide = (Button) findViewById(R.id.divide);
TextView title = (TextView) findViewById(R.id. title);
final TextView answer = (TextView) findViewById(R.id. answer);
final EditText value1 = (EditText) findViewById(R.id. value1);
final EditText value2 = (EditText) findViewById(R.id. value2);
if(value1.getText()!=null)
{val1 = Integer.parseInt(value1.getText().toString());}
if(value2.getText()!=null)
{val2 = Integer.parseInt(value2.getText().toString());}
add.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
answer.setText(String.valueOf(val1 + val2));
}
});
multiply.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
answer.setText(String.valueOf(val1 * val2));
}
});
minus.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
answer.setText(String.valueOf(val1 - val2));
}
});
divide.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
answer.setText(String.valueOf(val1 / val2));
}
});
}
}
But it's still giving me a blank white page, I'm using gradle by the way
Here's the Android manifest file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Drift.app" >
<application
android:allowBackup="true"
android:icon="#drawable/calc"
android:label="#string/app_name"
android:theme="#android:style/Theme.Holo.Light" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
java.lang.RuntimeException: Binary XML file line #51: You must supply a layout_width attribute.
All views must have android:layout_width and android:layout_height defined. At least your Buttons don't have either.
Try to check EditText value before use it :
if(value1.getText().toString().trim().length()>0){
val1 = Integer.parseInt(value1.getText().toString());
}
if(value2.getText().toString().trim().length()>0){
val2 = Integer.parseInt(value2.getText().toString());
}
It is a probably a problem with a parsing. You are getting value from empty editText. You should not do that in on create method. You should check if value2 and value1 are null before using it.
if(value2.getText()!=null){val2 = Integer.parseInt(value2.getText().toString());}
here is the full working code. i have executed this and its working fine.
ur activity
public class MainAvtivity extends Activity {
int val1;
int val2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.seekbar);
work();
}
public void work() {
// create parameter for add button
Button add = (Button) findViewById(R.id.add);
Button multiply = (Button) findViewById(R.id.multiply);
Button minus = (Button) findViewById(R.id.minus);
Button divide = (Button) findViewById(R.id.divide);
TextView title = (TextView) findViewById(R.id.title);
final TextView answer = (TextView) findViewById(R.id.answer);
final EditText value1 = (EditText) findViewById(R.id.value1);
final EditText value2 = (EditText) findViewById(R.id.value2);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!"".equals(value1.getText().toString())) {
val1 = Integer.parseInt(value1.getText().toString());
}
if (!"".equals(value2.getText().toString())) {
val2 = Integer.parseInt(value2.getText().toString());
}
answer.setText(String.valueOf(val1 + val2));
}
});
multiply.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!"".equals(value1.getText().toString())) {
val1 = Integer.parseInt(value1.getText().toString());
}
if (!"".equals(value2.getText().toString())) {
val2 = Integer.parseInt(value2.getText().toString());
}
answer.setText(String.valueOf(val1 * val2));
}
});
minus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!"".equals(value1.getText().toString())) {
val1 = Integer.parseInt(value1.getText().toString());
}
if (!"".equals(value2.getText().toString())) {
val2 = Integer.parseInt(value2.getText().toString());
}
answer.setText(String.valueOf(val1 - val2));
}
});
divide.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
if (!"".equals(value1.getText().toString())) {
val1 = Integer.parseInt(value1.getText().toString());
}
if (!"".equals(value2.getText().toString())) {
val2 = Integer.parseInt(value2.getText().toString());
}
answer.setText(String.valueOf(val1 / val2));
}
});
}
}
and u have not specified height and weight in xml.
try this xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/numbs"
android:gravity="center" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_gravity="top|center"
android:background="#000000"
android:text="Calculator"
android:textColor="#E11608"
android:textSize="35sp" />
<EditText
android:id="#+id/value1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:layout_marginBottom="80dp"
android:background="#FFFFFF"
android:ems="10"
android:hint="Type in your next value"
android:inputType="number" />
<EditText
android:id="#+id/value2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:layout_marginBottom="148dp"
android:background="#FFFFFF"
android:ems="10"
android:hint="Type in your first value"
android:inputType="number" />
<Button
android:id="#+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|left"
android:onClick="onClick"
android:text="+" />
<Button
android:id="#+id/minus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="40dp"
android:onClick="onClick"
android:text="-" />
<Button
android:id="#+id/divide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="40dp"
android:onClick="onClick"
android:text="÷" />
<Button
android:id="#+id/multiply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|right"
android:onClick="onClick"
android:text="×" />
<TextView
android:id="#+id/answer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="bottom|center"
android:layout_marginBottom="40dp"
android:background="#FFFFFF"
android:text="Your answer should display here"
android:textColor="#E11608"
android:textSize="10sp" />
</FrameLayout>

Handling RadioGroup and Button Events in Android

I am a beginner in Android and I am trying to create a simple Android app
which has two Text boxes (EditText) to take two Numbers as Input. Then I am Creating Radio Group having four Radio Buttons (Add,Sub,Multiply,Divide).
Then I am having a Button to get the results. I am getting an AlertBox saying "Unfortunately, SimpleApp has stopped working" the moment I click the Button. Kindly assist me.
*Here is my Java code *
package com.example.simpleapp;
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.RadioButton;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
private EditText txt1;
private EditText txt2;
private String input1;
private String input2;
private Double Num1;
private Double Num2;
private String flag;
private EditText TextResult;
Double Result=0.0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initControls();
}
private void initControls()
{
((Button)findViewById(R.id.button1)).setOnClickListener(this);
}
public void onradioButtonClicked(View view)
{
boolean checked = ((RadioButton) view).isChecked();
switch(view.getId())
{
case R.id.radio0:
if(checked)
flag = "add";
//Result = Num1+Num2;
break;
case R.id.radio1:
if(checked)
flag = "sub";
//Result = Num1-Num2;
break;
case R.id.radio2:
if(checked)
flag = "prod";
//Result = Num1*Num2;
break;
case R.id.radio3:
if(checked)
flag = "div";
//Result = Num1/Num2;
break;
}
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextResult = (EditText)findViewById(R.id.textView2);
TextResult.setText("0");
txt1 = (EditText)findViewById(R.id.editText1);
txt2 = (EditText)findViewById(R.id.editText2);
input1 = txt1.getText().toString();
input2 = txt2.getText().toString();
Num1 = Double.parseDouble(input1);
Num2 = Double.parseDouble(input2);
if(flag == "add")
Result = Num1+Num2;
else if(flag == "sub")
Result = Num1-Num2;
else if(flag == "prod")
Result = Num1*Num2;
else Result = Num1/Num2;
TextResult.setText(String.valueOf(Result));
}
}
Here is my Activity_Main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
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" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="102dp"
android:layout_marginTop="66dp"
android:orientation="vertical" >
</LinearLayout>
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/linearLayout1"
android:layout_marginTop="34dp" >
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tableRow1"
android:layout_below="#+id/tableRow1"
android:layout_marginLeft="14dp" >
</TableRow>
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tableRow1"
android:layout_alignTop="#+id/tableRow2"
android:ems="10"
android:hint="Enter Second Number"
android:inputType="number" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/linearLayout1"
android:layout_centerHorizontal="true"
android:ems="10"
android:hint="Enter First Number"
android:inputType="number" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText1"
android:layout_alignParentTop="true"
android:layout_marginTop="14dp"
android:text="Simple Calculator App"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText2"
android:layout_below="#+id/editText2"
android:layout_marginTop="36dp" >
</TableRow>
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_below="#+id/editText2" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Add"
android:onClick="onradioButtonClicked"/>
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sub"
android:onClick="onradioButtonClicked"/>
<RadioButton
android:id="#+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Multiply"
android:onClick="onradioButtonClicked"/>
<RadioButton
android:id="#+id/radio3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Divide"
android:onClick="onradioButtonClicked"/>
</RadioGroup>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/linearLayout1"
android:layout_below="#+id/radioGroup1"
android:text="Check"
/>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Your if then statements have wrong syntax. Try this:
if(flag == "add"){
Result = Num1+Num2;
}else if(flag == "sub"){
Result = Num1-Num2;
}else if(flag == "prod"){
Result = Num1*Num2;
}else{
Result = Num1/Num2;
}
try this ... calculation on click as well as on radio button checked change
public class MainActivity extends Activity implements OnClickListener,
OnCheckedChangeListener {
private EditText txt1;
private EditText txt2;
// private String input1;
// private String input2;
private Double Num1;
private Double Num2;
// private String flag;
private TextView TextResult;
Double Result = 0.0;
RadioButton radio0, radio1, radio2, radio3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
findViewById(R.id.button1).setOnClickListener(this);
radio0 = (RadioButton) findViewById(R.id.radio0);
radio0.setOnCheckedChangeListener(this);
radio1 = (RadioButton) findViewById(R.id.radio1);
radio1.setOnCheckedChangeListener(this);
radio2 = (RadioButton) findViewById(R.id.radio2);
radio2.setOnCheckedChangeListener(this);
radio3 = (RadioButton) findViewById(R.id.radio3);
radio3.setOnCheckedChangeListener(this);
TextResult = (TextView) findViewById(R.id.textView2);
TextResult.setText("0");
txt1 = (EditText) findViewById(R.id.editText1);
txt2 = (EditText) findViewById(R.id.editText2);
}
public void onradioButtonClicked(View view) {
}
#Override
public void onClick(View v) {
getCalculationValue();
// input1 = txt1.getText().toString();
// input2 = txt2.getText().toString();
}
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
getCalculationValue();
}
private void getCalculationValue() {
Num1 = Double.parseDouble(txt1.getText().toString().trim());
Num2 = Double.parseDouble(txt2.getText().toString().trim());
if (radio0.isChecked())
Result = Num1 + Num2;
else if (radio1.isChecked())
Result = Num1 - Num2;
else if (radio2.isChecked())
Result = Num1 * Num2;
else if (radio3.isChecked())
Result = Num1 / Num2;
TextResult.setText(String.valueOf(Result));
}
}
You are Typecasted TextView into EditText.
So Android rises ClassCastException
08-03 17:08:30.415: E/AndroidRuntime(7702): java.lang.ClassCastException: android.widget.TextView
private EditText TextResult;
TextResult = (EditText)findViewById(R.id.textView2);
Change these lines into
private TextView TextResult;
TextResult = (TextView) findViewById(R.id.textView2);

Error in android Code :Syntax error on token "Private", private expected also for "Public";

I am Writing my first code to add and fetch some data from a database .:
Following is the code that I wrote in the Mainactivity.java file and its giving some errors
Such as Syntax error on token "Private", private expected also for "Public";
I have attached the screen shots as well can someone help regarding this .Because I am new to java and android codes
package com.example.ghrixdatabse;
import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.widget.TableLayout;
public class MainActivity extends Activity {
private static final EditText FindViewById = null;
String fname,lname,email;
SQLiteDatabase db;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db=openOrCreateDatabase("Mydb1", MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS Student(fname VARCHAR,lname VARCHAR,email VARCHAR);" );
}
Private void Adddata(View view)
{
EditText edittext1=(EditText)findViewById(R.id.firstname);
EditText edittext2=(EditText)findViewById(R.id.lastname);
EditText edittext3=(EditText)findViewById(R.id.email);
fname= edittext1.getText().toString();
lname= edittext2.getText().toString();
email= edittext3.getText().toString();
db.execSQL("INSERT INTO Student VALUES('"+fname+"','"+lname+"','"+email+"');");
}
Private void Showdata(View view)
{
Cursor c = db.rawQuery(SELECT * FROM from",null);
int count= c.getCount();
c.moveToFirst();
TableLayout tableLayout = new TableLayout(getApplicationContext () );
tableLayout.setVerticalScrollBarEnabled(true);
tableRow = new TableRow(getApplicationContext());
textView = new TextView(getApplicationContext());
textView.setText("Firstname");
textView.setColor(Color.Red);
textView.setTypeface(null,Typeface.Bold);
textView.setPadding(20,20,20,20);
tableRow.addView(textView);
tableRow = new TableRow(getApplicationContext());
textView = new TextView(getApplicationContext());
textView.setText("LastName");
textView.setColor(Color.Red);
textView.setTypeface(null,Typeface.Bold);
textView.setPadding(20,20,20,20);
tableRow.addView(textView);
tableRow = new TableRow(getApplicationContext());
textView = new TextView(getApplicationContext());
textView.setText("Email");
textView.setColor(Color.Red);
textView.setTypeface(null,Typeface.Bold);
textView.setPadding(20,20,20,20);
tableRow.addView(textView);
tableLayout.addView(tableRow);
for (Integer j=0;j < count;j++)
{
tableRow =new TableRow(getApplicationContext());
textView1 = new TextView(getApplicationContext());
textView1.setText(c.getString(c.getColoumIndex('fname')));
textView2 = new TextView(getApplicationContext());
textView2.setText(c.getString(c.getColoumIndex('lanme')));
textView3 = new TextView(getApplicationContext());
textView3.setText(c.getString(c.getColoumIndex('email')));
textView1.setPadding(20,20,20,20);
textView2.setPadding(20,20,20,20);
textView3.setPadding(20,20,20,20);
tableRow.addView(textView1);
tableRow.addView(textView2);
tableRow.addView(textView3);
tablelayout.addView(tableRow);
c.moveToNext();
}
setContentView(tableLayout);
db.close();
}
Public void close (View view)
{
System.exit(0);
}
}
HERE is the XML FILE :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android: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" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="49dp"
android:text="FirstName" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="61dp"
android:text="Lastname" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_centerVertical="true"
android:text="E-mail" />
<EditText
android:id="#+id/firstname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView1"
android:layout_alignBottom="#+id/textView1"
android:layout_alignParentRight="true"
android:ems="10" />
<EditText
android:id="#+id/lastname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView2"
android:layout_alignBottom="#+id/textView2"
android:layout_alignParentRight="true"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView3"
android:layout_alignLeft="#+id/lastname"
android:ems="10" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignRight="#+id/email"
android:layout_marginBottom="24dp"
android:onClick="showdata"
android:text="Show Table" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button3"
android:layout_alignRight="#+id/button3"
android:layout_marginBottom="26dp"
android:layout_marginRight="17dp"
android:onClick="close"
android:text="Close" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button2"
android:layout_alignBottom="#+id/button2"
android:layout_toRightOf="#+id/textView3"
android:onClick="Adddata"
android:text="Create" />
</RelativeLayout>
change Private to private and Public to public. p in private and public should be in small cases
java is case sensitive, So use private instead of Private for access specifier. As Private is not a keyword and has no special meaning. Same applies for Public, use public.
May this help you:
You are getting this Error because java is case sensitive, So, Change these lines:
Private void Adddata(View view)
Private void Showdata(View view)
Public void close (View view)
To:
private void Adddata(View view)
private void Showdata(View view)
public void close (View view)
Edit:
You are also getting error in this line also Check it:
Cursor c = db.rawQuery(SELECT * FROM from"// write here your table name,null);
Change it to:
Cursor c = db.rawQuery("SELECT * FROM from"// write here your TableName,null);

Categories

Resources