Why doesn’t RecyclerView have onItemClickListener()?

tl;dr 2016 Use RxJava and a PublishSubject to expose an Observable for the clicks. public class ReactiveAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { String[] mDataset = { “Data”, “In”, “Adapter” }; private final PublishSubject<String> onClickSubject = PublishSubject.create(); @Override public void onBindViewHolder(final ViewHolder holder, int position) { final String element = mDataset[position]; holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View … Read more

How to convert a Drawable to a Bitmap?

This piece of code helps. Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon_resource); Here a version where the image gets downloaded. String name = c.getString(str_url); URL url_value = new URL(name); ImageView profile = (ImageView)v.findViewById(R.id.vdo_icon); if (profile != null) { Bitmap mIcon1 = BitmapFactory.decodeStream(url_value.openConnection().getInputStream()); profile.setImageBitmap(mIcon1); }

Android Studio: Add jar as library?

I’ve been struggling with the same thing for many hours, trying to get the Gson jar to work no less. I finally cracked it – here are the steps I took: Put the Gson jar (in my case, gson-2.2.4.jar) into the libs folder Right click it and hit ‘Add as library’ Ensure that compile files(‘libs/gson-2.2.4.jar’) … Read more

Static way to get ‘Context’ in Android?

Do this: In the Android Manifest file, declare the following. <application android:name=”com.xyz.MyApplication”> </application> Then write the class: public class MyApplication extends Application { private static Context context; public void onCreate() { super.onCreate(); MyApplication.context = getApplicationContext(); } public static Context getAppContext() { return MyApplication.context; } } Now everywhere call MyApplication.getAppContext() to get your application context statically.

How to manage startActivityForResult on Android

From your FirstActivity, call the SecondActivity using the startActivityForResult() method. For example: int LAUNCH_SECOND_ACTIVITY = 1 Intent i = new Intent(this, SecondActivity.class); startActivityForResult(i, LAUNCH_SECOND_ACTIVITY); In your SecondActivity, set the data which you want to return back to FirstActivity. If you don’t want to return back, don’t set any. For example: In SecondActivity if you want … Read more

How to add dividers and spaces between items in RecyclerView

October 2016 Update The version 25.0.0 of Android Support Library introduced the DividerItemDecoration class: DividerItemDecoration is a RecyclerView.ItemDecoration that can be used as a divider between items of a LinearLayoutManager. It supports both HORIZONTAL and VERTICAL orientations. Usage: DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), layoutManager.getOrientation()); recyclerView.addItemDecoration(dividerItemDecoration); Previous answer Some answers either use methods that have since … Read more

Download a file with Android, and showing the progress in a ProgressDialog

There are many ways to download files. Following I will post most common ways; it is up to you to decide which method is better for your app. Use AsyncTask and show the download progress in a dialog ============================================================= This method will allow you to execute some background processes and update the UI at the … Read more

R cannot be resolved – Android error

After tracking down this problem as well, I found this note in the Android documentation: http://source.android.com/source/using-eclipse.html *Note: Eclipse sometimes likes to add an “import android.R” statement at the top of your files that use resources, especially when you ask Eclipse to sort or otherwise manage imports. This will cause your make to break. Look out … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)