How to open Webview on top layer of my activity? (Android) - android

I have a problem to add a webview after I use startactivity. I have an intent and start it :
final Intent intent = new Intent(Intent.ACTION_VIEW, uri);
String uriString = uri.toString();
String extension = uriString.substring(uriString.lastIndexOf('.') + 1);
intent.setClass(this, extensionToActivity.get(extension));
startActivity(intent);
after my activity ran completely, I need to open a webview so I go to Oncreate function and add my code
public void onCreate(Bundle savedInstanceState)
{
WebView wv = (WebView)findViewById(R.id.webView1);
String summary = "<html><body><h1>some test</h1></body></html>";
wv.loadData(summary, "text/html", null);
wv.setVisibility(View.VISIBLE);
}
but my program crash!!! but if i show alert inside onCreat it works fine, what is the problem?
what is the problem?

You forgot to set layout by using setContentView() method. FYI, You can't find any views without setting content views to activity.

Try this,
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView wv = (WebView)findViewById(R.id.webView1);
String summary = "<html><body><h1>some test</h1></body></html>";
wv.loadData(summary, "text/html", null);
wv.setVisibility(View.VISIBLE);
}
EDIT:
#Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

I found that the Intent that I work uses another activity inside, this is because my program crash. I found that activity and add my webview there, now it is solved
setContentView(R.layout.browseritems);
WebView wv = (WebView)findViewById(R.id.webView1);
String summary = "<html><body><h1>some test</h1></body></html>";
wv.loadData(summary, "text/html", null);
wv.setVisibility(View.VISIBLE);

Related

how to open html page a massage cones up ( file:///android_assets/html/1%20html ERR_FILE_NOT_FOUND)

when i want to open html page the massage comes up ( webpage at file:///android_assets/html/1%20html ERR_FILE_NOT_FOUND)
public class Webhtm extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webhtm);
WebView webView = (WebView)findViewById(R.id.webview);
Intent data = getIntent();
Integer page = data.getExtras().getInt("page");
page++;
webView.loadUrl("file:///android_assets/html/"+ page +" +html");
}
}
Replace:
"file:///android_assets/html/"
with:
"file:///android_asset/html/"
The directory on your development machine is assets/ (plural). The fake filesystem path for use in WebView is android_asset (singular).
and, no, I do not know why they did it that way...
("file:///android_assets/html/"+ page +" +html");
}
With
("file:///android_asset/html/"+ page +" .html");
}

Open first Link searched against a keyword

I have 2 activities in my app, First Activity is simple editText and submit Button, When I press submit, it launches webView Activity and searches the keyword entered in the first activity. Now I want to open the first link Automatically in webview.
public class MainActivity extends AppCompatActivity {
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient());
String query = "firebase";// Get the text from EditText here
String url = "https://www.google.com/search?q="+query;
webView.loadUrl(url);
}
}

Android: WebView simple app

I am learning Android development and have completed the first app (https://developer.android.com/training/basics/firstapp/index.html). Now, I added a second text field and a second button. The idea is that the text that the user will enter in the text field will be used as an input in a Google Search Engine.
So I did the following:
Modified the AndroidManifest.xml to add the permission <uses-permission android:name="android.permission.INTERNET" />.
Modified my MainActivity.java to add a call to my new activity:
/** Called when the user taps the Search button */
public void searchMessage(View view) {
Intent intent = new Intent(this, DisplaySearchActivity.class);
EditText editText = (EditText) findViewById(R.id.editText3);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
Then, created a new activity that has a WebView and, for now just displays the main page of Google.com
public class DisplaySearchActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_search);
// Get the Intent that started this activity and extract the string
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("https://www.google.com");
}
}
Finally, my activity_display_search.xml looks like this
<?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="com.example.myapplication.DisplaySearchActivity">
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</android.support.constraint.ConstraintLayout>
When I click the Search button the app takes me to the Search WebView, but nothing happens. What am I missing here?
You need to provide a WebViewClient for your WebView.
public class DisplaySearchActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_search);
// Get the Intent that started this activity and extract the string
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("https://www.google.com/search?q=" + message);
}
}

How can I display a pdf document into a TextView?

I want to read pdf files and display contents on TextView. is it possible ? or just show pdf into WebView or pdfViewer?
i want to do like it,
public class MainActivity extends Activity {
private TextView showText;
String url="http://www.adobe.com/devnet/acrobat/pdfs/pdf_open_parameters.pdf";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showText= (TextView)this.findViewById(R.id.showtext);
showText.setText( ++ convertPdfTotext ++);
}
public void convertPdfTotext(View view)
{
}
}
If you want to use it in text view you can convert it with itext as string,but you'll loose formatting:
PdfTextExtractor parser =new PdfTextExtractor(new PdfReader("C:/Text.pdf"));
parser.getTextFromPage(3);
Use WebView instead and you can enable even zoom.
WebView mWebView=new WebView(MyPdfViewActivity.this);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginsEnabled(true);
mWebView.loadUrl("https://docs.google.com/gview?embedded=true&url="+LinkTo);

loading division of webpage in android webview

My webpage is divided in various parts instead of loading full page, i want to load a part of that page div id = map_canvas.
Below code is giving me full page but i want only one part map_canvas
public class Jetbill extends Activity {
public WebView myWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.jetbill);
myWebView = (WebView) findViewById(R.id.webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl("http://jetbill.biz/users/login.php");
}
}

Categories

Resources