Using Kotlin Library in java - java

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;
}
});

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;
}
});

something like This::
bottomNavigation.setOnShowListener( new IBottomNavigationListener(Model model)
{
} );

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;
}
});

use
implementation 'com.etebarian:meow-bottom-navigation-java:1.2.0'
for details watch
https://www.youtube.com/watch?v=MiphbOtSyWY

Related

How do I detect more than single tap?

I would like to detect single taps several times. I have an activity where the user can tap on an image, they should receive the Toast message, and the program should change the image to another one, and whenever the user taps on this image, there's should be another Toast message. To be more specific, I'll try to provide an example:
Let's say we have two images:
1 - straight line
2 - circle
when the user will enter the specified activity, should see one of those images in the toolbar (let's say that first of them are straight line). When the user will click on this image, the app should display Toast, and then change the straight line to a circle.
*By changing images I mean setting method like
circle.setVisibility(View.VISIBLE); // circle.setVisibility(View.GONE);
Unfortunately, my switch case doesn't work as I intended. Whenever a user clicks on the first image, it's changing the visibility of those two, but look's like the OnClick method doesn't recognize another tap. Here's the code
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.line: {
Toast.makeText(TAG.this, "line", Toast.LENGTH_SHORT).show();
test = 2;
break;
}
case R.id.circle: {
Toast.makeText(TAG.this, "circle", Toast.LENGTH_SHORT).show();
break;
}
}
switchImages();
}
private void switchImages(){
switch (test){
case 1:
line.setVisibility(View.GONE);
circle.setVisibility(View.VISIBLE);
break;
case 2:{
line.setVisibility(View.VISIBLE);
circle.setVisibility(View.GONE);
break;
}
}
}
You missed something.. ;)
public void onClick(View v) {
switch (v.getId()) {
case R.id.line: {
Toast.makeText(TAG.this, "line", Toast.LENGTH_SHORT).show();
test = 2;
break;
}
case R.id.circle: {
Toast.makeText(TAG.this, "circle", Toast.LENGTH_SHORT).show();
break;
}
}
switchImages();
}
In the first case you are assigning test = 2 but in the second one you don't do anything with that variable, so method switchImages() won't really do anything, since it depends on test which is 2 all the time (images will not switch accordingly)
You are initializing test in first case as test = 2 but forget to initialize it in second case
So just change the code :
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.line: {
Toast.makeText(TAG.this, "line", Toast.LENGTH_SHORT).show();
test = 2;
break;
}
case R.id.circle: {
Toast.makeText(TAG.this, "circle", Toast.LENGTH_SHORT).show();
test = 1;
break;
}
}
switchImages();
}
I would have changed only onClick(View v) and removed switchImages();
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.line: {
Toast.makeText(TAG.this, "line", Toast.LENGTH_SHORT).show();
line.setVisibility(View.GONE);
circle.setVisibility(View.VISIBLE);
break;
}
case R.id.circle: {
Toast.makeText(TAG.this, "circle", Toast.LENGTH_SHORT).show();
line.setVisibility(View.VISIBLE);
circle.setVisibility(View.GONE);
break;
}
}
}
As per your question, if you click line, it will toast and make visible circle and hide line and vice-versa

two onclick on activity convert into fragment

I am new to the fragment.
in Activity, I have two onClick methods which are:
click that override the method
normal onclick
how can I change the onclick that override the method to fragment?
private void searchMobileNumber() {
mRecyclerView.addOnItemTouchListener(new SelectExistingOrNewNoFragment.RecyclerTouchListener(PostpaidRegistrationActivity.this, mRecyclerView, new SelectExistingOrNewNoFragment.ClickListener() {
#Override
public void onClick(View view, int position) {
selectedPostion = position;
mob_number_detail_lyt.setVisibility(View.GONE);
mobile_no_head_lyt.setVisibility(View.VISIBLE);
mobile_number_success.setImageDrawable(getResources().getDrawable(R.drawable.validation_correct));
if (simCardFirstTime) {
simCardFirstTime = false;
final Intent intent = new Intent(PostpaidRegistrationActivity.this, MyScanActivity.class);
intent.putExtra("ocrType", "Barcode");
intent.putExtra("message", "Please scan your SIM card");
startActivityForResult(intent, MposConstants.SIMCARD_FIRST_TIME);
}
}
}
If I get this clear, you have two buttons. One of which is to start a fragment or do something with it.. You just need a switch case. Also, use the R class for the cases.
Use view.getId()
Code:
#Override
public void onClick(View view, int position) {
switch(view.getId()){
case R.id.fragment_button:
// do something with fragment
break;
default: break;
}
}

Adding fragment to switch statement to allow pages to swipe across

I am making an android app, and when I open up a page I can then swipe between 'Line Details' and 'Verify Line'. However when I swipe to the second page 'Verify Line' the objects i.e. textboxes in 'Line Details' move across as well but I don't want this to happen, could anyone tell me why? I also have a fragment in my code.
#Override
public int getCount() {
return mCount;
}
#Override
public Fragment getItem(int position) {
Fragment fragment = new LineDetails();
fragment.setArguments(extras);
switch (position) {
case 0:
return new LineDetails();
case 1:
return new VerifyLine();
default:
return new LineDetails();
}
}
#Override
public void finishUpdate(ViewGroup container) {
super.finishUpdate(container);
}

Button long click does not work

I'm working on a soundboard and I want to implement a long click to share the sound.
I am working with a switch Case for each button
public void MainMMP(View view){
switch (view.getId()) {
case R.id.button1:
MainMMP.release();
MainMMP = MediaPlayer.create(this, R.raw.xxx1);
MainMMP.start();
break;
case R.id.button2:
MainMMP.release();
MainMMP = MediaPlayer.create(this, R.raw.xxx2);
MainMMP.start();
break;
case R.id.button3:
MainMMP.release();
MainMMP = MediaPlayer.create(this, R.raw.xxx3);
MainMMP.start();
break;
And now I want to implement the long click. I tried a lot of different code here but it is not working for me.
I do not know where to put the onLongClick statement and how.
Can somebody show me a working method and in case of long click it should just send me a Toast that I know the method works?
You could add the OnLongClickListener where you want, in the onCreate method for example.
Try to use the following code:
Button button = (Button)findViewById(R.id.button);
button.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
//Your code
return false; // True if you want to execute simple click code too
}
});
You can use this
private View.OnLongClickListener listener = new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
switch (view.getId())
case R.id.button1:
// Do something...
break;
case R.id.button2:
// Do something else...
break;
// If you still want to get normal click callbacks return true,
// if you do not then return false.
return true;
}
}
Somewhere in your code
Button button1 = (Button)findViewById(R.id.button1);
Button button2 = (Button)findViewById(R.id.button2);
button1.setOnLongClickListener(listener);
button2.setOnLongClickListener(listener);
Or better this
One common recommended way to get onClick/onLongClick/whatever callbacks is to make the Activity implement the callback interfaces.
class YourActivity extend Activity implements View.OnLongClickListener {
#Override
public boolean onCreate(/* ... */) {
// ...
button1.setOnLongClickListener(this);
button2.setOnLongClickListener(this);
}
#Override
public boolean onLongClick(View view) {
// Same code as the one above
}
}

Disable fragment back stack animations - Android

How are you able to disable all fragment animations including the exit animation when clearing the back stack, currently I am using the following code but it doesn't seem to stop the animation exiting the screen, rather it only stops the animation of the new fragment entering the screen.
The code that I'm using is as follows:
Menu Fragment - code used to clear back stack
private void clearBackStack() {
FragmentUtils.sDisableFragmentAnimations = true;
getSupportFragmentManager().popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
FragmentUtils.sDisableFragmentAnimations = false;
}
Fragment Utils Class:
public class FragmentUtils {
public static boolean sDisableFragmentAnimations = false;
}
All fragments have this:
#Override
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
if (FragmentUtils.sDisableFragmentAnimations) {
Animation a = new Animation() {};
a.setDuration(0);
return a;
}
return super.onCreateAnimation(transit, enter, nextAnim);
}
Navigation menu code (where I want to disable the animations)
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_events:
replaceFragment(new MainFragment());
break;
case R.id.action_promotions:
replaceFragment(new MainFragment());
break;
case R.id.action_menu:
clearBackStack();
replaceFragment(new MenuFragment());
break;
}
return true;
}
});
public void replaceFragment(BaseFragment fragment, int resId, boolean addToBackStack) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(0, 0);
transaction.replace(resId, fragment);
if (addToBackStack) {
transaction.addToBackStack(null);
}
transaction.commitAllowingStateLoss();
}

Categories

Resources