How I Solved Android Stuffs

Hi I’m Jan, Android Developer and I’m Using Android Studio in Windows. This is just a list of how I do things.
Outline
1. Failed to Download Library
2. LayoutManager for RecyclerView
3. RecyclerView’s OnClickListener
4. Unsupported Modules Detected
5. Using Android Service
6. IllegalStateException: Use Theme.AppCompat theme (or descendant) with this activity.
7. Supporting Multiple Screens
Failed to Download Library (from gradle repository)
After merging my branch with another branch I’ve started to clean the project and sync gradle but after it stops and says;
android studio failed to resolve downloading library
It includes 4 libraries that Android Studio should download but problem still persist I keep on clicking Sync Gradle button but still no luck. After searching google I’ve found this post that I can relate to. He solves his problem by adding this line of code in build.gradle of his project
repositories {
maven { url 'http://clinker.47deg.com/nexus/content/groups/public' }
}after adding the block of code I Sync Gradle and it sync successfully.
Scott Barta also explains as to why this happens
Gradle, the build system that Android Studio uses, has the ability to automatically download library dependencies from the Internet. By and large this is a big boon for developers, because instead of having to manually download archive files, put them in the right place in your project, check them into source control, and repeat the process for new versions, now you just have to add a line of build script and the build system takes care of the housekeeping for you. The major downsides are Internet connectivity woes, which affect different developers to different degrees, and some added confusion about what it means when you get an error.
Source:
LayoutManager for RecyclerView
After running the app to check if my implementation of Custom AppCompatDialog works this happens to me

As you can see the dialog occupies most of the screen even though I have set the wrap_content to support v7 recyclerview’s height
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
My problems as of now the time I’m writing entry RecyclerView does not support wrap_content in layout_height. Good thing I’ve found this library for linear layout manager which supports wrap_content.
Instead of using
mLinearLayoutManager = new LinearLayoutManager(mContext);
you can use this directly to your LinearLayoutManger
mLinearLayoutManager = new org.solovyev.android.views.llm.LinearLayoutManager(context);
It works well now after applying the serso’s LinearLayoutManager library

Gradle dependency:
compile 'org.solovyev.android.views:linear-layout-manager:0.4@aar'
Source:
RecyclerView’s OnClickListener
I’m updating how I handle OnClick in RecyclerView Thanks to Rhymart Manchus (ryhmart@nueca.net) he share what he found to me and the implementation is awesome.
This is my old implementation I’ll just save it here
mRecyclerView.addOnItemTouchListener(new RecyclerTouchListener(context, mRecyclerView, new ClickListener() {
@Override
public void onClick(View v, int position) {
}
@Override
public void onLongClick(View v, int position) {
}
}));public interface ClickListener {
void onClick(View v, int position);
void onLongClick(View v, int position);
}
static class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {
private GestureDetector detector;
private ClickListener clickListener;
public RecyclerTouchListener(Context context, final RecyclerView pRecyclerView, final ClickListener clickListener) {
this.clickListener = clickListener;
detector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
//Log.d("JN", "onSingleTap" + e);
return true;
}
@Override
public void onLongPress(MotionEvent e) {
View child = pRecyclerView.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null) {
clickListener.onLongClick(child, pRecyclerView.getChildAdapterPosition(child));
}
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child = rv.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null && detector.onTouchEvent(e)) {
clickListener.onClick(child, rv.getChildAdapterPosition(child));
}
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}As you can see its a long line of code in one class but in the new implementation you only have to do this
- Create an Interface for ClickListener
protected OnItemClickListener mOnItemClickListener;
protected OnItemLongClickListener mOnItemLongClickListener;
public interface OnItemClickListener {
void onItemClicked(View view, int position);
}
public interface OnItemLongClickListener {
void onItemLongClicked(View view, int position);
}2. In your RecyclerViewAdapter’s ViewHolder extend it to View.OnClickListener and OnLongClickListener
and its setOnItemClickListener and setOnLongClickListener.
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener { public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this)
}}
3. implement ClickListener’s methods
@Override
public void onClick(View v) {
}
@Override
public boolean onLongClick(View v) {
return true;
}
4. inside onClick(View v) and onLongClick(View v) implement the interface method
@Override
public void onClick(View v) {
if(mOnItemClickListener != null) {
mOnItemClickListener.onItemClicked(v, getLayoutPosition());
}
}
@Override
public boolean onLongClick(View v) {
if(mOnItemClickListener != null) {
mOnItemLongClickListener.onItemLongClicked(v, getLayoutPosition());
}
return true;
}
5. don’t forget to add setter method for your the interface listeners
public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
this.mOnItemClickListener = mOnItemClickListener;
}
public void setOnItemLongClickListener(OnItemLongClickListener mOnItemLongClickListener) {
this.mOnItemLongClickListener = mOnItemLongClickListener;
}That’s it! so before setting recyclerview adapter you can pass the interface OnItemClickListener which you first created (see item no. 1)
customModuleAdapter.setOnItemClickListener( ... );
Thanks!
Unsupported Modules Detected

Everything was fine before I update Android Studio to AI-141.2071668 canary channel soon after Android Studio restarted this Unsupported Modules Detected pops up.
After searching the net I have found out a way to solve this. I will just have delete the .iml in the root project and to re-import gradle.
I have followed Rob Meeuwisse answer. I know the solution is quite far from the answer but it worked for so far I’m coding again thanks!
The following works for me when renaming the project:
1. Close Android Studio
2. Delete the *.iml file(s) in your project’s root directory
3. Delete everything in the .idea directory except workspace.xml and tasks.xml.
4. Rename your project’s root directory to the new project name
5. Restart Android Studio and import the project from the new directory.
Note: If you don’t care about losing your workspace settings (window sizes, etc.) and you don’t have no tasks then you can just delete the .idea directory entirely.
Using Android Service
A service is a component that runs in the background to perform long-running operations without needing to interact with the user and it works even if application is destroyed.
Android Service has two States which is Started and Bound.
When using Started Service, can call startService() method from an Activity and stop it by calling stopService(). Once started, a started service can run in the do long background operations anytime even if the activity in which it is started is destroyed.
A Bound Service is when a application component (Activities and etc.) binds to it using the method bindService(). One good thing about bounded service is it provides a client-server interface (Service Connection)that allows components to interact with the service bind to it.
Android Service Life Cycle

Currently I’m constructing algorithm as to how will I use this to state service. Rhymart Manchus has given me the idea on how will I implement it and it was awesome.
First, I will start the service using startService() method on onStart or onCreate. The service is now started but I can’t access its methods because I don’t really have a connection between the two. Second, Since I only needed the service when I will fetch data. I will call bindService() method before using its function. And Lastly, I’ll call unBindService() after I have used the services long background process methods.
Check if Android Service is Running
According to the answer of geekQ use this function
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}And call it using:
isMyServiceRunning(MyService.class)
source:
Use Theme.AppCompat theme (or descendant) with this activity
After the release of support libraries We all agreed that all of our activities should extend AppCompatActivity to align with material design but after running it IllegalStateException occured.

Anyway I’ve look into Android Manifest file and found out the the THEME of the activity is not set, Since it’s using AppCompat Library I should also use Theme.AppCompat.Light or Dark. I’ve set it and now it’s working!
Here’s how I’ve solved it:
Android Manifest
<activity
android:name=".C_Login2"
android:label="@string/app_name"
android:theme="@style/AppTheme">
</activity>
Styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primary_dark</item>
<item name="colorAccent">@color/accent</item>
<item name="android:windowActionBarOverlay">true</item>
</style>
Activity (which extends AppCompatActivity)
public class LoginActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}