Here I am creating a simple Droid LoginApp, which simply tells login successfull or not. But the if statement is not giving me the desired result.
Following is the code : MainActivity.java
package com.example.login1;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener
{
public EditText t1,t2;
public Button b1;
String user,pass;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1=(EditText)findViewById(R.id.editText1);
t2=(EditText) findViewById(R.id.editText2);
b1= (Button) findViewById(R.id.button1);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void onClick(View arg0)
{
user = t1.getText().toString();
pass = t2.getText().toString();
if (((user.equals("user")) && (pass.equals("user"))))
{
Toast.makeText(this, "Login Sucessful", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(this, "Sorry", Toast.LENGTH_SHORT).show();
t1.setText("");
t2.setText("");
}
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="fill_vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="29dp"
android:layout_marginTop="28dp"
android:text="Username"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:ems="10"
android:inputType="text" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textView1"
android:layout_below="#+id/editText1"
android:layout_marginTop="53dp"
android:text="Password"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText1"
android:layout_centerVertical="true"
android:ems="10"
android:inputType="textPassword" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="86dp"
android:clickable="true"
android:onClick="onClick"
android:text="Login" />
</RelativeLayout>
While I run the App, on clicking the Login button, the if statement does not give a correct result.. I simply don't know where is the error.
Well, you should compare strings with "equals()" and not with "==".
Replace:
user == "user"
With:
user.equals("user")
And also, I think there's no need for this extra line you have:
if (arg0.findViewById(R.id.button1)==b1)
^ Why are you checking this?
use this code, for click command
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
user = t1.getText().toString();
if (user.equals("user")){
Toast.makeText(this, "Login Sucessful", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Sorry", Toast.LENGTH_SHORT).show();
}
}
}
Related
1.Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
2.First I added my two activities then I got this error
Below is XML code:-
Exact what line i got this error please give the solution with code.
<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"
tools:context=".MainActivity2">
<TextView
android:id="#+id/textview"
android:layout_width="346dp"
android:layout_height="40dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="Text to Speech"
android:textColor="#F32269"
android:textSize="28dp"
android:textStyle="bold" />
<SeekBar
android:id="#+id/seek_bar_speed"
android:layout_width="389dp"
android:layout_height="28dp"
android:layout_below="#id/textview"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="31dp"
android:layout_marginEnd="4dp"
android:progress="50" />
<TextView
android:layout_width="match_parent"
android:layout_height="28dp"
android:layout_alignBottom="#id/textview"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="1dp"
android:layout_marginTop="41dp"
android:layout_marginBottom="-29dp"
android:gravity="center"
android:text="Speed"
android:textColor="#color/black"
android:textSize="20dp" />
<ScrollView
android:layout_width="wrap_content"
android:layout_height="363dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_centerInParent="true"
android:layout_marginStart="0dp"
android:layout_marginTop="100dp"
android:layout_marginEnd="2dp">
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint=" Enter Text "
android:textColor="#000000"
android:textColorHint="#51FA2B"
android:textSize="20dp"
tools:layout_conversion_absoluteHeight="45dp"
tools:layout_conversion_absoluteWidth="89dp"
tools:layout_editor_absoluteX="12dp"
tools:layout_editor_absoluteY="179dp" />
</ScrollView>
<Button
android:id="#+id/button"
android:layout_width="418dp"
android:layout_height="71dp"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="0dp"
android:layout_marginTop="74dp"
android:layout_marginEnd="-7dp"
android:layout_marginBottom="134dp"
android:text="Text to speech"
android:textSize="18dp"
android:textStyle="bold"/>
</RelativeLayout>
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.Locale;
import android.widget.Toast;
import android.content.Intent;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.SeekBar;
public class MainActivity2 extends AppCompatActivity {
TextView textView;
private SeekBar mseekbar;
TextToSpeech t1;
EditText ed1;
Button b1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed1 = findViewById(R.id.editText);
b1 = findViewById(R.id.button);
textView = findViewById(R.id.text);
mseekbar = findViewById(R.id.seek_bar_speed);
t1 = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if (status != TextToSpeech.ERROR) {
t1.setLanguage(Locale.US);
}
}
});
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String toSpeak = ed1.getText().toString();
float speed = (float) mseekbar.getProgress() / 50;
t1.setSpeechRate(speed);
Toast.makeText(MainActivity2.this.getApplicationContext(), toSpeak, Toast.LENGTH_SHORT).show();
t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
}
});
}
public void onPause() {
if (t1 != null) {
t1.stop();
}
super.onPause();
}
}```
/* i have tried many times but i am not able to navigate to another activity while clicking on the item in the navigation drawer and when i click on that Item the app crashes(error:open app again).*/
drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/admin"
android:title="Admin Profile"
android:icon="#drawable/ic_dashboard"/>
<item
android:id="#+id/createaccount"
android:title="User Registration"
android:icon="#drawable/ic_event_black_24dp"/>
<item
android:id="#+id/manageuser"
android:title="Manage User"
android:icon="#drawable/ic_settings_black_24dp"/>
<item
android:id="#+id/us"
android:title="About us"
android:icon="#drawable/ic_call_to_action_black_24dp"/>
<item
android:id="#+id/logout"
android:title="Logout"
android:icon="#drawable/ic_cancel_black_24dp"/>
</menu>
/activity_navigation_bar.xml/
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
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:id="#+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingBottom="16dp"
android:paddingTop="16dp"
tools:context="com.ereports.navigationBar">
<android.support.v7.widget.LinearLayoutCompat
android:background="#drawable/bg"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.LinearLayoutCompat>
<android.support.design.widget.NavigationView
app:headerLayout="#layout/header"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#color/white"
app:itemTextColor="#color/darkgray"
app:itemIconTint="#color/darkgray"
app:menu="#menu/drawermenu"
android:layout_gravity="start">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
/navigationBar.java/
package com.ereports;
import android.content.Intent;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import static com.ereports.R.id.us;
public class navigationBar extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_bar);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.Open, R.string.Close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.us) {
Intent intent = new Intent(this,Registration.class);
this.startActivity(intent);
return true;
}
if (id == R.id.action_search) {
Toast.makeText(this, "Android Menu is Clicked", Toast.LENGTH_LONG).show();
return true;
}
if (id == R.id.action_settings) {
Toast.makeText(this, "Android Menu is Clicked", Toast.LENGTH_LONG).show();
return true;
}
return super.onOptionsItemSelected(item);
}
}
}
/*another activity to navigate for::
'Registration.java*/
package com.ereports;
import android.annotation.SuppressLint;
import android.content.res.ColorStateList;
import android.content.res.XmlResourceParser;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Registration extends Fragment implements OnClickListener {
private static View view;
private static EditText fullName, emailId, mobileNumber, location,
password, confirmPassword;
private static TextView login;
private static Button signUpButton1;
private static CheckBox terms_conditions;
public Registration() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_registration, container, false);
initViews();
setListeners();
return view;
}
// Initialize all views
private void initViews() {
fullName = (EditText) view.findViewById(R.id.fullName);
emailId = (EditText) view.findViewById(R.id.userEmailId);
mobileNumber = (EditText) view.findViewById(R.id.mobileNumber);
location = (EditText) view.findViewById(R.id.location);
password = (EditText) view.findViewById(R.id.password);
confirmPassword = (EditText) view.findViewById(R.id.confirmPassword);
signUpButton1 = (Button) view.findViewById(R.id.signUpBtn);
login = (TextView) view.findViewById(R.id.already_user);
terms_conditions = (CheckBox) view.findViewById(R.id.terms_conditions);
// Setting text selector over textviews
#SuppressLint("ResourceType") XmlResourceParser xrp = getResources().getXml(R.drawable.text_selector);
try {
ColorStateList csl = ColorStateList.createFromXml(getResources(),
xrp);
login.setTextColor(csl);
terms_conditions.setTextColor(csl);
} catch (Exception e) {
}
}
// Set Listeners
private void setListeners() {
signUpButton1.setOnClickListener(this);
login.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.signUpBtn1:
// Call checkValidation method
checkValidation();
break;
}
}
// Check Validation Method
private void checkValidation() {
// Get all edittext texts
String getFullName = fullName.getText().toString();
String getEmailId = emailId.getText().toString();
String getMobileNumber = mobileNumber.getText().toString();
String getLocation = location.getText().toString();
String getPassword = password.getText().toString();
String getConfirmPassword = confirmPassword.getText().toString();
// Pattern match for email id
Pattern p = Pattern.compile(Utils.regEx);
Matcher m = p.matcher(getEmailId);
// Check if all strings are null or not
if (getFullName.equals("") || getFullName.length() == 0
|| getEmailId.equals("") || getEmailId.length() == 0
|| getMobileNumber.equals("") || getMobileNumber.length() == 0
|| getLocation.equals("") || getLocation.length() == 0
|| getPassword.equals("") || getPassword.length() == 0
|| getConfirmPassword.equals("")
|| getConfirmPassword.length() == 0)
new CustomToast().Show_Toast(getActivity(), view,
"All fields are required.");
// Check if email id valid or not
else if (!m.find())
new CustomToast().Show_Toast(getActivity(), view,
"Your Email Id is Invalid.");
// Check if both password should be equal
else if (!getConfirmPassword.equals(getPassword))
new CustomToast().Show_Toast(getActivity(), view,
"Both password doesn't match.");
// Make sure user should check Terms and Conditions checkbox
else if (!terms_conditions.isChecked())
new CustomToast().Show_Toast(getActivity(), view,
"Please select Terms and Conditions.");
// Else do signup or do your stuff
else
Toast.makeText(getActivity(), "User Registered", Toast.LENGTH_SHORT)
.show();
}
}
activity_registration.java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="20dp" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="#string/signUp"
android:textColor="#color/white_greyish"
android:textSize="25sp"
android:textStyle="bold" />
<EditText
android:id="#+id/fullName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#android:color/transparent"
android:drawableLeft="#drawable/user"
android:drawablePadding="8dp"
android:gravity="center_vertical"
android:hint="#string/fullName"
android:inputType="textCapWords"
android:padding="10dp"
android:singleLine="true"
android:textColor="#color/white"
android:textColorHint="#color/white"
android:textSize="16sp" />
<View
android:layout_width="fill_parent"
android:layout_height="1px"
android:background="#color/white_greyish" />
<EditText
android:id="#+id/userEmailId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#android:color/transparent"
android:drawableLeft="#drawable/email"
android:drawablePadding="8dp"
android:gravity="center_vertical"
android:hint="#string/email"
android:inputType="textEmailAddress"
android:padding="10dp"
android:singleLine="true"
android:textColor="#color/white"
android:textColorHint="#color/white"
android:textSize="16sp" />
<View
android:layout_width="fill_parent"
android:layout_height="1px"
android:background="#color/white_greyish" />
<EditText
android:id="#+id/mobileNumber"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#android:color/transparent"
android:drawableLeft="#drawable/phone"
android:drawablePadding="8dp"
android:gravity="center_vertical"
android:hint="#string/mobileNumber"
android:inputType="phone"
android:padding="10dp"
android:singleLine="true"
android:textColor="#color/white"
android:textColorHint="#color/white"
android:textSize="16sp" />
<View
android:layout_width="fill_parent"
android:layout_height="1px"
android:background="#color/white_greyish" />
<EditText
android:id="#+id/location"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#android:color/transparent"
android:drawableLeft="#drawable/location"
android:drawablePadding="8dp"
android:gravity="center_vertical"
android:hint="#string/location"
android:inputType="textCapWords"
android:padding="10dp"
android:singleLine="true"
android:textColor="#color/white"
android:textColorHint="#color/white"
android:textSize="16sp" />
<View
android:layout_width="fill_parent"
android:layout_height="1px"
android:background="#color/white_greyish" />
<EditText
android:id="#+id/password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#android:color/transparent"
android:drawableLeft="#drawable/password"
android:drawablePadding="8dp"
android:gravity="center_vertical"
android:hint="#string/passowrd"
android:inputType="textPassword"
android:padding="10dp"
android:singleLine="true"
android:textColor="#color/white"
android:textColorHint="#color/white"
android:textSize="16sp" />
<View
android:layout_width="fill_parent"
android:layout_height="1px"
android:background="#color/white_greyish" />
<EditText
android:id="#+id/confirmPassword"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#android:color/transparent"
android:drawableLeft="#drawable/confirm_password"
android:drawablePadding="8dp"
android:gravity="center_vertical"
android:hint="#string/confirmPassword"
android:inputType="textPassword"
android:padding="10dp"
android:singleLine="true"
android:textColor="#color/white"
android:textColorHint="#color/white"
android:textSize="16sp" />
<CheckBox
android:id="#+id/terms_conditions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="7dp"
android:text="#string/terms_conditions"
android:textColor="#color/white"
android:textSize="14sp" />
<Button
android:id="#+id/signUpBtn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:background="#drawable/loginbutton_selector"
android:padding="3dp"
android:text="Register"
android:textColor="#color/background_color"
android:textSize="17sp"
android:textStyle="bold" />
</LinearLayout>
/*app is crashing
expected results: it should navigate to Registration.java
actual results: app is crashing*/
Use fragments to switch between activities
[https://www.youtube.com/watch?v=J8GB_b8qyK8]
this covers with a good explanation. I tried it myself
I've been following this tutorial: https://www.youtube.com/watch?v=sKCdqCuFFFk
I completed the tutorial, but when I wanted to launch, it gave an error. These are my codes:
(MainActivity.java)
package com.example.admir_000.jdsfnjls;
import android.content.Intent;
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 {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText etUsername = (EditText) findViewById(R.id.etUsername);
final EditText etPassword = (EditText) findViewById(R.id.etPassword);
final Button bLogin = (Button) findViewById(R.id.bLogin);
final TextView tvRegisterHere = (TextView) findViewById(R.id.tvRegisterHere);
registerLink.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent registerIntent = new Intent(LoginActivity.this, RegisterActivity.class);
LoginActivity.this.startActivity(registerIntent);
}
});
}
}
and this is my xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.admir_000.jdsfnjls.MainActivity">
<TextView
android:text="Register here"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/bLogin"
android:layout_centerHorizontal="true"
android:id="#+id/tvRegisterHere" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/etPassword"
android:hint="Password"
android:layout_marginTop="36dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/etUsername"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="Username" />
<Button
android:text="Login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/bLogin"
android:layout_below="#+id/etPassword"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
Does anyone know how to fix this?
I am sure, without read the logcat, that you got a Null Pointer Exception.... why?
registerLink.setOnClickListener....
Where did you initialized registerLink variable? Maybe you wanted to put tvRegisterHere.setOnClickListener...
P.S: I suggest you to already read the stacktrace and then search for the particular Exception
I have tried many answers already given to this, but nothing seems to work properly. And the answers are only for changing state of the button, not colour and text. What am I missing?
I am a very VERY new learner to both Android and programming. And this is my first question on Stack Overflow. Hope it's as per guidelines.
I have a login page which looks like below (img1) [disabled][1]. If textfield has any value, the button should get enabled (img2) [enabled][2]. The Java and XML files are given below.
package io.kaapi.kaapimobileassistant.Activities;
import android.content.Intent;
import android.net.Uri;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.TextView;
import io.kaapi.kaapimobileassistant.Misc.StorageManager;
import io.kaapi.kaapimobileassistant.R;
public class LoginActivity extends AppCompatActivity {
private final static String TAG = "LoginActivity";
private Button login_button;
private TextInputLayout login_activation_layout;
private EditText login_activation_code;
private LinearLayout login_signup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
login_button = (Button) findViewById(R.id.login_button);
login_activation_layout = (TextInputLayout) findViewById(R.id.login_acitivation_layout);
login_activation_code = (EditText) findViewById(R.id.login_activation_code);
login_signup = (LinearLayout) findViewById(R.id.login_signup);
login_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.v(TAG, "Login pressed");
String activation_code_layout = login_activation_layout.getEditText().getText().toString();
Log.v(TAG, "Layout "+activation_code_layout);
String activation_code = login_activation_code.getText().toString();
Log.v(TAG, "Code "+activation_code);
if(activation_code.equalsIgnoreCase("")){
Log.v(TAG, "It's blank");
login_activation_layout.setError("Please enter an activation code");
} else {
Log.v(TAG, "Call login API, validate and show errors or login");
//StorageManager.write(LoginActivity.this, null, "client_domain", "http://ankit50.kaapi.io");
//StorageManager.write(LoginActivity.this, null, "client_logo", "http://cdn.kaapi.io/static");
startActivity(new Intent(LoginActivity.this, HomeActivity.class));
}
}
});
login_activation_code.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
enableSubmitIfReady();
}
#Override
public void afterTextChanged(Editable s) {
}
public void enableSubmitIfReady() {
Button login_button = (Button) findViewById(R.id.login_button);
if(login_activation_code.toString().trim().length()==0){
login_button.setEnabled(false);
} else {
login_button.setEnabled(true);
}
}
});
login_signup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Uri uri = Uri.parse("https://business.kaapi.io");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
}
}
The XML file is below
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:background="#android:color/white"
android:gravity="center_vertical|center_horizontal"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="io.kaapi.kaapimobileassistant.Activities.LoginActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_marginLeft="71dp"
android:layout_marginRight="71dp"
android:layout_marginTop="66dp"
android:src="#drawable/kaapi_logo_login"
android:scaleType="fitCenter"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="65dp"
android:layout_marginLeft="28dp"
android:layout_marginRight="28dp"
android:textStyle="bold"
android:gravity="center"
android:textColor="#color/colorTitle"
android:text="Activate Mobile Assistant"
android:textSize="24sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center|top"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="36dp"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorText"
android:src="#drawable/ic_info_outline_black_24dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorText"
android:textSize="14sp"
android:textStyle="italic"
android:layout_marginLeft="5dp"
android:text="The code was sent to you in sign up email and your web dashboard." />
</LinearLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/login_acitivation_layout"
android:layout_width="match_parent"
android:layout_marginTop="29dp"
android:layout_height="wrap_content">
<EditText
android:id="#+id/login_activation_code"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:maxLength="20"
android:hint="Activation code"
android:maxLines="1"
android:textSize="14sp" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
style="#style/Widget.AppCompat.Button"
android:text="Activate"/>
<LinearLayout
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center|top"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text="New to Kaapi? " />
<LinearLayout
android:id="#+id/login_signup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center|top"
android:text="Sign up first"
android:textColor="#color/colorPrimary"
android:textSize="12sp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/colorPrimary" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
try this
public void enableSubmitIfReady() {
Button login_button = (Button) findViewById(R.id.login_button);
if(login_activation_code.toString().trim().length()==0){
login_button.setClickable(false);
login_button.setBackgroundColor(getResources().getColor(R.color.holo_light_green));// change color here so it's look like button disable
} else {
login_button.setClickable(true);
login_button.setBackgroundColor(getResources().getColor(R.color.holo_dark_green));
}
}
i am making an android app that uses the gps to mark you lat/long, speed etc but when i try to change the fields the app nolonger loads up, any ideas why? the first page to load up is MainActivity.java but this is just a logo screen that loads the app then uses
setContentView(R.layout.main_page);
to load the MainPageActivity.Java that i will be using to do most of the app stuff here is the code for that
here is the code for MainActivity.Java:
package com.aramis;
import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends Activity implements Runnable {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new CountDownTimer(5000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
setContentView(R.layout.main_page);
}
}.start();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
public void run() {
// TODO Auto-generated method stub
}
public void GoToMap(View view) {
setContentView(R.layout.map);
}
public void GoToOpt(View view) {
setContentView(R.layout.options);
}
public void GoToStart(View view) {
setContentView(R.layout.main_page);
}
}
and here is the code for MainPageActivity.Java:
package com.aramis;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainPageActivity extends Activity {
/** Called when the activity is first created. */
EditText et_gpsLAT;
EditText et_gpsLONG;
ProgressDialog progressDialog;
Thread thread;
String info;
LocationManager mlocManager=null;
LocationListener mlocListener;
AlertDialog.Builder alert;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_page);
et_gpsLAT=(EditText)findViewById(R.id.et_gpsLAT);
et_gpsLONG=(EditText)findViewById(R.id.et_gpsLONG);
mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
Button btn_gps=(Button)findViewById(R.id.btn_gps);
btn_gps.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
if (mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
if(MyLocationListener.latitude>0 )
{
et_gpsLAT.setText("Latitude: "
+ MyLocationListener.latitude
+ '\n');
et_gpsLONG.setText("Longitude: "
+ MyLocationListener.longitude);
}
}
}
}
);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_page, menu);
return true;
}
}
and here is a copy of the page that will display the lat/long:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/backrepeat"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:text="#string/LatENG"
tools:context=".MainActivity" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="20dp"
android:text="#string/Long"
tools:context=".MainActivity" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_below="#+id/textView2"
android:layout_marginTop="20dp"
android:text="#string/Alt"
tools:context=".MainActivity" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView3"
android:layout_below="#+id/textView3"
android:layout_marginTop="20dp"
android:text="#string/Speed"
tools:context=".MainActivity" />
<TextView
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView2"
android:layout_alignParentRight="true"
android:layout_marginRight="21dp"
android:text="#string/Current_Lat"
tools:context=".MainActivity" />
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView2"
android:layout_alignBottom="#+id/textView2"
android:layout_alignRight="#+id/TextView02"
android:text="#string/Current_Long"
tools:context=".MainActivity" />
<TextView
android:id="#+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView4"
android:layout_alignRight="#+id/TextView01"
android:text="#string/Current_Alt"
tools:context=".MainActivity" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView4"
android:layout_alignBottom="#+id/textView4"
android:layout_alignRight="#+id/TextView03"
android:text="#string/Current_Speed"
tools:context=".MainActivity" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:onClick="GoToMap"
android:text="#string/mapbtn" />
<Button
android:id="#+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:onClick="GoToOpt"
android:text="#string/optbtn" />
<Button
android:id="#+id/btn_gps"
style="#style/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/textView5"
android:layout_marginTop="33dp"
android:text=" GPS " />
<EditText
android:id="#+id/et_gpsLAT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/btn_gps"
android:layout_centerHorizontal="true"
android:editable="false"
android:ems="10" />
<EditText
android:id="#+id/et_gpsLONG"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/et_gpsLAT"
android:layout_below="#+id/et_gpsLAT"
android:editable="false"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/tv_gps"
style="#style/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/btn_gps"
android:layout_alignParentLeft="true"
android:text="Location :" />
</RelativeLayout>
any help with this would be apreciated i have been stuck on this for a while now