I have a webview set up but when I click on the item, an empty fragment shows up where the actual web item is supposed to be. I have tried loading my urls on mobile browser and they work fine. I have an internet permission in android manifest.
web view layout file
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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=".activities.HomeActivity">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/webView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
webview fragment
public class WebViewDetailsFragment extends Fragment {
private WebView detailsWebView;
private String articleSource;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_web_view_details, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
detailsWebView = view.findViewById(R.id.webView);
detailsWebView.getSettings().setLoadsImagesAutomatically(true);
detailsWebView.getSettings().setJavaScriptEnabled(true);
detailsWebView.getSettings().setUseWideViewPort(true);
detailsWebView.getSettings().setLoadWithOverviewMode(true);
detailsWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
// Configure the client to use when opening URLs
detailsWebView.setWebViewClient(new WebViewClient());
setItems();
detailsWebView.loadUrl(articleSource);
}
private void setItems(){
NewsArticle newsArticle = NewsFragment.news.get(NewsAdapter.pos);
articleSource = "https://www.google.com";
}
}
I too tried your Code. it is working fine and good.There may be a chances of issue in fragmentManager. Did you checked the view is visible or not
Related
Here is the java code with error
public class fragemnt extends Fragment {
public fragemnt() {
// 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_fragemnt, container, false);
}
}
here my javaFragment cannot find the Fragement_fragment layout eventhough the xml file shows in the layout direcotry
heres my Xml file
<?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"
tools:context=".FirstSemester.fragemnt">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/hello_blank_fragment" />
</FrameLayout>
try to clean, the click on build. if it did not work try to invalidate cache and restart
For a BottomNavigationView's last tab, I want the content to be below the status bar and make the status bar completely transparent. This works as expected but the BottomNavigationView jumps up leaving a blank space in the bottom when the last tab is selected.
There were similar topics in SO that said to set fitWindowSystems to false. I tried it, but it is not helping.
ExplorerFragment.java
public class ExplorerFragment extends Fragment {
public ExplorerFragment() {
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Window window = getActivity().getWindow();
window.getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE);
window.setStatusBarColor(ContextCompat.getColor(getContext(), android.R.color.transparent));
}
}
#Override
public void onDestroy() {
super.onDestroy();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
View decor = getActivity().getWindow().getDecorView();
decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
}
}
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_explorer, container, false);
}
}
fragment_explorer.xml
<android.support.constraint.ConstraintLayout
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" xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.orevon.fflok.fragments.ExplorerFragment"
android:fitsSystemWindows="false">
<View android:layout_width="0dp" android:layout_height="180dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:background="#drawable/splash_background4"/>
</android.support.constraint.ConstraintLayout>
activity_mail.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false">
<FrameLayout
android:id="#+id/fragment_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/mainBottomNavigation"
android:layout_width="match_parent"
android:layout_height="68dp"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:background="#drawable/bottombar_bg"
app:itemIconTint="#color/bottom_nav_color"
app:itemTextColor="#color/bottom_nav_color"
app:menu="#menu/main_bottom_navigation"/>
</android.support.constraint.ConstraintLayout>
The solution to the problem is setting the bottomNavigationView setOnApplyWindowsListener to null:
setOnApplyWindowInsetsListener(null)
I struggled with this problem when implementing a bottom navigation view to my AR Core Sceneform fragment.
Extra padding issue
Extra padding issue resolved
It may be useful for those who face a problem in the future.
I solved this issue just by adding
ViewCompat.requestApplyInsets(view)
to my fragment's onActivityCreated method.
view - fragment's root view
in my case it looks like:
KOTLIN
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
view?.let {
ViewCompat.requestApplyInsets(it)
}
}
JAVA
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
View view = getView()
if(view != null){
ViewCompat.requestApplyInsets(view)
}
}
Update (6/27/2020):
Since onActivityCreated method is deprecated on SDK >=29 you could use the code above inside onViewCreated method:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
ViewCompat.requestApplyInsets(view)
}
I have a Android fragment containing a ListView, and I have attached an ArrayAdapter to the ListView, but when I insert items into the ArrayAdapter, the ListView only shows the first item.
onCreateView iterates over my Technologies, if they're unlocked, it adds them to the ArrayList shownTechs, but only the first item ever appears
public class TabTechnologyList extends Fragment {
private static final String TAB_NAME = "tab_name";
private ListView techList;
private ArrayList<Technology> shownTechs = new ArrayList<>();
ArrayAdapter<Technology> techListAdapter;
public TabTechnologyList() { }
public static TabTechnologyList newInstance() {
TabTechnologyList fragment = new TabTechnologyList();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tabData = Globals.getTabData();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_technology_list, container, false);
techList = (ListView) view.findViewById(R.id.tabTechListList);
techListAdapter = new ArrayAdapter<>(getActivity().getApplicationContext(), R.layout.layout_techlist_element, shownTechs);
techList.setAdapter(techListAdapter);
for(Technology tech: tabData.getTechnologies()) {
shownTechs.add(tech);
}
//Only the first one is shown on the screen
return view;
}
}
}
When I google for similar issues, people seem to be recreating the ArrayAdapter instead of editing the existing one, but I'm not doing that, and cant' figure out what problem remains. Any suggestions?
fragment_technology_list.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"
tools:context="com.tbohne.kittens.front.TabTechnologyList"
>
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/tabTechListList" />
</RelativeLayout>
layout_techlist_element.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TextView>
Addition
I have just realized that this fragment is not filling the parent height, and is only tall enough to show the one entry, as if the fragment height were wrap_content. (I can't scroll it, so it appears as if there's only the one). This makes me think the nesting of fragments may be related, so here's the code that creates the fragment:
public class TabTechnology extends Fragment {
private TabTechnologyList table;
private OnFragmentInteractionListener mListener;
public TabTechnology() {}
public static TabTechnology newInstance(String tabName) {[SNIP]}
#Override public static onAttach(Activity activity) {[SNIP]}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_technology, container, false);
table = TabTechnologyList.newInstance(tabName);
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.view_table, table);
transaction.commit();
return view;
}
}
fragment_technology.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"
tools:context="com.tbohne.kittens.front.TabTechnology"
android:orientation="vertical">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:id="#+id/view_table"
android:layout_above="#+id/view_details" />
<View android:layout_width="match_parent"
android:layout_height="1px"
android:background="?android:attr/listDivider" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:id="#+id/view_details"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true" />
</LinearLayout>
Putting the Fragment inside the ScrollView was the source of the problem. Simply replaced those with FrameLayout, and suddenly everything magically works. Very strange.
I suspect the problem has something to do with nesting scrollable objects.
I have a listfragment class:
public class activityfeedScreen extends ListFragment implements OnItemClickListener {
private ListView activityfeed_feedList_listView;
private ActivityFeedBaseAdapter adapter;
public static final activityfeedScreen newInstance()
{
activityfeedScreen fragment = new activityfeedScreen();
Bundle bdl = new Bundle(1);
fragment.setArguments(bdl);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_activityfeed_screen, container, false);
activityfeed_feedList_listView = (ListView)v.findViewById(R.id.activityfeed_feedList_listView);
activityfeed_feedList_listView.setAdapter(adapter);
activityfeed_feedList_listView.setOnItemClickListener(this);
return v;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void onItemClick(AdapterView<?> activityfeedlistview, View view, int position, long itemID) {
Intent intent_activityfeed_showitemdescription = new Intent(view.getContext(), activityfeedItemScreen.class);
startActivity(intent_activityfeed_showitemdescription);
}
}
In this class I am getting an error at the line:
activityfeed_feedList_listView = (ListView)v.findViewById(R.id.activityfeed_feedList_listView);
I initially thought it was a problem due to the getView() being null so I moved that from the onCreate method to the onCreateView() method. But I am getting the runtime error as stated in the title.
Here is my layout file:
<?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:background="#E5E5E5"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp">
<ImageView
android:id="#+id/activityfeed_profilePicture_imageView"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:src="#drawable/default_profile"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#B9B9B9"
android:layout_marginTop="20dp">
<ListView
android:id="#+id/activityfeed_feedList_listView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</RelativeLayout>
I understand that I must change the listview id to
android:id="#android:id/list"
but I am not sure then how to call that in my onCreateView method.
Thanks for all your help!
Yes. First of all you do have to change the ID of your ListView like so:
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
In your onCreateView you can reference the ID by android.R.id.list. This is because Android built-in resource reside in android.R. You can also find there drawables, styles, etc. (you can call them in a similar maneer).
Taking the above into consideration, your onCreateView method should look like this :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_activityfeed_screen, container, false);
activityfeed_feedList_listView = (ListView)v.findViewById(android.R.id.list);
activityfeed_feedList_listView.setAdapter(adapter);
activityfeed_feedList_listView.setOnItemClickListener(this);
return v;
}
That's all ;)
In onCreateView() you need to provide a layout that has a ListView with and id of #android:id/list.
ListFragment provides some helper methods (e.g setListAdapter()) that require the layout to have a ListView with that id so it can reference it and set the adapter for you.
Solved! Thanks everyone for your help.
I am struggling with Fragments as a concept, and especially seem stuck on this one thing. What I'm trying to do is, using my fragment, manipulate its own layout's data. Every time I try to access an ImageButton from within the Fragment, it crashes the application. It works fine from the activity. Am I just misunderstanding Fragments fundamentally?
Code(cut down for size)-
This is the beginning of the activity my fragment is called from:
Display.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
MyFragmentClass MyFragment = new MyFragmentClass();
fragmentTransaction.add(R.id.fragment_container, MyFragment);
fragmentTransaction.commit();
The XML for that Activity:
activity_display.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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.mycompanyname.myprojectname.Display"
android:id="#+id/display_layout">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragment_container">
</FrameLayout>
</RelativeLayout>
The Fragment
MyFragmentClass.java
public class MyFragmentClass extends Fragment
{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_layout_screen, container, false);;
}
public void MethodTest()
{
}
}
The Fragment's XML file:
fragment_layout_screen.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"
android:id="#+id/fragment_layout_screen"
tools:context="com.mycompanyname.myprojectname.MyFragmentClass">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/my_button"
android:clickable="true"
android:onClick="buttonPress"
android:src="#drawable/button"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
</RelativeLayout>
Basically I had all of this originally in Display.java, but wanted to add fragments, so I'm trying to move stuff out and into fragments, but I still need the ability to manipulate the xml info, I just can't.
From inside the Display.java activity I can easily call the ImageButton like this:
ImageButton myButton = (ImageButton) findViewById(R.id.my_button)
but if I do the same from MethodTest in the fragment, the app crashes.
I've searched many suggestions on here, trying various solutions from here: findViewById in Fragment but none of those seemed to work.
I've been reading through this for the setup: http://developer.android.com/guide/components/fragments.html and can't seem to find what I'm doing wrong.
Any help would be appreciated, and if you have any questions about my setup, please ask.
declare on fragment private View rootView;
On the onCreateView(...) set rootView = inflater.Inflate(..); and then access the imagebutton as ImageButton mButton =(ImageButton) rootView.findViewById(..);
Do like this in fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.gridview, container, false);
GridView gridview = (GridView) view.findViewById(R.id.grid);
return view ;