Build a Simple Web Browser App in Android

 

1. button_bg.xml (drawable)

<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="#66FFFFFF"> <item> <shape android:shape="rectangle"> <solid android:color="#3F51B5"/> <corners android:radius="12dp"/> </shape> </item> </ripple>

2. edit_text_bg.xml (drawable)

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#FFFFFF"/> <corners android:radius="12dp"/> <stroke android:width="1dp" android:color="#DDDDDD"/> </shape>

3. activity_main.xml (layout)

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#F5F5F5"> <TextView android:id="@+id/title" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="80dp" android:gravity="center" android:text="Open Website" android:textColor="#333" android:textSize="28sp" android:textStyle="bold" app:layout_constraintBottom_toTopOf="@+id/url_input" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <EditText android:id="@+id/url_input" android:layout_width="0dp" android:layout_height="55dp" android:layout_margin="32dp" android:background="@drawable/edit_text_bg" android:hint="Enter URL (e.g. example.com)" android:inputType="textUri" android:padding="16dp" android:textSize="16sp" app:layout_constraintTop_toBottomOf="@+id/title" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" /> <Button android:id="@+id/open_button" android:layout_width="0dp" android:layout_height="55dp" android:layout_margin="32dp" android:background="@drawable/button_bg" android:text="Open in Browser" android:textAllCaps="false" android:textSize="18sp" app:layout_constraintTop_toBottomOf="@+id/url_input" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>

4. MainActivity.java

package com.tecvipul.webbrowseropen; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { EditText urlInput; Button openBtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); urlInput = findViewById(R.id.url_input); openBtn = findViewById(R.id.open_button); openBtn.setOnClickListener(v -> { String url = urlInput.getText().toString().trim(); if (TextUtils.isEmpty(url)) { Toast.makeText(this, "Please enter a URL", Toast.LENGTH_SHORT).show(); return; } if (!url.startsWith("http://") && !url.startsWith("https://")) { url = "http://" + url; } startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url))); }); } }
Details & Tips:
  • Use res/drawable/ for XML drawables and res/layout/ for layouts.
  • ConstraintLayout makes your UI adapt to all screen sizes.
  • Always validate & prepend http:// if the user omits it.

If you have any query then comment below in comment section and do not forget to follow this blog.



Post a Comment

0 Comments