I have a web view in which i will enter the URL and load that content to that web view.
activity_main.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"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world"
android:visibility="gone" />
<LinearLayout
android:id="#+id/layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="#+id/urlField"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="9"
android:ems="10"
android:inputType="textUri"
android:imeOptions="actionDone"
android:hint="http://proteam.in" />
<Button
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="#+id/layout"
android:layout_centerHorizontal="true"
android:layout_weight="1"
android:onClick="open"
android:text="#string/browse" />
</LinearLayout>
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_alignParentBottom="true"
android:layout_below="#+id/layout" />
</RelativeLayout>
MainActivity.java
package com.example.mayanmarbroswer;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText field;
private WebView browser;
private ProgressDialog pDialog;
final Context context = this;
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
field = (EditText)findViewById(R.id.urlField);
browser = (WebView)findViewById(R.id.webView1);
browser.setWebViewClient(new MyBrowser());
//myTextView.setTypeface(typeFace);
WebSettings webSettings = browser.getSettings();
webSettings.setDefaultTextEncodingName("utf-8");
webSettings.setFixedFontFamily("file:///android_asset/font/Sanpya.ttf");
ActionBar bar = getActionBar();
bar.hide();
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
field.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
// TODO Auto-generated method stub
if (actionId == EditorInfo.IME_ACTION_DONE)
{
if(field.getText().toString().trim().equals(""))
{
Toast.makeText(getApplicationContext(), "Enter Valid WebSite", Toast.LENGTH_SHORT).show();
}
else
{
open(v);
}
}
return false;
}
});
}
public void open(View view){
String url;
if(field.getText().toString().trim().equals(""))
{
Toast.makeText(getApplicationContext(), "Enter Valid WebSite", Toast.LENGTH_SHORT).show();
}
else
{
if(field.getText().toString().startsWith("http://"))
{
url = field.getText().toString();
}
else
{
url = "http://" + field.getText().toString();
}
browser.getSettings().setLoadsImagesAutomatically(true);
browser.getSettings().setJavaScriptEnabled(true);
browser.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
browser.loadUrl(url);
}
}
private class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
/*pDialog = new ProgressDialog(context, ProgressDialog.THEME_HOLO_DARK);
// set indeterminate style
pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
// set title and message
//pDialog.setTitle("Please wait");
pDialog.setMessage("Loading Pls Wait");
// and show it
pDialog.show();*/
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
//do what you want to do
/*if (pDialog.isShowing())
{
pDialog.dismiss();
}*/
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
URL 1: http://www.mora.gov.mm/
URL 2: http://www.saikwan.info/
i tested this in few android device like moto e (4.4.2), Dell venue 7 (4.4.2), Samsung Duos GT-s7392 (4.1.2) and Videocon z40pro(4.2.2) the above given URL with myanmar font is working correctly. but when i try to load that in Samsung GT-s7582(4.2.2) the myanmar fonts are missing in web view. so, please let me know how to solve this issue.
If you fetch data from resource folder i mean asset folder then try this way.
Java Code
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonPlayVideo = (Button)findViewById(R.id.playvideoplayer);
Button buttonPlayVideo1 = (Button)findViewById(R.id.button1);
Button acceess = (Button)findViewById(R.id.button2);
getWindow().setFormat(PixelFormat.UNKNOWN);
//Displays a video file.
buttonPlayVideo1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// VideoView refference see main.xml
VideoView mVideoView = (VideoView)findViewById(R.id.videoview);
//String uriPath = "android.resource://com.android.AndroidVideoPlayer/"+R.raw.k;
String uriPath = "android.resource://com.android.AndroidVideoPlayer/"+R.raw.demo;
Uri uri = Uri.parse(uriPath);
mVideoView.setVideoURI(uri);
mVideoView.requestFocus();
mVideoView.start();
}
});
If you fetch from URL or FTP Server OR HTTP Connection then try this way
Java Page
buttonPlayVideo.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//VideoView mVideoView = (VideoView)findViewById(R.id.videoview);
Intent browserIntent =
new Intent(Intent.ACTION_VIEW, Uri.parse("http://192.168.0.8/aeon/")); // For Video Content
// new Intent(Intent.ACTION_VIEW, Uri.parse("ftp://movies:movies#192.168.0.8/Transport_Streams/"));
startActivity(browserIntent);
}});
acceess.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(AndroidVideoPlayer.this,Videopage.class);
startActivity(i);
}
});
}
XML Code Of Both Way are is Given Following way
XML Page for when fetch from the resource
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF8800"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Welcome To Aeon Group"
android:textColor="#9933CC"
android:textSize="3mm" />
<VideoView
android:id="#+id/videoview"
android:layout_width="fill_parent"
android:layout_height="394dp"
android:layout_above="#+id/playvideoplayer"
android:layout_alignParentLeft="true" />
<Button
android:id="#+id/playvideoplayer"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="22dp"
android:background="#drawable/button"
android:text="Aeon Content" />
<!-- <VideoView
android:id="#+id/videoView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/playvideoplayer"
android:layout_below="#+id/textView1" />
-->
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/playvideoplayer"
android:layout_alignBottom="#+id/playvideoplayer"
android:layout_alignParentLeft="true"
android:background="#drawable/button"
android:text="Play" />
<Button
android:id="#+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/playvideoplayer"
android:layout_alignBottom="#+id/playvideoplayer"
android:layout_toRightOf="#+id/playvideoplayer"
android:background="#drawable/button"
android:text="Acces" />
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
Related
I am creating a chat app in which you can create groups. to create groups I have created a new activity. it shows up in preview but won't show up when I run the app and open the activity. I tried on another phone and only the CircleImageView showed up. but it's OnClickListner won't work. there are no errors while compiling, logging or debbugging.
I have even tried breakpoints to check the error, but no output. Exept for the ad shows up. tried changing layouts but nothing shows up.
I don't know the error so I am pasting the CreateGroupActivity.java file as well as activity_create_group.xml
CreateGroupActivity.java:
package com.satyamedh.chitchatmessenger;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.theartofdev.edmodo.cropper.CropImage;
import com.theartofdev.edmodo.cropper.CropImageView;
import de.hdodenhof.circleimageview.CircleImageView;
public class CreateGroupActivity extends AppCompatActivity
{
private CircleImageView circleImageView;
private EditText editText;
private Button buttonconf, buttoncanc;
private final static int chosenProfileImage = 1;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_group);
initializeFields();
buttoncanc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent profileIntent = new Intent(CreateGroupActivity.this, MainActivity.class);
startActivity(profileIntent);
}
});
buttonconf.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (TextUtils.isEmpty(editText.getText())){
Toast.makeText(CreateGroupActivity.this, "Please enter text..", Toast.LENGTH_SHORT).show();
}
}
});
circleImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent galleryIntent = new Intent();
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent, chosenProfileImage);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == chosenProfileImage && resultCode == RESULT_OK && data != null)
{
Uri imageUri = data.getData();
CropImage
.activity()
.setGuidelines(CropImageView.Guidelines.ON)
.start(this);
}
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE)
{
final CropImage.ActivityResult result = CropImage.getActivityResult(data);
if(resultCode == RESULT_OK) {
assert result != null;
Uri resultUri = result.getUri();
DatabaseReference rootref = FirebaseDatabase.getInstance().getReference();
Intent settingsIntent = new Intent(CreateGroupActivity.this, chooseUserActivity.class);
String key = rootref.push().getKey();
settingsIntent.putExtra("groupKey", key);
settingsIntent.putExtra("groupImage", resultUri);
settingsIntent.putExtra("groupName", editText.getText().toString());
startActivity(settingsIntent);
}
}
}
private void initializeFields()
{
MobileAds.initialize(this, "ca-app-pub-3127817354023186~6842500243");
final AdView mAdView;
mAdView = findViewById(R.id.my_adView_createGroup);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
mAdView.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
// Code to be executed when an ad finishes loading.
}
#Override
public void onAdFailedToLoad(int errorCode) {
// Code to be executed when an ad request fails.
}
#Override
public void onAdOpened() {
// Code to be executed when an ad opens an overlay that
// covers the screen.
}
#Override
public void onAdClicked() {
// Code to be executed when the user clicks on an ad.
}
#Override
public void onAdLeftApplication() {
// Code to be executed when the user has left the app.
}
#Override
public void onAdClosed() {
// Code to be executed when the user is about to return
// to the app after tapping on an ad.
}
});
circleImageView = findViewById(R.id.create_group_image);
editText = findViewById(R.id.create_group_name);
buttonconf = findViewById(R.id.confirm_button);
buttoncanc = findViewById(R.id.cancel_button);
}
}
activity_create_group.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".CreateGroupActivity">
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="51dp"
android:layout_marginTop="87dp"
android:layout_marginEnd="259dp"
android:layout_marginBottom="551dp"
android:src="#drawable/profile_image"
android:id="#+id/create_group_image"/>
<EditText
android:layout_width="250dp"
android:layout_height="50dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="167dp"
android:layout_marginTop="88dp"
android:layout_marginEnd="4dp"
android:layout_marginBottom="596dp"
android:background="#drawable/inputs"
android:id="#+id/create_group_name"
android:inputType="text"
android:hint="Group name here"/>
<Button
android:id="#+id/confirm_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="168dp"
android:layout_marginTop="141dp"
android:layout_marginEnd="156dp"
android:layout_marginBottom="542dp"
android:text="Confirm"
android:background="#drawable/buttons"/>
<Button
android:id="#+id/cancel_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="263dp"
android:layout_marginTop="140dp"
android:layout_marginEnd="60dp"
android:layout_marginBottom="546dp"
android:text="Cancel"
android:background="#drawable/buttons"/>
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/my_adView_createGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
</com.google.android.gms.ads.AdView>
</RelativeLayout>
android phone(redmi 2) screenshot:
https://drive.google.com/file/d/18MMzLvcdIi8iLog1nR_NrJPcJf-NZG51/view?usp=sharing
pc android studio screenshot:
https://drive.google.com/file/d/1E3Suy967AS3aQS9TEkUlAh5kPjWr86u-/view?usp=sharing
I am newbie to stackoverflow so I can't attach images. sorry.
no errors also.
it's normal because you used so much alignment value and attribute which made your layout not responsive , please use the xml below and don't forget to add the image:src and background you wanted
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".CreateGroupActivity">
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="20dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:id="#+id/create_group_image"/>
<EditText
android:layout_toRightOf="#id/create_group_image"
android:layout_width="250dp"
android:layout_marginTop="20dp"
android:layout_height="wrap_content"
android:id="#+id/create_group_name"
android:inputType="text"
android:hint="Group name here"/>
<Button
android:layout_below="#id/create_group_name"
android:layout_toRightOf="#id/create_group_image"
android:id="#+id/confirm_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Confirm"/>
<Button
android:layout_below="#id/create_group_name"
android:layout_toRightOf="#id/confirm_button"
android:id="#+id/cancel_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"/>
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/my_adView_createGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
</com.google.android.gms.ads.AdView>
</RelativeLayout>
I have created a simple browser application with a edittext , go button and history button.
Please suggest what coding do i need to implement for history button such that when i press it my browsing history gets displayed on the hi.xml layout. Please suggest what coding changes do i need to make in my xml file as well as java file .
MainActivity.java:
package com.example.history;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener{
EditText t;
Button g,h;
WebView w;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
g=(Button) findViewById(R.id.button1);
h=(Button) findViewById(R.id.button2);
t=(EditText) findViewById(R.id.editText1);
w=(WebView) findViewById(R.id.webView1);
h.setOnClickListener(this);
g.setOnClickListener(this);
w.getSettings().setJavaScriptEnabled(true);
w.getSettings().setLoadWithOverviewMode(true);
w.getSettings().setUseWideViewPort(true);
w.setWebViewClient(new Callback());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId())
{
case R.id.button1:
String c;
c=t.getText().toString();
String theWebsite=("http://").concat(c);
t.setText(theWebsite);
w.loadUrl(theWebsite);
InputMethodManager imm=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(w.getWindowToken(),0);
break;
case R.id.button2:
Intent a=new Intent(MainActivity.this,h.class);
startActivity(a);
break;
}
}
}
class Callback extends WebViewClient{
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return (false);
}
}
activity_main.xml:
<LinearLayout 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:orientation="vertical"
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:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginLeft="15dp"
android:layout_marginTop="16dp"
android:ems="10" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_marginTop="34dp"
android:layout_toRightOf="#+id/textView1"
android:text="Go" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button1"
android:layout_centerVertical="true"
android:text="History" />
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
h.java:
package com.example.history;
import android.app.Activity;
import android.os.Bundle;
public class h extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.hi);
}
}
hi.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" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
When you open a URL using the WebView it gets stored in the phone's web history.
You can access it by using the below code.Later add it to the listview you use in hi.xml
try{
Cursor mCur = null;
try{
String sortOrder = Browser.BookmarkColumns.DATE + " ASC";
mCur= context.getContentResolver().query(Browser.BOOKMARKS_URI, Browser.HISTORY_PROJECTION, null, null, sortOrder);
mCur.moveToFirst();
if (mCur.moveToFirst() && mCur.getCount() > 0) {
while (mCur.isAfterLast() == false) {
String title = mCur.getString(Browser.HISTORY_PROJECTION_TITLE_INDEX);
String url = mCur.getString(Browser.HISTORY_PROJECTION_URL_INDEX);
long date = mCur.getLong(Browser.HISTORY_PROJECTION_DATE_INDEX);
mCur.moveToNext();
}
}else{
mCur.close();
}
}catch(Exception e){
}finally{
mCur.close();
}
}catch(Exception e){
}
Also you need to give the below permission in your manifest file. Hope this helps.
<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS" />
You need to implement the onPageFinished Method on the WebView Client Class
and you need to save your loaded url to the localdatabase or if you want that history for perticular session of the app then you can also use the other structure like arraylist...
class Callback extends WebViewClient{
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return (false);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
/// Save the url to the localdatabase or to any structure if you want temporary
}
}
and while user press on the history button you need to fetch the data from localdatabase to cursor
and use cursoradapter to display the list of the history url
If you have any further doubt then feel free to ask me...
I have this main page where I have set multiple webViews. Basically the idea is simple. I use three webViews and all of them take different content from an online website. The moment they click on the web view, it should take them to another activity. Then leave the rest to me. I just need help with linking the web view to another activity using the onTouchListener.
Here is my Java File: Look for the // THE CODE FOR THE OnTOUCHEVETN STARTS HERE! line. That is where I am trying my webview 1 to start a second activity. I am trying to use this: Intent myIntentActivity1 = new Intent(this, ReadComments.Class); but java gives me the following error:
Remove arguments to match 'Intent()'
package com.testapp;
import com.testapp.R.menu;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageView;
public class mainlogin extends Activity implements OnClickListener{
private Button mRegister, mlogin, mcontactus;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mainlogin);
ImageView img = (ImageView)findViewById(R.id.centennialmsssite);
img.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse("http://mywebsite.com"));
startActivity(intent);
}
});
WebView wv = (WebView)findViewById(R.id.my_webview);
wv.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
wv.loadUrl("http://mywebsite.com/main-content");
// THE CODE FOR THE OnTOUCHEVETN STARTS HERE!
WebView wv1 = (WebView)findViewById(R.id.topic_one);
wv1.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
wv1.loadUrl("http://mywebsite.com/content-one");
wv1.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){
Intent myIntentActivity1 = new Intent(this, ReadComments.Class);
//call N_X and wait for result
startActivity(myIntentActivity1);
return true;
}
return false;
}
});
// IT ENDS HERE!
WebView wv2 = (WebView)findViewById(R.id.topic_two);
wv2.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
wv2.loadUrl("http://mywebsite.com/content-two");
WebView wv3 = (WebView)findViewById(R.id.topic_three);
wv3.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
wv3.loadUrl("http://mywebsite.com/content-three");
mRegister = (Button)findViewById(R.id.btn_register);
mRegister.setOnClickListener(this);
mlogin = (Button)findViewById(R.id.btn_login);
mlogin.setOnClickListener(this);
mcontactus = (Button) findViewById(R.id.btn_contact_us);
mcontactus.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_login:
Intent i = new Intent(this, Login.class);
startActivity(i);
break;
case R.id.btn_register:
Intent i1 = new Intent(this, Register.class);
startActivity(i1);
break;
case R.id.btn_contact_us:
Intent i2 = new Intent(this, contactus.class);
startActivity(i2);
break;
default:
break;
}
}
}
Here is my XML file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
android:orientation="vertical" >
<Button
android:id="#+id/btn_register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btn_login"
android:layout_alignBottom="#+id/btn_login"
android:layout_alignRight="#+id/textView1"
android:background="#333333"
android:minHeight="40dp"
android:minWidth="140dp"
android:text="#string/mainpage_register_button"
android:textColor="#999999" />
<Button
android:id="#+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="280dp"
android:background="#333333"
android:minHeight="40dp"
android:minWidth="140dp"
android:text="#string/main_button_login"
android:textColor="#999999" />
<Button
android:id="#+id/btn_contact_us"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/btn_login"
android:layout_alignRight="#+id/btn_register"
android:layout_below="#+id/btn_register"
android:background="#333333"
android:minHeight="40dp"
android:layout_marginTop="2dp"
android:text="#string/mainpage_contact_us"
android:textColor="#999999" />
<ImageView
android:id="#+id/centennialmsssite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/btn_contact_us"
android:layout_below="#+id/btn_contact_us"
android:layout_marginTop="14dp"
android:contentDescription="#string/mss_button_description"
android:src="#drawable/msswebsitebutton" />
<TextView
android:id="#+id/textView1"
android:layout_width="286dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:gravity="center"
android:text="#string/welcome_mainpage"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
android:textStyle="bold" />
<WebView
android:id="#+id/my_webview"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignLeft="#+id/textView1"
android:layout_alignRight="#+id/sitebutton"
android:layout_below="#+id/textView1"
android:layout_marginTop="12dp"
android:maxHeight="40dp"
android:minHeight="40dp" />
<WebView
android:id="#+id/topic_one"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_alignLeft="#+id/my_webview"
android:layout_alignRight="#+id/my_webview"
android:layout_below="#+id/my_webview"
android:layout_marginTop="5dp"
android:maxHeight="70dp"
android:minHeight="70dp" />
<WebView
android:id="#+id/topic_two"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_alignLeft="#+id/topic_one"
android:layout_alignRight="#+id/topic_one"
android:layout_below="#+id/topic_one"
android:maxHeight="70dp"
android:minHeight="70dp" />
<WebView
android:id="#+id/topic_three"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_alignLeft="#+id/topic_two"
android:layout_alignRight="#+id/topic_two"
android:layout_below="#+id/topic_two"
android:maxHeight="70dp"
android:minHeight="70dp" />
</RelativeLayout>
What could be causing this error?
Try this-
Intent myIntentActivity1 = new Intent(mainlogin.this, ReadComments.class);
startActivity(myIntentActivity1);
Or-
Intent myIntentActivity1 = new Intent(getBaseContext(), ReadComments.class);
startActivity(myIntentActivity1);
I get an error when I run my android app on my android device. It says Unfortunately, GuessingGame has stopped. It was running before but ever since I added a EditText to the layout, it has been crashing. Here is my MainActivity.java:
package com.h2ogaming.numberguessinggame;
import java.util.Random;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView textOne = (TextView) findViewById(R.id.textView1);
Button pressMe = (Button) findViewById(R.id.button1);
pressMe.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String randText = "";
// TODO Auto-generated method stub
Random randGen = new Random();
int random = randGen.nextInt(5) + 1;
randText = Integer.toString(random);
textOne.setText(randText);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Here is my Activity_Main XML file:
<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"
android:background="#2288ee">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="79dp"
android:text="#string/guess_text"
android:textColor="#ffffff"
android:textSize="26sp" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp"
android:text="#string/text_view1"
android:textColor="#ffffff"
android:textSize="26sp" />
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp"
android:text="#string/btn_press" />
</RelativeLayout>
Please help me. Thanks!
Change your initialization for components.declare proper all components globally. then initialize it like below code:
public class MainActivity extends Activity {
TextView textOne;
Button pressMe;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textOne = (TextView) findViewById(R.id.textView1);
pressMe = (Button) findViewById(R.id.button1);
I'm making a program for Android using Eclipse. Here is my code for MainAcitvity.java:
package com.example.changeme;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
Button set = (Button)findViewById(R.id.set);
TextView out = (TextView)findViewById(R.id.out);
EditText in = (EditText)findViewById(R.id.input);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
set.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setcontent();
}
});
}
public void setcontent()
{
String con = in.getText().toString();
out.setText(con);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return false;
}
}
Layout for main activity :
<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:id="#+id/out"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/set"
android:layout_alignParentBottom="true"
android:layout_marginBottom="37dp"
android:text="#string/Change"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/set"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/input"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text="#string/Set" />
<EditText
android:id="#+id/input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="37dp"
android:ems="10"
android:inputType="textPersonName" />
It crashes when I run the program on my N4.
You need to inflate your layout before retrieve your elements, otherwise findViewById will return null and hence the line set.setOnClickListener(/**/); will throw a NullPointerException which make your app crashing.
Button set;
TextView out;
EditText in;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //<-- the layout is inflated, i.e it's rendered
/******************************************************/
//Now you can retrieve your elements from this layout
set = (Button)findViewById(R.id.set);
out = (TextView)findViewById(R.id.out);
in = (EditText)findViewById(R.id.input);
/*****************************************************/
set.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setcontent();
}
});
}