I want to implement WebView in a such way that it should get instantiated in one Activity (along with loading of html) it should not be visible on first page and only webview is visible in another activity. I have tried following way, but unable to achieve it, following is my MainActivity
public class MainActivity extends Activity {
private WebView webView = null;
private Button startAppBtn = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createWebView();
startAppBtn = (Button) findViewById(R.id.button1);
startAppBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, SecondActivity.class);
Bundle bundle = new Bundle();
intent.putExtra("state",bundle);
webView.saveState(bundle);
startActivity(intent);
}
});
}
public void createWebView(){
setWebView((WebView) findViewById(R.id.webview1));
getWebView().getSettings().setJavaScriptEnabled(true);
getWebView().getSettings().setDatabaseEnabled(true);
getWebView().getSettings().setLoadWithOverviewMode(true);
getWebView().setScrollBarStyle(WebView.SCROLLBARS_INSIDE_OVERLAY);
getWebView().setScrollbarFadingEnabled(false);
getWebView().loadUrl("file:///android_asset/My.html");
getWebView().setVisibility(View.GONE);
}
public WebView getWebView() {
return webView;
}
public void setWebView(WebView webView) {
this.webView = webView;
}
}
and following is SecondActivity,
public class SecondActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_layout);
Intent intent = getIntent();
WebView webview = (WebView) findViewById(R.id.webview1);//at this line I am getting exception, I think because webview is in activity_main layout.
webview.restoreState((Bundle) intent.getParcelableExtra("state"));
webview.getSettings().setJavaScriptEnabled(true);
webview.setVisibility(View.VISIBLE);
}
}
I am new to android world, struggling on it please anybody can help me to fix this issue.
Hope this helps
First Activity
startAppBtn = (Button) findViewById(R.id.button1);
startAppBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putString("websiteURL", "file:///android_asset/My.html");
intent.putExtras(bundle);
startActivity(intent);
}
});
Second Activity
String sWebsite;
Bundle bundle = getIntent().getExtras();
if (bundle != null)
{
sWebsite = bundle.getString("websiteURL");
}
else if (bundle== null)
{
System.out.println("Null");
}
WebView webView = (WebView) findViewById(R.id.webview1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(sWebsite);
webView.setWebViewClient(new WebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
return false;
}
});
Related
I have setup a WebViewActivity which gets the URL from the intent. I have setup an onClickListener for my Recyclerview which opens the WebViewActivity and loads the URL. However the WebView does not cache each an every page it opens. What changes should i make in the code?
WebViewActivity.java
public class WebViewActivity extends AppCompatActivity {
WebView myWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Search");
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(), MainActivity.class));
}
});
Intent intent = getIntent();
String url = intent.getStringExtra("url");
myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient());
WebSettings webSettings = myWebView.getSettings();
myWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl(url);
}
#Override
public void onBackPressed() {
if(myWebView.canGoBack()){
myWebView.goBack();
}else{
super.onBackPressed();
finish();
}
}
}
ArticleAdapter.java
public class ArticleAdapter extends RecyclerView.Adapter<ArticleAdapter.ArticleViewHolder> {
private static final String TAG = "ArticleAdapter";
private Context mContext;
private ArrayList<String> mlist;
private String url;
public ArticleAdapter(Context context, ArrayList<String> list) {
mContext = context;
mlist = list;
}
#Override
public ArticleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Log.d(TAG, "onCreateViewHolder: called");
Log.d(TAG, "onCreateViewHolder: GOT REQUEST CALLED===================");
LayoutInflater layoutInflater = LayoutInflater.from(mContext);
View view = layoutInflater.inflate(R.layout.article_list, parent, false);
return new ArticleViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull final ArticleViewHolder articleViewHolder, int i) {
if(mlist.size()!=0){
url = mlist.get(articleViewHolder.getAdapterPosition());
articleViewHolder.name.setText(url);
articleViewHolder.name.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(mContext, WebViewActivity.class);
intent.putExtra("url", articleViewHolder.name.getText());
mContext.startActivity(intent);
}
});
}
}
class ArticleViewHolder extends RecyclerView.ViewHolder {
TextView name;
public ArticleViewHolder(View itemView) {
super(itemView);
name = itemView.findViewById(R.id.tv_title);
}
}
#Override
public int getItemCount() {
return mlist.size();
}
}
You can write URL to the shared preference or file (with append mode), then whenever you need history just read the contents from the location
If you want cache web URLs then there is an option for WebView
Java code
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
websettings.setAppCachePath(getContext().getCacheDir().toString());
websettings.setAppCacheEnabled(true);
websettings.setCacheMode(WebSettings.LOAD_DEFAULT);
this and this I have found useful, please refer these
there is Images, Names and URL object in MainActivity code which is view in recycler item:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
toolbar.setTitle(getResources().getString(R.string.app_name));
recyclerView = findViewById(R.id.recyclerView);
images = new int[]{R.drawable.google, R.drawable.facebook,
R.drawable.gaana, R.drawable.youtube, R.drawable.oracle,
R.drawable.wikipedia};
webNames = new String[]{"Google", "Gacebook", "Gaana",
"YouTube", "Oracle", "Wikipedia"};
webUrl = new String[]{"https://www.google.com.",
"https://www.facebook.com",
"https://www.gaana.com",
"https://www.youtube.com",
"https://www.oracle.com",
"https://www.wikipedia.org"};
Adapter which hold Image, Name and Url which parse to recyclerview items and by onClicklistener it load in default browser:
#Override
public void onBindViewHolder(final PlaceViewHolder holder, int position) {
holder.webName.setText(webNames[position]);
holder.wIcon.setImageResource(images[position]);
holder.wIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(context, WebPage.class);
intent.putExtra("webUrl", webUrl.toString());
// Intent intent = new Intent(Intent.ACTION_VIEW);
// intent.setData(Uri.parse(placeGuide[holder.getAdapterPosition()]));
context.startActivity(intent);
}
});
}
try to load url in webview:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_page);
WebView webView = findViewById(R.id.weView);
webView.getSettings().setJavaScriptEnabled(true);
Bundle bundle = getIntent().getExtras();
webView.loadUrl(bundle.getString("webUrl"));
}
i want to load Url in Webview
RecycleView
Intent intent = new Intent(context, WebPage.class);
intent.putExtra("webUrl", webUrl[position]);
context.startActivity(intent);
Another Activity
Intent intent=getIntent();
String url=intent.getStringExtra("webUrl");
webView.loadUrl(url);
Replace webUrl.toString() in RecyclerViewAdapter to webUrl[position], everything will be ok.
The code below is intended to pass the data, using bundle, collected on signing up in Signup.java to ViewProfile.java which displays the data. On checking for a key in the bundle, it returns true, however, the bundle is null when checked in ViewProfile.java. Help will b appreciated.
Signup.java
public class Signup extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
final EditText n=(EditText)findViewById(R.id.editText3);
final EditText u=(EditText)findViewById(R.id.editText4);
final EditText p=(EditText)findViewById(R.id.editText5);
final EditText c=(EditText)findViewById(R.id.editText6);
Button s=(Button)findViewById(R.id.button4);
final Userdatabase udb=new Userdatabase(this);
s.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
String name=n.getText().toString();
String email=u.getText().toString();
String password=p.getText().toString();
String phone=c.getText().toString();
boolean b=udb.insertuser(name,email,password);
if(b==true) {
Intent i = new Intent(Signup.this, MainActivity.class);
Bundle bundle=new Bundle();
bundle.putString("NAME",name);
bundle.putString("ID",email);
bundle.putString("PHONE",phone);
i.putExtras(bundle);
startActivity(i);
}
else
Toast.makeText(getApplicationContext(),"Please try again",Toast.LENGTH_SHORT).show();
}
});
}
}
ViewProfile.java
public class ViewProfile extends AppCompatActivity {
String name,username,contact,profession;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_profile);
Intent intent=getIntent();
Bundle b=intent.getExtras();
if(b!=null) {
name = b.getString("NAME");
username = b.getString("ID");
contact = b.getString("PHONE");
TextView tv1=(TextView)findViewById(R.id.textView17);
TextView tv2=(TextView)findViewById(R.id.textView18);
TextView tv3=(TextView)findViewById(R.id.textView19);
tv1.setText(name);
tv2.setText(username);
tv3.setText(contact);
}
ImageView img=(ImageView)findViewById(R.id.imageView4);
img.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent i=new Intent(ViewProfile.this,Profile.class);
startActivity(i);
}
});
}
}
You are calling the wrong activity in the SignUp activity:
if(b==true) {
Intent i = new Intent(Signup.this, MainActivity.class);//problem here
You have set the intent as MainActivity.class and sending data to MainActivity and not to ViewProfile activity.
Change to this in SignUp activity:
if(b==true) {
Intent i = new Intent(Signup.this, ViewProfile.class);
I have two activities, MainActivity and SecondActivity. While passing int value from MainActivity to SecondActivity it becomes "0". I have tried with and without bundle, tried various solution already present on StackOverFlow, but no go.
Here is my code:
MainActivity
final Intent intent = new Intent(MainActivity.this, SecondActivity.class);
final TextView textView = findViewById(R.id.textView);
textView.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Bundle bundle = new Bundle();
bundle.putInt("category",9);
intent.putExtras(bundle);
startActivity(intent);
}
});
SecondActivity:
package com.example.app;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class SecondActivity extends AppCompatActivity {
ViewPager viewPager;
TabsPagerAdapter pagerAdapter;
private int mMedCategory = 6;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
// String stringCategory = getIntent().getStringExtra("category_string");
int medCategory = getIntent().getIntExtra("category_int", -1);
setMedCategory(medCategory);
viewPager = findViewById(R.id.viewpager);
pagerAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(pagerAdapter);
}
public int getMedCategory(){
return mMedCategory; //This value goes to TabsPagerAdapter
}
public void setMedCategory(int i){
mMedCategory = i;
}
}
You can try this:
MainActivity
TextView textView = findViewById(R.id.textView);
textView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("category_int", 9);
intent.putExtra("category_string", "9");
startActivity(intent);
}
});
SecondActivity
private int medCategoryInt;
private String medCategoryString;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
medCategoryInt = getIntent().getIntExtra("category_int", 0);
medCategoryString = getIntent().getStringExtra("category_string");
}
You can have a look at How do I pass data between Activities in Android application?
Use Intent#getIntExtra() to retrieve an integer value from the intent:
From MainActivity:
Intent myIntent = new Intent(MainActivity.this, SecondActivity.class);
myIntent.putExtra("category", 9);
MainActivity.this.startActivity(myIntent);
From SecondActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
medCategory = intent.getIntExtra("category", -1);
}
Note that this would assign a default value of -1 to medCategory in the event that the key cannot be found.
I'm not sure but You may use this for second activity:
SecondActivity secondActivity = new SecondActivity();
if (secondActivity.getIntent.getExtras() != null)
{
int medCat = secondActivity.getIntent.getExtras().getInt("category");
}
Use this code. Remove final and try to initialize intent in onclick method not outside of it.
In your OnClick
Intent intent=new Intent(this,Main2Activity.class);
Bundle bundle = new Bundle();
bundle.putInt("category",9);
intent.putExtras(bundle);
startActivity(intent);
In your second activity
public class Main2Activity extends AppCompatActivity {
private int mMedCategory =0;
public int getmMedCategory() {
return mMedCategory;
}
public void setmMedCategory(int mMedCategory) {
this.mMedCategory = mMedCategory;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Bundle bundle=getIntent().getExtras();
if(bundle!=null){
int demoCategory =bundle.getInt("category");
setmMedCategory(demoCategory);
}else {
Toast.makeText(this, "null", Toast.LENGTH_SHORT).show();
}
}
public void Show(View view) {
Toast.makeText(this, String.valueOf(getmMedCategory()), Toast.LENGTH_SHORT).show();
}
}
EDIT
Created a demo project for you it is working. Show method is onclick method of a button.
i'm getting this error
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.bassammetwally.like/com.example.bassammetwally.like.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
when i'm trying to switch activity in another method in the
mainActivity.class
The Code i'm trying to run(not going to include libraries);
public class MainActivity extends AppCompatActivity {
final Intent i = new Intent(this, profile.class);
ImageButton ButtonOne = (ImageButton) findViewById(R.id.profile);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Intent i = new Intent(this, profile.class);
ImageButton ButtonOne = (ImageButton) findViewById(R.id.profile);
ButtonOne.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
button();
}
});
}
public void button()
{
startActivity(i);
}
}
code before that worked
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Intent i = new Intent(this, profile.class);
ImageButton ButtonOne = (ImageButton)findViewById(R.id.profile);
ButtonOne.setOnClickListener(new View.OnClickListener(){
public void onClick( View v ){
startActivity(i);
}
});
}}
Questions:
What is the meaning of the error?
why is this error showing?
Try this code:
final Intent i ;
ImageButton ButtonOne ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
i =new Intent(MainActivity.this, profile.class);
ButtonOne = (ImageButton) findViewById(R.id.profile);
ButtonOne.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
button();
}
});
}
public void button()
{
startActivity(i);
}
public class MainActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton ButtonOne = (ImageButton) findViewById(R.id.profile);
ButtonOne.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(this, profile.class);
startActivity(i);
}
});
}
}
Also Added Profile.java in manifest file
You're initiating variable i twice. When going to function button() you are using the one in public scope (above the onCreate method), not the one from onCreate. Your code should look like this:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Intent i = new Intent(this, profile.class);
ImageButton ButtonOne = (ImageButton) findViewById(R.id.profile);
ButtonOne.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
button(i);
}
});
}
public void button(Intent i)
{
startActivity(i);
}
}