Sunday, 6 February 2022

Android App - Browser Specific URL testing - Source Code

1) activity_main.xml - Layout design

 <?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">

<Button
android:id="@+id/btnChrome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="55dp"
android:layout_marginTop="163dp"
android:layout_marginBottom="273dp"
android:onClick="btnChrome"
android:text="Chrome"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/urlText" />

<Button
android:id="@+id/btnEdge"
android:layout_width="96dp"
android:layout_height="51dp"
android:layout_marginStart="94dp"
android:layout_marginEnd="69dp"
android:layout_marginBottom="273dp"
android:onClick="btnEdge"
android:text="Edge"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/btnChrome" />

<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="91dp"
android:layout_marginBottom="69dp"
android:fontFamily="sans-serif-medium"
android:gravity="center"
android:text="Browser URL Tester"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@+id/urlText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />

<EditText
android:id="@+id/urlText"
android:layout_width="242dp"
android:layout_height="48dp"
android:layout_marginEnd="55dp"
android:layout_marginBottom="160dp"
android:ems="10"
android:hint="Enter URL"
android:inputType="text"
app:layout_constraintBottom_toTopOf="@+id/btnEdge"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView3" />

<TextView
android:id="@+id/textView3"
android:layout_width="74dp"
android:layout_height="31dp"
android:layout_marginStart="30dp"
android:layout_marginTop="80dp"
android:layout_marginBottom="171dp"
android:gravity="center"
android:text="http://"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/btnChrome"
app:layout_constraintEnd_toStartOf="@+id/urlText"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="1.0" />

<androidx.constraintlayout.helper.widget.Flow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@+id/btnEdge"
app:layout_constraintStart_toEndOf="@+id/btnChrome" />

<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="289dp" />

<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="286dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java - Main Java File
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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

public void btnChrome(View view){
TextView tView = findViewById(R.id.textView);
EditText urlTxt = findViewById(R.id.urlText);
String URL = urlTxt.getText().toString();
tView.setText(URL.toString());
openUrlChrome("http://"+URL.toString());
}

public void btnEdge(View view){
TextView tView = findViewById(R.id.textView);
EditText urlTxt = findViewById(R.id.urlText);
String URL = urlTxt.getText().toString();
tView.setText(URL.toString());
openUrlEdge("http://"+URL.toString());
}

public void openUrlChrome(String url) {
//url = url.replace("http://","");
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setPackage("com.android.chrome");
try {

startActivity(intent);
} catch (ActivityNotFoundException ex) {

intent.setPackage(null);
startActivity(Intent.createChooser(intent, "Select Browser"));
}

}

public void openUrlEdge(String url) {
//url = url.replace("http://","");
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setPackage("com.android.edge");
try {

startActivity(intent);
} catch (ActivityNotFoundException ex) {

intent.setPackage(null);
startActivity(Intent.createChooser(intent, "Select Browser"));
}
}
}





No comments:

Post a Comment