How to call an id hosted in the Activity layout extended by Fragment?

Asked

Viewed 182 times

1

Well, I’m developing an app, in which if you have a navigation Drawer and in this navigation I put to open each item in a new Activity, and in each acitivty will have a layout, right so far okay. But now, I want to call a webview with Progress bar, It is already all done in the layout, but when I call by id (findViewById) in Activity presents errors, because I’m using extends Fragment and does not extend Acitivity. In this case, if I replace Fragment for Activity, it simply turns null, and the screen turns white, with no content. You can help me call this webview along with fragment?

Main activity:

//Here is the part of Activity where declares which Fragment will be the main, which will be the first screen:

    if (id == R.id.home) {
        //Aplicar fragment principal
        home fragment = new home();
        android.support.v4.app.FragmentTransaction fragmentTransaction =
                getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, fragment);
        fragmentTransaction.commit();           

And here is Activity home.java, as stated earlier:

import android.os.Bundle; 
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


        public class home extends Fragment {

            public home() {
            }

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                     Bundle savedInstanceState) {
                // Chamando o layout nesta activity
                return inflater.inflate(R.layout.home, container, false);
            }

And here follows my home.xml layout

<ProgressBar
    android:id="@+id/progressBar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

<WebView
    android:id="@+id/web"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/progressBar"/>

What I want is to declare the webview and progressBar in Activity home.java. Thank you very much!

1 answer

3


First, I think you’re confusing a few concepts. If your class home extends a Fragment is because it is a Fragment and not a Activity.

It’s very easy to manipulate a WebView within your Fragment. Realize that the method onCreateView returns a View inflated by LayouInflater. That one View nothing more than the entire layout of your Fragment. So you can change the class home to manipulate the WebView as follows:

public class HomeFragment extends Fragment {
    private WebView webView;
    private ProgressBar progressBar;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_home, container, false);
        webView = (WebView) root.findViewById(R.id.web_view);
        progressBar = (ProgressBar) root.findViewById(R.id.progress_bar);

        return root;
    }
} 

With these defined attributes you can manipulate these views at will. You could also pass them to Activity containing the HomeFragment, as I think was your intention. However, I recommend that any operation relating to these elements be done in the Fragment to which they belong.

  • Hello friend, I don’t know how to thank you. Thank you very, very much. You saved me from this. Thanks!

  • @Techpositivo of nothing, I’m glad to have helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.