BottomNavigationView not working with navigation - java

I'm trying to make this BottomNavigationView work with navigation but is giving me head-aches.
I'll explain my setup :
this the fragment of my activity_home.xml
<fragment
android:id="#+id/nav_host_fragment"
android:layout_width="match_parent"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_height="match_parent"
android:layout_marginBottom="?attr/actionBarSize"
app:defaultNavHost="true"
app:navGraph="#navigation/nav_graph" />
Also I've tried with FragmentContainerView but is not working neither.
The error is on the navigation/nav_graph.xml as the error says :
Exception inflating package:navigation/nav_graph line 7
Where line 7 is :
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/nav_graph"
app:startDestination="#id/featureHomeNavGraph">
<include //Line 7
android:id="#+id/featureHomeNavGraph"
app:graph="#navigation/feature_home_nav_graph"/>
<include
android:id="#+id/featureFavouritesNavGraph"
app:graph="#navigation/feature_favourites_nav_graph" />
....
</navigation>
Then I've created different layouts for the navigations as :
feature_home_nav_graph.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:startDestination="#id/homeFragment">
<fragment
android:id="#+id/homeFragment"
android:name="package.HomeFragment"
android:label="HomeFragment" />
</navigation>
And feature_favourites_nav_graph.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:startDestination="#id/favouritesFragment">
<fragment
android:id="#+id/favouritesFragment"
android:name="package.FavouritesFragment"
android:label="FavouritesFragment" />
</navigation>
Then I think the problem also comes by the implementation on my HomeActivity.kt where I have to setup the navigation stuff... I'm doing it like this :
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)
setSupportActionBar(findViewById(R.id.toolbar))
val navHostFragment = supportFragmentManager
.findFragmentById(R.id.nav_host_fragment) as NavHostFragment?
NavigationUI.setupWithNavController(
bottomNavigation,
navHostFragment!!.navController
)
}
Also I've tried the NavigationExtension followed on this tutorial but it did not work neither.
Note
I'm using the same id on my items of bottom_menu.xml than on the navigation graph id for the fragments.

I guess the feature_home_nav_graph doesn't have an id on the navigation tag?

Related

Bottom Navigation Bar in Android Studio not Working?

bottomNavigationView.setOnNavigationItemSelectedListener{
when (it.itemId){
R.id.local1 -> make_Fragment(fragment1)
R.id.local2-> make_Fragment(fragment2)
R.id.local3 -> make_Fragment(fragment3)
}
true
}
Here bottomNavigationView is throwing error : Unresolved reference: bottomNavigationView and also i defined the id on mainactivity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".main.MainActivity">
<FrameLayout
android:id="#+id/fl_wapper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/bottomNavigationView"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:menu="#menu/bottom_nav"
/>
</RelativeLayout>
Here bottomNavigationView shows error and also when(it.itemId){
the bottomNavigationView is the id of the bottom bar.i also tried clean project and rebuild project.
tell me a solution to resolve this problem?
add these in build.gradle(:app)
id 'kotlin-android'
id 'kotlin-android-extensions'
this in dependencies
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.10"
like this
dependencies {
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.10" }
add this import statement in MainActivity.kt
import kotlinx.android.synthetic.main.mainactivity.*
it will work just fine :)...
Add this dependencies in build.gradle
implementation "androidx.navigation:navigation-fragment-ktx:2.3.5"
implementation "androidx.navigation:navigation-ui-ktx:2.3.5"
Add kotlin code implementation
Please refer this my github repository for better understanding.
https://github.com/meshramaravind/BottomNavigtaionApp

artibutes are not allowed in main activity [duplicate]

I'm trying to make a kind of tick-tac-toe android app (though with a 4x4 grid). I decided using buttons to depict each grid square and wanted to use data bindings to bind the text on the buttons to data from a String[][] array that would represent the grid internally. I tried doing something similar to what was presented here http://www.vogella.com/tutorials/AndroidDatabinding/article.html so I created this class:
public class ModelJoc extends BaseObservable{
private String[][] tabla_joc;
public ModelJoc() {
for (String[] line : tabla_joc)
for (String element : line)
element = "";
tabla_joc[0][0] = "M";
tabla_joc[0][1] = "W";
}
And then added the data binding to the activity_main.xml:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.tgeorge.temajoc.MainActivity">
<data>
<variable
name="state"
type="com.example.tgeorge.temajoc.ModelJoc"/>
</data>
And then tried to set the text of a button to a value from the array:
<Button
android:id="#+id/button1"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="#={state.getBlockState()[0][0]}"/>
But it gives me these errors: "Element data is not allowed here" as well as "Attribute is missing the android: prefix". I can't tell what I'm doing wrong from the example in the tutorial so the question is where exactly should I be putting these?
I think I see the problem now. You must outline your layout with a <layout> tag:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.tgeorge.temajoc.MainActivity">
<data>
<variable
name="state"
type="com.example.tgeorge.temajoc.ModelJoc"/>
</data>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- ... -->
<Button
android:id="#+id/button1"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="#{state.getBlockState()[0][0]}"/>
If you don't do that, android data binding won't recognize it as a data bound layout file.

Android include layout: pass Drawable parameter using data binding gives error: AAPT: error: attribute mysrc (aka com.example.APPNAME:mysrc) not found

I am trying to pass a Drawable as parameter into my include.
I have enabled dataBinding in my build.gradle file:
android {
dataBinding {
enabled = true
}
....
}
action_bar_button.xml:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="mysrc" type="android.graphics.drawable.Drawable"/>
</data>
<ImageButton android:id="#+id/notification"
android:background="#null"
android:foreground="?android:attr/actionBarItemBackground"
android:layout_width="?attr/actionBarSize"
android:layout_height="?attr/actionBarSize"
android:padding="15dp"
android:src="#{mysrc}"
android:scaleType="fitCenter"></ImageButton>
</layout>
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity"
android:id="#+id/mainlayout">
<include layout="#layout/action_bar_button" app:mysrc="#{#drawable/notification}"/>
</RelativeLayout>
This keeps giving me error pointing to the include statement in my activity_main.xml:
AAPT: error: attribute mysrc (aka com.example.APPNAME:mysrc) not found.
If I change the app:mysrc in activity_main.xml to android:mysrc, it gives me error:
AAPT: error: attribute android:mysrc not found.
I have been stuck with this for few hours and have tried quite a few Stack Overflow posts but can't seem to resolve it.
Here, there must be a drawable variable in both the action_bar_button.xml and activity_main.xml layout files
I assume you should have this in your included layout:
<data>
<variable name="mysrc" type="android.graphics.drawable.Drawable"/>
</data>

How to apply transition to fragments using the new Navigation Graph?

I have created an animation using XML for fade in and out and attached it to the fragment using the drop down in navigation drop for enter and exit but the fragments are not being animated.
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/navigation_graph"
app:startDestination="#id/mainFragment">
<fragment
android:id="#+id/mainFragment"
android:name="com.cheapapps.randomquotesmachine.MainFragment"
android:label="fragment_main"
tools:layout="#layout/fragment_main">
<action
android:id="#+id/action_mainFragment_to_quoteDetailFragment"
app:destination="#id/quoteDetailFragment"
app:enterAnim="#anim/fade_in"
app:exitAnim="#anim/fade_out" />
</fragment>
<fragment
android:id="#+id/quoteDetailFragment"
android:name="com.cheapapps.randomquotesmachine.QuoteDetailFragment"
android:label="fragment_quote_detail"
tools:layout="#layout/fragment_quote_detail">
<argument
android:name="position"
android:defaultValue="0"
app:argType="integer" />
</fragment>
</navigation>
I'm not sure how your fade_in and fade_out files look like so I'll provide you an example below:
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="500" />
And:
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_interpolator"
android:fromAlpha="1.0" android:toAlpha="0.0"
android:fillAfter="true"
android:duration="500" />
To apply this fade transition to your fragments, you should set enterAnim and exitAnim as you already did but together with popEnterAnim and popExitAnim as in the following example:
<action android:id="#+id/action_mainFragment_to_quoteDetailFragment"
app:destination="#id/quoteDetailFragment"
app:popEnterAnim="#anim/fade_in"
app:popExitAnim="#anim/fade_out"
app:enterAnim="#anim/fade_in"
app:exitAnim="#anim/fade_out"/>

Android cannot find BindingConversion in include tag

I have made a BindingConversion from boolean to visibility, however Android can't find it, but only when I use it in an include tag. It works at other elements like FrameLyout.
In my abstract ViewModel:
#BindingConversion
public static int convertBooleanToVisibility(boolean visible) {
return visible ? View.VISIBLE : View.GONE;
}
Then in my xml (TestViewModel inherits from ViewModel):
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="viewModel"
type="oliveradam.testapp.viewmodels.TestViewModel"/>
</data>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
app:theme="#style/toolbarStyleMusicView"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:minHeight="?attr/actionBarSize">
</android.support.v7.widget.Toolbar>
<include
android:visibility="#{viewModel.isVisible}"
layout="#layout/layout_test"
app:viewModel="#{viewModel}" />
</LinearLayout>
</layout>
Error:(139, 29) error: cannot find symbol method setVisibility(boolean)
I'm working on Android Studio 2.2 RC.
I don't know why this is not working, but I think its the layout tag in include. You can't use these tags for databinding. So I deleted the includes, used a viewstub and changed the layouts programmatically.

Categories