View.Frame equivalent in the Android - android

I have been working on iOS for a while and implemented the following code where it shows WebView in the View.
UIView contentView = new UIView();
UIWebView webView = new UIWebView(contentView.Frame);
I wonder how the above code could be written in the Android. I have written the following but getting error. I am newbie to the Android.
View contentView = new View();
WebView webView = new WebView(contentView.LayoutParamaters);

I think this is a misunderstanding of the differences between iOS and Android and how Xamarin supports cross platform C#. The APIs between the 2 platforms are very different, while UIView is conceptually similar to View in reality they map to different APIs. UIView is an iOS specific api and View is an Android specific API, they are each a thin layer on top of the corresponding native controls and have nothing to do with each other.
I'd suggest working through some of the Xamarin.Android tutorials to get an idea of the differences between Android and iOS.
Regarding the errors:
The constructor for View needs a Context provided through to the constructor. You can do this in 2 ways, provide the owning activity or the global application context:
// In the scope of an Activity.
View contentView = new View(this); // "this" is the activity reference.
// In another scope...
View contentView = new View(Android.App.Application.Context);
For your 2nd error, WebView does not have a constructor that takes that parameter.
Fix it by providing nothing:
WebView webView = new WebView();

Here's a minimal example of how to show a WebView. The layout XML for your Activity would be something like this:
<?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" >
<WebView
android:id="#+id/yourWebView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout
And the code of your Activity would just be this...
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
public class WebViewActivity extends AppCompatActivity {
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activity_layout);
webView = (WebView) findViewById(R.id.yourWebView);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("http://www.google.com/");
}
}
Or if you wanted to add the WebView programmatically, your Activity code would look something like this...
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RelativeLayout;
public class WebViewActivity extends AppCompatActivity {
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
RelativeLayout parent = (RelativeLayout) findViewById(R.id.containerView);
webView = new WebView(this);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("http://www.google.com/");
parent.addView(webView, 0);
}
}

Related

WebView for secified Area in LibGdx based Android app

I have built an app for crypto currencies, to maintain a portfolio. I planned to show the latest news items in the same app.
The whole project is an Android app done in LibGdx.
Question
How to have a webview for the area "Part-B"?
LigGdx have anything like WebView?
Does LibGdx has any extensions to take care of WebView?
Screenshot
Starting with your last two questions; no. LibGDX does currently no extensions, nor native support for webviews or something similar. You can, however, create a custom layout. LibGDX has a initializeForView method, which you can use to grab the View itself. This is inflated in a Fragment, which is added to the layout itself. The WebView is then the other view.
First off, the launcher:
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.badlogic.gdx.backends.android.AndroidFragmentApplication;
public class AndroidLauncher extends FragmentActivity implements AndroidFragmentApplication.Callbacks {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gdxwebview);
// Create the fragment
GDXWithWebview fragment = new GDXWithWebview();
getSupportFragmentManager().beginTransaction().
add(R.id.fragmentRoot, fragment).
commit();
}
#Override
public void exit() {}
}
Since fragments are involved, the launcher is different from the generated launcher
For the fragment:
The reason behind using a fragment is to inflate LibGDX into the fragment, which ends up as a sub-unit of the activity. If you set the content view in the activity, you will not get a WebView in there as well.
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.badlogic.gdx.backends.android.AndroidFragmentApplication;
public class GDXWithWebview extends AndroidFragmentApplication{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// return the GLSurfaceView on which libgdx is drawing game stuff
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();//Configure with whatever you need
return initializeForView(new MyGdxGame(), config);//WARNING!! Replace MyGdxGame with your game class.
}
}
And finally, the layout. It's a basic layout, and the weights (/sizes) may need tweaking for you to get it to look just like you want it to
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.badlogic.gdx.backends.android.AndroidFragmentApplication;
public class AndroidLauncher extends FragmentActivity implements AndroidFragmentApplication.Callbacks {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gdxwebview);//The layout; comes later
// Create the fragment
GDXWithWebview fragment = new GDXWithWebview();
getSupportFragmentManager().beginTransaction().
add(R.id.fragmentRoot, fragment).
commit();//Add the fragment
}
#Override
public void exit() {}
}
In addition, if you plan on using the WebView to go online, you need the Internet permission (if you use it to execute JavaScript or other web content locally, without going on the Internet, you shouldn't need it):
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.badlogic.gdx.backends.android.AndroidFragmentApplication;
public class GDXWithWebview extends AndroidFragmentApplication{
View root;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// return the GLSurfaceView on which libgdx is drawing game stuff
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();//Configure with whatever you need
root = initializeForView(new MyGdxGame(), config);//WARNING!! Replace MyGdxGame with your game class.
//declaredInTheClassWebView = (WebView) root.findViewById(R.id.webView);//For initialization, make sure you call root.findViewById, not findViewById. You have to specify the view in which to find the ID when dealing with Fragments.
return root;
}
}
And per the LibGDX documentation on use with Fragments, you also need the v4 support library. Assuming you use Gradle 4.1 and Android Studio:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/fragmentRoot"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</FrameLayout>
<WebView android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9"/>
</LinearLayout>
If you use an earlier version of Gradle and the Gradle plugin:
<uses-permission android:name="android.permission.INTERNET" />
And if you don't use Gradle, find the appropriate call for your build system. If you don't have a build system, grab the jar manually and add it to the classpath.

Can not resolve method 'findViewById(int)'

I'm having a trouble with findViewByid but I can't find where the problem is.
Here's my FirstFragment class code:
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import java.util.ArrayList;
import java.util.List;
public class FirstFragment extends Fragment {
public static final String TAG = "first";
private WebView mWebView;
ProgressBar progressBar;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.google.com");
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.secondefragment, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
You need to do this in onCreateView:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.secondefragment, container, false);
mWebView = (WebView) view.findViewById(R.id.activity_main_webview);
progressBar = (ProgressBar) view.findViewById(R.id.progressBar1);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.google.com");
return view;
}
Fragment doesn't provide thefindViewById() method. This is provided in Activity or View. When implementing a Fragment you don't inflate your views in onCreate() (like you normally do in an Activity.) Instead, you do it in onCreateView() and you need to use the inflated root View to find the ID within the layout you inflated.
getActivity().findViewById() works. However, this isn't a good practice because the fragment may be reused in another activity.
The recommended way for this is to define an interface.
The interface should contain methods by which the fragment needs to communicate with its parent activity.
public interface MyInterfcae {
void showTextView();
}
Then your activity implements that Interface.
public class MyActivity extends Activity implements MyInterfcae {
#Override
public void showTextView(){
findViewById(R.id.textview).setVisibility(View.VISIBLE);
}
}
In that fragment grab a reference to the interface.
MyInterface mif = (MyInterface) getActivity();
Call a method.
mif.showTextView();
This way, the fragment and the activity are fully decoupled and every activity which implements that fragment, is able to attach that fragment to itself.
I had this problem when I downloaded sample project from github. This project had
compileSdkVersion 23
buildToolsVersion "23.0.0"
targetSdkVersion 23
in Activity context and findViewById() were in red color inside onCreate() method. So I updated the above versions to
compileSdkVersion 25
buildToolsVersion "25.0.3"
targetSdkVersion 25
and error got resolved.
I meet the same question in Android studio learning,just because I create the project by Fragment. Choose Blank Activity with no Fragment would solve it.
I found solution by changing targetSdkVersion from 23 to 21
Add getView() before findViewById() method.
Like this:
getView().findViewById(R.id.potatoes);
getActivity().findViewById()
This works it removes the red colour of findviewby id and it doesnt say cannot resolve symbol but when i put a colin in the end the whole statement is underlined and it says unreachable statement.
barChart =(BarChart)getActivity().findViewById(R.id.barchart);
I am also doing this in fragment.
This is how you can solve the problem:
toggleButton1=(ToggleButton)findViewById(R.id.toggleButton1);
toggleButton2=(ToggleButton)findViewById(R.id.toggleButton2);

Android - Using Fragments

I'm very new to Java, and Android development. I want to be able to display a local page page within my App using the fragments (because I don't want the entire app to be web based).
So, I've gone into my arview.xml and added a fragment:
<fragment
android:id="#+id/fragment_bbc"
android:name="android.webkit.WebViewFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" />
Now, I want to place a web page in there (for now, I'll just use the bbc website).
Within my 'onCreate(Bundle savedInstanceState)' I have this:
webFrag = (WebViewFragment) mGUIView.findViewById(R.id.fragment_bbc);
mWebView = (WebView) mGUIView.findViewById(R.id.fragment_bbc);
myWebView.loadUrl("http://www.bbc.co.uk");
I have errors of all 3 of those lines, all saying they can't be resolved.
Where am I going wrong? Thanks
You shouldn't use the "tag" fragment in xml, just do a classic layout and use it in a Class that extend Fragment
Example :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved) {
_view = inflater.inflate(R.layout.fragment_accueil, group, false);
// get views
_textViewTitle = (TextView) _view.findViewById(R.id.title_home);
_buttonLocalSearch = (Button) _view.findViewById(R.id.btn_local_search);
return _view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
{
_textViewTitle.setText(getActivity().getString(R.string.tle_home));
_buttonLocalSearch.setOnClickListener(this);
}
In Java you need to initialize the components you want to use and select the variable-type on your own. In your case try changing
webFrag = (WebViewFragment) mGUIView.findViewById(R.id.fragment_bbc);
mWebView = (WebView) mGUIView.findViewById(R.id.fragment_bbc);
to
WebViewFragment webFrag = (WebViewFragment) mGUIView.findViewById(R.id.fragment_bbc);
WebView mWebView = (WebView) mGUIView.findViewById(R.id.fragment_bbc);
or you do it entirely different and try something like this:
WebView wv = new WebView();
wv.findViewById();
Did you previously instantiate those variables?
Try:
WebViewFragment webFrag = (WebViewFragment) findViewById(R.id.fragment_bbc);
WebView mWebView = (WebView) findViewById(R.id.fragment_bbc);
mWebView.loadUrl("http://www.bbc.co.uk");
I thik your way of implementation is wrong.
First of all simply you have to do this
Add Fragment to your parent activity
Then using instance of your WebViewFragment call the method loadUrl()
And here is the code for WebViewFragment just supply your url to loadUrl() method thats it.
Please check that link that will guide you well.

Want to show webpage on clicking images in viewpager

I want to show webpage when we click an image in viewpager. Can you please help me in this. Also I want to know if multiple images are there in viewpager so how will I get the respective webpages of them.
Thanks in advance.
**MainActivity.java**
package com.example.viewimage;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Context;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
final Context context = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImageAdapter adapter = new ImageAdapter(this);
viewPager.setAdapter(adapter);
viewPager.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v){
// open the desired page
Intent browserIntent = new Intent("android.intent.action.VIEW",
Uri.parse("http://www.craftsvilla.com/anvi-s-classic-nawabi-earrings-studded-with-white-stones-and-emeralds.html"));
startActivity(browserIntent);
}
});
}
}
If you want to show this webpage inside your app, you would have to add a WebView. Set its visibility to View.GONE. Add OnClickListener to your viewPager. On click set the visibility of the webView to be View.VISIBLE. To go back, override the functionality of 'back' key - and on its click hide the webpage.
Or you can open other activity that holds the webView.
You can also open the webpage with android's browser.
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri);
startActivity(browserIntent);
Edited :
Add to your xml before closing your RelativeLayout
<LinearLayout
android:id="#+id/cover_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"/>
instead of 'viewPager.setOnClickListener' write
(findViewById(R.id.cover_layout))

Android MapView With Sliding Menu Obscures Menu

I have a map view for android maps api v2 in an activity that uses this sliding menu https://github.com/iPaulPro/SlidingMenu. The sliding menu works great except for on the map page. There is a black view covering the sliding menu that is the exact size of the map. This is an example with the map height set at 100dp to outline what I mean.
If I touch that view it will go away. How would I get rid of it or make it transparent? I've tried the requestTransparentRegion() trick. No dice there.
Found this stack overflow post ViewPager with Google Maps API v2: mysterious black view and used this class in place of the normal map fragment.
package com.myapp.gms.maps;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import com.google.android.gms.maps.SupportMapFragment;
/**
* #author btate
*/
public class TransparentSupportMapFragment extends SupportMapFragment {
public TransparentSupportMapFragment() {}
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup view,
Bundle savedInstance) {
View layout = super.onCreateView(inflater, view, savedInstance);
FrameLayout frameLayout = new FrameLayout(getActivity());
frameLayout.setBackgroundColor(
getResources().getColor(android.R.color.transparent));
((ViewGroup) layout).addView(frameLayout,
new ViewGroup.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT
)
);
return layout;
}
}
TransparentSupportMapFragment solved the problem for android 2.3.7
Thank you!
There is one more solution to this problem. I am showing MapFragment within another fragment. The MapFragment is dynamically added into the a FrameLayout.
The solution is to use frameLayout.setVisibility(View.Visible) and frameLayout.setVisibility(View.Gone) on open and close events of sliding menu. It doesn't require an extra view to be added. And the black area is completely gone.
getSlidingMenu().setOnOpenListener(
new OnOpenListener() {
#Override
public void onOpen() {
frameLayout.setVisibility(View.GONE);
}
}
);
getSlidingMenu().setOnClosedListener(
new OnClosedListener() {
#Override
public void onClosed() {
frameLayout.setVisibility(View.VISIBLE);
}
}
);

Categories

Resources