Wednesday, 19 October 2016

Android TV - WebView

I am assuming, you people having basic knowledge of android tv.
In today's blog, I will tell you how to open webview  in android Tv.

You should be knowing about android.support.v17.leanback.app.OnboardingFragment it is use for onboarding step, But I have also used it for opening web view as shown in above image

web_view.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:gravity="center"
android:orientation="vertical"
android:paddingLeft="591dp">
 
   <WebView  
 android:id="@+id/webview"  
android:layout_width="fill_parent"    
android:layout_height="0dp"        
android:layout_weight="1"        
android:background="#ffffff" />
</LinearLayout>

WebViewFragment.java
public class WebViewFragment extends android.support.v17.leanback.app.OnboardingFragment {
  
   private WebView wview;

   @Nullable    
@Override    
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
       View view = inflater.inflate(R.layout.web_view, null);
       wview = (WebView) view.findViewById(R.id.webview);
       wview.loadUrl("https://www.google.co.in");
       return view;
   }
   @Override    
protected void onFinishFragment() {
       super.onFinishFragment();
      getActivity().finish();
   }
   @Override   
protected int getPageCount() {
       return 1;
   }
   @Override   
protected String getPageTitle(int pageIndex) {
       return null;
   }
   @Override    
protected String getPageDescription(int pageIndex) {
       return null;
   }
   @Nullable  
 @Override   
protected View onCreateBackgroundView(LayoutInflater inflater,
ViewGroup container) {    
       return null;
   }
   @Nullable  
 @Override    
protected View onCreateContentView(LayoutInflater inflater, ViewGroup container) {
       return null;
   }
@Nullable
@Override   
protected View onCreateForegroundView(LayoutInflater inflater, ViewGroup container) {
       return null;
   }
}
Now use this fragment in activity
web_activity.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/webfragment"    
android:name="WebViewFragment"    
android:layout_width="match_parent"   
android:layout_height="match_parent" />

WebViewActivity.java

public class WebViewActivity extends Activity {
 
   @Override   
public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.web_activity);
   }
}


Now u just have to call startActivity(new Intent(context, WebViewActivity.class));

No comments:

Post a Comment

Apple Glass Concept

Apple Glass Concept Shows Us All the AR Goodness We Need These days  smart glasses  are back in full force, courtesy of so...