Monday, August 8, 2022

gradle dependancy structure

 Here's how we used to declare dependencies:

  • compile 'test.dependency:1.0.0'

Here are the new configurations which should replace compile:

  • implementation 'test.dependency:1.0.0' --> this dependency is only used within this module
  • api 'test.dependency:1.0.0' --> this dependency will also be available in any builds that depend on this module

Monday, September 21, 2020

Static content in screen bottom coming over the recyclerview

Bottomsheet in activity covering over the recyclerview content. 

After little search and different ways to fix it found workarround to tackle this. 

as follow. 


<android.support.v7.widget.RecyclerView
...
android:clipToPadding="false"
android:paddingBottom="72dp"
/>

Saturday, August 1, 2020

Memory leak in startactivityforresult

If we have to start Activity B in Activity A for result as follow 

We will call startActivityForResult from Activity A we will set some result in out activity B and return result.
But what if user presses back button on Activity B how should we handle this case.

I was running LeakCanary and found surprising result that it was always leaking my Activity B. reason is that it wasn't finishing and staying in memory even after returning to Activity B with Back Press.

So here is the solution that I found to resolve this. So quick fix is call method finishActivity with request code that you have passed while calling activity B. 

class ActivityA: AppCompatActivity(){

    private val requestCode = 1001

    fun startActivityB(){
    startActivityForResult(Intent(this, ActivityB::class.java).apply 
                {
                    putExtra("category_id", currentCategory._id)
                    putExtra("category_color", currentCategory.color)
                    putExtra("category_name", currentCategory.name)

                }, requestCode)
    }

    override fun onActivityReenter(resultCode: Int, data: Intent?) {
        super.onActivityReenter(resultCode, data)
        finishActivity(requestCode)
    }

}

Saturday, June 6, 2020

RxAndroid basic example

RxAndroid is flavour of reactive programming implementation for Android.

Base of reactive programming is Observer design pattern.

Let's understand Observer design pattern first.

Observer Design Pattern:
Observer design pattern is one of the behavioural design pattern. It's about communication between objects. In observer design pattern main object emits the change in it and all the object s who are observing to change in main object gets notification about it.


Friday, December 13, 2019

using Java, Kotlin and dagger together in project

If we are using java, kotlin and dagger together in project.

Follow this steps

kapt
Kapt is the Kotlin Annotation Processing Tool, and it’s in pretty good shape these days. If you want to be able to reference generated code from Kotlin, you need to use kapt. To do that, simply include the plugin in your build.gradle file with the line:


apply plugin: 'kotlin-kapt'







And in dependencies

kapt 'com.google.dagger:dagger-compiler:2.13'

Thursday, June 27, 2019

Block touch event in view

Add an onTouchEvent method to the view with top position then return true. True will tell the event bubbling that the event was consumed therefore prevent event from bubbling to other views.

protected boolean onTouchEvent (MotionEvent me) {
    return true;
}

For v1 you would do an import:
import android.view.View.OnTouchListener;
Then set the onTouchListener:

v1.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return true;
    }
});

Force hide soft keyboard

InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
    //Find the currently focused view, so we can grab the correct window token from it.
    View view = activity.getCurrentFocus();
    //If no view currently has focus, create a new one, just so we can grab a window token from it
    if (view == null) {
        view = new View(activity);
    }
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);