multiple webviews in Android - android

I am developing an application in which I would like to create different webViews in some conditions. Despite having more than one webView, the idea is to show only one of them.
I have seen that the best way is create a new class which extends Activity.
This is the main class
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent(this, WebScreen.class);
startActivity(intent);
}
This is the second class I have done to create new Webviews
public class WebScreen extends Activity {
private WebView myWebView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_PROGRESS);
}
public void onClick(View arg0) {
return;
}
#Override
protected void onPause() {
myWebView = null;
super.onPause();
}
#Override
protected void onStart(){
}
#Override
protected void onResume() {
super.onResume();
myWebView = new WebView(this);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl("myURL");
setContentView(myWebView);
}
#Override
public void onBackPressed() {
super.onBackPressed();
myWebView = null;
}

In the same way you have used a layout on your first activity setContentView(R.layout.main);. You have to set another layout on the WebScreen.onCreate. p.e.
myweblayout.xml
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
Set it at onCreate
setContentView(R.layout.myweblayout)
And get the tag from the activity using
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://www.example.com");
I suggest you to read Building Web Apps in WebView

You can create two webviews in the same activity and same xml file,just keep the visibility of one of the views as gone and one as visible .You can always switch between the views by changing the visibilities.

Related

I want to add menu and score

I'm a newbie here learning how to create a game in android and stumbled on a tutorial http://williammora.com/a-running-game-with-libgdx-part-1
i already finished the game but it lacks menu and score. I am getting overwhelmed by the number of classes and i don't know where to put the menu screen. there is a java class there that is named Android Launcher and i think its the one that calls the game to start so I created a layout xml menu and try to call it after pressing a button this is my code. there's no error but the game crashes
public class AndroidLauncher extends AndroidApplication {
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next = findViewById(R.id.btnLetsgo);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
initialize(new RollOut(), config);
}
});
}
}
ive read some tutorials and its possible to create a layout for the menu. ill add another class Main.class and `public class Main extends AndroidApplication {
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next = findViewById(R.id.btnLetsgo);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
gotoNext();
}
});
}
private void gotoNext(){
Intent i = new Intent(this, AndroidLauncher.class);
startActivity(i);
}
}`
and the androidlauncher class
public class AndroidLauncher extends AndroidApplication {
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
initialize(new RollOut(), config);
Intent i = getIntent();
}
}
but its still crashing
It sounds that maybe you are very new in libgdx. It very hard to say with you where should be puth your Menu Screen. Let spend your time to study Screen Class. Which you can extend to create you MenuScreen. Here is more information I hope useful for you:
example: https://www.gamedevelopment.blog/full-libgdx-game-tutorial-menu-control/
wiki: https://github.com/libgdx/libgdx/wiki/Scene2d
class MainScreen extends Screen{ public MainScreen{} }
And in game class:
this.setScreen(new MainScreen()) ;

Android activity transition does not appear

I have been trying for some months to get an activity transition to work. I have tried a number of approaches, using XML and styles, and code, to no avail. Below is my most recent attempt. Does anyone know why I can't get any transitions to appear?
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addSlideTransitions();
setContentView(R.layout.my_activity_layout);
....
}
protected void addSlideTransitions()
{
getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
Slide slide = new Slide();
slide.setDuration(1000);
getWindow().setEnterTransition(slide);
getWindow().setExitTransition(slide);
}
You must start the activity with an ActivityOptions bundle created with makeSceneTransitionAnimation.
That type of transition also only works when in the same task (as of N).
Try to do like this,I am sure this will work for you.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addSlideTransitions();
setContentView(R.layout.activity_main);
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
protected void addSlideTransitions()
{
getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
Slide slide = new Slide();
slide.setDuration(1000);
getWindow().setEnterTransition(slide);
getWindow().setExitTransition(slide);
}
}
Try adding the call to addSlideTransitions before super.onCreate.
#Override
protected void onCreate(Bundle savedInstanceState)
{
addSlideTransitions();
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity_layout);
....
}

SplashScreen with download website and pass it to webview

I would like to open app with splashscreen activity and download homepage of my website. When download complete I'd like to start MainActivity, pass webssite to webview and display it.
I tried some different methods but nothing works..
My current version load webview when MainActivity starts and that not looks good when website is loading.
Do you have any ideas?
public class SplashScreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen_layout);
try {
url = new URL(link);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
new WczytywaczUrl().execute(url);
}
//asynctask class and methods
#Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
Log.d(TAG, result.toString());
Intent intent = new Intent(SplashScreen.this, MainActivity.class);
intent.putExtra("strona", result);
startActivity(intent);
finish();
}
public class MainActivity extends Activity implements OnClickListener{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "metoda onCreate");
setContentView(R.layout.activity_main);
webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setUseWideViewPort(true);
Intent i = getIntent();
String strona = i.getStringExtra("strona");
Log.d(TAG, "strona: " + strona.toString());
webView.loadDataWithBaseURL(link, strona, "text/html", "UTF-8", null);
}
}
What about replacing "Activity" with just another layout?
<FrameLayout>
android:layout_width="match_parent"
android:layout_height="match_parent">
<Layout1
android:layout_width="match_parent"
android:layout_height="match_parent">
// Layout contents that origin from your splash activity layout
</Layout1>
<Layout2
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible">
// Layout contents that origin from your main activity layout
</Layout2>
</FrameLayout>
WebViewClient has "public void onPageFinished(WebView view, String url)" callback.
If you implement code that change visibility of Layout1 and Layout2 in that callback,
then the webview may be shown after it finished loading.

Best practice: subclass or send intent?

I have two activities almost doing the same thing. The only thing that differs them is a URL to be parsed.
What is considered best practice regarding Android development, subclass just to set the URL or send the URL via an intent?
public SuperActivity extends Activity{
protected String pageUrl;
#Override
protected void onCreate(Bundle savedInstanceState) {
[...lots of stuff...]
super.onCreate(savedInstanceState);
}
}
public SubActivityOne extends SuperActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
pageUrl = "http://urlOne.com"
super.onCreate(savedInstanceState);
}
}
public SubActivityTwo extends SuperActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
pageUrl = "http://urlTwo.com"
super.onCreate(savedInstanceState);
}
}
or
public SuperActivity extends Activity{
private String pageUrl;
#Override
protected void onCreate(Bundle savedInstanceState) {
Bundle extras = getIntent().getExtras();
pageUrl = extras.getString("intent_key_url");
[...lots of stuff...]
super.onCreate(savedInstanceState);
}
}
if you have multiple activities, which share similar functionality (and/or variables/methods) -> go for sub-classing.
if all you need is to pass a value from the invoking activity, use intents (or maybe Static)
In your case, I would subclass, but do it slightly differently than you. It's dangerous to put some code in onCreate when it's not definitely needed. (You may get lost in your hierarchy and not call exactly what you want to call in the correct order) I would use an overriden method rather than a variable. Do that:
public abstract SuperActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
[...lots of stuff...]
// instead of using pageUrl, use a method when you need it: pageUrl()
super.onCreate(savedInstanceState);
}
protected abstract String pageUrl();
}
And your subactivities would look like: (only one shown here)
public SubActivityOne extends SuperActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
protected String pageUrl() {
return "http://urlOne.com";
}
}
The intent way is good too, but it might become complicated if later on you want to add more differences in your subactivities. With subclasses, it's very flexible.
To avoid duplicate code (which is mostly the good thing), make base activity class (make it abstract) and put your common code there. Then make your "real" activities extend base class. So option "A" is the way.
A third option would be to make a callback interface and have any class you want implement it. Something like "PageUrlProvider"
public interface PageUrlProvider
{
String getPageURL();
}
Then the concrete implementation would be
public MyActivity extends Activity implements PageUrlProvider
{
private String pageUrl ="http://example.com/";
#Override
protected void onCreate(Bundle savedInstanceState)
{
String myURL = getPageURL();
super.onCreate(savedInstanceState);
}
#Override
public String getPageURL()
{
return pageUrl;
}
}
Which option you choose is largely dependent on what you are trying to accomplish. But this option offers a lot of flexibility.

Attach/detach Android view to/from layout

I want to create a WebView in onCreate() method of a derivative of Application class, then attach it to the main layout when an activity onCreate() is called and detach it when onDestroyed() is called. So, every time when an activity is being created/destroyed, the WebView component will be the same (kinda singleton). The problem is I (with my Windows API background) have no ideas how to do this. Just new WebView()/addiew()/removeView()?
Why do I want to do this, you asked? Prevent Android activity from being recreated on turning screen off In several words, the WebView should never be destroyed.
Nothing special. Register MyApp as application class name in the manifest.
public class MyApp extends Application
{
public WebView _WebView = null;
#Override
public void onCreate()
{
_WebView = new WebView(getApplicationContext());
// Settings etc.
_WebView.loadUrl("url");
super.onCreate();
}
}
Remove the view from main.xml.
public class MyActivity extends Activity
{
WebView _WebView;
RelativeLayout _Layout; // Should be declared in main.xml.
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
_Layout = (RelativeLayout) findViewById(R.id.rl);
ViewTreeObserver vto = _Layout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new MyLayoutListener()); // .layout(0,0,width,height);
Display display = getWindowManager().getDefaultDisplay();
MyApp app = (MyApp) this.getApplication();
_WebView = app._WebView;
_Layout.addView(_WebView, display.getWidth(), display.getHeight());
}
#Override
protected void onDestroy()
{
_Layout.removeView(_WebView);
super.onDestroy();
}
}
private class MyLayoutListener implements OnGlobalLayoutListener
{
public void onGlobalLayout()
{
Display display = getWindowManager().getDefaultDisplay();
_WebView.layout(0, 0, display.getWidth(), display.getHeight());
//_Layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
}

Categories

Resources