Skip to content

Instantly share code, notes, and snippets.

View mahdi-malv's full-sized avatar

Mahdi Malvandi mahdi-malv

View GitHub Profile
@mahdi-malv
mahdi-malv / ListClick.java
Last active July 6, 2018 17:44
RecyclerView Easy Adapting [refer to MainActivity.kt to see usage]
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
public class ListClick implements RecyclerView.OnItemTouchListener {
private GestureDetector gestureDetector;
private ClickListener clickListener;
@mahdi-malv
mahdi-malv / FlipAnimation.kt
Created July 11, 2018 18:15
Flip animation for any view (Also supports two views) - Kotlin
/**
* Creates a 3D flip animation between two views.
*
* @param fromView First view in the transition.
* @param toView Second view in the transition.
*/
class FlipAnimation(private var fromView: View?, private var toView: View?) : Animation() {
private var camera: Camera? = null
private var centerX: Float = 0.toFloat()
@mahdi-malv
mahdi-malv / Permission.kt
Created July 12, 2018 11:03
Ask for permission with TedPermission
//Ted Permission is a great permission library to manage Runtime permission added since Android 6.0(Marshmallow)
//This example Uses RxJava 2 to get permission.
/**
* <b>Remember this need one dependency code</b>
* Code: implementation 'gun0912.ted:tedpermission-rx2:2.2.0' // Or later
* Might also need: implementation 'io.reactivex.rxjava2:rxandroid:2.0.2' //Or later
* @param c is the Context
* @param deniedTitle is the Title when permission denied and you are showing a message that you need it
* @param deniedMessage is the Message
* @param doWithPermissionResult is a function that has a Boolean. If true the permission is granted
@mahdi-malv
mahdi-malv / ListClick.java
Created July 21, 2018 14:47
RecyclerAdapter to simplify adapting recyclerView
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
public class ListClick implements RecyclerView.OnItemTouchListener {
@mahdi-malv
mahdi-malv / SimpleAnim.java
Last active November 8, 2018 11:15
Fade in animation for fast splash usage
public void singleFadeInView(View v, long duration, final Consumer<Animation> onAnimEnd) {
AlphaAnimation anim = new AlphaAnimation(1.0f, 0.0f);
anim.setDuration(duration);
anim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {}
@Override
public void onAnimationEnd(Animation animation) {
onAnimEnd.accept(animation);
@mahdi-malv
mahdi-malv / CheckNetwork.kt
Last active November 10, 2018 08:13
A function (Using RxJava) to check network availability and be notifed when it's status changes.
//Check network with RxJava2
@SuppressLint("MissingPermission") // AccessNetworkState -> Add it yourself
fun isInternetOn(context: Context): Observable<Boolean> {
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val activeNetworkInfo: NetworkInfo?
activeNetworkInfo = connectivityManager.activeNetworkInfo
return Observable.just(activeNetworkInfo != null && activeNetworkInfo.isConnected)
.map { it }
.distinctUntilChanged()
@mahdi-malv
mahdi-malv / DoubleClick.kt
Created November 10, 2018 08:16
Detect double click on a view
//Double click detection
var timeSaver = 0L // Globaly
view.setOnTouchListener { _, event ->
when(event.action) {
MotionEvent.ACTION_DOWN -> {
if (System.currentTimeMillis() - timeSaver <= 500) {
// Do with double click
Toast.makeText(this, "DOUBLE_CLICK", Toast.LENGTH_SHORT).show()
}
timeSaver = System.currentTimeMillis()
@mahdi-malv
mahdi-malv / CollectionSort.java
Created November 10, 2018 08:18
Sort objects of the list by an attribute of them.
//Sort objects by a value
Collections.sort(songList, new Comparator<Song>(){
public int compare(Song a, Song b){
// Here song is a class that has some attrs including title.
// We want to sort songs by title
return a.getTitle().compareTo(b.getTitle());
}
});
@mahdi-malv
mahdi-malv / Convert.java
Created November 10, 2018 08:19
change dp or sp to px in android
public static float dpOrSpToPx(final Context context, final float dpOrSpValue) {
return dpOrSpValue * context.getResources().getDisplayMetrics().density;
}
@mahdi-malv
mahdi-malv / ListScrllingDetection.java
Created November 10, 2018 08:22
Detect scrolling up and down in a recyclerView
//تشخیص اسکرول کردن در لیست
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if (dy > 0) {
//Scrolling down
} else if (dy < 0) {
//Scrolling up
}