Syntax error on token, delete this token - android

I am having this syntax error on token, delete this token error. Below are my two related files
MainActivity.java
package com.example.androidwebviewexample;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.webkit.WebView;
import com.example.androidwebviewexample.R;
public class MainActivity extends Activity {
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("www.yahoo.com"); // ERROR !!!!!!
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="#dimen/padding_medium"
android:text="#string/hello_world"
tools:context=".MainActivity" />
<WebView
android:id="#+id/webview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>

Where is onCreate() of your Activity?
You forgot to define onCreate() of your Activity. (From your code)
Now try this,
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("www.yahoo.com");
}
}
UPDATE:
After updated your question,
myWebView.loadUrl("www.yahoo.com");
you can not initialize or declare any method directly outside of Activity's any method.
Just put this line in onCreate() after of setContentView(R.layout.activity_main);
as I write in my above code, Then its works.

Related

How can I add Share Button to my android app

How can I add Share Button to my android app, I tried several solutions here in and nothing seems to work.
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.package.name.MainActivity"
android:weightSum="1">
<include
android:id="#+id/toolbar"
layout="#layout/toll_bar"
/>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="368dp"
android:layout_height="495dp"
tools:context=".MainActivity"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp">
tools:ignore="MergeRootFrame">
<WebView
android:id="#+id/activity_main_webview"
android:layout_width="383dp"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar"
android:layout_marginTop="30dp"
/>
</FrameLayout>
</LinearLayout>
Toll_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:elevation="4dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:text="#string/app_name"
android:textAlignment="viewStart"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textColorHighlight="#000000"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/about"
android:layout_width="45dp"
android:layout_height="45dp"
android:text="About"
android:background="#drawable/icon"
android:layout_marginLeft="127dp"
/>
<Button
android:id="#+id/share"
android:layout_width="45dp"
android:layout_height="45dp"
android:text="share"
android:background="#drawable/share"
android:layout_marginLeft="10dp"
/>
<Button
android:id="#+id/rate"
android:layout_width="45dp"
android:layout_height="45dp"
android:text="Rate Us"
android:background="#drawable/rate"
android:layout_marginLeft="10dp"
/>
</android.support.v7.widget.Toolbar>
Main_Activity.xml
package com.package.name;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.ShareActionProvider;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
#Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar)findViewById(R.id.toolbar);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
mWebView.loadUrl("http://beta.html5test.com/");
mWebView.loadUrl("file:///android_asset/Html36289/index.html");
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());
// Stop local links and redirects from opening in browser instead of WebView
mWebView.setWebViewClient(new MyAppWebViewClient());
mWebView .getSettings().setJavaScriptEnabled(true);
Button btnRate = (Button) findViewById(R.id.rate);
btnRate.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
onClickRateThisApp(v);
}});
}
private boolean isActivityStarted(Intent aIntent) {
try
{
startActivity(aIntent);
return true;
}
catch (ActivityNotFoundException e)
{
return false;
}
}
public void onClickRateThisApp(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=com.supercell.hayday"));
if (!isActivityStarted(intent)) {
intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=com.package.name"));
if (!isActivityStarted(intent)) {
Toast.makeText(this, "Could not open Android market, please check if the market app installed or not. Try again later", Toast.LENGTH_SHORT).show();
}
}
}
}
Thank you for your answer, i tried different solution and it worked.
youtube.com/watch?v=U39RsxrWhIA
In my opinion, toll_bar.xml is unnecessary.
First, in activity_main.xml, please change
<include
android:id="#+id/toolbar"
layout="#layout/toll_bar" />
to
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:actionBarSize"
android:background="#color/colorPrimary"/>
Second, create a menu file named toolbar.xml place it into res/menu directory.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/menu_about"
android:enabled="true"
android:icon="#drawable/icon"
android:title="about"
app:showAsAction="always" />
<item
android:id="#+id/menu_share"
android:enabled="true"
android:icon="#drawable/share"
android:title="share"
app:showAsAction="always" />
<item
android:id="#+id/menu_rate"
android:enabled="true"
android:icon="#drawable/rate"
android:title="rate us"
app:showAsAction="always" />
</menu>
Third, here is the new MainActivity.java
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
#Override
public void onBackPressed() {
if (mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
mWebView.loadUrl("http://beta.html5test.com/");
mWebView.loadUrl("file:///android_asset/Html36289/index.html");
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());
// Stop local links and redirects from opening in browser instead of WebView
mWebView.setWebViewClient(new MyAppWebViewClient());
mWebView.getSettings().setJavaScriptEnabled(true);
}
private boolean isActivityStarted(Intent aIntent) {
try {
startActivity(aIntent);
return true;
} catch (ActivityNotFoundException e) {
return false;
}
}
public void onClickRateThisApp() {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=com.supercell.hayday"));
if (!isActivityStarted(intent)) {
intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=com.package.name"));
if (!isActivityStarted(intent)) {
Toast.makeText(this, "Could not open Android market, please check if the market app installed or not. Try again later", Toast.LENGTH_SHORT).show();
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.toolbar, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_about:
//click the about button
break;
case R.id.menu_share:
//click the share button
break;
case R.id.menu_rate:
onClickRateThisApp();// param view is unnecessary
break;
}
return true;
}
}
I hope that will help you. If so, please adopt my answer.:-D Have a nice day.
You can add this code this would help you.
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("text/plain");
share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
startActivity(Intent.createChooser(share, ""));
This will open the multiple sharing option of app.
This help me to share with another one .

Why does my Browser open another Browser?

I am creating an Android application to work as a simple browser. The user enters a web address and then clicks a button. After this, the application should the website in the WebView. However, after the button is clicked, a notification pops up asking me to choose a browser to complete my action. [The options in the pop up are my default browser, mozilla, etc.]
However, I do not want to use any of them. What should I do ?
This is my .java file:
package com.example.all_in_one;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
public class SimpleBrowser extends Activity implements OnClickListener{
WebView myBrowser;
EditText URLaddress;
Button Go;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_browser);
myBrowser = (WebView) findViewById(R.id.WV);
URLaddress = (EditText) findViewById(R.id.webaddress);
Go = (Button) findViewById(R.id.go);
Go.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.go:
String address = URLaddress.getText().toString();
myBrowser.loadUrl("http://" + address);
break;
}
}
}
This is my .xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/LL1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="5" >
<EditText
android:id="#+id/webaddress"
android:inputType="textNoSuggestions"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
/>
<Button
android:id="#+id/go"
android:text="Go"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="4"
/>
</LinearLayout>
<WebView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/WV"
android:layout_below="#+id/LL1" />
</RelativeLayout>
I have added the Internet permission in my Manifest file.
Please help. Thank you in advance.
You have to pass the WebViewClient class object to browser.setWebViewClient(new BrowserClient()) to open the links in the webview.
private class BrowserClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d(tag, "shouldOverrideUrlLoading");
view.loadUrl(url);
return true;
}
}
and in your onCreate() call like this
myBrowser.setWebViewClient(new BrowserClient());

display webview below progressbar in framelayout

I want to display webview below progressbar. How to do it ?
My code is as below :
<FrameLayout
android:id="#+id/flWeb"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<ProgressBar
android:id="#+id/pbWebsite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|top" />
<WebView
android:id="#+id/wvWebsite"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>
I used this and solved my problem...
<FrameLayout
android:id="#+id/flWebpre"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<WebView
android:id="#+id/wvWebsite"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<ProgressBar
android:id="#+id/pbWebsite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/white"
android:layout_gravity="center_horizontal" />
</FrameLayout>
This is the code i have used to show the progress:
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
WebView mWvUrl;
#Override
protected void onCreate(Bundle savedInstanceState) {
final Activity activity=this;
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_main);
activity.setProgressBarIndeterminateVisibility(false);
mWvUrl = (WebView) findViewById(R.id.wv_url);
mWvUrl.getSettings().setJavaScriptEnabled(true);
Button btnLoad = (Button) findViewById(R.id.btn_load);
btnLoad.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
EditText etUrl = (EditText) findViewById(R.id.et_url);
mWvUrl.loadUrl(etUrl.getText().toString());
activity.setProgressBarIndeterminateVisibility(true);
}
});
mWvUrl.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
/** This prevents the loading of pages in system browser */
return false;
}
/** Callback method, executed when the page is completely loaded */
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Toast.makeText(getBaseContext(),
"Page loaded Completely",
Toast.LENGTH_SHORT).show();
/** Hiding Indeterminate Progress Bar in the title bar*/
activity.setProgressBarIndeterminateVisibility(false);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
You need another Layout. The FrameLayout is not the better way to do alignement between components. If you are using a FrameLayout, components will be superimposed. I suggest you to use a LinearLayout vertical or a RelativeLayout.
Try this:
<?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" >
<ProgressBar
android:id="#+id/pbWebsite"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|top" />
<WebView
android:id="#+id/wvWebsite"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

My ProgressBar won't show while loading my WebView, why?

I have created a WebViewActivity extending Activity and showing a WebView inside of my app instead of redirecting to the browser.
So I tried to use AsyncTask to show my ProgressBar while the view is loading. But the progress bar doesn't display at all. Here is the code I'm using:
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.app.Activity;
public class WebViewActivity extends Activity {
WebView webView;
TextView textView;
ProgressBar bar;
private class Async extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... arg0) {
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
String url = new String(getIntent().getStringExtra("url"));
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.loadUrl(url);
textView.setText(getIntent().getStringExtra("title"));
bar.setVisibility(ProgressBar.INVISIBLE);
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_web_view);
vueText = (TextView) findViewById(R.id.barTitle);
vueWeb = (WebView) findViewById(R.id.WebView);
bar = (ProgressBar) findViewById(R.id.progressBar);
bar.setVisibility(ProgressBar.VISIBLE);
new Async().execute();
}
}
And here is my layout:
<?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="#color/grayBackground"
android:orientation="vertical" >
<TextView
android:id="#+id/barTitle" />
<ScrollView
android:id="#+id/sc1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/barTitle"
android:fillViewport="true" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<WebView
android:id="#+id/WebView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:visibility="visible" />
</RelativeLayout>
You should perform the background action (loading the URL) of the AsyncTask in the doInBackground() method.
The onPostExecute() is invoked on the UI thread so it will freeze the UI while running.
See the documentation of AsyncTask for more details.
You have to use pubishProgress() inside the doInBackground.
Then override onProgressUpdate

Android fragment not created after runtime

I'm trying to add a fragment to an activity after the content view in the activity has been set. I'm not using the compatibility package and have my target API set to 16. I'm expecting the activity to be set to the fragments layout after the setContentView method in the main activity but on runtime, the fragment is never added and the screen in blank. The callbacks for the life cycle events in the fragment are never called thus the System.outs don't output. Logcat doesn't output any errors relating to fragments either. If a fragment element is made in the activity_main.xml, the fragment works fine but adding it to the RelativeLayout in the class doesn't work. Can someone please help me? Thanks!
Edit: Tried using a FrameLayout as the fragment_container but no difference.
This is my main activity:
package com.example.derp;
import android.os.Bundle;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fragment frag = new Fragment();
FragmentManager fManager = getFragmentManager();
fManager.beginTransaction().add(R.id.fragment_container, frag).commit();
}
}
This is my fragment:
package com.example.derp;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Frag extends Fragment{
public Frag(){
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
System.out.println("onAttach");
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("onCreate");
}
#Override
public void onStart() {
super.onStart();
System.out.println("onStart");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
System.out.println("onCreateView");
return inflater.inflate(R.layout.fragment_frag, container, false);//
}
}
This is the fragment_frag.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Button" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="64dp"
android:ems="10"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
</RelativeLayout>
The 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"
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" >
<RelativeLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
</RelativeLayout>
You are not creating the instance of your fragment class. Rather you are creating the base fragment class's object.
Change...
Fragment frag = new Fragment();
To...
Fragment frag = new Frag();

Categories

Resources