Android Programming -7 java programming

Hi guys in this tutorial we will continue the java programming.

In my previous tutorial, I started the java programming. If you have not read it yet, I recommend you read it first.

https://ittutorial.org/android-programming-6-introduction-to-java-programming/

Today, we will take a string that the user writes in edittext, check it and perform different scenarios according to the result.

package com.example.mac.makale;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
EditText kullanici_adi,sifre;
Button login;
TextView mesaj;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        kullanici_adi=(EditText)findviewbyid(R.id.user);
        sifre=(EditText)findviewbyid(R.id.password);
        login=(Buton)findviewbyid(R.id.login);
        mesaj=(TextView)findviewbyid(R.id.text); 
        
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (kullanici_adi.getText()=="kubrahebes" && sifre.getText()=="12345") {
                    mesaj.setText("Başarılı giriş");
                }
                else {
                    mesaj.setText("Başarısız Giriş !!!");
                }
            }
        });
    }
}

If we examine our example;

We define edittext, textview and button globally  variables. Then we assign the components we define in xml codes to our variables with findviewbyid function. By setting the onclick feature of the Login button, we encode the actions to be performed even when the button is clicked.

With the gettext feature of Edittext, we take and check the user name and password values ​​entered by the user.  We define manually User name ‘kubrahebes’, password ‘12345′.  And by comparing the values entered by the user with these values.

We inform the user by typing whether the login is successful or not. We provide this information through the textview settext function.

Kübra Hebeş

Bilgisayar Mühendisi

Leave a Reply