Skip to content

Instantly share code, notes, and snippets.

View EfeBudak's full-sized avatar
learn->work->learn->work

Efe Budak EfeBudak

learn->work->learn->work
View GitHub Profile
@EfeBudak
EfeBudak / LoginFragment.java
Created July 11, 2017 06:07
Application context on Toast message sample
public class LoginFragment extends Fragment{
@Inject
Context mContext;
@Override
public void onAttach(Context context) {
AndroidSupportInjection.inject(this);
super.onAttach(context);
}
@EfeBudak
EfeBudak / AppModule.java
Created July 11, 2017 06:08
Application context on Toast message sample
@Module
public class AppModule {
@Singleton
@Provides
Context provideContext(Application application) {
return application;
}
}
@EfeBudak
EfeBudak / BitmapUtils.java
Last active August 10, 2018 04:46
Vector drawable to bitmap
public static BitmapDescriptor generateBitmapDescriptorFromRes(
Context context, int resId) {
Drawable drawable = ContextCompat.getDrawable(context, resId);
drawable.setBounds(
0,
0,
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
Bitmap bitmap = Bitmap.createBitmap(
drawable.getIntrinsicWidth(),
@EfeBudak
EfeBudak / fragment_with_return_to_top_button.xml
Created June 28, 2018 13:22
Return to Top button layout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
android:animateLayoutChanges="true">
<Button
android:id="@+id/button_return_to_top"
android:layout_width="wrap_content"
override fun start() {
itemDBObservable = itemDao.getAllItems()
compositeDisposable.clear()
compositeDisposable += itemDBObservable
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.scan { previousList: List<Item>, newList: List<Item> ->
val newItems = newList - previousList
if (previousList.isNotEmpty() && newItems.isNotEmpty()) {
showReturnToTopButton()
recyclerView.layoutManager = object :
LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false) {
override fun onLayoutCompleted(state: RecyclerView.State?) {
super.onLayoutCompleted(state)
if (showReturnToTopButton
&& (findLastVisibleItemPosition()
- findFirstVisibleItemPosition()
+ 1
< adapter.itemCount)) {
buttonReturnToTop.visibility = View.VISIBLE
recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
if ((recyclerView?.layoutManager as LinearLayoutManager)
.findFirstCompletelyVisibleItemPosition() == 0) {
buttonReturnToTop.visibility = View.GONE
}
}
})
@EfeBudak
EfeBudak / Create a Notification Channel
Created August 15, 2019 07:40
Code piece that creates a notification channel on Android
private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val importance = NotificationManager.IMPORTANCE_DEFAULT
val channel = NotificationChannel(CHANNEL_ID, CHANNEL_NAME, importance).apply {
description = CHANNEL_DESCRIPTION
}
val notificationManager: NotificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
@EfeBudak
EfeBudak / Create a Simple Notification
Created August 15, 2019 07:43
Create a Simple Notification on Android
val builder = NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("Interactive Notification Title")
.setContentText("Interactive Notification Text")
val notSoInteractiveNotification = builder.build()
class InteractiveNotificationBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
Log.d("InteractiveNotificationBroadcastReceiver", "onReceive")
}
companion object {
fun newPendingIntent(context: Context): PendingIntent {
val intent = Intent(context, InteractiveNotificationBroadcastReceiver::class.java)
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)