I created a Drawer activity, right now i have MainActivity and RegisterFragment. XML and other things are done right now i can open RegisterFragment with Navigation Drawer but how can i show that RegisterFragment when application starts?
MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
}
//unnecessary code block removed
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
Fragment fragment = null;
if (id == R.id.nav_home) {
} else if (id == R.id.nav_gallery) {
fragment = new RegisterFragment();
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_tools) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
if (fragment != null){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.screen_area, fragment);
fragmentTransaction.commit();
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
As i said it working well basically i want to open RegisterFragment when application starts.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
// change fragment at onCreate method
changeFragment(new RegisterFragment());
}
private void changeFragement(Fragment fragment){
if (fragment != null){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.screen_area, fragment);
fragmentTransaction.commit();
}
}
//unnecessary code block removed
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
Fragment fragment = null;
if (id == R.id.nav_home) {
} else if (id == R.id.nav_gallery) {
fragment = new RegisterFragment();
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_tools) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
changeFragment(fragment);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
put same code
fragment = new RegisterFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.screen_area, fragment);
fragmentTransaction.commit();
in onCreate of your activity
Related
navigation Screen opening when i click on button but its not closing again when i click it again. below is my code.
this is my java code:
toolbar = findViewById(R.id.maintoolbar);
toolbar.setTitleTextAppearance(this, R.style.ActionBarTitle);
// Sets the Toolbar to act as the ActionBar for this Activity window.
// Make sure the toolbar exists in the activity and is not null
setSupportActionBar(toolbar);
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationOnClickListener(new NavigationIconClickListener(
getBaseContext(),
findViewById(R.id.mainContainer),
new AccelerateDecelerateInterpolator(),
getApplicationContext().getResources().getDrawable(R.drawable.branded_menu), // Menu open icon
getApplicationContext().getResources().getDrawable(R.drawable.close_menu)));
bNavVu = findViewById(R.id.bottom_nav_vu);
final View activityRootView = findViewById(R.id.rootLayout);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Rect r = new Rect();
//r will be populated with the coordinates of your view that area still visible.
activityRootView.getWindowVisibleDisplayFrame(r);
int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
hideSystemUI();
}
}
});
if (savedInstanceState == null) {
getSupportFragmentManager()
.beginTransaction()
.add(R.id.mainContainer, new CategoryFragment())
.commit();
}
try this way , i hope works with you.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Menu 1");
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
Toolbar toggle = new Toolbar(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
the listener
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_menu1) {
// Handle the camera action
} else if (id == R.id.nav_menu2) {
} else if (id == R.id.nav_menu3) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
I know that this subject has been already discussed, but I have difficulties with implementing navigation between fragments. I want to return to a previous fragment or main activity using the back button. Now, the back button only closes the navigation drawer or exits the app. I know I should use addToBackStack but I don't know exactly how.
My MainActivity:
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);
setTitle("infoGmina");
ImageView gminaImageView = (ImageView) findViewById(R.id.gminaImageView);
ImageView weatherImageView = (ImageView) findViewById(R.id.weatherImageView);
ImageView partlyCloudImageView = (ImageView) findViewById(R.id.partlyCloudImageView);
partlyCloudImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
PogodaFragment fragment = new PogodaFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.replacableConstraintLayout, fragment);
transaction.commit();
}
});
weatherImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
WeatherFragment fragment = new WeatherFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.replacableConstraintLayout, fragment);
transaction.commit();
}
});
gminaImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AnnouncementListFragment fragment = new AnnouncementListFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.replacableConstraintLayout, fragment);
transaction.commit();
}
});
ParseAnalytics.trackAppOpenedInBackground(getIntent());
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);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_gmina) {
AnnouncementListFragment listFragment = new AnnouncementListFragment();
FragmentManager manager = getFragmentManager();
manager.beginTransaction().replace(R.id.replacableConstraintLayout, listFragment).commit();
setTitle("Gmina");
} else if (id == R.id.nav_restauracja) {
} else if (id == R.id.nav_sklep) {
} else if (id == R.id.nav_weather) {
WeatherFragment weatherFragment = new WeatherFragment();
FragmentManager manager = getFragmentManager();
manager.beginTransaction().replace(R.id.replacableConstraintLayout, weatherFragment).commit();
setTitle("Jakość powietrza");
} else if (id == R.id.nav_pogoda) {
PogodaFragment pogodaFragment = new PogodaFragment();
FragmentManager manager = getFragmentManager();
manager.beginTransaction().replace(R.id.replacableConstraintLayout, pogodaFragment).commit();
setTitle("Pogoda");
} else if (id == R.id.nav_onas) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
I create activity with Android studio, however I don't know how to setup the fragment class when the activity is launched. Whenever, I start the activity I am getting a blank screen. This means that the home screen is not a fragment class screen.
Code is below:
public class Dashboard extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
ActionBarDrawerToggle toggle;
DrawerLayout drawer;
NavigationView navigationView;
FloatingActionButton fab;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
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();
}
});
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
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);
//navigationView.setNavigationItemSelectedListener(this);
}
#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.dashboard, 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) {
// Handle navigation view item clicks here.
selectDrawerItem(item);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
private void selectDrawerItem(MenuItem menuItem) {
Fragment fragment = null;
Class fragmentClass;
switch(menuItem.getItemId()) {
case R.id.nav_dashboard:
fragmentClass = DashboardFragment.class;
break;
case R.id.nav_importexport:
fragmentClass = ImportexportFragment.class;
break;
case R.id.nav_backuprestore:
fragmentClass = BackuprestoreFragment.class;
break;
case R.id.nav_setting:
fragmentClass = SettingFragment.class;
break;
default:
fragmentClass = DashboardFragment.class;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
// Insert the fragment by replacing any existing fragment
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_dashboard, fragment).commit();
// Highlight the selected item has been done by NavigationView
menuItem.setChecked(true);
// Set action bar title
setTitle(menuItem.getTitle());
// Close the navigation drawer
}
}
you have already setup fragment classes on click of drawer item.
case R.id.nav_dashboard:
fragmentClass = DashboardFragment.class;
break;
Here is the code for multiple fragments where you are getting id and opening DashboardFragment.
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_dashboard, fragment).commit();
fragments are well replaced over here.
How can I have an item ("R.id.nav_code" for example) automatically selected when I open a page ?
#Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
FragmentManager fragmentManager = getFragmentManager();
if (id == R.id.nav_code) {
fragmentManager.beginTransaction()
.replace(R.id.content_frame
, new code())
.commit();
} else if (id == R.id.nav_documentation) {
fragmentManager.beginTransaction()
.replace(R.id.content_frame
, new documentation())
.commit();
} else if (id == R.id.nav_number) {
fragmentManager.beginTransaction()
.replace(R.id.content_frame
, new number())
.commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
If you want to start default fragment everytime your activity start then add this code into your activity onCreate Method
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.YourLayout);
FragmentManager fragmentManager=getFragmentManager();
FragmentTransaction transaction=fragmentManager.beginTransaction();
code_Fragment code=new code_Fragment(); //Create Object Of your Fragment
transaction.replace(R.id.FragmentLoginRegistration,code);
transaction.commit();
}
I've implemented the following code , but when I run the app and click on an item in Drawer nothing happened. I guess it's a problem related to the Listener but I couldn't grasp how to fix it. or maybe other problem!
once I click on one item the only thing happen is the following two lines in the logcat :
01-12 03:14:26.606 1565-1565/? I/LatinIME: Starting input. Cursor position = -1,-1
01-12 03:14:29.601 1565-1565/? I/LatinIME: Starting input. Cursor position = 0,0
here is the code:
public class Welcome extends AppCompatActivity {
private DrawerLayout mDrawer,dlDrawer;
private NavigationView nvDrawer;
private ActionBarDrawerToggle drawerToggle;
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
// Set a Toolbar to replace the ActionBar.
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Find our drawer view
dlDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerToggle = setupDrawerToggle();
// Tie DrawerLayout events to the ActionBarToggle
dlDrawer.setDrawerListener(drawerToggle);
nvDrawer = (NavigationView) findViewById(R.id.nvView);
// Setup drawer view
setupDrawerContent(nvDrawer);
FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
tx.replace(R.id.drawer_layout, new EditPersonal());
tx.commit();
}
private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
selectDrawerItem(menuItem);
return true;
}
});
}
private ActionBarDrawerToggle setupDrawerToggle() {
return new ActionBarDrawerToggle(this, dlDrawer, toolbar, R.string.drawer_open, R.string.drawer_close);
}
public void selectDrawerItem(MenuItem menuItem) {
// Create a new fragment and specify the planet to show based on
// position
Fragment fragment = null;
Class fragmentClass;
switch(menuItem.getItemId()) {
case R.id.nav_first_fragment:
fragmentClass = EditPersonal.class;
break;
case R.id.nav_second_fragment:
fragmentClass = Chronogram.class;
break;
case R.id.nav_third_fragment:
fragmentClass = BloodTest.class;
break;
case R.id.nav_forth_fragment:
fragmentClass = GlucoseTest.class;
break;
case R.id.nav_fifth_fragment:
fragmentClass = SendRecord.class;
break;
case R.id.nav_sixth_fragment:
fragmentClass = ReviewLogin.class;
break;
case R.id.nav_seventh_fragment:
fragmentClass = SignOut.class;
break;
default:
fragmentClass = EditPersonal.class;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent,fragment).commit();
// Highlight the selected item, update the title, and close the drawer
menuItem.setChecked(true);
setTitle(menuItem.getTitle());
mDrawer.closeDrawers();
}
// ...
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (drawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
// Make sure this is the method with just `Bundle` as the signature
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggles
drawerToggle.onConfigurationChanged(newConfig);
}
}
the app screen shot:
On your switch case, try something like this:
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
case R.id.nav_first_fragment:
EditPersonal editPersonal= new EditPersonal();
fragmentTransaction.replace(R.id.flContent, editPersonal);
fragmentTransaction.commit();
break;
Do this on other cases too.
Have you tried putting logs into your code to see where the logic flows to or fails to flow to?
For example inside your switch statement, try seeing if menuItem.getItemId() actually matches your case statements. It'd help to see if your listener is actually the problem or if it's just not matching the cases. Or it could be a problem with your fragment manager.
Check out http://developer.android.com/reference/android/util/Log.html. You should be able to see the logs in logcat. Good luck!
Basically I have changed the code using the Activity (Navigation Drawer Activity) of Android studio itself. so here is the code :
public class Welcome extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar 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.drawer_open, R.string.drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#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.review_login_listview, 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.menu_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
if (id == R.id.nav_first_fragment) {
// Handle the camera action
}
else {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}