Write a Program to create a text field and a button “Browse”. When you enter “www.google.com” and press browse button it should open google page.
Code:
Activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_above="@id/t1"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:hint="Enter URL"
android:ems="10"
android:id="@+id/e1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Browse"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:id="@+id/t1"
/>
</RelativeLayout>
Main_Activity.java:
package com.example.myapplication;
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;
public class Main3Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
final EditText e1=(EditText)findViewById(R.id.e1);
final Button b1=(Button)findViewById(R.id.t1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String n = e1.getText().toString();
Intent c =new Intent(Intent.ACTION_VIEW,Uri.parse(n));
startActivity(c);
}
});
}
}
Output:
1]Original Layout 2] After Click on Browse button
Comments
Post a Comment