Android Programming-6 Introduction to Java programming

Hi guys in this tutorial we will start to java programming.

In my previous tutorial, I talked about the imageview components. If you have not read it yet, I recommend you read it first.

https://ittutorial.org/android-programming-5-imageview-components/

With the xml components that we have learned , we can design and code applications in many different scenarios. So we will now examine the Java code on the back of these components.

package com.example.teka.icim_rahat;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

EditText kullanici_adi, sifre;
Button gris;


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

//tanımlamalar
kullanici_adi= (EditText) findViewById(R.id.kullanici);
sifre = (EditText) findViewById(R.id.sifre);
gris = (Button) findViewById(R.id.gris);

 // griş buttonuna tıklanınca
                gris.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(MainActivity.this, "Giriş Butonuna Tıkladınız" , Toast.LENGTH_SHORT).show();

If we examine our example;

First of all we need to import all libraries which we will use. After import we define the MainActivity class. We designed and coded xml application files to be displayed on the phone screen we need an Activity class so MainActivity class is too important. Activity class have specific functions. The most important of these is the onCreate function. This function works when the activity class is created.

When we write code, we need to know these functions very well and write our codes into the function we need accordingly. Otherwise, our application will not work correctly. We will examine these functions in more detail in our next tutorial.

Before defining the Oncreate function, we define our global variables that we will use in the application. If you remember when coding xml, all the components were defined id. With the findviewbyid function of java codes, we assign values to global variables.

To activate the click feature of the button component, we create the setOnClickListener function of the button. In this function, we need to write the codes of the events that we want to be clicked on the button.

 16 views today,  1 views in last 3 hour

Kübra Hebeş

Bilgisayar Mühendisi

Leave a Reply