Android URL Display and Open Example
This Android application allows users to enter a URL, display it on a TextView, and open it in a web browser using an Intent.
XML Layout File (activity_p22_bca.xml)
<?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:gravity="center"
android:padding="24dp"
android:background="#F3F4F6">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter URL (https://)"
android:inputType="textUri"
android:importantForAutofill="no"
android:layout_marginBottom="16dp"
android:padding="12dp"
android:background="@drawable/edittext_background"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Display URL"
android:backgroundTint="#3498db"
android:textColor="#ffffff"
android:layout_marginBottom="24dp"/>
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="URL will appear here"
android:textSize="18sp"
android:textColor="#2c3e50"
android:gravity="center"
android:padding="16dp"/>
<Button
android:id="@+id/websiteButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open in Browser"
android:backgroundTint="#2ecc71"
android:textColor="#ffffff"/>
</LinearLayout>
Java Code (P22BCA.java)
// Package declaration
package com.vipul.p6;
// Import statements
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 android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
// Main activity class
public class P22BCA extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_p22_bca);
// Initialize UI components
EditText editText = findViewById(R.id.editText);
Button btnDisplay = findViewById(R.id.button);
TextView textView = findViewById(R.id.textView);
Button btnOpenBrowser = findViewById(R.id.websiteButton);
// Display URL button click handler
btnDisplay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String url = editText.getText().toString().trim();
if (!url.isEmpty()) {
textView.setText(url);
} else {
editText.setError("Please enter a URL");
}
}
});
// Open browser button click handler
btnOpenBrowser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String url = editText.getText().toString().trim();
if (url.isEmpty()) {
Toast.makeText(P22BCA.this,
"Please enter a URL first",
Toast.LENGTH_SHORT).show();
return;
}
if (!url.startsWith("http://") && !url.startsWith("https://")) {
url = "https://" + url;
}
Intent browserIntent = new Intent(
Intent.ACTION_VIEW,
Uri.parse(url)
);
if (browserIntent.resolveActivity(getPackageManager()) != null) {
startActivity(browserIntent);
} else {
Toast.makeText(P22BCA.this,
"No browser app found",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
Explanation
- XML Layout: Defines UI elements - EditText, Buttons, and TextView.
- Java Code: Handles user interaction, fetching text from EditText, displaying it in TextView, and opening it using an Intent.
- Intent: Used to open the entered URL in a browser.
- Toast Message: Displays a message if the URL field is empty.
0 Comments
If you have any doubts, Please let me know