Instantiation problems with id - android

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);

Related

Button not respond to on click event

I'm trying for hours to make the button click but for some weird reason it doesnt work.
I have the main activity and from there I set the content view to first run activity and this is the code of the first run activity
import android.app.Activity;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class FirstRun extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_run);
final Button button = (Button) findViewById(R.id.nextBtn);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d("Test","test");
System.out.println("Button one is pressed");
button.setText("Test");
//Get edit text values
EditText nameET = (EditText) findViewById(R.id.Name);
EditText ageET = (EditText) findViewById(R.id.Age);
EditText emailET = (EditText) findViewById(R.id.Email);
EditText phoneET = (EditText) findViewById(R.id.Phone);
//set data in the info
//info = new UserInfo(nameET.getText().toString(),emailET.getText().toString(),phoneET.getText().toString());
if (nameET.getText().toString() != "" && ageET.getText().toString() != "" && emailET.getText().toString() != "" && phoneET.getText().toString() != "") {
SharedPreferences settings = getSharedPreferences("UserInfo", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("Name", nameET.getText().toString());
editor.putString("Email", emailET.getText().toString());
editor.putString("Phone", phoneET.getText().toString());
editor.putInt("Age", Integer.parseInt(ageET.getText().toString()));
}
}
});
}
}
Xml Layout details about the layout of the app and the button
i tried to add to the button onclick event on xml but every time I did it the app just crush and didn't respond at all
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:layout_editor_absoluteY="0dp"
tools:layout_editor_absoluteX="8dp"
android:background="#drawable/backgroundimage">
<TextView
android:id="#+id/WelcomeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="#string/welcome_to_my_app"
android:textColor="#android:color/background_dark"
android:textSize="24sp" />
<Space
android:layout_width="fill_parent"
android:layout_height="10dp" />
<TextView
android:id="#+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="#string/please_fill_the_information"
android:textColor="#android:color/holo_blue_dark"
android:textSize="20sp" />
<Space
android:layout_width="fill_parent"
android:layout_height="10dp" />
<GridLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnCount="3"
android:rowCount="7">
<TextView
android:id="#+id/nameTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_gravity="left"
android:layout_row="2"
android:text="#string/name"
android:textColor="#color/colorAccent"
android:textSize="18sp" />
<EditText
android:id="#+id/Name"
style="#style/Widget.AppCompat.EditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="2"
android:capitalize="sentences"
android:ems="14"
android:inputType="textPersonName"
android:visibility="visible" />
<TextView
android:id="#+id/phoneTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_gravity="left"
android:layout_row="3"
android:text="#string/phone"
android:textColor="#color/colorAccent"
android:textSize="18sp" />
<EditText
android:id="#+id/Phone"
style="#style/Widget.AppCompat.EditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="3"
android:ems="14"
android:inputType="phone"
android:visibility="visible" />
<TextView
android:id="#+id/emailTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_gravity="left"
android:layout_row="4"
android:text="#string/email"
android:textColor="#color/colorAccent"
android:textSize="18sp" />
<EditText
android:id="#+id/Email"
style="#style/Widget.AppCompat.EditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="4"
android:ems="14"
android:inputType="textEmailAddress"
android:visibility="visible" />
<TextView
android:id="#+id/ageTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_gravity="left"
android:layout_row="5"
android:text="#string/age"
android:textColor="#color/colorAccent"
android:textSize="18sp" />
<EditText
android:id="#+id/Age"
style="#style/Widget.AppCompat.EditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="5"
android:ems="14"
android:inputType="number"
android:visibility="visible" />
</GridLayout>
<Button
android:id="#+id/nextBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="#android:color/holo_blue_dark"
android:text="#string/next" />
</LinearLayout>

Unable to retrieve values from buttons press in Android Calculator App

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

Default pin lock screen layout

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>

Activity With buttons show blank

Notice I edited the XML File!!!!
I got an Activity that create a button and when you click on the button it calls to another Activity but when i run the app it's shows just a blank page.
Activity:
package com.elichai.tfillin;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
public void OnCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
private void displayCamera() {
Intent i = new Intent(this, CameraActivity.class);
startActivityForResult(i, 0);
}
public void addListenerOnButton() {
final Button button1 = (Button) findViewById(R.id.button1);
//Button button2 = (Button) findViewById(R.id.button2);
//Button button3 = (Button) findViewById(R.id.button3);
//Button button4 = (Button) findViewById(R.id.button4);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
displayCamera();
//finish();
}
});
}
}
And here is the activity_main.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" >
<Button
android:id="#+id/button1"
android:layout_width="166dp"
android:layout_height="216dp"
android:layout_marginLeft="7dp"
android:text="#string/button" />
<Button
android:id="#+id/button2"
android:layout_width="166dp"
android:layout_height="216dp"
android:layout_marginLeft="182dp"
android:text="#string/button" />
<Button
android:id="#+id/button3"
android:layout_width="166dp"
android:layout_height="216dp"
android:layout_marginLeft="7dp"
android:layout_marginTop="260dp"
android:text="#string/button" />
<Button
android:id="#+id/button4"
android:layout_width="166dp"
android:layout_height="216dp"
android:layout_marginLeft="182dp"
android:layout_marginTop="260dp"
android:text="#string/button" />
<TextView
android:id="#+id/textView1"
android:layout_width="223dp"
android:layout_height="52dp"
android:layout_marginLeft="85dp"
android:layout_marginTop="215dp"
android:text="#string/switch_cam"
android:textSize="30sp" />
</LinearLayout>
Thanks!
There is nothing wrong with the layout. In your MainActivity class, the onCreate() method signature is written incorrectly. Use onCreate() instead of OnCreate(). (small 'o')
The system calls onCreate() when creating your activity.
Replace the FrameLayout with a vertical LinearLayout
Try using A RelativeLayout in place of FrameLayout unless there was relay a need of using it.
or if you want to use FrameLayout try this
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/camera_preview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
<Button
android:id="#+id/button1"
android:layout_width="166dp"
android:layout_height="216dp"
android:layout_gravity="top"
android:layout_marginLeft="7dp"
android:text="#string/button" />
<Button
android:id="#+id/button2"
android:layout_width="166dp"
android:layout_height="216dp"
android:layout_gravity="top"
android:layout_marginLeft="182dp"
android:text="#string/button" />
<Button
android:id="#+id/button3"
android:layout_width="166dp"
android:layout_height="216dp"
android:layout_gravity="top"
android:layout_marginLeft="7dp"
android:layout_marginTop="260dp"
android:text="#string/button" />
<Button
android:id="#+id/button4"
android:layout_width="166dp"
android:layout_height="216dp"
android:layout_gravity="top"
android:layout_marginLeft="182dp"
android:layout_marginTop="260dp"
android:text="#string/button" />
<TextView
android:id="#+id/textView1"
android:layout_width="223dp"
android:layout_height="52dp"
android:layout_gravity="top"
android:layout_marginLeft="85dp"
android:layout_marginTop="215dp"
android:text="switchcam"
android:textSize="30sp" />
</FrameLayout>
layout_ margins does not work in FrameLayout until you also set your subviews layout gravity e.g.
android:layout_gravity="top"
( for framelayout)

onClick not executing on Buttons in Scrollview?

I have made a vertical linear layout made up of 6 horizontal linear layouts. Each horizontal layout contains 10+ buttons on a scrollview. Below is the main linear layout with one of the horizontal layouts included.
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:gravity="center"
xmlns:android="http://schemas.android.com/apk/res/android">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="75dp">
<Button
android:id="#+id/button1"
android:layout_width="75dp"
android:layout_height="75dp"
android:background="#drawable/button1"/>
<Button
android:id="#+id/button2"
android:layout_width="75dp"
android:layout_height="75dp"
android:background="#drawable/button2"/>
<Button
android:id="#+id/button3"
android:layout_width="75dp"
android:layout_height="75dp"
android:background="#drawable/button3"
android:text="" />
<Button
android:id="#+id/button4"
android:layout_width="75dp"
android:layout_height="75dp"
android:background="#drawable/button4"
android:text="" />
<Button
android:id="#+id/button5"
android:layout_width="75dp"
android:layout_height="75dp"
android:background="#drawable/button5"
android:text="" />
<Button
android:id="#+id/button6"
android:layout_width="75dp"
android:layout_height="75dp"
android:background="#drawable/button6"
android:text="" />
<Button
android:id="#+id/button7"
android:layout_width="75dp"
android:layout_height="75dp"
android:background="#drawable/button7"
android:text="" />
<Button
android:id="#+id/button8"
android:layout_width="75dp"
android:layout_height="75dp"
android:background="#drawable/button8"
android:text=""
android:textSize="" />
<Button
android:id="#+id/button9"
android:layout_width="75dp"
android:layout_height="75dp"
android:background="#drawable/button9"
android:text="" />
<Button
android:id="#+id/bCaitlyn"
android:layout_width="75dp"
android:layout_height="75dp"
android:background="#drawable/caitlynsplash"
android:text="" />
<Button
android:id="#+id/button10"
android:layout_width="75dp"
android:layout_height="75dp"
android:background="#drawable/button10"
android:text=""
android:textSize="10.5dp" />
<Button
android:id="#+id/button11"
android:layout_width="75dp"
android:layout_height="75dp"
android:background="#drawable/button11"
android:text=""
android:textSize="11dp" />
<Button
android:id="#+id/button12"
android:layout_width="75dp"
android:layout_height="75dp"
android:background="#drawable/button12"
android:text="" />
<Button
android:id="#+id/button13"
android:layout_width="75dp"
android:layout_height="75dp"
android:background="#drawable/button13"
android:text=""
android:textSize="11dp" />
</LinearLayout>
</HorizontalScrollView>
There seems to be a problem with the onClick command not executing on press. The button is recognizing the touch(as it changes colors) however nothing seems to happen.
package com.LeagueProject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Project extends Activity implements View.OnClickListener{
Button Button1, Button2, Button3, Button4, Button5, Button6, Button7, Button8,
Button9, Button10, Button11, Button12, Button13;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
projectbuttons();
Button1.setOnClickListener(this);
Button2.setOnClickListener(this);
Button3.setOnClickListener(this);
}
// Assigned Buttons
private void projectbuttons() {
Button1 = (Button) findViewById(R.id.button1);
Button2 = (Button) findViewById(R.id.button2);
Button3 = (Button) findViewById(R.id.button3);
Button4 = (Button) findViewById(R.id.button4);
Button5 = (Button) findViewById(R.id.button5);
Button6 = (Button) findViewById(R.id.button6);
Button7 = (Button) findViewById(R.id.button7);
Button8 = (Button) findViewById(R.id.button8);
Button9 = (Button) findViewById(R.id.button9);
Button10 = (Button) findViewById(R.id.button10);
Button11 = (Button) findViewById(R.id.button11);
Button12 = (Button) findViewById(R.id.button12);
Button13 = (Button) findViewById(R.id.button13);
}
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.button1:
setContentView(R.layout.screen1);
break;
case R.id.button2:
setContentView(R.layout.screen2);
break;
case R.id.button3:
setContentView(R.layout.screen3);
break;
}
}
}
Any insight as well as tips/techniques are highly welcomed.
When you implements View.OnClickListener in your Activity then you have to
Override the onClick() method else it never executes..
#Override
public void onClick(View v) {
.
.
.
}
i tried your code using default buttons without giving any images to button and it is working fine... it goes to the corresponding layout....
I'm not sure if this is what you need, but you can define the onClick() method within the layout file itself.
<Button
android:id="#+id/button1"
android:layout_width="75dp"
android:layout_height="75dp"
android:background="#drawable/button1"
android:onClick="onClick"
/>
Can you try this?

Categories

Resources