Android Java derslerimize devam ediyoruz. Bu örneğimizde Android Intent kullanımına değineceğim. Intent kullanarak Butona basıldığında editText’ e girilen url (web sayfası adresi) nin açılmasını sağlayacağız.
Android’de Intent, etkinlikler, hizmetler, yayın alıcıları ve içerik sağlayıcılar gibi başka bir uygulama bileşeninden bir işlem istemek için kullanılan bir mesajlaşma nesnesidir.
Intent’ ler genellikle, aynı uygulamadaki uygulama bileşenleri ile diğer uygulamaların bileşenleri arasındaki iletişimi sürdürmemize yardımcı olur.
Tasarımımız aşağıdaki gibi olacaktır.

activity_main.xml dosyamız:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <EditText android:id="@+id/editTextUrl" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="32dp" android:layout_marginEnd="8dp" android:ems="10" android:inputType="textPersonName" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="32dp" android:text="Button" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/editTextUrl" /> </androidx.constraintlayout.widget.ConstraintLayout> |
MainActivity.java dosyamız:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | package com.example.websiteacma; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import java.net.URISyntaxException; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EditText editTextAdress=(EditText) findViewById(R.id.editTextUrl); Button btn=(Button) findViewById(R.id.button); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String url=editTextAdress.getText().toString(); Intent intent=new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse(url)); startActivity(intent); } }); } } |
Ekran Çıktımız:

