Fullscreen WebView Android - android

WebView in my app shows as window, but I want it be fullscreen! Here is screenshort of it:
Here is my code:
public class DockViewerActivity extends Activity {
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWebView = new WebView(this);
setContentView(mWebView);
mWebView.loadUrl("file:///android_asset/1.html");
Toast.makeText(this, getString(R.string.loading), Toast.LENGTH_LONG).show();
}
}

XML
set the height and width to match_parent like below:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/conrainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<WebView
android:id="#+id/webview"
android:layout_width="match_parent "
android:layout_height="match_parent "/>
</RelativeLayout>
Activity::
to remove the status bar you have to use:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
to remove titlebar,you have to use:
requestWindowFeature(Window.FEATURE_NO_TITLE);
both of the above features you have to set before set your content view like below i am doing:
#Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.base_screen);
}
if you Create webView programtically then set the height, width to match_parent like:
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
mWebView .setLayoutParams(layoutParams);
and its done now!! #cheers!!

Here is my codes to achieve full screen webview.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
setContentView(R.layout.activity_main);
myWebView = (WebView)findViewById(R.id.webview);
...
}
And XML Layout of this activity:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/mains"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
...
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="visible"
/>
...
</RelativeLayout>

Related

Android 8: WebView - vertical scrollbar - incorrect work

I has activity with webView. Here layout xml:
<?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"
android:orientation="vertical">
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Here activity code:
public class TestActivity extends AppCompatActivity {
public static final String HTML_TEXT = "HTML_TEXT";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_activity);
String htmlText = getIntent().getExtras().getString(HTML_TEXT);
WebView webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
String resultHTML = "<html><head><meta charset=utf-8></head><body>" + htmlText + "</body></html>";
webview.loadDataWithBaseURL("fake://", resultHTML, "text/html", StringUtil.encodingUTF8, null);
}
}
And here result:
And as you can see on right side success show scrollbar when scrolling.
This work on Android 4.0, 5.0, 6.0, 7.0. OK.
But on Android 8.0 (Emulator) when scrolling to item "Test many posts 7/15" (about half screen) scrollbar is hide. I'm scrolling to bottom of screen but scrollbar not show.
Here result:
and scrolling to bottom of screen (scrollbar is hide):
How fix this?
in your onCreate() method try this :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_web_view_layout);
browser = (WebView)findViewById(R.id.your_webview);
WebSettings mWebSettings = browser.getSettings();
mWebSettings.setBuiltInZoomControls(true);
browser.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
browser.setScrollbarFadingEnabled(false);
browser.loadUrl("file:///android_asset/index.html");
}

why doesnt this open the webviewer when i press the button?

So i have this button that I would like it to open a webviewer when the button is pressed.
Here is the xml where the button is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:id="#+id/signIn"
android:layout_width="match_parent"
android:layout_height="61dp"
android:src="#drawable/signin"
android:textAlignment="center" />
</LinearLayout>
Here the class code for the click button
public class SignIn extends Activity {
ImageView img;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
img = (ImageView) findViewById(R.id.signIn);
signIn();
}
public void signIn() {
img.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(SignIn.this, WebViewActivity.class);
startActivity(intent);
}
});
}
}
here is where i have the webviwer.xml
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
And here is the webviewer class
public class WebViewActivity extends Activity{
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.google.com");
}
}
When click nothing happens, I also edited the manifest to have internet access
Make a small correction. In XML you are using "ImageButton" but in java file your are using "ImageView".
<ImageView
android:id="#+id/signIn"
android:layout_width="match_parent"
android:layout_height="61dp"
android:src="#drawable/signin"
android:textAlignment="center" />
I think it is wrong to write in XML ImageButton and in java file to search for an ImageView by id.
Use an ImageView or an ImageButton!
if you want to use an imageView don forget to make it clicable.

Web View Zooming Control

htmlp = URLEncoder.encode(desc,"utf-8").replaceAll("\\+"," ");
WebView wv=(WebView)findViewById(R.id.webView1);
wv.setInitialScale(70);
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setPluginsEnabled(true);
wv.getSettings().setSupportZoom(true);
wv.getSettings().setLoadWithOverviewMode(true);
wv.getSettings().setBuiltInZoomControls(true);
wv.loadData(htmlp,"text/html", "utf-8");
In htmlp contains HTML content(tags). Now I have to enable zooming control, but using above code it is not working. Is anything I have to enable in xml part of the webview.
Thanks in advance
This works good in my case.Try it for your case..
In manifest file..
<uses-permission android:name="android.permission.INTERNET">
</uses-permission>
webview.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/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
In activity..
public class Main extends Activity {
private WebView myWebView;
private static final FrameLayout.LayoutParams ZOOM_PARAMS = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT,Gravity.BOTTOM);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.webview);
this.myWebView = (WebView) this.findViewById(R.id.webView);
FrameLayout mContentView = (FrameLayout) getWindow().
getDecorView().findViewById(android.R.id.content);
final View zoom = this.myWebView.getZoomControls();
mContentView.addView(zoom, ZOOM_PARAMS);
zoom.setVisibility(View.GONE);
this.myWebView.loadUrl("http://www.facebook.com");
}
}
wv.getSettings().setBuiltInZoomControls(false);
change to false
At last I have solved this problem. The problem was in the design section. Just changed the layout param to this:
WebView
android:layout_width="match_parent"
android:layout_height="match_parent"

Help understanding why a webview layout won't load the page

I have the following code that works but doesn't use my layout webscreen.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webscreen);
String turl = getIntent().getStringExtra(URL);
Log.v(TAG, "Recipe url = "+turl);
WebView webview = new WebView(this);
setContentView(webview);
webview.clearCache(true);
webview.loadUrl(turl);
If I change the line setcontentview(webview) to setcontentview(R.layout.webscreen) my layout loads but the web page doesn't.
Sorry I am a newbie.
Kind Regards,
Mike
Try This,
public class Main extends Activity {
WebView webview1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webview1 = (WebView)findViewById(R.id.webview01);
webview1.getSettings().setJavaScriptEnabled(true);
webview1.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webview1.getSettings().setBuiltInZoomControls(true);
webview1.setInitialScale(50);
webview1.loadUrl("http://www.mywebsite.com/");
}
}
xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="fill_parent">
<ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent" android:scrollbars="horizontal">
<WebView android:layout_marginTop="60sp" android:id="#+id/webview01" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
</ScrollView>
</LinearLayout>

how to set custom title bar TextView Value dynamically in android?

friends,
i have created custom title bar using following titlebar.xml file with code
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myTitle"
android:text="This is my new title"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#color/titletextcolor"
android:layout_marginLeft="25px"
android:paddingTop="3px"
/>
and java code to display custom title bar on each activity.
#Override
public void onCreate(Bundle savedInstanceState)
{
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
now i want to set textview value dynamically in each activity can any one guide me how can i achieve this?
using findviewbyid here i dont get reference of that textview to set value because
main layout does not contains any textbox with such a name but mytitle.
any help would be appriciated.
This is the way to set the custom title:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
if ( customTitleSupported ) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);
}
final TextView myTitleText = (TextView) findViewById(R.id.myTitle);
if ( myTitleText != null ) {
myTitleText.setText("========= NEW TITLE ==========");
myTitleText.setBackgroundColor(Color.GREEN);
}
}
SDK 2.1+
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setTitle("========= NEW TITLE ==========");
}
Have you tried the setTitle() method of your Activity?
my_title.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+id/header"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:background="#d4e9a9">
<ImageView android:src="#drawable/jetpack"
android:layout_width="wrap_content" android:layout_alignParentLeft="true"
android:layout_centerVertical="true" android:id="#+id/back"
android:layout_height="wrap_content" android:layout_alignParentTop="true" />
<TextView android:id="#+id/title" android:layout_width="wrap_content"
android:gravity="center_vertical" android:textSize="20px"
android:textColor="#ffffff" android:layout_alignParentRight="true"
android:text="New Title" android:background="#a5c639"
android:layout_height="wrap_content" android:layout_alignParentTop="true"
android:padding="9dip" android:layout_margin="5dip" />
</RelativeLayout>
Code:
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.my_title);
((TextView)findViewById(R.id.title)).setText("gradient shadow");
findViewById(R.id.back).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
((TextView)findViewById(R.id.title)).setText("loce");
}
});
Because custom title default is fixed you should write yourself a theme:
<application android:icon="#drawable/icon" android:label="#string/app_name" android:theme="#style/MyTheme">
Try the following:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);
TextView mytitletext = (TextView) findViewById(R.id.myTitle);
mytitletext.setText("========= NEW TITLE ==========");
}
You can use the hierarchyviewer to see if your view is accessible (you will see its id in the hierarchy graph)

Categories

Resources