If it's fragment initialization, I'll ask you a question!

Asked 2 years ago, Updated 2 years ago, 97 views

Hello, I'd like to create an application based on fragments.

Through the frame layout area of the main activity that you want to do as a home screen

When you click the button on the menu, the web view is displayed in the FrameLayout area.

The problem here is

I also want to set up a web view in the Frame Layout area of the main activity

What should I do at times like this?

(I'm saying that I want to set the initial screen of Frame Layout, which appears by default, but I don't know if you understood)/)

activity_main.xml

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/fragment_container">



    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Home screen."
        android:id="@+id/textView"
        android:layout_gravity="center"
        android:textStyle="bold" />



</FrameLayout>

This is the code for the main activity

When I googled it, it was not through the tag's "android: name"

There was a way to set the initial screen,

Originally, I wrote the addView attribute of FrameLayout in main java and made the code


     FrameLayout fragment_container;

      ....

        final View activity2= LayoutInflater.from(this).inflate(R.layout.activity2, null);
        fragment_container.addView(activity2);

If I change it to fragment, that part has an error.TT

I don't know what to do.

android fragment

2022-09-22 21:25

1 Answers

The intention of the question is I want to run a web view using a fragment on the initial screen.I understood as . Please let me know if you misunderstood the intention.

The answer explains how to add fragments through XML. Read the following article for additional methods and details on fragment generation.

Add fragments to the layout file of the main activity as follows: In this case, the android:name="xxx.xxx.WebViewFragment" will contain the fragment classes you want to instantiate.

activity_main.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">

    <fragment
        android:id="@+id/web"
        android:name="xxx.xxx.WebViewFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</FrameLayout>

When the system creates the activity_main.xml layout, it creates the fragments specified in the layout. That is, WebView Fragments created in XML are created. This generated fragment can be accessed through the findFragmentById() function in FragmentManager.

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        WebViewFragment webViewFragment = (WebViewFragment) getSupportFragmentManager().findFragmentById(R.id.web);
        webViewFragment.loadUrl("https://www.hashcode.co.kr");
    }

}

The Web view fragment was simplified as follows: Add more code as needed.

WebViewFragment.java

public class WebViewFragment extends Fragment {

    private WebView webView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        webView = new WebView(getActivity());
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebChromeClient(new WebChromeClient());
        webView.setWebViewClient(new WebViewClient());
        return webView;
    }

    public void loadUrl(String url) {
        webView.loadUrl(url);
    }
}

The results of the above code are as follows.


2022-09-22 21:25

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.