4

I currently have developed an app with some GUI and network operations, but I need to make it more of a library without the GUI.

I know that there is a "is library" option under Properties/Android. But the question is: how to move the GUI elements out of the project to a different app, so that the library/project will have only java code; any suggestion ?

Thanks.

  • Sounds like you just need to decouple the two. Make the library into an API that the GUI calls – telkins Sep 23 '13 at 17:19
  • @trevor-e So in general, libraries do not contain GUI elements right, just a bunch of java code. So would the GUI app just import and call the library through a simple intent ? (I am kinda new to android) – androidnoob Sep 23 '13 at 18:20
  • It's even simpler. You can import the library and then use the classes like they were from your code. Unless the library is GUI related, it will not contain GUI code because otherwise it makes it less reusable. You should instead use the library to help populate your GUI. I can type this in an answer for you more specifically instead. – telkins Sep 23 '13 at 18:29
  • @trevor-e I think I got what you said but Yes, an example or a few lines of relevant code would be nice. Thanks so much. – androidnoob Sep 23 '13 at 18:40

1 Answers1

1

If you are making code into a library, you want to try and decouple it as much as you can from anything else. This makes it much more portable so that more people can use the library how they wish. Even though you are using this library just for yourself right now, later on you may wish to release it to others.

For example, maybe your code is like this currently:

public void computeSum(int a, int b) {
    int sum = a + b;
    mTextView.setText(String.valueOf(sum));
}

Right now this code is tightly coupled with mTextView. Instead, it makes sense to rewrite the code like this:

//In library
public int computeSum(int a, int b) {
    return a+b;
}

and

//Somewhere in your app
mTextView.setText(String.valueOf(computeSum(3,4)));

This is a small change, but you can see that computeSum() is no longer coupled with mTextView. This makes it much easier to use throughout your project and even other projects. Now the computeSum() method is part of an API.

So for your network calls, try to decouple them from your GUI stuff either by using callbacks or return values.

In regards to your latest comment:

You could create a wrapper like so:

public class UIWrapper {
    public Runnable runnable;
    public SomeUiCallback callback;
}

And then use this in your AsyncTask:

public class YourTask extends AsyncTask<UIWrapper, Void, Void> {
    SomeUiCallback mCallback;
     protected void doInBackground(UIWrapper... wrapper) {
         mCallback = UiWrapper.callback;
         UIWrapper.runnable.run();
     }

     protected void onProgressUpdate() {
     }

     protected void onPostExecute() {
        mCallback.runYourUiStuff();
     }
 }

I wrote that code quickly so it likely won't compile, but hopefully you get the idea. I think something like this would work, not sure if it's the most elegant solution. You can replace the Runnable with whatever you want to run in the thread.

So both UIWrapper and YourTask would reside in your library. You would create the UIWrapper and then use that in the YourTask.

telkins
  • 10,440
  • 8
  • 52
  • 79
  • Makes much more sense now, but is the GUI part ("mTextView.setText(String.valueOf(computeSum(3,4))); ") still in the library code or in a separate app which imports the library ? – androidnoob Sep 23 '13 at 19:16
  • @user2808163 Sorry, I'll edit that. That code would be outside of the library in your app. – telkins Sep 23 '13 at 19:20
  • Awesome, Thanks . I wish I had enough rep to upvote the answer :) – androidnoob Sep 23 '13 at 19:24
  • @user2808163 No problem, let me know if there are other parts unclear. You can try to accept this as an answer to mark it as resolved if you have enough rep power to do that. :) – telkins Sep 23 '13 at 19:28
  • I still have a question, the thing is my library consists of network operations in an async task and I put all the GUI operations on the post execute method. SO when I am try to move those GUI XML elements out to a different app and call the methods, it is not recognizing them as they are private. I can add some code if you'd like – androidnoob Sep 24 '13 at 14:20
  • If you are using an Asynctask is would probably be easiest to wrap a Runnable and UI callback. I edited my post, hopefully it's not too confusing. That's the solution that popped into my head, could be a cleaner way of doing it. – telkins Sep 24 '13 at 14:52
  • Oh ok, Runnable as in create a thread mentioned in. I thought that all I had to do was to set the user credentials and in my app and call the async task, http://stackoverflow.com/questions/1921514/how-to-run-a-runnable-thread-in-android ? – androidnoob Sep 24 '13 at 15:54
  • @user2808163 Runnable is just a generic interface used in many places. You can still use it within AsyncTask or get rid of it completely to do what you want. – telkins Sep 24 '13 at 16:00