How to embed WebView in Java in Android Auto - java

I'm new to Java and am currently developing an Android Auto app.
It's using WebView to display local HTML files.
I've tried many ways, but everything went wrong.
My App can start in Android Auto, but embedding WebView fails every time.
And the Internet is like empty on this subject.
Here is what I'm trying currently:
package com.example.webviewandroidauto;
import android.webkit.WebView;
import androidx.annotation.NonNull;
import androidx.car.app.CarContext;
import androidx.car.app.Screen;
import androidx.car.app.model.Template;
public class StartScreen extends Screen {
public StartScreen(CarContext carContext) {
super(carContext);
}
#NonNull
#Override
public Template onGetTemplate() {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView mywebview = (WebView) findViewById(R.id.webView);
mywebview.loadUrl("file:///android_asset/myresource.html");
}
}
Can someone please help me out?
Thank you!
Proof that my app starts in Android Auto: https://i.stack.imgur.com/OXC80.png

Related

WebView won't execute site commands

I have a WebView that loads this WebSite
If you try to put an identification and click out of the box, it shows a little loading and tells if the identification is correct it shows a virtual keyboard within the site, not a device keyboard, if its incorrect it shows a error message, like this
Site showing error
However, if i put the same website on my WebView, it loads the site perfectly, but it won't execute these commands, if i put any identification and click out it doesn't shows the loading, it won't do anything.
Funny part is, if i do the same WebView in Xcode for iPhone, it does everything normally, even the commands, it checks for the identification, etc...
What i've tried
Turning on/off:
Javascript
Plugins(even deprecated)
My code:
package com.example.viskee.webview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView wbAba = (WebView) findViewById(R.id.wbAba);
wbAba.setWebViewClient(new WebViewClient());
wbAba.getSettings().setJavaScriptEnabled(true);
wbAba.loadUrl("https://ib.rendimento.com.br");
}
}
EDIT:
If i use
wbAba.setChromeClient(new ChromeClient);
it works fine, however i don't want to use ChromeClient because it adds an address bar, etc... I want my WebView to be FullScreen, show only the website
Have you try this:
WebView wbAba = (WebView) findViewById(R.id.wbAba);
wbAba.setWebViewClient(new WebViewClient());
wbAba.getSettings().setUseWideViewPort(true);
wbAba.getSettings().setLoadWithOverviewMode(true);
wbAba.getSettings().setJavaScriptEnabled(true);
wbAba.loadUrl("https://ib.rendimento.com.br");
and one final thing, don't forget to set internet permission on manifest.xml file:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
Let me know, if this doesn't work.
I found a solution, all i had to do was:
wbAba.getSettings().setDomStorageEnable(true);

Redirect to a webpage while opening an android app

I need to develop an app which redirects users to my website right after the app was started.
Thats all I want in my mobile app.
I am using Android studio but I am not familiar with XML. So I am stuck there. Which code should I write to do this redirection? Hope you all can help me. Thank you.
First you need to import this:
import android.content.Intent;
import android.net.Uri;
In your MainActivity.java class, OnCreate Method you may add
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent i = new Intent(
Intent.ACTION_VIEW,
Uri.parse("http://www.google.com")
);
// Starts Implicit Activity
startActivity(i);
}
You can use a web view in XML file and load your url in it
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://www.google.com");

Unfortunately my app stopped working? [duplicate]

This question already has answers here:
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
Closed 8 years ago.
I tried to display "hello world !" by designing an app using a custom font stored in src/Main/CustomF/times.ttf in my laptop.....while execution it disappears very soon telling that "unfortunately your app stopped"....my coding is as below.....please help me..
package com.example.nambimanavalan.customfonts;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView t;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t=(TextView)findViewById(R.id.mine);
Typeface custom=Typeface.createFromAsset(getAssets(),"CustomF/times.ttf");
t.setTypeface(custom);
}
}
you need to put your font in assets/Font folder and than you can use
Typeface custom=Typeface.createFromAsset(getAssets(),"Font/times.ttf");
put your custom font in : assets/CustomF/times.ttf and maybe then it will work
assets folder is directly under your project folder, and whenever you create a fresh project it is empty.

Creating Background Image for Android App - Java

I'm just getting started with android development and I need help with background images. I want to be able to have a background image and then overlay other items (buttons, text, etc.) on top of that background with a layout. I used a LinearLayout just for the sake of being simple and because I don't know what's best for me at the moment.
Anyways, I can't get an image to display using the following code:
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.*;
public class NewGameActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setBackgroundDrawable(Drawable.createFromPath("/assets/images/androidBackground.png"));
this.setContentView(ll);
}
}
ll.setBackgroundResource(R.drawable.image_name);
http://developer.android.com/reference/android/view/View.html#setBackgroundResource%28int%29
This is the preferred way of accessing drawable resources. The image_name is a png image in your drawable or drawable-mdpi folder.
yes if you are using XML then find the id of that linearlayout like
ll=(LinearLayout)findViewById(R.id.linear1)
Then set its background as
ll.setBackgroundResource(R.drawable.image_name);
else here as your code given here you can directly go for
ll.setBackgroundResource(R.drawable.image_name);

how to view youtube video

I am finding great difficulties to view YouTube videos in my app.
here is my code:
package com.example.webvideo;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;
public class WebVideo extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
VideoView vv = (VideoView) findViewById(R.id.videoView);
MediaController mc = new MediaController(this);
mc.setAnchorView(vv);
vv.setMediaController(mc);
vv.setVideoURI(Uri.parse("http://youtu.be/2OIOOb-0t44"));
vv.start();
}
}
The emulator is showing an error that the video cannot be played.
What am I doing wrong? Am I giving the URL in a wrong format?
I would expect that the URI you need to give is to the actual media file to be played. I wouldn't count on redirects working either... and anyway that redirect you give seems to point to a YouTube web page, which I sure wouldn't expect the video player to be able to render.
http://youtu.be/2OIOOb-0t44 is certainly an invalid URL. Maybe you meant http://youtube.com/watch?v=2OIOOb-0t44?
The URL you are using is for the webpage where you can view it, not for the video itself. The embedding URL appears to be http://www.youtube.com/embed/2OIOOb-0t44, but I think it's HTML5, not flash... you may have to go old school and track down an AVI or MPEG file.

Categories

Resources