ANDROID: Facebook and Browser Open inside my app, not in their own - java

this will be a bit annoying to explain, but bear with me:
So i have a simple android app with buttons, one opens a facebook page (first checks if the app is installed, and will open in that, else will open same page in browser), the other opens a website url in the browser
The problem i'm having is that, instead of opening them as seperate applications, it seems to be opening the browser/fb app inside my original app.
So if i open the fb page through my app, and click the back button, it brings me back to my app's home screen. If i click the fb button, and minimize the app, and go into the ACTUAL fb app, it hasn't changed anything there.
So my app is not actually using the standalone fb app, just calling it up inside itself.
(hope that makes sense...) here is the code i used;
//just defined button variables
button facebook, shop;
//now for the onCreate method
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
facebook = (Button) findViewById(R.id.facebook);
shop = (Button) findViewById(R.id.shop);
facebook.setOnClickListener(new View.onClickListener(){
public void onClick(View v) {
try{ //left out the profile id on purpose...
Intent facebook = new Intent(Intent.ACTON_VIEW, Uri.parse("fb://profile/xxxx"));
startActivity(facebook);
}catch(Exception e){
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse ("http://www.facebook.com/example")));
}
}
});
The shop button is set up the same, only without the try/catch, it will only open in the browser.
How do i make it so that it sends the request to the actual fb app/browser, instead of what it's currently doing? Any help/tips are gratefully welcome :)

Create a new layout with layout/a.xml
Open the layout file & then add a WebView, assign an ID so you can access the widget later.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<WebView android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
Insert Permission in your manifest file as you are accessing INTERNET in your application
<uses-permission android:name="android.permission.INTERNET" />
Inside the new Activity -->WebActivity.java
public class WebActivity extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
WebView mywebview = (WebView) findViewById(R.id.webview);
WebSettings webSettings = mywebview.getSettings();
webSettings.setJavaScriptEnabled(true);
mywebview.loadUrl("http://www.facebook.com");
}
}
If you want to handle navigation inside the webView on your own
mywebview.setWebViewClient(new WebViewClient())
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
Now to redirect from your main activity to these new activity ::
facebook.setOnClickListener(new View.onClickListener(){
public void onClick(View v) {
try{
ApplicationInfo info = getPackageManager().getApplicationInfo("com.facebook.katana", 0 );
boolean isFacebookInstalled = true;
}
catch( PackageManager.NameNotFoundException e ){
isFacebookInstalled=false;
}
if(isFacebookInstalled)
{
//start the facebook app
Intent intent = new Intent("android.intent.category.LAUNCHER");
intent.setClassName("com.facebook.katana", "com.facebook.katana.LoginActivity");
startActivity(intent);
}
else
{
Intent facebook = new Intent(getApplicationContext(),WebActivity.class);
startActivity(facebook);
}
});
Same way you can also insert for your shop button

Related

How to get all URL using WebView in android studio

Good day to all,
I'm making a simple app(for practice purposes) that uses a WebView in android studio. I want my app to toast all the URL's that I enter/browse. Ex, When I open Facebook it will toast its URL and when I log-in to my account it will toast again my facebook account, sounds like that. Here's my code. I'm new to stuff thing, any suggestions or help will be highly appreciated. Thank you so much.
xml layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.nevigrof.ymuc.Back_order">
<WebView
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:focusable="true"
android:id="#+id/webview1"/>
</RelativeLayout>
Java Codes
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_survey_form);
webview = (WebView) findViewById(R.id.webview1);
webview.setWebViewClient(new MyWebViewClient());
openURL();
}
private void openURL() {
webview.requestFocus(View.FOCUS_DOWN | View.FOCUS_UP);
webview.loadUrl("http:/https://www.facebook.com/");
webview.getSettings().setJavaScriptEnabled(true);
String webUrl = webview.getUrl();
Toast.makeText(Back_order.this, webUrl, Toast.LENGTH_LONG).show();
}
}
I suppose MyWebViewClient inherits from WebViewClient; you should override
onPageStarted and/or onPageFinished in MyWebViewClient class.
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// Toast
}
Have a look to :
WebViewClient onPageFinished Android doc
Load an HTML document Android training
In your code, you are only displaying the toast once, when you first display facebook.com in the webview. You should add code to notify you if the webview has displayed another page (i.e. another URL). You can use WebViewClient.shouldOverrideUrlLoading(WebView view,String url) or other code to check if a new page is loaded/being loaded.

Need to Navigate to next page Android Bluetooth

I am very new to Andriod Programming and this is probably very basic question.
In my Application, the first page contains just button (for simplicity) login button.
After the user clicks login button it displays the toast and then I need to navigate to new class(page B) where I want to connect to a specific health sensor.
Problem
1. I tried implementing the just the basic part with onClickListener for button and then when clicked, go to next page, where enable Bluetooth,etc. I could not get to next page
MainActivity.java :
public class MainActivity extends AppCompatActivity {
Button button;
PollingTest pd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getBaseContext(),"Logged In",Toast.LENGTH_SHORT).show();
pd = new PollingTest();
pd.call();
}
});
}
}
Second Page (Where wanted to Control BT). Never got to this page while testing on the tablet: -
For now just included if I could get a Toast atleast from this page: -
public class PollingTest extends Activity {
BluetoothAdapter btAdapter;
Button btn2;
protected void call() {
//super.onCreate(savedInstanceState);
//setContentView(R.layout.pairinglistactivity);
btn2 = (Button)findViewById(R.id.pollB);
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "hello", Toast.LENGTH_LONG).show();
}
});
}
}
Here app crashes after clicking on Login button in first page.
I have actually got some different errors with different code, I was not able to make proper Toast or turn BT on in the second page as it was trying them in static method.( very Confusing:( )
Please help me. I know this v v basic Q..
EDIT:
Sorry, this Q is already answered here: -
Moving from one activity to another Activity in Android
You don't start an activity by instantiating it like a normal Java class. So this is wrong
pd = new PollingTest();
pd.call();
you should be using an Intent
and follow the Activity Lifecycle
so you would want something like
Intent i = new Intent(MainActivity.this, PollingTest.class);
startActivity(i);
then override onCreate() in PollingTest.java and put what is in call() in there or call that method from onCreate().
Also, a Toast should use Activity Context

How to setup Android WebView to make it work identically Android browser?

What are the settings to enable or disable in WebView to do this?
If you want to use a webview with exactly the same features as the native android browser, you have to check MANY options and settings. A WebView is made to NOT use all of the browsers options and settings for better performance and for having more controll on what the users can and can not do while browsing. to figure out all the opportunities you should read through the api documentation here:
http://developer.android.com/reference/android/webkit/WebView.html
For some further dealing and handling with the WebView class also here are some usefull sample codelines:
http://pankajchunchun.wordpress.com/2013/01/09/example-of-webview-with-result-handler-and-connection-timeout-handler/
I hope this will help you.
this is the webview example source..
what kind of setting do you wanna know??
public class MainActivity extends Activity {
WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview = (WebView)findViewById(R.id.webview);
webview.setWebViewClient(new WebClient());
WebSettings set = webview.getSettings();
set.setJavaScriptEnabled(true);
set.setBuiltInZoomControls(true);
webview.loadUrl("http://www.google.com");
findViewById(R.id.btnStart).setOnClickListener(onclick);
}
OnClickListener onclick =new OnClickListener() {
#Override
public void onClick(View v) {
String url= null;
EditText add = (EditText)findViewById(R.id.add);
url = add.getText().toString();
webview.loadUrl(url);
}
};
class WebClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
In addition to #Ssam's answer, you might want to manage the back button press so your app/Activity doesn't get closed on back press.
#Override
public void onBackPressed() {
if (webview.canGoBack()) {
webview.goBack();
}else
super.onBackPressed();
}
where webview is your webview

android application is opening a browser application instead of a webView, how do I stop that?

Right now when I push a specific button it starts an intent to open a web page into a webView, but instead it opens a Browser Application, revealing the address bar which is not what I want.
After doing some research I found out I can use the shouldOverrideUrlLoading() method to stop this from happening and just open the page in a webView, but I don't know how to implement this method in my code, help would be greatly appreciated!
This is what I have in the class where I'm opening the web page:
public class Codes extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.codes);
View titleView = getWindow().findViewById(android.R.id.title);
if (titleView != null) {
ViewParent parent = titleView.getParent();
if (parent != null && (parent instanceof View)) {
View parentView = (View)parent;
parentView.setBackgroundColor(Color.rgb(228, 108, 10));
}
}
WebView codes = (WebView)findViewById(R.id.codesWebView);
codes.getSettings().setJavaScriptEnabled(true);
codes.loadUrl("http://dev10.affirmaconsulting.com:24009/keyentry");
shouldOverrideUrlLoading(codes, "http://dev10.affirmaconsulting.com:24009/keyentry");
}
}
I had this same problem, that pages were not loading in my webview but instead via the browser application. I resolved the issue by implementing a WebViewClient class and overriding the shouldOverrideUrlLoading method.
WebViewClient webClient = new WebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
return false;
}
};
webView.setWebViewClient(webClient);
The following link proved to be helpful:
http://www.firstdroid.com/2010/08/05/override-url-loading-in-webview-android-tutorial/
We do this exact task in one of our activities and haven't ran into this issue. Here is what ours looks like:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.document_viewer_activity);
WebView wv = (WebView) findViewById(R.id.documentViewerWebview);
wv.getSettings().setJavaScriptEnabled(true);
String url = getString(R.string.document_viewer_url) +
getIntent().getExtras().getString("media");
wv.loadUrl(url);
}
With the webview defined in the layout file looking like this:
<WebView
android:id="#+id/documentViewerWebview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
Hope this helps.

Gestures with WebView

What I'm trying to do:
I want the user to be able to custom gestures and name them "up","down"..etc. Then when the user runs my application, it loads up a WebView with a particular url like http://google.com. Then the user can draw the gesture he created earlier. So if he draws the "up" gesture, then another webview opens.
My problem:
When I try to draw the gesture over the webview, it doesn't draw. Rather it scrolls the webview.
What have I tried:
Already created a GestureOverlayView and put the WebView within that. Unfortunately, the GestureOverlayView is unable to detect the gestures as the WebView gets scrolled.
My Code:
public class WebViewCenter extends Activity implements OnGesturePerformedListener {
/** Called when the activity is first created. */
GestureLibrary mLibrary;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//WebView webview = (WebView)findViewById(R.id.WebView01);
//webview.loadUrl("http://www.google.com");
//Button btnStart = (Button)findViewById(R.id.Start);
//btnStart.setOnClickListener(this);
mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures_overlay);
gestures.addOnGesturePerformedListener(this);
WebView webview = (WebView)findViewById(R.id.WebView);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://live.com");
}
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
mLibrary.load();
ArrayList<Prediction> predictions = mLibrary.recognize(gesture);
Log.i("Default Marker", predictions.get(0).toString());
if (predictions.size() > 0) {
String action = predictions.get(0).name;
Log.i("Gesture", action);
if ("up".equals(action)) {
Log.i("Up_gesture", "Up gesture detected");
Intent int_up = new Intent(WebViewCenter.this, WebViewUp.class);
WebViewCenter.this.startActivity(int_up);
//return true;
//Toast.makeText(this, "Adding a contact", Toast.LENGTH_SHORT).show();
}
/*else
{
Log.i("Marker 2", "god knows what ur upto");
}*/
}
}
Any advice as to how can I separate the WebView scrolling from the gesture building?
Thanks,
Pawan

Categories