Android Development Login Form Code For Java and XML
Java Code (LoginActivity.java)
Create a new Java class (e.g., LoginActivity.java) and add the following code to handle the login logic:
// LoginActivity.java
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class LoginActivity extends AppCompatActivity {
private EditText editTextUsername, editTextPassword;
private Button buttonLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// Initialize views
editTextUsername = findViewById(R.id.editTextUsername);
editTextPassword = findViewById(R.id.editTextPassword);
buttonLogin = findViewById(R.id.buttonLogin);
// Set click listener for login button
buttonLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Retrieve input values
String username = editTextUsername.getText().toString().trim();
String password = editTextPassword.getText().toString().trim();
// Check if username and password are correct
if (username.equals("your_username") && password.equals("your_password")) {
// Successful login
Toast.makeText(LoginActivity.this, "Login Successful", Toast.LENGTH_SHORT).show();
// Add your code to proceed to the next activity or perform other actions
} else {
// Invalid login
Toast.makeText(LoginActivity.this, "Invalid username or password", Toast.LENGTH_SHORT).show();
}
}
});
}
}
Create a new XML file in your res/layout
directory called activity_login.xml
:
<!-- activity_login.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/editTextUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:inputType="text"/>
<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"/>
<Button
android:id="@+id/buttonLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"/>
</LinearLayout>
- Layout XML (activity_login.xml):
- Defines a simple login form with two EditText fields for username and password, and a Button for login.
- Java Code (LoginActivity.java):
- Sets up the LoginActivity class which extends AppCompatActivity.
- Retrieves references to the EditText fields and Button from the layout XML.
- Sets an OnClickListener on the login button to handle login attempts.
- Checks if the entered username and password match hardcoded values ("your_username" and "your_password" in this example).
- Displays a toast message indicating success or failure of the login attempt.
- Security: Hardcoding usernames and passwords directly in code is insecure. In a real application, you would use secure storage methods like encryption or authentication services.
- Error Handling: This example does minimal error handling (e.g., empty fields), which should be expanded upon in a real application.
No comments
info.kroyhouse24@gmail.com