Android WebView: A particular site doesn't render properly. - java

I am working with a WebView which is supposed to open any website.
I have 2 activities-CallingActivity and UrlBrowser. The webview is in UrlBrowser.
The CallingActivity has an EditText which takes the URL from a user and supplies it to the UrlBrowser activity via a bundle object passed as an extra with an Intent object. The UrlBrowser activity uses this URL to load into a webview.
I have to update a progress bar with the loading progress also for which I've used the onProgressChanged method to update values .
The problem I'm facing is that a particular website: www.flipkart.com is not rendering properly. A blank light brownish patch is all that is visible in the webview.
I tried changing the user agent of the WebView. On putting the user agent string of a desktop chrome browser all websites including flipkart opened perfectly, however, their desktop versions opened. I need the mobile version. As soon as I revert to using the user agent of a mobile browser, the same problem occurs. This led me to believe that maybe something is wrong with the user agents so I faked the user agent to chrome's user agent and the problem still persisted.
Here is the code for the two activities and other relevant files:
UrlBrowser.Java
public class UrlBrowser extends AppCompatActivity
{
WebView webView;
ProgressBar progressBar;
String finalUrl;
Context appcontext;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_url_browser);
webView=(WebView)findViewById(R.id.webView);
progressBar=(ProgressBar)findViewById(R.id.progressBar);
progressBar.setMax(100);
Bundle b= getIntent().getExtras();
String finalUrl="http://"+b.getString("url");
appcontext=getApplicationContext();
viewPage(finalUrl);
}
public class MyWebViewClient extends WebViewClient
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
Toast.makeText(appcontext,"Waiting for "+url ,Toast.LENGTH_SHORT).show();
view.loadUrl(url);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
progressBar.setVisibility(view.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url)
{
progressBar.setVisibility(view.INVISIBLE);
}
}
public void viewPage(String url)
{
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.ge webView.getSettings().setAppCachePath("/data/data"+getPackageName()+"/cache");
webView.getSettings().setAllowFileAccess(true);
webView.setWebViewClient(new MyWebViewClient());
webView.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
progressBar.setProgress(newProgress);
super.onProgressChanged(view, newProgress);
}
});
webView.loadUrl(url);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_url_browser, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
CallingActivity.java
public class CallingActivity extends AppCompatActivity{
Button goButton;
EditText urlField;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calling);
goButton=(Button)findViewById(R.id.goButton);
urlField=(EditText)findViewById(R.id.urlField);
}
public void onClick(View v)
{
String url=urlField.getText().toString();
Intent i=new Intent(this,UrlBrowser.class);
Bundle b=new Bundle();
b.putString("url",url);
i.putExtras(b);
startActivity(i);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_calling, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
How can I solve this? Any help appreciated

Related

Multiple markers at this line help me

I am getting the message multiple markers at this line:
- Syntax error, insert ")" to complete
Expression
- Syntax error, insert ";" to complete
Statement
- Syntax error, insert "}" to complete
ClassBody
This is the code:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Menemukan lokasi tombol di activity_main.xml
Button tombol = (Button) findViewById(R.id.button1);
//Menangkap Klik pada Tombol
tombol.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
//TODO Auto-generated method stub
Intent Intentku = new Intent(MainActivity.this,AktivitasBaru.class);
startActivity(Intentku);
} <<<<<<<<<< eror
}
#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 boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
You have not closed your on click listener anonymous class properly, you should have:
tombol.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent Intentku = new Intent(MainActivity.this,AktivitasBaru.class);
startActivity(Intentku);
}
});
You are missing the last line.

how to save my text in textview android

I have a code to change the text of textview in a layout according to the value of the edittext (et) in the previous layout
there is MorningDrsGeneral :
public class MorningDrsGeneral extends ActionBarActivity {
Button button ;
EditText et;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.morningdrs);
et = (EditText) findViewById(R.id.et);
addListenerOnButton1();
}
public void addListenerOnButton1() {
final Context context = this;
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, bookingKamal.class);
intent.putExtra("fn" , et.getText().toString());
startActivity(intent);}
});}
#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 boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
and there is bookingKamal.java :
public class bookingKamal extends ActionBarActivity {
Button button ;
TextView textView3 ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bookingkamal);
textView3 = (TextView) findViewById(R.id.textView3) ;
String A = textView3.getText().toString();
String N = " " ;
if (A.equals(N)){
Intent intent = getIntent();
String texx = intent.getStringExtra("fn") ;
textView3.setText(texx);
}}
#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 boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I have to keep the text in the bookingkamal layout .
It means when I go back from this layout and back to it the text should be the same as previous.
Then, it should be like this
bookingKamal.java
String texx;
void onBackPressed() {
Intent intent = new Intent(getApplicationContext(), MorningDrsGeneral.class);
intent.putExtra("texx" , texx);
startActivity(intent);
}
MorningDrsGeneral.java
protected void onCreate(Bundle savedInstanceState) {
et = (EditText) findViewById(R.id.et);
Intent intent2 = getIntent();
if (intent2.getStringExtra("texx") != "") {
String abcd = intent2.getStringExtra("texx");
et.setText(abcd);
}
}
You can use SharedPreferences, this will save everything if you leave the layout or even (if you want) if you close your app. Take a look at the Android documentation at http://developer.android.com/reference/android/content/SharedPreferences.html

How to add progress bar line in webview

I am trying to add a progress bar or loading bar to my application which uses webview. I have to application set up to run all the links within the app but i am confused on how to implement a progress bar for whenever a link is clicked on
this is my code
public class MainActivity extends ActionBarActivity {
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("http://caknowledge.in");
mWebView.setWebViewClient(new MyAppWebViewClient());
}
#Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class MyAppWebViewClient extends WebViewClient {
}
And XML.JAVA Code is as below
<WebView android:id="#+id/activity_main_webview" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
just use progressBar above your webview and when your web view start loading page make it visiible and when your webview page loaded hide the progressbar
you can add below code on your webviewclient to show progress dialog
private class MyAppWebViewClient extends WebViewClient {
#Override
public void onPageStarted(WebView p_view, String p_url, Bitmap p_favIcon)
{
try {
if (progressDialog != null && !isFinishing())
progressDialog.show();
} catch (Throwable ex) {
//ignore
}
super.onPageStarted(p_view, p_url, p_favIcon);
}
#Override
public void onPageFinished(WebView p_view, String p_url)
{
try {
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
} catch (Throwable ex) {
//ignore
}
super.onPageFinished(p_view, p_url);
}
}
Progress bar declaration in activity
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading..");
Hope it works for you !!
layout code
<ProgressBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/progress_bar"
style="?android:attr/progressBarStyleHorizontal"/>
<WebView
android:layout_below="#id/progress_bar"
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
java code
webView.setWebViewClient(new HelloWebViewClient());
progressBar.setVisibility(View.INVISIBLE);
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
// TODO show you progress image
progressBar.setVisibility(View.VISIBLE);
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url)
{ progressBar.setVisibility(View.INVISIBLE);
// TODO hide your progress image
super.onPageFinished(view, url);
}
}

Android RadioButton CompoundButton.OnCheckedChangeListener Error

I have a question for my code, it should change the background color of my Start-Activity. I should choose between blue and red with radio buttons (in Radiogroup). I always get this error:
Error:(8, 8) error: com.example.clecks.reaction_game.OnCheckedChangeListener is not abstract and does not override abstract method onCheckedChanged(CompoundButton,boolean) in android.widget.CompoundButton.OnCheckedChangeListener
C:\Users\clecks\Desktop\asdfasdf\app\src\main\java\com\example\clecks\reaction_game\OnCheckedChangeListener.java
When I click on the error message there opens a new class named
OnCheckedChangeListener.java :
public class OnCheckedChangeListener implements CompoundButton.OnCheckedChangeListener {}
Here is my code:
public class activity_settings extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_settings);
colorchange();
}
public void colorchange() {
final RelativeLayout background = (RelativeLayout) findViewById(R.id.start);
final RadioButton changeToBlue = (RadioButton) findViewById(R.id.button_blue);
final RadioButton changeToRed = (RadioButton) findViewById(R.id.button_red);
final Button button_save = (Button) findViewById(R.id.button_save);
button_save.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
changeOption(background);
}
});
changeToBlue.setOnCheckedChangeListener(new RadioButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
changeToBlue(background);
}
});
changeToRed.setOnCheckedChangeListener(new RadioButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
changeToRed(background);
}
});
}
public void changeOption(RelativeLayout background) {
if (background.isEnabled()) {
background.setEnabled(false);
} else {
background.setEnabled(true);
}
}
public void changeToBlue(RelativeLayout background) {
background.setBackgroundColor(0x0000FF00);
background.invalidate();
}
public void changeToRed(RelativeLayout background) {
background.setBackgroundColor(0x0000FF00);
background.invalidate();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_activity_settings, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}}
CompoundButton itself is an abstract class (extending `Button).
You should not have CompoundButton in your activity_activity_settings layout.
Use Button for example instead.

Toast Is not being Displayed in The Android App , But first it was being diplayed then after two days it stopped

I am Very new to Android Programming.. I have listed my code below. MainActivity.java and activity_main.xml. I have tried following solutions but to no avail.
using only this
using getapplicationcontext()
using getbasecontext()
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button text2 = (Button) findViewById(R.id.text2);
final WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
text2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(false);
Toast.makeText(getApplicationContext(), "Disabled", Toast.LENGTH_LONG).show();
}
}
}); // Disabling/Enabling Wifi Mechanism
}
#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 boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Toast.makeText(MainActivity.this, "Disabled", Toast.LENGTH_LONG).show();
Try using the activity context rather than the application context. See this link for a more thorough description:
When to call activity context OR application context?

Categories