running 2 Webviews in a view - android

I'm unable to run two web views at once I've tried playing with it but need some help... its not as simple as saying View2 like in Swift.
layout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:background="#ffb067"
tools:context=".FeedFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#android:color/white"
android:textSize="20sp"
android:textStyle="bold"
android:text="Feed Fragment" />
<WebView
android:id="#+id/livefeed"
android:layout_width="match_parent"
android:layout_height="310dp"
android:layout_marginTop="200dp" />
<WebView
android:id="#+id/livestream"
android:layout_width="match_parent"
android:layout_height="200dp" >
</WebView>
</FrameLayout>
fragment
public class FeedFragment extends Fragment {
private WebView livestream;
private WebView livefeed;
public FeedFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_feed, container, false);
}
#Override
public void onViewCreated(View view , Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
livestream = (WebView) view.findViewById(R.id.livestream);
livestream.setWebViewClient(new WebViewClient());
livestream.loadUrl("website1");
livefeed = (WebView) view.findViewById(R.id.livefeed);
livefeed.setWebViewClient(new WebViewClient());
livefeed.loadUrl("website2");
}
}
One view is a video stream other is the notes for the stream.

You need to make your layout scrollable and chnage the textview's height to wrap_content
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:background="#ffb067"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#android:color/white"
android:textSize="20sp"
android:textStyle="bold"
android:text="Feed Fragment" />
<WebView
android:id="#+id/livefeed"
android:layout_width="match_parent"
android:layout_height="310dp"
android:layout_marginTop="20dp" />
<WebView
android:id="#+id/livestream"
android:layout_width="match_parent"
android:layout_height="200dp" >
</WebView>
</LinearLayout>
</ScrollView>
I strongly recommend that you read Google's a layout .

Related

findViewById sometimes returns null in fragment

findViewById is returning null when searching for an ImageView inside a fragment, but not for a TextView.
Here is my code:
fragment.xml:
<android.support.design.widget.CoordinatorLayout 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"
tools:context="com.herecomesthefragmentname">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/card_1"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/txt_1"
android:textAppearance="#android:style/TextAppearance.Medium" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/2_card"
app:cardUseCompatPadding="true"
app:cardBackgroundColor="#android:color/white">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/mr_layout">
<ImageView
android:id="#+id/2_imag"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#drawable/img_1"
android:scaleType="fitStart"
android:adjustViewBounds="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txt_2"
android:textAppearance="#style/TextAppearance.AppCompat.Large.Inverse"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
<!-- here goes another cardview -->
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
fragment.java (not all):
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment, container, false);
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
//Get Views
txt_container = (TextView) view.findViewById(R.id.txt_2);
img_view = (ImageView) view.findViewById(R.id.2_imag);
}
The strange thing is that the txt_2 view is found but not the 2_imag one.
I have tried renaming it, but it does nothing. Also, I have notice that i can't find the RelativeLayout and CardView containig both either.
UPDATE: It seems that I can't find even the main CoordinatorLayout
Solved, a fragment-v21.xml file was being loaded instead of fragment.xml.
The v21 one had different code.

How to initialize OnClickListener for included layout inside fragment?

This is representation of my app layout:
Where should I create onClickListeners for included layouts? I tried inside the fragment but I could not get through with findViewById. So I tried from Main Activity but I'm not sure how to get to included layouts from there.
I also tried this inside the fragment:
public class MainMenu extends Fragment implements View.OnClickListener{
View button_call;
#Override
public View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedinstanceState) {
View myView = inflater.inflate(R.layout.fragment_main_menu, container, false);
button_call = myView.findViewById(R.id.btn_call);
button_call.setOnClickListener(this);
return myView;
}
#Override
public void onClick(View v) {
// implements your things
}
public MainMenu() {
}
}
But then Fragment seems to be empty
Fragment XML:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/mainmenu_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingRight="60dp">
<include android:id="#+id/btn_call"
layout="#layout/call_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="30dp"
android:paddingBottom="30dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="5dp"/>
<include android:id="#+id/button2"
layout="#layout/message_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="30dp"
android:paddingBottom="30dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp" />
<include android:id="#+id/button3"
layout="#layout/navigate_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="30dp"
android:paddingBottom="30dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"/>
<include android:id="#+id/button4"
layout="#layout/remind_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="30dp"
android:paddingBottom="30dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"/>
</LinearLayout>
</ScrollView>
Steps
Create a xml file you want to inclue (eg. web.xml)
use include tag in fragment's xml and connect your layout you need to include using layout="#layout/
Initialize include tag inside fragment (when you do this make sure about the root tag of your web.xml)
Once initializing include tag is done access views inside the layout that included (web.xml) using include tag
Example
Here in my fragment I have an include tag in it's XML. It connects to my web.xml and ..web.xml's parent tag/ root tag is a FrameLayout ..
Now see how I initialize my include tag..(If you do not use the right root tag to initialize include,it will crash with a null pointer)
I have an id called g_betta in my web.xml( XML layout that I included in my fragment)
see how I access that view..
public class FragmentAbout extends Fragment {
private RelativeLayout relativeLayoutConnectedInsideIncludeTag;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.my_fragment,container,false);
FrameLayout view =(FrameLayout) rootView.findViewById(R.id.include_tag);
relativeLayoutConnectedInsideIncludeTag = (RelativeLayout) view.findViewById(R.id.g_betta);
relativeLayoutConnectedInsideIncludeTag.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Yes I Found you", Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
}
my fragment xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/mainmenu_layout"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical">
<include
android:id="#+id/include_tag"
layout="#layout/web"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
my include--> web.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/maroon"
android:gravity="center_horizontal">
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/g_betta"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="HELLO"
android:textColor="#FFF"
android:textSize="20dp" />
<RelativeLayout
android:id="#+id/lin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="16dp"
android:background="#color/colorPrimaryDark"
android:gravity="bottom"
android:paddingLeft="24dp"
android:paddingRight="24dp" />
</RelativeLayout>
<ImageView
android:id="#+id/imageone"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_below="#+id/lin"
android:layout_margin="16dp"
android:background="#drawable/girl"
android:scaleType="fitXY" />
<ImageView
android:id="#+id/imagetwo"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_below="#+id/imageone"
android:layout_margin="16dp"
android:background="#drawable/amanda"
android:scaleType="fitXY" />
</RelativeLayout>
</ScrollView>
</FrameLayout>
Do like this:
button_call = myView.findViewById(R.id.btn_call);
button_call.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// write your code here...
});

layout_anchor and app:layout_anchorGravity not working in fragments

I'm using CoordinateLayout in fragment's layout file. There I need to use ImageButton between two layouts. I got sample code from [this][1] answer.
but when i put my fragment in viewpager it dose not work correctly here is my code.
<img src="https://i.stack.imgur.com/wZasU.png"/>
public class Profile extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View Rootview =inflater.inflate(R.layout.tab_profile_fragment, container, false);
CircleImageView fab = (CircleImageView) Rootview.findViewById(R.id.fab);
return Rootview;
}
}
Xml code :
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/aasda"
android:weightSum="1"
android:orientation="vertical">
<LinearLayout
android:id="#+id/viewA"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".3"
android:orientation="horizontal">
</LinearLayout>
<RelativeLayout
android:id="#+id/viewB"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".7"
android:background="#color/white"
android:gravity="top"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="#+id/tab_profile_cardviewname"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
>
</android.support.v7.widget.CardView>
</RelativeLayout>
</LinearLayout>
<com.citizenassistant.View.Componnent.CircleImageView
android:id="#+id/fab"
android:layout_width="90dp"
android:layout_height="90dp"
android:clickable="true"
android:src="#color/blue"
android:backgroundTint="#color/white"
app:layout_anchor="#id/tab_profile_cardviewname"
app:layout_anchorGravity="top|center_horizontal"/>

view scrolls down to tabhost when fragment opened

When I open fragment with scrollview and tabhost inside the scrollview, it automatically scrolls down to the start of the tabhost hiding the top part of the scrollview. I've tried changing various things, nothing worked so far. Will keep trying, just throwing this here in hopes someone can help me. Here is the relevant parts of the code.
FragmentProfile.java
public class FragmentProfile extends Fragment {
private FragmentTabHost mTabHost;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_profile, container, false);
mTabHost = (FragmentTabHost) view.findViewById(android.R.id.tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("", ContextCompat.getDrawable(getActivity(), R.drawable.ic_c_text)),
FragmentProfileTab1.class, null);
return view;
}
}
fragment_profile.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollview"
android:fillViewport="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="70dp"
android:background="#fff"
android:weightSum="4">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:padding="2dp"
android:text="rolands"
android:textColor="#000"
android:textSize="18dp" />
</LinearLayout>
<android.support.v4.app.FragmentTabHost
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:background="#6e0d16"
android:orientation="horizontal" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"
android:background="#fff" />
<FrameLayout
android:id="#+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
</ScrollView>
FragmentProfileTab1.java
public class FragmentProfileTab1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_profile_tab1, container, false);
return view;
}
}
fragment_profile_tab1.xml
<?xml version="1.0" encoding="utf-8"?>
<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:background="#fff"
android:orientation="vertical"
tools:context=".DeviceFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="3000dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="#333"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
The view atuomatically scrolls down to the start of the tabwidget. I'll keep editing as I try new things.
I tried scrollView.scrollTo(0, 0) and scrollView.fullScroll(ScrollView.FOCUS_UP) without success.

Web Fragment not Full Fill

How can I load a web view full Fill into a fragment?
FragmentActivity.java
public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved)
{
View rootView = inflater.inflate(R.layout.web, group, false);
wb = ((WebView)rootView.findViewById(R.id.webViewApp));
wb.getSettings().setJavaScriptEnabled(true);
pg = ((ProgressBar)rootView.findViewById(R.id.progressBar2));
wb.setWebViewClient(new WebViewClient());
wb.getSettings().setDomStorageEnabled(true);
wb.getSettings().setLoadWithOverviewMode(true);
wb.getSettings().setUseWideViewPort(true);
wb.getSettings().setSupportMultipleWindows(true);
wb.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
wb.setHorizontalScrollBarEnabled(false);
wb.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
wb.getSettings().setAllowFileAccessFromFileURLs(true);
wb.getSettings().setAllowUniversalAccessFromFileURLs(true);
wb.setWebChromeClient(new WebChromeClient()
{
public void onProgressChanged(WebView paramAnonymousWebView, int paramAnonymousInt)
{
web.this.pg.setVisibility(0);
web.this.pg.setProgress(paramAnonymousInt);
if (paramAnonymousInt == 100) {
web.this.pg.setVisibility(8);
}
}
});
this.pg.setVisibility(0);
this.wb.loadUrl("http://stackoverflow.com/");
return rootView;
}
Web.xml
<LinearLayout
android:orientation="vertical"
android:background="#ffffe0b2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1.0"
xmlns:android="http://schemas.android.com/apk/res/android">
<ProgressBar
android:id="#id/progressBar2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal" />
<WebView
android:id="#id/webViewApp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_below="#id/progressBar2"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />
Screenshot
Thanks :))
I think, you have to simplify your layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<WebView
android:id="#id/webViewApp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/progressBar2"/>
<ProgressBar
android:id="#+id/progressBar2"
style="?android:attr/progressBarStyleHorizontal" />
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
And make sure, that in your Activities' layout Fragment's Container is below Toolbar, something like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="#dimen/action_bar_height"/>
<FrameLayout
android:layout_below="#+id/toolbar"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>

Categories

Resources