I have a Navigation Drawer and want open another isolated fragment - java

I have a navigation drawer and when i click on one button i want to go to other fragment where i can't access to the navigation drawer and anything else. I just want have a black arrow in the top-left to go back and return to the navigation drawer with all the fragments.
I tried somethings but don't works like i want normally navigator continues accessible and see bot layouts one over the other.
MAIN FRAGMENTS CONTROLLER
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
DrawerLayout drawerLayout=findViewById(R.id.drawer);
NavigationView navigationView=findViewById(R.id.nav_view);
setSupportActionBar(toolbar);
appBarConfiguration= new AppBarConfiguration.Builder(R.id.nav_home,
R.id.nav_user_data,R.id.nav_join_group,
R.id.nav_user_groups,R.id.nav_search_documents)
.setDrawerLayout(drawerLayout).build();
navController = Navigation
.findNavController(this,R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this,navController,appBarConfiguration);
NavigationUI.setupWithNavController(navigationView,navController);
}
#Override
public boolean onSupportNavigateUp() {
return NavigationUI.navigateUp(navController,appBarConfiguration)
|| super.onSupportNavigateUp();
}
I'm trying with a botton on my ActionBar, when i click it should go to my new fragment (with a generic app back but with one back button)

You simply should specify your action in navigation.xml file like others.
It will change hamburger icon by itself because you synchronize your navigation controller with NavDrawer in this two lines of code:
NavigationUI.setupActionBarWithNavController(this,navController,appBarConfiguration);
NavigationUI.setupWithNavController(navigationView,navController);

We need to add one fragment in our navigation.xml (the file where we define our fragments associated with the Fragment and the layout) for the Fragment where we want to go. Add there an action with a unique id and where we want to go in the fragment where we are.
Sorry, i know it's a little bit confused.
NAVIGATION
We go from there
<fragment
android:id="#+id/nav_user_data"
android:name="com.example.androidapplication_reto2.project.activities.navigationfragments.SeeAndModifyUserDataFragment"
android:label="User Data"
tools:layout="#layout/fragment_see_and_modify_user_data"
>
<action
android:id="#+id/action_nav_user_data_to_nav_modify_data"
app:destination="#+id/nav_modify_data" />
</fragment>`
To there
<fragment
android:id="#+id/nav_modify_data"
android:name="com.example.androidapplication_reto2.project.activities.navigationfragments.ModifyUserDataFragment"
android:label="Modify User Data"
tools:layout="#layout/fragment_modify_user_data"
/>
With the action that we defined in the origen fragment.
ORIGEN FRAGMENT
And in the Fragment when you click on one button just happen this.
Navigation.findNavController(getView()).navigate(R.id.action_nav_user_data_to_nav_modify_data);

Related

Reset selection in BottomNavigationView when navigate to the fragment

Currently in my app i have BottomNavigationView, that consists for example from 3 menu items, that linked with fragments:
Profile Fragment
Reader Fragment
Bookmarks Fragment
Also i have another fragments (Settings, Edit Profile, etc)
So when i navigate to the fragment that doesn't appear in BottomNavigationView menu i want to reset selection of menu item in BottomNavigationView.
In my code i only have NavController and this method
#Override
public void onFragmentNavigation(int id) {
navController.navigate(id);
}

Data binding "error: cannot find symbol class Models"

First, I need to acknowledge the clearly very similar but not duplicate issue here. None of the proposed solutions in that thread work.
My application file structure is as follows:
app
java
[mydomain].[myapplication]
Models
DataModel.java
MainActivity.java
res
layout
activity_main.xml
content_main.xml
my_inner_layout.xml
My app build.gradle contains
dataBinding {
enabled = true
}
In MainActivity.java I have
import [mydomain].[myapplication].Models.DataModel;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemsSelectedListener {
DataModel dataModel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
... <other layout creation template code> ...
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
dataModel = new DataModel();
binding.setValues(dataModel);
}
<navigation and auto-generated methods>
}
My my_inner_layout.xml contains
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="values"
type="[mydomain].[myapplication].Models.DataModel" />
</data>
<android.support.constraint.ConstraintLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
>
<TextView
android:id="#+id/intro_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:text="#{values.valueOne}"/>
<TextView
android:id="#+id/buying_recommendation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/intro_text"
app:layout_constraintTop_toBottomOf="#id/intro_text"
android:text="#{values.valueTwo}"/>
</android.support.constraint.ConstraintLayout>
</layout>
I am passing bind:values="#{values}" through from activity_main to its included app_bar_main to its content_main to its my_inner_layout with that same <data> value in each. Android Studio is telling me "namespace 'bind' is not bound".
If I try to run this, I get "Compilation failed; see the compiler error output for details." Looking in the build output, I see:
In text, the errors are variously error: cannot find symbol class Models and error: package Models does not exist
If I move DataModel.java out of the Models package and directly in to [mydomain].[myapplication], then I get a different result. It does build and run in the emulator, but with much of the layout information failing to appear. No hamburger menu in the top left, no title text in the header, and no settings button in the top right values previously automatically included by the autogenerated code in Android Studio. I am unable to set the title in code using setTitle, either.
Swiping from the left does bring in the navigation drawer however.
I have tried invalidating caches and restarting, cleaning, rebuilding both with the model file in Models and separately.
What I want, chiefly, is to be able to use the project structure I want. To put my models classes in a models sub-package. Once that is complete, I want to make sure the full layout information comes through, including the hamburger menu icon, settings icon, and title. How can I achieve this?
Okay, I realised where the "class Models does not exist" thing comes from. I don't know whether to blame my own stupidity or the stupidly nitpicky way this binding is implemented on Android. The package needed to be called models with a lower case "m", not Models. The binding auto-name-conversion thing must have thought Models was a class, not a package.
To fix the layout, the onCreate method had to be changed to
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
dataModel = new DataModel();
cycleInformationBinding.setRecommendation(dataModel);
// set toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Drawer layout setting
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);
}
Specifically, things had to happen in the order:
setContentView to the main activity
Set up the data model binding
Layout concerns like drawer and toolbar.
Any other order would cause either the model binding to fail or the toolbar to not display correctly.
Just convert your existing layouts to data binding layouts (Don't forget to add variable in your xml with your activity mentioned in type)
Example:
<variable name="navdrawer" type="com.example.sampleapp.HomeScreenActivity" />
This will generate a data binding class for NavigationHeaderView in this format(May differ) NavHeaderYourActivityName.
When you bind your parent activity, you will use that binding instance to get DrawerLayout and your NavigationView, respectively.
A sample code to reduce the boilerplate for initializing views in your code:
NavHeaderHomeScreenBinding navHeaderHomeScreenBinding =
DataBindingUtil.setContentView(this, R.layout.nav_header_home_screen);
AppBarHomeScreenBinding appBarHomeScreenBinding =
DataBindingUtil.setContentView(this, R.layout.app_bar_home_screen);
ActionBarDrawerToggle toggle =
new ActionBarDrawerToggle(this, binding.drawerLayout, appBarHomeScreenBinding.toolbar,
R.string.navigation_drawer_open, R.string.navigation_drawer_close);
binding.drawerLayout.addDrawerListener(toggle);
toggle.syncState();
binding.navView.setNavigationItemSelectedListener(this);
I hope this helps someone! Thank you!
First try to remove redurant setContentView(R.layout.activity_main);
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
Second add to end of "onCreate" function
binding.included.setValues(dataModel);
binding.executePendingBindings();
if you use include than you may add to your include id for example
<include
android:id="#+id/included"
layout="#layout/content_main"
app:values="#{DataModel}"/>
and use
binding.included.setValues(dataModel);
research about using databinding with included layouts

How to set a temporary fragment to my "home screen" Android studio

I have started coding and I really like it, I have been on/off on diffrent project (to learn diffrent things) But now i got a problem, I "app"reciate all the help i can get.
I am using the navigation drawer and got three fragments installed. I can navigate through the navigation drawer and select a fragment and it loads on my screen. But when i start my app the screen is just white and the navigation drawer is on the left and i can pick a fragment. Is it possible to have a fragment on my "home screen" so when I start the app there is a fragment already placed on the screen BUT! when i chosse another fragment in the navigation drawer i would like to have my fragment who loaded at the beginning to disappear. Otherwise both fragments will show on eachother (Text on text). Maybe to have the temporary fragment to "finish" itself somehow. Please look at my imgur image to get my idea.Imgur image press here to view
Here is a generic solution on how to use the FragmentManager. It should give a good idea on how to display a Fragment. Besides that you could include a static Fragment in your layout. It's up to you how to approach it.
So the FragmentManager solution looks like this:
YourActivity.java
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentManager fragmentManager = getFragmentManager();
Fragment fragment = AnyFragment.instantiate(this, AnyFragment.class.getName());
fragmentManager.beginTransaction().add(R.id.fragment_placeholder, fragment).addToBackStack(null).commit();
}
The target id is defined in your activity layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="#+id/fragment_placeholder"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"></FrameLayout>
</LinearLayout>
Later if you want to replace the current Fragment you can use the remove and add method of the FragmentManager (the replace method is kinda buggy).
Fragment currentFragment = fragmentManager.findFragmentById(R.id.fragment_placeholder))
fragmentManager.beginTransaction().remove(currentFragment).add(R.id.fragment_placeholder, yourNewFragment).addToBackStack(null).commit();

Android: Refresh current fragment after language change

So my MainActivity has a navigation drawer with a set of fragments.
On top of the nav bar, I have 2 flags which represent a language. If an user clicks on a language, the app changes the language of the whole app.
Everything is working smoothly (activity refreshes, navigation drawer and all fragments get translated) but the fragment that's open doesn't. This means that the user needs to click on the navigation drawer and select the fragment again to see it translated.
How can I refresh the current fragment the user is in?
Add tag to your fragment when you commit it:
fragmentManager.beginTransaction().replace(R.id.your_id, fragment, "Your_Fragment_TAG").commitAllowingStateLoss();
Then You can detach and attach again your fragment for refresh:
// Reload current fragment
Fragment frg = null;
frg = getSupportFragmentManager().findFragmentByTag("Your_Fragment_TAG");
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();

Adding a menu to an activity than a fragment

I have a MainActivity along with a fragment. I have a refresh menu option that will refresh the data (Using AsyncTask). Is there any difference in inflating the refresh menu option in the fragment than in the MainActivity?
Note: This is in context with Udacity's Developing Android Apps, Lesson 2.
Yes, if i understand right, you want to inflate your activity toolbar menu from the the fragment. If that is the case you can do it like this
Yout Fragment class
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setHasOptionsMenu is important
//it's telling the parent activity that he wants to participate in inflation of the menu
setHasOptionsMenu(true);
}
//Rest of your methods (onCreateView, onPause, onResume etc...)
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
//inflate the menu file
inflater.inflate(R.menu.your_menu_xml, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(android.view.MenuItem item) {
switch (item.getItemId()) {
case R.id.refresh:
//handle click
return true;
}
return super.onOptionsItemSelected(item);
}
You can also use setMenuVisibility(boolean) if you want to hide/show menu in certain childfragments
If you inflate the menu from within the activity, it will be shown in all of the fragments you load inside the activity, but the action will be available to activity only (This is good if you want to do some general stuff with your menu action like starting new activity, displaying information popup etc). If you inflate the menu from within the fragment you will be able to handle menu items from withing the fragment, which will allow you to create more specific menu actions based on which fragment is currently active. For example if you have viewpager with 3 different fragments lets say:
FragmentOne for image browsing
FragmentTwo for video browsing
FragmentThre for Text browsing
Lets say you want to allow users to upload the images only, and you want that upload button to be located in the menu.
If you inflate the the menu from within the activity your upload button will be visible inside all of your fragments, and you would have to create a custom/logic for showing hiding menu items. If you create a menu from within the fragment, you will be able to handle and show the menu for the fragment you need
Long story short i think this depends on the use case of the activity/fragment and what you want to achieve with it

Categories