android setVisibility(LinearLayout.VISIBLE) not work in WebView #JavascriptInterface Function - java

activity_main.xml content
<?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=".MainActivity">
<WebView
android:id="#+id/ny_web_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ny_sms"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorBlack"
android:isScrollContainer="false"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:visibility="gone"></LinearLayout>
</android.support.constraint.ConstraintLayout>
MainActivity.java content
package com......;
import android.Manifest;
import android.blablabla.and.other.imports;
...
...
public class MainActivity extends AppCompatActivity {
static WebView mWebView;
static LinearLayout ny_sms;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
ny_sms =(LinearLayout)findViewById(R.id.ny_sms);
mWebView = (WebView)findViewById(R.id.ny_web_view);
mWebView.setWebViewClient(new WebViewClient());
mWebView.loadUrl("file:///android_asset/main.html");
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.addJavascriptInterface(new WebAppInterface(this),"Android");
}
public static void showLayoutMA() {
mWebView.setVisibility(WebView.INVISIBLE);
ny_sms.setVisibility(LinearLayout.VISIBLE);
}
}
class WebAppInterface {
Context mContext;
WebAppInterface(Context c) {
mContext = c;
}
#JavascriptInterface
public void showLayout() {
MainActivity.showLayoutMA();
}
}
main.html content
<html>
<body>
<center>
Visiblity Test
</center>
</body>
</html>
all permissions ok. al settings ok, app version 28 (android 9 pie)
im click "Visiblity Test" link not change any screen activity,
and after scroll to bottom nofication top bar click toggle auto rotate screen button
auto refresh and show LinearLayout front of the webview control.
how to fix this problem.
not showing LinearLayout and webView not invisible with "Visiblity Test" clicked. after need a refreshing action.
Sorry my English is bad :(

im found this problem,
returned error "Only the original thread that created a view hierarchy can touch its views"
So then you need me: runOnUiThread()
extend this code...
public static void showLayoutMA() {
CurrentActivitiy.runOnUiThread(new Runnable() {
#Override
public void run() {
//mWebView.setVisibility(WebView.INVISIBLE);
ny_sms.setVisibility(LinearLayout.VISIBLE);
}
});
why not use direct runOnUiThread(new Runnable() {} function, because my function is static, im add "CurrentActivitiy" declaration of MainActivity.
static Activity CurrentActivitiy;
CurrentActivitiy = (Activity)MainActivity.this;
and after few hours happy ending, problem is resolved..

Related

Android start Activity 2 but rest at MainActivity

I have MainActivity which has got a button that opens Activity 2. Everything from Activity 2 should be calculated and be run but the user should rest at MainActivity? How do I do this?
I found a solution where I dont run the "setContentView()" but then my app crashes.
You want to change the view of application but does not want the user to change activity.
Use two different layout in xml file with id :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/first_view">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
android:id="#+id/button"
android:background="#358a32" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/second_view"
android:visibility="gone">
</LinearLayout>
<LinearLayout>
Then onclick of button :
public class MainActivity extends Activity {
boolean firstViewOff = false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button1);
LinearLayout first_view = (LinearLayout) findViewById(R.id.first_view);
LinearLayout second_view = (LinearLayout) findViewById(R.id.second_view);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
firstViewOff = true;
first_view.setVisibility(View.GONE);
second_view.setVisibility(View.VISIBLE);
}
});
}
#override
public void onBackPressed(){
super.onBackPressed();
if(firstViewOff){
second_view.setVisibility(View.GONE);
first_view.setVisibility(View.VISIBLE);
firstViewOff = false;
}
}
}
Why i attached backpressed :
Because when user backpress then it will just directly show firstview without closing,

WebView scroll up automatically when scrolling it down little

In web view when I scroll the page little bit down then it scroll up automatically on some pages not all pages but on some pages.Why this happen and what is it solution
Here is xml code
<android.support.design.widget.CoordinatorLayout android:id="#+id/nonVideoLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipeContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
>
<WebView
android:id="#+id/webView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</android.support.v4.widget.SwipeRefreshLayout>
</android.support.design.widget.CoordinatorLayout>
Here is my code you can check this
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
bindLayout();
mySwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
webView.reload();
}
});
CookieManager.getInstance().setCookie(url, cookie);
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
ProgressDialogHelper.showProgress(WebViewActivity.this);
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, final String url) {
mySwipeRefreshLayout.setRefreshing(false);
}
});
webView.loadUrl(url);
}
Here is my java code I set the cookie also to web view and I set also layer type. I search that everyone says to set layer type then this problem solved but it does not solved.
try this
final WebSettings settings = mWebView.getSettings();
settings.setDomStorageEnabled(true);
settings.setJavaScriptEnabled(true);
mWebView.setVerticalScrollBarEnabled(false);
Just remove
webView.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
all others are OK
try this in AndroidManifest.xml
set android:hardwareAccelerated="true" in the application and activity tag.

How to use custom webview AND override functions?

Bouncing off this question:
I have made a custom webview called FlingView that incorporates gesture events. The strategy in the linked question above works fine for me; my activity layout xml is fine:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
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:id="#+id/drawer_layout_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="start">
<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:id="#+id/coordinator_layout_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="net.brianclements.android.WebViewActivity">
<net.brianclements.android.FlingView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:keepScreenOn="true" />
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view_webview_left"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_webview_drawer_left" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view_webview_right"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_webview_drawer_right" />
</android.support.v4.widget.DrawerLayout>
onCreate() does this:
setContentView(R.layout.activity_web_view);
and I've used this method to successfully load it:
mWebview = (FlingView) findViewById(R.id.webview);
Now, the problem is when I try to override functions inside of it:
mWebview = new FlingView(this, null) {
#Override
public void onLeftFling() {
Log.d(TAG, ACTIVITY_TAG + "onFling left: ");
someDynamicObject.thatCantBeUsedStatically();
}
#Override
public void onRightFling() {
Log.d(TAG, ACTIVITY_TAG + "onFling right: ");
anotherDynamicObject.thatCantBeUsedStatically();
}
};
So how do I now insert mWebview back into the layout? The first casting method made it so simple. But from my research, I believe it now involves some combination of inflators, findViewById(), addView(), removeView() but I can't seem to figure it out. Any ideas here?
Views defined in your layout are instantiated in the setContentView() call, and you can't override class methods after instantiation. Swapping out the layout-defined View for your anonymous instance with the necessary overrides would work, but it's essentially discarding the instance created from the layout, and isn't really the cleanest way to this.
I would suggest another approach. Create an interface in your FlingView class that can be used to communicate those actions back to your Activity; very similar to an OnClickListener.
For example:
public class FlingView extends WebView {
public interface FlingListener {
void onLeftFling();
void onRightFling();
}
private FlingListener mListener;
public void setFlingListener(FlingListener listener) {
mListener = listener;
}
public void onLeftFling() {
if (mListener != null) {
mListener.onLeftFling();
}
}
public void onRightFling() {
if (mListener != null) {
mListener.onRightFling();
}
}
...
}
In your Activity, after finding your FlingView instance from the inflated layout, simply set an instance of the listener on it.
setContentView(R.layout.activity_web_view);
...
mWebview = (FlingView) findViewById(R.id.webview);
mWebview.setFlingListener(new FlingView.FlingListener() {
#Override
public void onLeftFling() {
Log.d(TAG, ACTIVITY_TAG + "onFling left: ");
someDynamicObject.thatCantBeUsedStatically();
}
#Override
public void onRightFling() {
Log.d(TAG, ACTIVITY_TAG + "onFling right: ");
anotherDynamicObject.thatCantBeUsedStatically();
}
}
);
If you want to replace the current FlingView with the one you just created you need to do the following:
CoordinatorLayout cl = (CoordinatorLayout) findViewById(R.id.coordinator_layout_webview);
cl.removeViewAt(0); // removing previous flingview
cl.addView(mWebView, 0); // adding the new inflated one

Android Studio: How to change the Background to jpg. images using OnclickListener

So far I know how to change the Background's color via html codes. Now I am Trying to change the background of the Layout of my main activity to different images using a button click. If possible using only one single button.
Thank you!
The following code is from this link Setting background image in java from Omi0301.
//assuming your Layout is named linearlayout1:
LinearLayout ll = (LinearLayout) findViewById(R.id.linearlayout1);
ll.setBackgroundResource(R.drawable.sample);
All you do is create a layout variable and set its background resource with the image that you would like it to be.
try to use "setImageResource" instead the "setBackgroundResourse"
Try below code
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/myLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="64dp"
android:layout_marginTop="71dp"
android:text="changeColor" />
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
/** Called when the activity is first created. */
Button button;
LinearLayout mainLayout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainLayout=(LinearLayout)findViewById(R.id.myLayout);
button=(Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
mainLayout.setBackgroundResource(R.drawable.newImage);
}
});
}
}

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