How to build your own browser app in android studio
Enter the following codes in Main activty.java
package com.my.nebrowser;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private EditText url;
private Button search1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
url = findViewById(R.id.url);
search1 = findViewById(R.id.searchurl);
search1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(TextUtils.isEmpty(url.getText().toString()))
{
Toast.makeText(MainActivity.this, "Please Enter the url you want to go", Toast.LENGTH_SHORT).show();
}
else
{ final String searchurl = url.getText().toString().replaceAll("https://www.", "");
Intent intent = new Intent(MainActivity.this, search.class); //we have to send the view to another activity lets make another activity
// if anyone does not enter https://www.com then nothing will shown in the result to prevent this
String https = "https://www.google.com/search?q=";
// google for google result
// if anyone enter the https in his web address then it will overwrite the https to prevent this
// now add this https with the entered url
intent.putExtra("myurl", https+searchurl);// if searchurl is emty then ??
startActivity(intent);
// now lets run the app
// thank you for watching
}
}
});
}
}
Enter the following codes in 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_width="300dp"
android:layout_height="40dp"
android:inputType="textUri"
android:id="@+id/url"
android:hint="Please Enter URL"
android:gravity="center"
android:background="@drawable/round"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"></EditText>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/url"
android:id="@+id/searchurl"
android:layout_centerHorizontal="true"
android:text="GO"
android:textColor="#000"
android:background="@drawable/round"
android:layout_marginTop="7dp"></Button>
</RelativeLayout>
Creat a new Empty activity and Enter the following codes in searchActivity.java
package com.my.nebrowser;
import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class search extends AppCompatActivity {
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
webView = findViewById(R.id.searchweb);
String url = getIntent().getExtras().getString("myurl");
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl(url);
webView.setWebViewClient(new WebViewClient());
webView.setWebChromeClient(new WebChromeClient());
}
@Override
public void onBackPressed()
{
if(webView.canGoBack())
{
webView.goBack();
}
else
{
super.onBackPressed();
}
}
}
Enter the following coades in serach.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=".search">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="2dp"
android:id="@+id/searchweb"></WebView>
</RelativeLayout>
For round Edit text create a Xml.Resource file and Enter the following codes
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="20dp"></corners>
<stroke android:color="@color/colorAccent">
</stroke>
<stroke android:width="1dp"></stroke>
</shape>
For any quary please comment below
Now run the app on your Emullator........
Comments
Post a Comment