I have a problem. I made the code by the example and found that fragments that are not used there in full screen, what reasons may affect this, I'm new to this, so it is a little difficult to understand why this may occur. MainActivity code:

package com.example.jone1.navigation_drawer; import android.app.Fragment; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v4.app.FragmentTransaction; import android.view.View; import android.support.design.widget.NavigationView; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); 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.addDrawerListener(toggle); toggle.syncState(); NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this); //add this line to display menu1 when the activity is loaded displaySelectedScreen(R.id.nav_menu1); } private void displaySelectedScreen(int itemId) { //creating fragment object android.support.v4.app.Fragment fragment = null; //initializing the fragment object which is selected switch (itemId) { case R.id.nav_menu1: fragment = new Menu1(); break; case R.id.nav_menu2: fragment = new Menu2(); break; case R.id.nav_menu3: fragment = new Menu3(); break; } //replacing the fragment if (fragment != null) { FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.replace(R.id.content_frame, fragment); ft.commit(); } DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); } @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(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @SuppressWarnings("StatementWithEmptyBody") @Override public boolean onNavigationItemSelected(MenuItem item) { displaySelectedScreen(item.getItemId()); //make this method blank return true; } } 

Menu1 code:

 package com.example.jone1.navigation_drawer; import android.content.Context; import android.content.Intent; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.util.DisplayMetrics; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; /** * Created by Belal on 18/09/16. */ public class Menu1 extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { //returning our layout file //change R.layout.yourlayoutfilename for each of your fragments // inflate mainXML // find container DisplayMetrics metrics = this.getResources().getDisplayMetrics(); int width = metrics.widthPixels; int height = metrics.heightPixels; container.setBackgroundColor(Color.argb(255,250,222,165)); container.addView(new DrawView(container.getContext())); return inflater.inflate(R.layout.fragment_menu_1, container, false); } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); //you can set the title for your toolbar here for different fragments different titles getActivity().setTitle("Menu 1"); } class DrawView extends View { Paint p; Rect rect; public DrawView(Context context) { super(context); p = new Paint(); rect = new Rect(); } protected void onDraw(Canvas canvas) { // Π·Π°Π»ΠΈΠ²ΠΊΠ° ΠΊΠ°Π½Π²Ρ‹ Ρ†Π²Π΅Ρ‚ΠΎΠΌ // настройка кисти // красный Ρ†Π²Π΅Ρ‚ p.setColor(Color.argb(255,253,181,91)); // Ρ‚ΠΎΠ»Ρ‰ΠΈΠ½Π° Π»ΠΈΠ½ΠΈΠΈ = 10 p.setStrokeWidth(2); p.setStyle(Paint.Style.FILL); canvas.drawRect(0, 0, canvas.getWidth(), 56, p); p.setStyle(Paint.Style.STROKE); rect.offset(150, 0); canvas.drawRect(rect, p); // рисуСм ΠΏΡ€ΡΠΌΠΎΡƒΠ³ΠΎΠ»ΡŒΠ½ΠΈΠΊ // лСвая вСрхняя Ρ‚ΠΎΡ‡ΠΊΠ° (200,150), ниТняя правая (400,200) canvas.drawRect(8, 64, canvas.getWidth()-8, 64 + 320, p); } } } 

application screenshot


I found a solution, the problem was in constants.

    1 answer 1

    This is about the markup, most likely either layout_margin or layout _padding of the container or fragment, attach the xml of the fragment and the container, to be more precise