Android: WebView simple app - android

I am learning Android development and have completed the first app (https://developer.android.com/training/basics/firstapp/index.html). Now, I added a second text field and a second button. The idea is that the text that the user will enter in the text field will be used as an input in a Google Search Engine.
So I did the following:
Modified the AndroidManifest.xml to add the permission <uses-permission android:name="android.permission.INTERNET" />.
Modified my MainActivity.java to add a call to my new activity:
/** Called when the user taps the Search button */
public void searchMessage(View view) {
Intent intent = new Intent(this, DisplaySearchActivity.class);
EditText editText = (EditText) findViewById(R.id.editText3);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
Then, created a new activity that has a WebView and, for now just displays the main page of Google.com
public class DisplaySearchActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_search);
// Get the Intent that started this activity and extract the string
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("https://www.google.com");
}
}
Finally, my activity_display_search.xml looks like this
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="com.example.myapplication.DisplaySearchActivity">
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</android.support.constraint.ConstraintLayout>
When I click the Search button the app takes me to the Search WebView, but nothing happens. What am I missing here?

You need to provide a WebViewClient for your WebView.
public class DisplaySearchActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_search);
// Get the Intent that started this activity and extract the string
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("https://www.google.com/search?q=" + message);
}
}

Related

Open first Link searched against a keyword

I have 2 activities in my app, First Activity is simple editText and submit Button, When I press submit, it launches webView Activity and searches the keyword entered in the first activity. Now I want to open the first link Automatically in webview.
public class MainActivity extends AppCompatActivity {
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient());
String query = "firebase";// Get the text from EditText here
String url = "https://www.google.com/search?q="+query;
webView.loadUrl(url);
}
}

Open OneSignal URL to WebView of another activity

With OneSignal Notification, I'm sending a notification containing URL. In my ADDITIONAL DATA section, I'm sending KEY: openURL and VALUE: http://example.com.
I'm trying to open the link inside the WebView of my DailyDose activity.
Here, my ProfileActivity activity, I'm trying to recieves the notification and send it to DailyDose activity.
public class ProfileActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
OneSignal.startInit(this)
.inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
.setNotificationOpenedHandler(new NotificationOpenHandler())
.unsubscribeWhenNotificationsAreDisabled(true)
.init();
}
class NotificationOpenHandler implements OneSignal.NotificationOpenedHandler {
#Override
public void notificationOpened(OSNotificationOpenResult result) {
OSNotificationAction.ActionType actionType = result.action.type;
JSONObject data = result.notification.payload.additionalData;
String openURL = getIntent().getStringExtra("openURL");
Intent intent = new Intent(getApplicationContext(), DailyDose.class);
intent.putExtra("openURL", openURL);
startActivity(intent);
}
}
}
And this is my DailyDose activity, where I'm trying to open the link inside the WebView:
public class DailyDose extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_daily_dose);
String openURL = getIntent().getStringExtra("openURL");
final TextView textView = findViewById(R.id.debug_view);
textView.setText("URL from additionalData: " + openURL);
WebView webView = findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
if (openURL == null)
webView.loadUrl("https://google.com");
else
webView.loadUrl(openURL);
But it's not working. Tapping on the notification, it just opens the ProfileActivity activity of the app. Doesn't do anything else. What am I doing wrong? I have been searching for a solution for a while but couldn't get anything that works. Will very much appreciate your suggestion.
When you click on the notification it opens the ProfileActivity because it is default behaviour of onesignal. It will open same activity that have initialization of onesignal. So first you need to disable this default behaviour of onesignal. Write below lines in your AndroidManifest file.
<application>
<meta-data android:name="com.onesignal.NotificationOpened.DEFAULT" android:value="DISABLE" />
</application>
Now run your project and check it will open DailyDose activity. Let me know if still you face any problems.
For more clarification on this please read onesignal's official document :
https://documentation.onesignal.com/docs/android-customizations#section-background-data-and-notification-overriding (Search : "Changing the open action of a notification" in the page).

How to create a Link button programmatically?

I am trying to convert a series of buttons that will open webpages in the browser from an xml layout file into my Activity class (programmatically). How can I do this?
I have not been able to find questions that answer this.
/**
* Open the specified URL on the device's browser
*
* #param context Current Context.
* #param url The url to display.
*/
public static void openWebPage(#NonNull Context context, String url) {
Uri webPage = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, webPage);
if (intent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(intent);
}
}
Then in the onCreate of your Activity you get the button:
View button = findViewById(R.id.my_button);
button.setOnClickListener(new onClickListener(){
... // call the above method with the corresponding url
});
More information here
The code....
public class MainActivity extends Activity {
TextView textView;
Spanned spannedText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.textView1);
spannedText = Html.fromHtml("text with a link in it <br />" +
"<a href='https://www.aol.com//'>aol.com</a>");
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setText(spannedText);
}
}
XML TextView
<TextView
android:id="#+id/textView1"
android:layout_width="match_content"
android:layout_height="match_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="LINK WILL SHOW UP HERE"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center" />
You need to use webview in your android application.
More about web view

how can I write a code when i click a button it have to go particular web site ...?

when i click a button it have to go one particular web site. i'm getting confusion how can write code on android please help me i did like this
xml code :
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click me"
android:id="#+id/button1"
android:onClick="search"/>
java code :
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void search(View view)
{
Intent intent=new Intent(getApplicationContext(),Main2Activity.class);
startActivity(intent);
}
}
You should write below codes to your onCreate method, after setContentView(R.layout.activity_main);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Change google url with your url
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);
}
});
Also you should delete android:onClick="search" from your xml
You need to be very clear in your question -based on your comment to Batuhan's answer it sounds like you want to use a WebView - either way, you need to clarify what you want;
Assuming you want a Webview - here is what you need to do:
Create a layout file that will load the Web page, call it my web_page_viewer:
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myWebPageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
Then you create a "Web Page Viewing Activity" (I assume you wanted to use Main2Activity) where you will initialize and load the URL to be displayed:
public class Main2Activity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//here you load your "Web Page Viewing" layout file
setContentView(R.layout.web_page_viewer);
//here you get handle to your WebView
webView = (WebView) findViewById(R.id.myWebPageView);
webView.getSettings().setJavaScriptEnabled(true);
//then specify the URL you want to load
webView.loadUrl("http://ishlema.co.za");
}
}
Then finally, your button click handler will now load the "Web Page Viewing" activity:
public void search(View view)
{
Intent intent=new Intent(getApplicationContext(),Main2Activity.class);
startActivity(intent);
}
I really hope this helps you. Please take some time and look at this complete and easy Android WebView Example

How to open Webview on top layer of my activity? (Android)

I have a problem to add a webview after I use startactivity. I have an intent and start it :
final Intent intent = new Intent(Intent.ACTION_VIEW, uri);
String uriString = uri.toString();
String extension = uriString.substring(uriString.lastIndexOf('.') + 1);
intent.setClass(this, extensionToActivity.get(extension));
startActivity(intent);
after my activity ran completely, I need to open a webview so I go to Oncreate function and add my code
public void onCreate(Bundle savedInstanceState)
{
WebView wv = (WebView)findViewById(R.id.webView1);
String summary = "<html><body><h1>some test</h1></body></html>";
wv.loadData(summary, "text/html", null);
wv.setVisibility(View.VISIBLE);
}
but my program crash!!! but if i show alert inside onCreat it works fine, what is the problem?
what is the problem?
You forgot to set layout by using setContentView() method. FYI, You can't find any views without setting content views to activity.
Try this,
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView wv = (WebView)findViewById(R.id.webView1);
String summary = "<html><body><h1>some test</h1></body></html>";
wv.loadData(summary, "text/html", null);
wv.setVisibility(View.VISIBLE);
}
EDIT:
#Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
I found that the Intent that I work uses another activity inside, this is because my program crash. I found that activity and add my webview there, now it is solved
setContentView(R.layout.browseritems);
WebView wv = (WebView)findViewById(R.id.webView1);
String summary = "<html><body><h1>some test</h1></body></html>";
wv.loadData(summary, "text/html", null);
wv.setVisibility(View.VISIBLE);

Categories

Resources