This is a question about the toolbar while studying Android.

Asked 2 years ago, Updated 2 years ago, 114 views

I want to make menus with the navigation drawer that I set when I first created the project, and if I press the second menu, I want to display the web view right away. Although the web view was displayed as an intent, the existing toolbar has disappeared, so in the new web view, The navigation drawer will be disabled.

MainActivity.java

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





    MainFragment fragment = new MainFragment();
    android.support.v4.app.FragmentTransaction fragmentTransaction =    getSupportFragmentManager().beginTransaction();

    fragmentTransaction.replace(R.id.fragment_container, fragment); 
    fragmentTransaction.commit();


    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });


    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 

    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();


    navigationView = (NavigationView) findViewById(R.id.nav_view); 
    //How to change elements in the header programatically
    View headerView = navigationView.getHeaderView(0);
    TextView emailText = (TextView) headerView.findViewById(R.id.email);
    emailText.setText("[email protected]");

    navigationView.setNavigationItemSelectedListener(this); 


}


public void onButton1Clicked(View v){
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://m.naver.com"));
    startActivity(intent);

}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } } else {
        super.onBackPressed();
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // // Handle action bar item clicks here. The action bar will
    // // automatically handle clicks on the Home/Up button, so long
    // // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    if (id == R.id.action_settings) {
        return true;
    } } else if(id ==R.id.action_search){

        return true;
    }

    return super.onOptionsItemSelected(item);
}



@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.sideMenu1) {
        //Set the fragment initially
        MainFragment fragment = new MainFragment();
        android.support.v4.app.FragmentTransaction fragmentTransaction =
                getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, fragment);
        fragmentTransaction.commit();
    }        }        else if (id == R.id.sideMenu2) {

         Intent intent = new Intent(getApplication(), webViewActivity.class);
         startActivity(intent);


    } } else if (id == R.id.sideMenu3) {

        Menu3Fragment fragment = new Menu3Fragment();   
        android.support.v4.app.FragmentTransaction fragmentTransaction =
                getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, fragment);
        fragmentTransaction.commit();



    } } else if (id == R.id.sideMenu4) {

    } } else if (id == R.id.sideMenu5) {

    } } else if (id == R.id.sideMenu6) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;

}

}

activity_web_view.xml

<WebView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/webView1"
    ></WebView>

webViewActivity.java

public class webViewActivity extends AppCompatActivity {

WebView webView1;
Toolbar toolbar;

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




    webView1 = (WebView) findViewById(R.id.webView1);
    WebSettings webSettings = webView1.getSettings();

    webSettings.setJavaScriptEnabled(true);

    webView1.loadUrl("http://m.naver.com");






}

}

android toolbar

2022-09-22 21:26

2 Answers

When a new activity is executed, the existing activity screen is covered.I think it'll be easy to understand if you approach it like this. In the code you posted, there is a navigation drawer in the main activity, but there is no navigation drawer in the web view activity. This means that the main activity with the navigation drawer is running a web view activity without the navigation drawer. For more information, read the Operations and Backstacks documentation.

If you want to leave the main activity's toolbar (and navigation drawer) intact and only show the area under the toolbar in a web view, use the fragment. Please refer to the attached simple sample code that works.

Change where you run existing webview activities to the code below

getSupportFragmentManager().beginTransaction()
    .add(R.id.fragment_container, WebViewFragment.newInstance("http://www.naver.com"))
    .addToBackStack(null)
    .commit();

;

additional java webviewfragment
public class WebViewFragment extends Fragment {

    private static final String URL = "url";

    public static Fragment newInstance(String url) {
        Fragment fragment = new WebViewFragment();
        Bundle args = new Bundle();
        args.putString(URL, url);
        fragment.setArguments(args);
        return fragment;
    }

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


2022-09-22 21:26

Thank you for your kind reply! But as you said, Main Activity.Java's Intent = new Intent (getApplication(), webViewActivity.class); startActivity(int); part getSupportFragmentManager().beginTransaction() .add(R.id.fragment_container, WebViewFragment.newInstance("http://www.naver.com")) .addToBackStack(null) .commit();

Even if I change to and add a new WebViewFragment.java, the webview appears, but the toolbar disappears. (There was a toolbar in the picture with the webview that I uploaded, but unlike the existing toolbar, the navigation drawer is It was an unusable toolbar. ) I might not have understood it because it was a beginner, so please give me a detailed answer Thank you


2022-09-22 21:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.