4

I am trying to use a GitHub library (MeowBottomNavigation)in Android Studio.But its written in kotlin and i cant use the listeners in it. The only thing which is given is this

bottomNavigation.setOnShowListener {
}

bottomNavigation.setOnClickMenuListener {
}

the suggestions shows to use

(Function1)

i am not sure as to how to implement this in java . Any help will be appreciated.

I am familiar with java but the library is written in Kotlin. Is there any way to use these listeners in java?

bottomNavigation.setOnClickMenuListener(new 
Function1<MeowBottomNavigation.Model, Unit>() {
        @Override
        public Unit invoke(MeowBottomNavigation.Model p1) {
            int i = p1.getId();
            switch (i){
                case 4:
                    Toast.makeText(UserMainActivity.this, i, Toast.LENGTH_SHORT).show();
                    break;
                case  1:
                    Toast.makeText(UserMainActivity.this, i, Toast.LENGTH_SHORT).show();
                    break;
                case 2:
                    Toast.makeText(UserMainActivity.this, i, Toast.LENGTH_SHORT).show();
                    break;
                case 3:
                    Toast.makeText(UserMainActivity.this, i, Toast.LENGTH_SHORT).show();
                    break;
            }
            return Unit.INSTANCE;
        }
    });
3
  • Sounds very much alike stackoverflow.com/q/44301301/4636715 except setOnClickListener is mentioned there. May give you a clue. Commented Mar 29, 2019 at 7:23
  • I am not familiar with kotlin i want to implement it in java.The problem is the listener has no body and suggestions shows ( Function1 <? super Model, Unit> ) i am not sure how to use this in java. Commented Mar 29, 2019 at 7:27
  • This link might be helpful: How to convert a kotlin source file to a java source file Commented Mar 29, 2019 at 7:46

4 Answers 4

4

Function0, Function1, Function2, ... FunctionN are higher-order functions in kotlin.

After converting to java, your click listeners become something like below.

// Set Menu Click Listener 
bottomNavigation.setOnClickMenuListener(new Function1<MeowBottomNavigation.Model, Unit>() {
        @Override
        public Unit invoke(MeowBottomNavigation.Model p1) {
            return Unit.INSTANCE;
        }
    });

// Set Menu Show listener
bottomNavigation.setOnShowListener(new Function1<MeowBottomNavigation.Model, Unit>() {
        @Override
        public Unit invoke(MeowBottomNavigation.Model s) {
            return Unit.INSTANCE;
        }
    });
Sign up to request clarification or add additional context in comments.

3 Comments

but how do i access the click event here. I tried using int i = p1.getId(); and then tried a toast to display the value.But its showing error android.content.res.Resources$NotFoundException: String resource ID #0x4
Please see above I have updated the code please tell me where i am wrong.
also, you should add kotlin dependencies to your java app otherwise you will have a problem that says <cannot resolve 'Unit'>. you can do it by creating an extra activity with kotlin codebase and Android Studio will handle gradle changes automatically. You can delete the extra activity after that :-)
0

something like This::

bottomNavigation.setOnShowListener( new IBottomNavigationListener(Model model)
{


} );

1 Comment

cannot resolve symbol IBottomNavigationListener & Model
0

if you are using fragments

//1.-declare fragments globally in your activity

private HomeFragment homeFragment = new HomeFragment();

//2.- declare a method to switch between fragments

public void loadFragment(Fragment fragment){
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.yourFragmentContainer,fragment);
        transaction.commit();
    }

//3.- in the Set Menu Click/show Listener call the fragment to show

// Set Menu Click Listener







      bottomNavigation.setOnClickMenuListener(new Function1<MeowBottomNavigation.Model, Unit>() {
                @Override
                public Unit invoke(MeowBottomNavigation.Model model) {
                    int i = model.getId();
                    switch (i){
                        case  1:
                            loadFragment(homeFragment);
                            break;
                            //...other cases
    
                    }
                    return Unit.INSTANCE;
                }
            });
        
        // Set Menu Show listener
        bottomNavigation.setOnShowListener(new Function1<MeowBottomNavigation.Model, Unit>() {
                @Override
                public Unit invoke(MeowBottomNavigation.Model model) {
                    int i = model.getId();
                    switch (i){
                        case  1:
                            loadFragment(homeFragment);
                            break;
                            //...other cases
                    }
                    return Unit.INSTANCE;
                }
            });

Comments

0

use implementation 'com.etebarian:meow-bottom-navigation-java:1.2.0'

for details watch https://www.youtube.com/watch?v=MiphbOtSyWY

2 Comments

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.