Creating A Login Page Activity in Android Studio Using Java and XML
Greetings to my favourite science community online, StemSocial.
It's @skyehi and for months without skipping a day, I'v been sharing a lot of medical science blogs to both contribute to the body of scientific knowledge and to create awareness for medical issues.
However, I am also well-versed in programming languages and for today I wanted to share a blog that is related to that.
Of course this wouldn't be a series but just a way to spice things up on StemSocial. I'll definitely be going back to my medical science blogs tomorrow since that's more like my area of expertise.
Original Image Source by 200degrees from Pixabay
In today's blog, we're going to briefly learn how to create a full login page activity android app in Android Studio using Java and XML programming language.
As I do with most of my programming blogs, I'll keep it simple and straight to the point so you can easily follow and learn how to get it done.
So without wasting anymore of your time guys, let's get right into the codes shall we.
Step 1: Setting Up the Project
Now first things first, make sure you've got Android Studio installed on your computer. Once that's sorted, let me share with you how you kick off a new Android project:
First Open up Android Studio, and you'll see a "Start a new Android Studio project" option. Click on that option.
Then after, you'll need to pick a project template and set things up your way.
Now when you're asked to add an activity, go for "Empty Activity," and just hit "Finish." I recommend empty activity so we can have it easy developing the codes without interference from other template codes.
Step 2: Designing Your Login Page
Now, it's time for my favorite part of building apps, playing designer.
First of all, open the "activity_main.xml" file in the "res/layout" folder. You can design it any how you see fit like you do with Picasso or any design tool or platform. However I've put together a simple layout code snippet that should help you design the layout with no issues.
<?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"
android:padding="16dp"
tools:context=".MainActivity">
<ImageView
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_gravity="center"
android:src="@drawable/ic_launcher_foreground"
android:contentDescription="Logo"/>
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"/>
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:id="@+id/loginButton"/>
</LinearLayout>
This layout we just designed gives you an image, a spot for username and password, and a button to hit. It's the basic things you would need for any login page. If you're having troubles understanding the codes, you can comment down below and I'll come to your aid guys.
Step 3: Writing the Java Code
Now guys, it's time for the backend code. This is where we'll make the functionality for your login app to work perfectly fine.
So I guess it's time to roll up our sleeves and write some java code.
I'v put together another snippet to help you get the job done. Feel free to work up from my codes. You can add any other Functionality if you wsih to. Just have fun out there guys.
To start coding, Open the "MainActivity.java" file and use this code:
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private EditText usernameEditText, passwordEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
usernameEditText = findViewById(R.id.username);
passwordEditText = findViewById(R.id.password);
Button loginButton = findViewById(R.id.loginButton);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = usernameEditText.getText().toString();
String password = passwordEditText.getText().toString();
// This is where you put your secret login recipe
if (username.equals("your_username") && password.equals("your_password")) {
// Bam! You're in, buddy.
Toast.makeText(MainActivity.this, "Login successful", Toast.LENGTH_SHORT).show();
} else {
// Uh-oh, looks like something's not right.
Toast.makeText(MainActivity.this, "Login failed", Toast.LENGTH_SHORT).show();
}
}
});
}
}
Now this code sets up the login page. The login logic here is super basic, but like I mentioned earlier, you can always spice it up later.
Now that's just it guys, you can hit the run button on Android studio after connecting your android phone to it. The run button is the green triangle or play button at the top of your Android studio screen.
And there you have it guys. It's a simple login page for your Android app.
Always remember, this is just the start. You can make it fancier and better looking by adding things like data checks and user authentication.
Practicing these codes and doing further research. definitely going to go a long way to help you improve.
I hope my readers found this blog both insightful and entertaining. Just in case you're going through Google looking for solutions to creating your login page as a beginner programmer, you can totally use this.
If you're new to StemSocial, I'll like to introduce it to you. It's in my opinion the best science community on any Blockchain based platform online. You will get a lot of science related content that will inform you of the advancements and prospects of the science world.
You can join this community and also contribute your knowledge of science.
Thank you so much for taking the time to read today's blog. As always, if you have any question or comment regarding today's blog, you can comment it down below and I'll totally respond to it.
Also guys if you're having trouble writing or running these codes, I'll totally be around to be of help, you can comment down below.
Have a lovely day and catch you next time on StemSocial. Goodbye 👨🔬
You Can Follow Me @skyehi For More Like This And Others
Congratulations!
✅ Good job. Your post has been appreciated and has received support from CHESS BROTHERS ♔ 💪
♟ We invite you to use our hashtag #chessbrothers and learn more about us.
♟♟ You can also reach us on our Discord server and promote your posts there.
♟♟♟ Consider joining our curation trail so we work as a team and you get rewards automatically.
♞♟ Check out our @chessbrotherspro account to learn about the curation process carried out daily by our team.
🏅 If you want to earn profits with your HP delegation and support our project, we invite you to join the Master Investor plan. Here you can learn how to do it.
Kindly
The CHESS BROTHERS team
Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!
Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).
Thanks for including @stemsocial as a beneficiary, which gives you stronger support.