Currently trying to get my simple Android Calculator App working. When I try to run on an emulator I get:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
I'm also having an issue returning the result to the textview. I've tried tvResult.setText(result); but it isn't allowing me to pass in a float to the text.
My Java:
package com.example.justin.calculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import javax.xml.transform.Result;
public class MainActivity extends Activity implements OnClickListener {
EditText digit1;
EditText digit2;
TextView tvResult;
Button nine, eight, seven, six, five, four, three, two, one, zero, decimal, plus, minus, divide, multiply, equals;
String s = "0";
char enterDigit = ' ';
String enterOperation;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//button elements
nine = (Button) findViewById(R.id.button9);
eight = (Button) findViewById(R.id.button8);
seven = (Button) findViewById(R.id.button7);
six = (Button) findViewById(R.id.button6);
five = (Button) findViewById(R.id.button5);
four = (Button) findViewById(R.id.button4);
three = (Button) findViewById(R.id.button3);
two = (Button) findViewById(R.id.button2);
one = (Button) findViewById(R.id.button1);
zero = (Button) findViewById(R.id.button0);
decimal = (Button) findViewById(R.id.buttondecimal);
plus = (Button) findViewById(R.id.buttonadd);
minus = (Button) findViewById(R.id.buttonminus);
divide = (Button) findViewById(R.id.buttondivide);
multiply = (Button) findViewById(R.id.buttonmultiply);
equals = (Button) findViewById(R.id.buttonequals);
tvResult = (TextView) findViewById(R.id.tvResult);
//set listeners
nine.setOnClickListener(this);
eight.setOnClickListener(this);
seven.setOnClickListener(this);
six.setOnClickListener(this);
five.setOnClickListener(this);
four.setOnClickListener(this);
three.setOnClickListener(this);
two.setOnClickListener(this);
one.setOnClickListener(this);
decimal.setOnClickListener(this);
plus.setOnClickListener(this);
minus.setOnClickListener(this);
divide.setOnClickListener(this);
multiply.setOnClickListener(this);
equals.setOnClickListener(this);
tvResult.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
float num1 = 0;
float num2 = 0;
float result = 0;
num1 = Float.parseFloat(digit1.getText().toString());
num2 = Float.parseFloat(digit2.getText().toString());
switch (v.getId()) {
case R.id.buttonadd:
enterOperation = "+";
result = num1 + num2;
break;
case R.id.buttonminus:
enterOperation = "-";
result = num1 - num2;
break;
case R.id.buttonmultiply:
enterOperation = "*";
result = num1 * num2;
break;
case R.id.buttondivide:
enterOperation = "/";
result = num1 / num2;
break;
default:
break;
}
}
}
My XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/tvResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="0"
android:gravity="right"
android:textSize="50sp"/>"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1"
android:onClick="enterDigit">
<Button
android:id="#+id/button7"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="7"
android:onClick="enterDigit"/>
<Button
android:id="#+id/button8"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="8"
android:onClick="enterDigit"/>
<Button
android:id="#+id/button9"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="9"
android:onClick="enterDigit"/>"
<Button
android:id="#+id/buttondivide"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="/"
android:onClick="enterOperation"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1">
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="4"
android:onClick="enterDigit"/>
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="5"
android:onClick="enterDigit"/>
<Button
android:id="#+id/button6"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="6"
android:onClick="enterDigit"/>
<Button
android:id="#+id/buttonmultiply"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="*"
android:onClick="enterOperation"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="1"
android:onClick="enterDigit"/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="2"
android:onClick="enterDigit"/>
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="3"
android:onClick="enterDigit"/>
<Button
android:id="#+id/buttonminus"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="-"
android:onClick="enterOperation"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1">
<Button
android:id="#+id/button0"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="0"
android:onClick="enterDigit"/>
<Button
android:id="#+id/buttondecimal"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="." />
<Button
android:id="#+id/buttonequals"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="="
android:onClick="calculate"/>
<Button
android:id="#+id/buttonadd"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="+"
android:onClick="enterOperation"/>
</LinearLayout>
</LinearLayout>
Your EditText widgets do not exist in your xml. They need to be added in your main LinearLayout somewhere. Then, in your onCreate() method, you need to set them up via the findViewById(R.id.digit1)... Also, get rid of the onClick attribute in the LinearLayout; because your linearLayout is not clickable, it is useless.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal>
<EditText
android:id="#+id/digit1"
android:layout_height="wrap_content"
android:layout_width="0dip"
android:layout_weight="1" />
<EditText
android:id="#+id/digit2"
android:layout_height="wrap_content"
android:layout_width="0dip"
android:layout_weight="1" />
</LinearLayout>
<TextView
android:id="#+id/tvResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="0"
android:gravity="right"
android:textSize="50sp"/>"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1">
<Button
android:id="#+id/button7"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="7"
android:onClick="enterDigit"/>
...
then, in your activity...
#Override
public void onCreate(Bundle sIS){
super.onCreate(sIS);
digit1 = (EditText)findViewById(R.id.digit1);
digit2 = (EditText)findViewById(R.id.digit2);
...
}
you need to init your EditText (digit1 and digit1) firstlg as your buttons do, if not it will lead to NullPointException just like your proplem
Related
I have built a calculator app which uses EditTexts for you to insert the two values, and then I also have 4 Buttons each with an operation (eg.+,-,/,x). It seems though as if the buttons aren't doing its function assigned to it, No value is being sent to the TextView. Could you please help me in making those buttons do its operation?
Here is my code below:
package com.example.user.zidcalculator;
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.TextView;
public class MainActivity extends AppCompatActivity {
int enter1;
int enter2;
int newHeight;
EditText et1;
EditText et2;
TextView tv;
Button bt1;
Button bt2;
Button bt3;
Button bt4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 = (EditText) findViewById(R.id.edittext1);
et2 = (EditText) findViewById(R.id.edittext2);
tv = (TextView) findViewById(R.id.tvans);
bt1 = (Button) findViewById(R.id.button);
bt2 = (Button) findViewById(R.id.button2);
bt3 = (Button) findViewById(R.id.button3);
bt4 = (Button) findViewById(R.id.button4);
newHeight = et2.getHeight();
tv.setHeight(newHeight);
bt1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
enter1 = Integer.parseInt(et1.getText().toString());
enter2 = Integer.parseInt(et2.getText().toString());
int answer = enter1 + enter2;
tv.setText(String.valueOf(answer));
}
});
bt2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
enter1 = Integer.parseInt(et1.getText().toString());
enter2 = Integer.parseInt(et2.getText().toString());
int answer = enter1 - enter2;
tv.setText(String.valueOf(answer));
}
});
bt3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
enter1 = Integer.parseInt(et1.getText().toString());
enter2 = Integer.parseInt(et2.getText().toString());
int answer = enter1 * enter2;
tv.setText(String.valueOf(answer));
}
});
bt4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
enter1 = Integer.parseInt(et1.getText().toString());
enter2 = Integer.parseInt(et2.getText().toString());
int answer = enter1 / enter2;
tv.setText(String.valueOf(answer));
}
});
}
}
Layout 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"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter two numbers below and perform an operation to see the output"
android:gravity="center_horizontal"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter first number here -->"
android:textSize="18sp" />
<EditText
android:id="#+id/edittext1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:inputType="numberSigned"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter second number here -->"
android:textSize="18sp" />
<EditText
android:id="#+id/edittext2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:inputType="numberSigned"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer -->"
android:textSize="18sp"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="#+id/tvans"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal" />
</LinearLayout>
</LinearLayout>
<Button
android:text="+"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:layout_centerVertical="true"
android:layout_alignParentStart="true" />
<Button
android:text="-"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/button"
android:layout_centerHorizontal="true"
android:id="#+id/button2" />
<Button
android:text="x"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button"
android:layout_alignEnd="#+id/button"
android:layout_marginTop="39dp"
android:id="#+id/button3" />
<Button
android:text="/"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/button3"
android:layout_alignStart="#+id/button2"
android:id="#+id/button4" />
</RelativeLayout>
When you getHeight of et2, it haven't text, so it height is 0. Then, you set height 0 for tv, you can't see it in screen althought it has text.
Try to run the code giving a constant height and width to answerTextView,tvans in your code(for eg: 30 DP height and 30 DP width).
This issue is not related to button. This is due to the invisibility of textView due to 0 height and width
<TextView
android:id="#+id/tvans"
android:layout_width="30dp"
android:layout_height="30dp"
android:gravity="center_horizontal"/>
I removed the Height variable newHeight and everything associated with it.
I want to provide lock screen, so every time user opens my app, he will be forced to enter pin. So I am looking for Android's default pin lock layout (as shown on image), which should work from Android 4.0. Can you tell me where to find layout of this or how to implement it properly?
I git below project from github and little changed it to provide a GUI like your image :
https://github.com/chinloong/Android-PinView
so create a project and in mainActivity insert this codes:
public class PinEntryView extends Activity {
String userEntered;
String userPin="8888";
final int PIN_LENGTH = 4;
boolean keyPadLockedFlag = false;
Context appContext;
TextView titleView;
TextView pinBox0;
TextView pinBox1;
TextView pinBox2;
TextView pinBox3;
TextView statusView;
Button button0;
Button button1;
Button button2;
Button button3;
Button button4;
Button button5;
Button button6;
Button button7;
Button button8;
Button button9;
Button button10;
Button buttonExit;
Button buttonDelete;
EditText passwordInput;
ImageView backSpace;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
appContext = this;
userEntered = "";
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main_layout);
//Typeface xpressive=Typeface.createFromAsset(getAssets(), "fonts/XpressiveBold.ttf");
statusView = (TextView) findViewById(R.id.statusview);
passwordInput = (EditText) findViewById(R.id.editText);
backSpace = (ImageView) findViewById(R.id.imageView);
buttonExit = (Button) findViewById(R.id.buttonExit);
backSpace.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
passwordInput.setText(passwordInput.getText().toString().substring(0,passwordInput.getText().toString().length()-2));
}
});
buttonExit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Exit app
Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
appContext.startActivity(i);
finish();
}
}
);
//buttonExit.setTypeface(xpressive);
buttonDelete = (Button) findViewById(R.id.buttonDeleteBack);
buttonDelete.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (keyPadLockedFlag == true)
{
return;
}
if (userEntered.length()>0)
{
userEntered = userEntered.substring(0,userEntered.length()-1);
passwordInput.setText("");
}
}
}
);
titleView = (TextView)findViewById(R.id.time);
//titleView.setTypeface(xpressive);
View.OnClickListener pinButtonHandler = new View.OnClickListener() {
public void onClick(View v) {
if (keyPadLockedFlag == true)
{
return;
}
Button pressedButton = (Button)v;
if (userEntered.length()<PIN_LENGTH)
{
userEntered = userEntered + pressedButton.getText();
Log.v("PinView", "User entered="+userEntered);
//Update pin boxes
passwordInput.setText(passwordInput.getText().toString()+"*");
passwordInput.setSelection(passwordInput.getText().toString().length());
if (userEntered.length()==PIN_LENGTH)
{
//Check if entered PIN is correct
if (userEntered.equals(userPin))
{
statusView.setTextColor(Color.GREEN);
statusView.setText("Correct");
Log.v("PinView", "Correct PIN");
finish();
}
else
{
statusView.setTextColor(Color.RED);
statusView.setText("Wrong PIN. Keypad Locked");
keyPadLockedFlag = true;
Log.v("PinView", "Wrong PIN");
new LockKeyPadOperation().execute("");
}
}
}
else
{
//Roll over
passwordInput.setText("");
userEntered = "";
statusView.setText("");
userEntered = userEntered + pressedButton.getText();
Log.v("PinView", "User entered="+userEntered);
//Update pin boxes
passwordInput.setText("8");
}
}
};
button0 = (Button)findViewById(R.id.button0);
//button0.setTypeface(xpressive);
button0.setOnClickListener(pinButtonHandler);
button1 = (Button)findViewById(R.id.button1);
//button1.setTypeface(xpressive);
button1.setOnClickListener(pinButtonHandler);
button2 = (Button)findViewById(R.id.button2);
//button2.setTypeface(xpressive);
button2.setOnClickListener(pinButtonHandler);
button3 = (Button)findViewById(R.id.button3);
//button3.setTypeface(xpressive);
button3.setOnClickListener(pinButtonHandler);
button4 = (Button)findViewById(R.id.button4);
//button4.setTypeface(xpressive);
button4.setOnClickListener(pinButtonHandler);
button5 = (Button)findViewById(R.id.button5);
//button5.setTypeface(xpressive);
button5.setOnClickListener(pinButtonHandler);
button6 = (Button)findViewById(R.id.button6);
//button6.setTypeface(xpressive);
button6.setOnClickListener(pinButtonHandler);
button7 = (Button)findViewById(R.id.button7);
//button7.setTypeface(xpressive);
button7.setOnClickListener(pinButtonHandler);
button8 = (Button)findViewById(R.id.button8);
//button8.setTypeface(xpressive);
button8.setOnClickListener(pinButtonHandler);
button9 = (Button)findViewById(R.id.button9);
//button9.setTypeface(xpressive);
button9.setOnClickListener(pinButtonHandler);
buttonDelete = (Button)findViewById(R.id.buttonDeleteBack);
//buttonDelete.setTypeface(xpressive);
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
//App not allowed to go back to Parent activity until correct pin entered.
return;
//super.onBackPressed();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.activity_pin_entry_view, menu);
return true;
}
private class LockKeyPadOperation extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
for(int i=0;i<2;i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return "Executed";
}
#Override
protected void onPostExecute(String result) {
statusView.setText("");
//Roll over
passwordInput.setText("");
;
userEntered = "";
keyPadLockedFlag = false;
}
#Override
protected void onPreExecute() {
}
#Override
protected void onProgressUpdate(Void... values) {
}
}
}
then create main_layout.xml file and insert below xml codes:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/image_background"
>
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:src="#drawable/backspace"
android:layout_above="#+id/view"
android:layout_marginBottom="10dp"
android:layout_alignRight="#+id/view"
android:id="#+id/imageView" />
<View
android:layout_width="200dp"
android:layout_height="1dp"
android:background="#FFF"
android:layout_above="#+id/numericPad"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:id="#+id/view" />
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/numericPad"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginTop="20dip"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:shrinkColumns="*"
android:stretchColumns="*"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
>
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:id="#+id/button1"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center"
android:text="1"
android:textSize="25sp"
android:textColor="#ffffff"
android:padding="6dip"
android:layout_margin="3dip"
android:background="#android:color/transparent"
>
</Button>
<Button
android:id="#+id/button2"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center"
android:text="2"
android:textSize="25sp"
android:textColor="#ffffff"
android:padding="6dip"
android:layout_margin="3dip"
android:background="#android:color/transparent"
>
</Button>
<Button
android:id="#+id/button3"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center"
android:text="3"
android:textSize="25sp"
android:textColor="#ffffff"
android:padding="6dip"
android:layout_margin="3dip"
android:background="#android:color/transparent"
>
</Button>
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:id="#+id/button4"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center"
android:text="4"
android:textSize="25sp"
android:textColor="#ffffff"
android:padding="6dip"
android:layout_margin="3dip"
android:background="#android:color/transparent"
>
</Button>
<Button
android:id="#+id/button5"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center"
android:text="5"
android:textSize="25sp"
android:textColor="#ffffff"
android:padding="6dip"
android:layout_margin="3dip"
android:background="#android:color/transparent"
>
</Button>
<Button
android:id="#+id/button6"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center"
android:text="6"
android:textSize="25sp"
android:textColor="#ffffff"
android:padding="6dip"
android:layout_margin="3dip"
android:background="#android:color/transparent"
>
</Button>
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:id="#+id/button7"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center"
android:text="7"
android:textSize="25sp"
android:textColor="#ffffff"
android:padding="6dip"
android:layout_margin="3dip"
android:background="#android:color/transparent"
>
</Button>
<Button
android:id="#+id/button8"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center"
android:text="8"
android:textSize="25sp"
android:textColor="#ffffff"
android:padding="6dip"
android:layout_margin="3dip"
android:background="#android:color/transparent"
>
</Button>
<Button
android:id="#+id/button9"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center"
android:text="9"
android:textSize="25sp"
android:textColor="#ffffff"
android:padding="6dip"
android:layout_margin="3dip"
android:background="#android:color/transparent"
>
</Button>
</TableRow>
<TableRow
android:id="#+id/tableRow4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:id="#+id/buttonExit"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Exit"
android:textSize="25sp"
android:textColor="#ffffff"
android:padding="6dip"
android:layout_margin="3dip"
android:background="#android:color/transparent"
>
</Button>
<Button
android:id="#+id/button0"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center"
android:text="0"
android:textSize="25sp"
android:textColor="#ffffff"
android:padding="6dip"
android:layout_margin="3dip"
android:background="#android:color/transparent"
>
</Button>
<Button
android:id="#+id/buttonDeleteBack"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Delete"
android:textSize="25sp"
android:textColor="#ffffff"
android:padding="6dip"
android:layout_margin="3dip"
android:background="#android:color/transparent"
>
</Button>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"></TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"></TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"></TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"></TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"></TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"></TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"></TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"></TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</TableRow>
</TableLayout>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/editText"
android:layout_alignBottom="#+id/imageView"
android:layout_centerHorizontal="true"
android:background="#android:color/transparent"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Enter Password"
android:id="#+id/statusview"
android:layout_below="#+id/time"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="23:17"
android:id="#+id/time"
android:textSize="100sp"
android:layout_marginTop="64dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
note : userPin variable is your password and you can change it.
in below bock in activity class you should insert your code that you want execute if user enter correnct password (e.g start a new activity )
if (userEntered.equals(userPin))
{
statusView.setTextColor(Color.GREEN);
statusView.setText("Correct");
Log.v("PinView", "Correct PIN");
finish();
}
Note: add below line to main activity node in mainfist.xml file
android:theme="#android:style/Theme.NoTitleBar"
so your Activity node must be like :
<activity
android:name="com.example.MainActivity"
android:theme="#android:style/Theme.NoTitleBar"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I have made a very simple android program with help of 3 imagebuttons(circle) ,Now i want is after some clicks on that partcular buttons its background images should be changed..i have tried the code as below but its not working:
main.java
package com.esp.therisemethod.ui;
import com.esp.therisemethod.R;
import com.esp.therisemethod.uc.Header;
import com.esp.therisemethod.uc.Menu;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class ProgressActivity extends Activity implements OnClickListener {
private ImageView resetall, undo, b1, b2, b3;
int cnt1,cnt2,cnt3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
b1 = (ImageView) findViewById(R.id.b1);
b2 = (ImageView) findViewById(R.id.b2);
b3 = (ImageView) findViewById(R.id.b3);
resetall = (ImageView) findViewById(R.id.reset_all);
undo = (ImageView) findViewById(R.id.undo);
setContentView(R.layout.activity_progress);
Header header =(Header)findViewById(R.id.header);
header = (Header) findViewById(R.id.header);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
header.btn_bak.setVisibility(View.VISIBLE);
header.btn_bak.setVisibility(View.VISIBLE);
Menu menu =(Menu)findViewById(R.id.prog_footer);
menu.setSelectedTab(1);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.b1:
cnt1++;
if(cnt1>1 && cnt1<=5)
{
b1.setBackgroundResource(R.drawable.circle_red);
}
else if (cnt1>=6 && cnt1<10) {
b1.setBackgroundResource(R.drawable.circle_orange);
}
else{
b1.setBackgroundResource(R.drawable.circle_green);
}
break;
case R.id.b2:
cnt2++;
if(cnt2<=5)
{
b2.setImageResource(R.drawable.circle_red);
}
else if (cnt2>=6 && cnt2<10) {
b2.setImageResource(R.drawable.circle_orange);
}
else{
b2.setImageResource(R.drawable.circle_green);
}
break;
case R.id.b3:
cnt3++;
if(cnt3<=5)
{
b2.setImageResource(R.drawable.circle_red);
}
else if (cnt3>=6 && cnt3<10) {
b2.setImageResource(R.drawable.circle_orange);
}
else{
b2.setImageResource(R.drawable.circle_green);
}
break;
case R.id.reset_all:
break;
case R.id.undo:
break;
}
}
}
main.xml
<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" >
<com.esp.therisemethod.uc.Header
android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:layout_below="#+id/header"
android:layout_above="#+id/prog_footer">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<LinearLayout
android:id="#+id/linearp1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/header"
android:orientation="vertical" >
<com.esp.therisemethod.uc.EspTextView
android:id="#+id/client_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:tag="800"
android:text="Welcome Back SAM!"
android:textSize="25dp"
android:textStyle="bold" />
<com.esp.therisemethod.uc.EspTextView
android:id="#+id/make_choice"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:tag="800"
android:text="Please make your choice:"
android:textSize="20dp" />
</LinearLayout>
<RelativeLayout
android:id="#+id/ralativep1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/linearp1"
android:layout_centerVertical="true"
android:layout_marginTop="10dp" >
<ImageView
android:id="#+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"
android:src="#drawable/cir_grey1" />
<ImageView
android:id="#+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:src="#drawable/cir_grey1" />
</RelativeLayout>
<ImageView
android:id="#+id/b3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/ralativep1"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:src="#drawable/cir_grey1" />
<LinearLayout
android:id="#+id/linear2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/b3"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="30dp"
android:weightSum="2" >
<Button
android:id="#+id/reset_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:background="#drawable/btn_med"
android:text="Reset all"
android:textColor="#ffffff" />
<Button
android:id="#+id/undo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:background="#drawable/btn_med"
android:text="Undo"
android:textColor="#ffffff" />
</LinearLayout>
</LinearLayout>
</ScrollView>
<com.esp.therisemethod.uc.Menu
android:id="#+id/prog_footer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>
If you have set src attribute in xml, then to update UI use setImageResource()
If you have set background attribute in xml then to update UI use setImageDrawable()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//First set content view..
setContentView(R.layout.activity_progress);
b1 = (ImageView) findViewById(R.id.b1);
b2 = (ImageView) findViewById(R.id.b2);
b3 = (ImageView) findViewById(R.id.b3);
resetall = (ImageView) findViewById(R.id.reset_all);
undo = (ImageView) findViewById(R.id.undo);
Header header =(Header)findViewById(R.id.header);
If Your onClickListener works correct, in Your case You have to change the source not background, for example:
b2.setImageResource(R.drawable.yourImage);
I've followed a tutorial to realise a calculator. It seems that my buttons objects that i define, and instantiate with the ID are not instantiated because when i add a listener on them, they return a null pointer exception.
here's the java code:
package com.example.calculette;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
//Variables declaration
Button b0;
Button b1;
Button b2;
Button b3;
Button b4;
Button b5;
Button b6;
Button b7;
Button b8;
Button b9;
Button bC;
Button bCE;
Button bEgal;
Button bPlus;
Button bMoins;
Button bDiviser;
Button bMultiplier;
EditText saisie;
private double number1 = 0;
private boolean clicOperateur = false;
private boolean update = true;
private String operateur = "";
//onCreate is the first procedure called by the activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b0 = (Button) findViewById(R.id.button0);
b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
b3 = (Button) findViewById(R.id.button3);
b4 = (Button) findViewById(R.id.button4);
b5 = (Button) findViewById(R.id.button5);
b6 = (Button) findViewById(R.id.button6);
b7 = (Button) findViewById(R.id.button7);
b8 = (Button) findViewById(R.id.button8);
b9 = (Button) findViewById(R.id.button9);
bC = (Button) findViewById(R.id.buttonC);
bCE = (Button) findViewById(R.id.buttonCE);
bEgal = (Button) findViewById(R.id.buttonEgal);
bPlus = (Button) findViewById(R.id.buttonPlus);
bMoins = (Button) findViewById(R.id.buttonMoins);
bDiviser = (Button) findViewById(R.id.buttonDiviser);
bMultiplier = (Button) findViewById(R.id.buttonMultiplier);
saisie = (EditText) findViewById(R.id.EditText01);
//Listeners declaration
b0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
numberClick("0");
}
});
(i don't link the whole code because it's still the same for the 9 buttons and after it's simple methods).
here you can see the XML associated:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="3">
<EditText android:id="#+id/EditText01"
android:layout_width="fill_parent"
android:layout_height="80px"
android:textSize="20px"
android:editable="false"
android:cursorVisible="false"
/>
<View
android:layout_height="3dip"
android:background="#FF999999"
/>
<TableRow>
<Button android:id="#+id/buttonCE"
android:text="CE"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_span="2"
style="#style/button_text"
android:background="#drawable/button"
/>
<Button android:id="#+id/buttonC"
android:text="C"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_span="2"
style="#style/button_text"
android:background="#drawable/button"
/>
</TableRow>
<TableRow>
<Button android:id="#+id/button7"
android:text="7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/button_text"
android:background="#drawable/button"
/>
<Button android:id="#+id/button8"
android:text="8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/button_text"
android:background="#drawable/button"
/>
<Button android:id="#+id/button9"
android:text="9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/button_text"
android:background="#drawable/button"
/>
<Button android:id="#+id/buttonPlus"
android:text="+"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="#style/button_text"
android:background="#drawable/button"
/>
</TableRow>
<TableRow>
<Button android:id="#+id/button4"
android:text="4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/button_text"
android:background="#drawable/button"
/>
<Button android:id="#+id/button5"
android:text="5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/button_text"
android:background="#drawable/button"
/>
<Button android:id="#+id/button6"
android:text="6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/button_text"
android:background="#drawable/button"
/>
<Button android:id="#+id/buttonMoins"
android:text="-"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="#style/button_text"
android:background="#drawable/button"
/>
</TableRow>
<TableRow>
<Button android:id="#+id/button1"
android:text="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/button_text"
android:background="#drawable/button"
/>
<Button android:id="#+id/button2"
android:text="2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/button_text"
android:background="#drawable/button"
/>
<Button android:id="#+id/button3"
android:text="3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/button_text"
android:background="#drawable/button"
/>
<Button android:id="#+id/buttonMultiplier"
android:text="*"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="#style/button_text"
android:background="#drawable/button"
/>
</TableRow>
<TableRow>
<Button android:id="#+id/button0"
android:text="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/button_text"
android:background="#drawable/button"
/>
<Button android:id="#+id/buttonPoint"
android:text="."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/button_text"
android:background="#drawable/button"
/>
<Button android:id="#+id/buttonEgal"
android:text="="
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/button_text"
android:background="#drawable/button"
/>
<Button android:id="#+id/buttonDiviser"
android:text="/"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="#style/button_text"
android:background="#drawable/button"
/>
</TableRow>
</TableLayout>
<ImageView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:src="#drawable/logosafran"
android:id="#+id/imageView1"
></ImageView>
</LinearLayout>
The logcat says that i have a nullPointerException on the first listener declaration with b0.
Thank you guys!
The problem is that in your activity you're trying to inflate the activity_main.xml file which obviously doesnt'hold your button declaration. You told that your buttons are defined into a button_classical.xml file.
You would have to change
setContentView(R.layout.activity_main);
in
setContentView(R.layout.button_classical);
I am having problems getting the integer values for a simple math program out of the edittext boxes. I can get a string out of it but when I try to convert it to a integer it crashes. I have tried:
int a = Intger.parseInt(X2.getText().toString());
and it still crashes. Any idea's would be greatly appreciated.
Here is source code.
package siu.edu.cs215;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
public class Quadratic extends Activity {
private TextView answer;
private EditText X2, X, K;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quad);
Button1 = (Button)findViewById(R.id.quad);
Button2 = (Button)findViewById(R.id.backwards);
answer = (TextView)findViewById(R.id.B1);
X2 = (EditText)findViewById(R.id.xsquared);
X = (EditText)findViewById(R.id.xcoeff);
K = (EditText)findViewById(R.id.kconst);
}
public Button Button1 = null;
public Button Button2 = null;
public void QA (View view){
//double sol1=0.0, sol2=0.0, disc=0.0;
int a = Integer.parseInt(X2.getText().toString());
//int b = Integer.getInteger(X.getText().toString());
//int c = Integer.getInteger(K.getText().toString());
//disc = Math.pow(b, 2) - 4.0*a*c;
//sol1 = (-b + Math.sqrt(disc)) / (2.0 * a);
//sol2 = (-b - Math.sqrt(disc)) / (2.0 * a);
answer.setText(a);
};
public void BACK (View view){
Intent i = new Intent(this, ExtraCreditActivity.class);
startActivity(i);
}
}
Here is the xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
android:id="#+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight=".6" >
</TableRow>
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" >
<EditText
android:id="#+id/xsquared"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:inputType="number" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="X^2 +"
android:textSize="20dp" />
<EditText
android:id="#+id/xcoeff"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="number" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="X + "
android:textSize="20dp" />
<EditText
android:id="#+id/kconst"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:inputType="number" >
<requestFocus />
</EditText>
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1.2" >
<Button
android:id="#+id/quad"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="QA"
android:text="Quadratic" />
</TableRow>
<TableRow
android:id="#+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight=".8" >
<TextView
android:id="#+id/B1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2pt"
android:text="#string/app_name" />
</TableRow>
<TableRow
android:id="#+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/backwards"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:onClick="BACK" />
</TableRow>
</TableLayout>
</LinearLayout>
Most likely you're problem is in trying to parse an empty string. Perhaps you can simply avoid performing any calculations unless all fields can be correctly parsed. Put your parsing into a try/catch block.