Multiple Log-Ins on Separate WebViews? (Android) - java

I'm fairly new to Android development, and I have a question about how WebViews handle data (in Java).
I'm assuming this would fall under the 'cookie' category. But what I have is two different webViews on different tabs of my application. I would like for one webView (call it webView1) to be logged into one account of a website, while the other (webView2) to be logged into another account of the same website. For example, I would want to log into two separate Gmail accounts within the two webViews at the same time.
The problem I am having is that once I log into an account on webView1, webView2 follows suit and logs me into that account. The same problem happens when I log into webView2, as webView1 logs into that account also naturally.
Is there any way to get around this? I want my two webViews to act completely independently from one another is what it comes down to.
Thanks!

I can definitely answer your question. I am mainly an iOS developer and I am creating an Android app with the exact opposite effect of webView cookies not sharing. I created my webviews in tabs to simulate the behavior I use in iOS, but my webViews are acting completely independant of one another. I want them to share the same login info, but as it stands I must login to each tab individually because they aren't playing nice.
Either way, my problem can definitely help you and I would hope that you share your info with me to create the cookie sharing effect I am searching for...
Here is my main.xml layout:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<WebView
android:id="#+id/web_engine"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:layout_marginTop="-45dp" />
<WebView
android:id="#+id/messages"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:layout_marginTop="-45dp" />
<WebView
android:id="#+id/myprofile"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:layout_marginTop="-45dp" />
<WebView
android:id="#+id/rncorner"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:layout_marginTop="-45dp" />
</FrameLayout>
</LinearLayout>
</TabHost>
Here is my Activity:
package com.example.tabs;
import android.app.TabActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TabHost;
public class TabsActivity extends TabActivity {
WebView webView;
final String DEFAULT_URL = "http://example.com";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost mTabHost = getTabHost();
mTabHost.addTab(mTabHost.newTabSpec("Home").setIndicator("Home",getResources().getDrawable(R.drawable.home)).setContent(R.id.web_engine));
mTabHost.addTab(mTabHost.newTabSpec("Messages").setIndicator("Messages",getResources().getDrawable(R.drawable.messages)).setContent(R.id.messages));
mTabHost.addTab(mTabHost.newTabSpec("My Profile").setIndicator("My Profile", getResources().getDrawable(R.drawable.myprofile)).setContent(R.id.myprofile));
mTabHost.addTab(mTabHost.newTabSpec("Map").setIndicator("Map", getResources().getDrawable(R.drawable.rncorner)).setContent(R.id.rncorner));
mTabHost.setCurrentTab(0);
//home
webView = (WebView)findViewById(R.id.web_engine);
webView.setWebViewClient(new MyWebViewClient());
webView.loadUrl(DEFAULT_URL);
//messages
webView = (WebView)findViewById(R.id.messages);
webView.setWebViewClient(new MyWebViewClient());
webView.loadUrl("http://example.com/index.php2");
//my profile
webView = (WebView)findViewById(R.id.myprofile);
webView.setWebViewClient(new MyWebViewClient());
webView.loadUrl("http://example.com/index.php3");
//rncorner
webView = (WebView)findViewById(R.id.rncorner);
webView.setWebViewClient(new MyWebViewClient());
webView.loadUrl("http://example.com/index.php4");
}
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
}
}

Related

Activity is opening very slowly

for some reason my activity is opening slowly, do you think that you can help me here?
Now, this is not the only code in Java class, I have 30 buttons that are exactly the same like the one below, only their ID is different since I have 30 card views (widget) in the activity, here is the picture of the design. I also reduced the pictures to the right size for the card, but that did not help. May you please help me?
Java Class:
private Button crkvaspip;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_zavrsenilista);
//Dashboard
crkvaspip = (Button) findViewById(R.id.crkvaspip);
crkvaspip.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) { openCrkvaSvetogPetraIPavla();
}
});
public void openCrkvaSvetogPetraIPavla(){
Intent intent = new Intent(zavrseni_category.this, bioskop_class.class);
startActivity(intent);
}
Android widget card view code:
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="210dp"
app:cardCornerRadius="20dp"
android:layout_margin="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/thumbnail"
android:layout_width="match_parent"
android:layout_height="170dp"
android:layout_alignParentTop="true"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:src="#drawable/bioskop1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/thumbnail"
android:maxLines="1"
android:padding="8dp"
android:fontFamily="#font/baloo"
android:text="3D Bioskop"
android:textColor="#222"
android:textSize="20dp" />
<Button
android:id="#+id/crkvaspip"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00000000" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
UPDATE LOGCAT INFO CODE:
2022-08-28 20:21:51.524 4585-4629/com.visit.bp I/OpenGLRenderer: Davey! duration=4111ms; Flags=1, IntendedVsync=38769851610176, Vsync=38773584943360, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=38773585749700, AnimationStart=38773585821500, PerformTraversalsStart=38773586648100, DrawStart=38773756771500, SyncQueued=38773757001800, SyncStart=38773757217800, IssueDrawCommandsStart=38773825367800, SwapBuffers=38773938657200, FrameCompleted=38773963626600, DequeueBufferDuration=332000, QueueBufferDuration=535000,
If the emulator is slow, have you tried disabling your antivirus? Another possibility is to optimize the local images, there are programs such as the free website: https://www.iloveimg.com/ and speed up loading.
Another idea would be to distribute the images in other tabs or activities, because your activity may be overloaded, or load them through other libraries like picasso (I believe it has already been mentioned).

Android - Add navigation drawer to multiple activities

I'm new to Android / Kotlin and I have an application with multiple activities.
So far I have created all the activities needed and the app works fine except for the fact now I need to add a Navigation Drawer.
Coming from iOS, I designed all my layouts thinking I can add the Navigation Drawer after:
<?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"
android:background="#color/backgroundColor"
android:backgroundTint="#color/backgroundColor">
<RelativeLayout
android:id="#+id/mainRelativeLayout"
android:layout_width="match_parent"
android:layout_height="44dp"
android:background="#drawable/navbar_border"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="#+id/ivNavBarLogo"
android:layout_width="102dp"
android:layout_height="24dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:contentDescription="#string/empty_description"
app:srcCompat="#mipmap/navbar_logo"/>
<ImageButton
android:id="#+id/btnSideMenu"
android:layout_width="25dp"
android:layout_height="15dp"
android:layout_centerVertical="true"
android:layout_marginStart="15dp"
android:background="#mipmap/sidemenu_button"
android:contentDescription="#string/empty_description"
app:srcCompat="#mipmap/sidemenu_button"/>
</RelativeLayout>
<!-- STUFF TO BE ADDED THERE -->
</android.support.constraint.ConstraintLayout>
My intention was to create a NavigationDrawer activity and call it when tapping the btnSideMenu on each activity.
However, I've been trying to get around this for a few days now without any luck. I never thought such a basic feature could be so hard to implement. I tried almost every suggestion and example found here, and also this: https://developer.android.com/training/implementing-navigation/nav-drawer.html
Am I thinking this issue the wrong way? Is there any way of doing this without having to recreate all my activities based on the template provided by Android Studio or using fragments?
LE: Forgot to mention that I removed the ActionBar from all activities.
To add a navigation drawer to multiple activities without going through and re-writing every activity's layout, you can leverage a base activity class and a ViewStub tag.
Create a layout like this (I'll refer to it as navdrawer_activity.xml later):
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="start">
<ViewStub
android:id="#+id/navdrawer_stub"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<!--
your drawer here
you can use android.support.design.widget.NavigationView
or some other view if you want
-->
</android.support.v4.widget.DrawerLayout>
Then create a class for all of your activities to extend:
public class NavDrawerActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.navdrawer_activity);
}
#Override
public final void setContentView(#LayoutRes int layoutResID) {
ViewStub stub = findViewById(R.id.navdrawer_stub);
stub.setLayoutResource(layoutResID);
stub.inflate();
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// set up nav drawer as normal, using whatever
// callback methods or helper methods you want
}
}
With these in place, the only thing you have to do is change any activity where you want the nav drawer to extend NavDrawerActivity instead of AppCompatActivity.
Well as the guide you included says:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout ...>
<!-- Layout to contain contents of main body of screen (drawer will slide over this) -->
<content goes here
... />
<!-- Container for contents of drawer -->
<left pane of drawer goes here
...
android:layout_gravity="start" />
</android.support.v4.widget.DrawerLayout>
You could define the left pane of the drawer as a compound view, and the content as a compound view. Kinda like this:
your_view.xml
<YourView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:id="#+id/..."
android:layout_width="..."
android:layout_height="..."
android:layout_centerInParent="true"/>
</YourView>
And
public class YourView extends RelativeLayout {
public YourView(Context context) {
super(context);
}
public YourView(Context context, AttributeSet attrs) {
super(context, attrs);
}
// ... two more constructors are possible here
private Button button;
#Override
public void onFinishInflate() {
super.onFinishInflate();
button = findViewById(R.id.button);
button.setOnClickListener((view) -> {
...
}
}
}
And you can do the same thing for the view of the left pane. Make sure you include the android:layout_gravity="start" as an attribute of <YourLeftPane in its xml.
And now you can do in your activity_whatever.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout ...>
<include layout="#layout/your_view"/>
<include layout="#layout/your_left_pane" />
</android.support.v4.widget.DrawerLayout>

Android Floating Action Button not being recognized

When I look at my code in the editor, nothing seems to be a problem. However, when I run the code on my phone and try to go from one activity to another which features a floating action button, the floating action button cannot find the view by Id despite the ID being exactly the same in both java and XML.
My Java:
package com.test.app.app2_test1;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class TermsOfUse extends AppCompatActivity {
FloatingActionButton mFloatingActionButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.terms_of_use);
mFloatingActionButton = (FloatingActionButton) mFloatingActionButton.findViewById(R.id.terms_back_fab);
mFloatingActionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
}
}
My XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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.test.app.app2_test1.TermsOfUse"
android:background="#color/backdrop">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="44dp"
android:orientation="vertical">
</ScrollView>
<View
android:layout_gravity="bottom"
android:layout_width="match_parent"
android:layout_height="44dp"
android:background="#color/colorPrimary"
android:elevation="4dp"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/terms_back_fab"
android:layout_gravity="bottom"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_margin="16dp"
android:src="#drawable/ic_arrow_back"
app:pressedTranslationZ="12dp"/>
</android.support.design.widget.CoordinatorLayout>
The message that I get in my console is the following:
Attempt to invoke virtual method 'android.view.View android.support.design.widget.FloatingActionButton.findViewById(int)' on a null object reference
Any help would be appreciated.
You are treating a view as a child of its own!
Replace:
mFloatingActionButton = (FloatingActionButton) mFloatingActionButton.findViewById(R.id.terms_back_fab);
With:
mFloatingActionButton = (FloatingActionButton)findViewById(R.id.terms_back_fab);

Loading a webpage into an Android App

I'm working on an android app and I want to be able to load a webpage in the application. I've browsed around the internet for a while and can't seem to get it just right. I know that my internet connection is established, as I can use my browser. I'm new to android development and learning as I go, so any elaboration would be amazing. Here's the code I'm working with:
public class MainActivity extends Activity {
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
Button btnBack;
WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview=(WebView)findViewById(R.id.webview1);
webview.setWebViewClient(new MyWebViewClient());
openURL();
}
/** Opens the URL in a browser */
private void openURL() {
webview.loadUrl("http://www.google.com");
webview.requestFocus();
}
}
and my xml
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_below="#+id/rlayout1"
android:id="#+id/rlayout2">
<WebView android:id="#+id/webview1" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0" />
</RelativeLayout>
Here's what happens when I run the application:
Wow guys, sorry if this is spammed but I just found a forum explanation that I didn't see before. Android requires internet permissions in an app for it to access the internet. I thought that it would give me a permissions exception, but instead it just doesn't load it. here's what I added to the xml manifest for it to work correctly
<uses-permission android:name="android.permission.INTERNET" />
Again, sorry I feel really stupid for not knowing about this. You'd think the console would print some sort of security/permission exception

Android flash magazine application

Is it possible to create a magazine based on Android embedding flash in it?
As your question is very wilde and generic, I can only say "yes that's possible"!
Here is a link to embed your swf file into an Android application.
Load an SWF into a WebView
public class Test3Activity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String url ="file:///android_asset/qualibus.swf";
WebView wv=(WebView) findViewById(R.id.webView1);
wv.getSettings().setPluginsEnabled(true);
wv.loadUrl(url);
}
}
main.xml:
<?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/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
</LinearLayout>
Result:

Categories

Resources