Android_slip_13A

1. Create the layout file activity_main.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">

    <TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="1">

    <TableRow>
    <TextView
    android:text="Username"
    android:padding="8dp"
    android:textSize="18sp" />

    <EditText
    android:id="@+id/editTextUsername"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="8dp"
    android:hint="Enter your username" />
    </TableRow>

    <TableRow>
    <TextView
    android:text="Password"
    android:padding="8dp"
    android:textSize="18sp" />

    <EditText
    android:id="@+id/editTextPassword"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="8dp"
    android:inputType="textPassword"
    android:hint="Enter your password" />
    </TableRow>

    <TableRow>
    <Button
    android:id="@+id/btnLogin"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Login" />
    </TableRow>
    </TableLayout>
    </LinearLayout>


2. Create Second Activity Layout (activity_second.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:gravity="center">

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Welcome to the Second Activity!"
    android:textSize="20sp"
    android:textStyle="bold"
    android:padding="16dp"/>

    </LinearLayout>


3. Create MainActivity Class (MainActivity.java):
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TableRow;
    import android.widget.Toast;

    import androidx.appcompat.app.AppCompatActivity;

    public class MainActivity extends AppCompatActivity {

    private EditText editTextUsername, editTextPassword;
    private Button btnLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    editTextUsername = findViewById(R.id.editTextUsername);
    editTextPassword = findViewById(R.id.editTextPassword);
    btnLogin = findViewById(R.id.btnLogin);

    btnLogin.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    performLogin();
    }
    });
    }

    private void performLogin() {
    String username = editTextUsername.getText().toString().trim();
    String password = editTextPassword.getText().toString().trim();

    // Check hardcoded login credentials (You can replace this with your own logic)
    if (username.equals("user") && password.equals("password")) {

    // Successful login, show a toast
    Toast.makeText(this, "Login Successful!", Toast.LENGTH_SHORT).show();

    // Start the second activity
    Intent intent = new Intent(MainActivity.this, SecondActivity.class);
    startActivity(intent);
    } else {
    // Invalid login, show a toast
    Toast.makeText(this, "Invalid username or password", Toast.LENGTH_SHORT).show();
    }
    }
    }


4. Create SecondActivity Class (SecondActivity.java):
    import android.os.Bundle;
    import android.widget.TextView;

    import androidx.appcompat.app.AppCompatActivity;
    public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);

    TextView textViewWelcome=findViewById(R.id.textViewWelcome);
    }
    }

No comments:

Post a Comment