Password text Field - java

I am doing a project for class where I would like to create a little password login in Android Studio. I want to create something simple and I know how to do it on Java but I don't know how I would go about doing it in this application. I basically want to get up a password box and a button. On down of the button I would like to test the input of the edit text password box to see if it equals the variable. This variable would be set and definite to something like root. I need to find a way to test that output on the password field to see if it equals the variable. If it does then it would move to another page. The code will be below
my Java file:
package com.example.murdocbgould.passwordpt4;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String passwordA = "root";
}
}
my XML file:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.murdocbgould.passwordpt4.MainActivity">
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword"
tools:layout_editor_absoluteX="85dp"
tools:layout_editor_absoluteY="260dp" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password:"
tools:layout_editor_absoluteX="160dp"
tools:layout_editor_absoluteY="226dp" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
tools:layout_editor_absoluteX="148dp"
tools:layout_editor_absoluteY="437dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="328dp"
android:layout_height="43dp"
android:text="Bluetooth Texting Login"
android:textSize="30sp"
tools:layout_editor_absoluteX="28dp"
tools:layout_editor_absoluteY="147dp" />
</android.support.constraint.ConstraintLayout>

Here getting text from edittest and compare on button click if it matches than go to another activity.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String passwordA = "root";
EditText editText = (EditText) findViewById(R.id.editText);
Button button = (Button) findViewById(R.id.button);
TextView textView2 = (TextView) findViewById(R.id.textView2);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textView2.setText(editText.getText().toString().trim());
if(editText.getText().toString().trim().equals(passwordA)){
Intent i = new Intent(MainActivity.this, SecondActivity.class);
startActivity(i);
}else{
// Do what you want when password is not matches.
}
}
});
}
}

you can get the input from the EditText and compare it with the password variable in onClick of the button.
//Making reference of edittext.
EditText etPassword = (EditText)findViewById(R.id.editText)
// setting onclick to Button.
((Button)findViewById(R.id.button)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// checking the required condition.
if(etPassword.getText().toString().trim().equal(YOUR_PASSWORD_VARIABLE))
{
//PASSWORD MATCHES
}else
{
//PASSWORD MISSMATCHE
}
}
});

EditText editText = (EditText)findViewById(R.id.edittext);
Button button = (Button)findViewById(R.id.Button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (editText.getText().toString().equals(passwordA)){
startActivity(new Intent(this,OtherActivity.class));
}
}
});

First, you need to reference your buttons in the java code and then you have to get references to all the necessary textfields too, see the simple implementation below:
package com.example.murdocbgould.passwordpt4;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//make references to your edittext and buttons in the activity_main.xml
EditText passwordField = (EditText) findViewById(R.id.editText);
Button submitButton = (Button) findViewById(R.id.button);
String passwordA = "root";
//listen for button clicks here
submitButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String password = passwordField.getText().toString();
//compare your password with password and continue
//if you wish to move to another page (which I assume is an activity), edit the next line
startActivity(new Intent(MainActivity.this, NewActivity.class));
}
});
}
}

EditText editText = (EditText)findViewById(R.id.edittext);
Button button = (Button)findViewById(R.id.Button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (editText.getText().toString().equals(passwordA)){
startActivity(new Intent(this,OtherActivity.class));
}else{
//Pasword Not Match
}
}
});

EditText mEditText = (EditText)findViewById(R.id.edittext);
Button mBtn = (Button)findViewById(R.id.Button);
mBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (editText.getText().toString().equals(passwordA)){
startActivity(new Intent(this,AnotherActivity.class));
}
}
});

button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textView.setText(editText.getText().toString().trim());
if(editText.getText().toString().trim().equals(passwordA)){
Intent i = new Intent(MainActivity.this, SecondActivity.class);
startActivity(i);
}else{
// Show the user as a Toast that the password is incorrect.
}
}
});

Related

How to pass edit Text value to Toast?

I've just started with android and I'm trying to make simple program to take string from user and display the result as Toast
EditText e = (EditText) findViewById(R.id.editText);
final String result = e.getText().toString();
Button btx = (Button) findViewById(R.id.button2);
btx.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
}
Now If I type anything in editText it's supposd to print that value but instead it's printing the default text value and even if I edit that it prints the same value
As you can see in the picture on pressing the button it shows "Name" on toast
and even upon changing it shows the same thing that is "Name". I want it too show the value that I typed later.
What should I do?
You store the text when you setup the views. Thats why you get the default text.
move
final String result=e.getText().toString();
into the onClick
#Override
public void onClick(View v) {
final String result = e.getText().toString();
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
and it should get the text when the button is pressed.
Reason is your variable String result is not taking latest value when button is clicked.
Try this:
final EditText e = (EditText) findViewById(R.id.editText);
Button btx = (Button) findViewById(R.id.button2);
btx.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String result = e.getText().toString();
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
}
your requirement is show EditText Text in Toast
first make EditText object as globle
class ActivityClassFileName extend AppCompactActivity
{
EditText ToastText;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activitylayout);
//initialize EditText object
ToastText = (EditText) findViewById(R.id.editText);
Button btx = (Button) findViewById(R.id.button2);
btx.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
//showing Toast
Toast.makeText(getApplicationContext(), ToastText.getText().toString(), Toast.LENGTH_LONG).show();
}
}
}
}
Sometimes you have to display toast outside of onclick,by creating new method.
public class MainActivity extends AppCompatActivity {
EditText editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Button button = findViewById(R.id.button);
editText=findViewById(R.id.editTextView);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String testString=editText.getText().toString();
showToast(testString);
}
});
}
private void showToast(String str) {
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
}
}

NullpointerException Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I want to do something with dialog in my code and I have a problem with this thing. My problem occurs at here:
btnDialogCancel.setOnClickListener(new View.OnClickListener()
Can anyone please help me ? thx a lot
Here is my MainActivity class:
public class MainActivity extends AppCompatActivity {
private ProgressBar prgChronemeter;
private TextView txtShowSecond;
private Handler handler = new Handler();
private Button btnSelectMin;
private Button btnDialogSelect;
private Button btnDialogCancel;
private EditText etMinute;
private Dialog dialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prgChronometer = (ProgressBar) findViewById(R.id.prgChronometer);
txtShowSecond = (TextView) findViewById(R.id.txtShowSecond);
btnSelectMin = (Button) findViewById(R.id.btnSelectMin);
btnSelectMin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
customDialogCalistir();
}
});
}
private void customDialogCalistir(){
dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog);
btnDialogCancel = (Button) findViewById(R.id.btnDialogCancel);
btnDialogSelect =(Button) findViewById(R.id.btnDialogSelect);
etMinute = (EditText) findViewById(R.id.etMinute);
btnDialogCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
btnDialogSelect.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
Here is my dialog.xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/btnDialogSelect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:text="SeƧ"
android:layout_marginStart="42dp"
android:layout_marginTop="155dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_centerHorizontal="true"
android:id="#+id/etMinute"
android:layout_marginTop="50dp"
/>
<Button
android:id="#+id/btnDialogCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginEnd="12dp"
/>
</RelativeLayout>
private void customDialogCalistir(){
dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog);
btnDialogCancel = (Button) dialog.findViewById(R.id.btnDialogCancel);
btnDialogSelect =(Button) dialog.findViewById(R.id.btnDialogSelect);
etMinute = (EditText) dialog.findViewById(R.id.etMinute);
btnDialogCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
btnDialogSelect.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
Use this will Solve your problem.
btnDialogCancel = (Button) findViewById(R.id.btnDialogCancel);
btnDialogSelect =(Button) findViewById(R.id.btnDialogSelect);
etMinute = (EditText) findViewById(R.id.etMinute);
you cant refer directly to Dialog elements directly grom main class using findViewByid method. Instead of reffering to dialog's elements,use this
btnDialogCancel = (Button) dialog.findViewById(R.id.btnDialogCancel);
btnDialogSelect =(Button) dialog.findViewById(R.id.btnDialogSelect);
etMinute = (EditText) dialog.findViewById(R.id.etMinute);

How to use EditText value (numberDecimal) in math operations Java

I have a EditText view :
<EditText
android:id="#+id/vorgabezeit"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:background="#0277BD"
android:imeOptions="actionDone"
android:inputType="numberDecimal"
android:maxLength="5"
android:textAlignment="textEnd"
android:textColor="#ffffff"
android:textCursorDrawable="#null"
android:textStyle="bold"
/>
The value entered in this EditText I would like to use it in an math operation.
Before I had fixed values :
public void submitOrder(View view) {
displayPrice((procente / myEditNum) * mitarbeiter);
}
where variables were defined as :
int procente = 120;
int mitarbeiter = 1;
Now, I searched for hours on the web how to take that value entered in EditText and use id further, and this is as close as I got :
public class MainActivity extends AppCompatActivity {
int procente = 120;
int mitarbeiter = 1;
EditText myEdit = (EditText) findViewById(R.id.vorgabezeit);
String myEditValue = myEdit.getText().toString();
double myEditNum = Double.parseDouble(myEditValue);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void submitOrder(View view) {
displayPrice((procente / myEditNum) * mitarbeiter);
}
}
The application crashes from the start.
Thank you for your patience and help.
Best regards,
Robert
First you need to post the LogCat when you are having a crash.
Second You can not call findViewById until after setContentView is called. You should to initialize your edit text in onCreate.
// Initialize values in onCreate after setContentView
EditText myEdit;
String myEditValue;
double myEditNum;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myEdit = (EditText) findViewById(R.id.vorgabezeit);
myEditValue = myEdit.getText().toString();
myEditNum = Double.parseDouble(myEditValue);
}
Make sure you add android:inputType="number" to your EditText on the xml to ensure the user can ONLY type numbers on it, then :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.button)
.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
displayPrice((procente / Double.parseDouble(((EditText) findViewById(R.id.vorgabezeit))
.getText().toString())) * mitarbeiter);
}
});
}
btw your code crashes because you called findviewById before setting the content view to the activity so basically it searches on a non-existing layout and your edit text and button become null because of it
What about getText(). I added button, since you have to get text after some user action, not right after onCreate event.
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton = (Button)findViewById(R.id.button);
mEdit = (EditText)findViewById(R.id.vorgabezeit);
mButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
Log.v("EditText", mEdit.getText().toString());
}
});
}

Android: My app won't run if I put Switch OnClickListener on a FragmentActivity

I'm a beginner at Android programming. Please help me with this issue.
Activity_main.xml
<Button
android:id="#+id/button1"
android:layout_width="70dp"
android:layout_height="20dp"
android:layout_alignLeft="#+id/textView4"
android:layout_below="#+id/imageView2"
android:background="#2c3e50"
android:enabled="false"
android:minHeight="15dip"
android:padding="3dp"
android:text="Read More..."
android:textColor="#fff"
android:textColorHint="#fff"
android:textSize="9sp" />
MainActivity.java
public class MainActivity extends FragmentActivity {
Button switchButton1 = (Button) findViewById(R.id.button1);
switchButton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,Caraga_Agusan_del_norte.class);
startActivity(intent);
}
});
}
This code won't run.
Try this way
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
Button switchButton1 = (Button) findViewById(R.id.button1);
switchButton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,Caraga_Agusan_del_norte.class);
startActivity(intent);
}
});
}
you have to run
Button switchButton1 = (Button) findViewById(R.id.button1);
switchButton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,Caraga_Agusan_del_norte.class);
startActivity(intent);
}
});
after you have called setContentView, and the button has to be declared inside the xml you provide as parameter to setContentView

how do i link a button to a second activity after linking the first buttons in my main activity to websites?

How do i make a button open a activity if the rest of my buttons are linked to web pages with a onClickListener?
I go the webpages to open correctly in eclipse by using:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b1=(Button)findViewById(R.id.button1);
public void onClick(View v) {
// TODO Auto-generated method stub
sendToCoaches();
}
private void sendToCoaches() {
// TODO Auto-generated method stub
String url = "http://www.signal5crossfit.com/coaches/";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(android.net.Uri.parse(url));
startActivity(i);
}
});
b7.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
sendToContacts();
}
public void sendToContacts() {
Intent intent = new Intent(AppActivity.this, App2Activity.class);
startActivity(intent);
}
});}}
I changed it to the second example that you up in to include the onClick in my xml file and this is the error i get.
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.Signal5.android/com.Signal5.android.App2Activity}: java.lang.NullPointerException
Does that mean its pointing to nowhere?
I'm guessing you're using android:onClick="onClick" in your xml layout.
Example:
<Button android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"
android:onClick="onClick" />
<Button android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"
android:onClick="onClick" />
You can check which view was clicked by getting its id: View.getId().
public void onClick(View v) {
switch(v.getId()){
case R.id.button1:
sendToCoaches();
break;
case R.id.button2:
doSomethingElse();
break;
}
}
Another way of doing this would be to set a View.OnClickListener for each button:
Button b1 = (Button) findViewById(R.id.button1);
Button b2 = (Button) findViewById(R.id.button2);
b1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
sendToCoaches();
}
});
b2.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
doSomethingElse();
}
});

Categories

Resources