-1

I am making a library where I want to show a layout which actually tells either my mPos device is connected with Bluetooth or not. Connection code is in Library and connection call will be started from MainActivity.

Now Problem starts from here: I want to show some Layout or define the layout in my library. It's fine I can pass this or context of MainActivity to my Library.

To make it clear my Library code is not inheritable from Activity class

Please don't tell me you can make connection without the library. I just need to show a Layout inside my library and then I want to remove (pull) it, so my main activity becomes visible. It should be something NavigationController

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Samrez Ikram
  • 591
  • 5
  • 15

3 Answers3

1

I think you are looking at this the wrong way round.

You are thinking [My Project] - starts -> [Activity] and [My Project] handles everything the activity does.

In fact because the OS (Android) can only run Activities (not your project classes independently) it should be [Activity] - starts -> [My Project Service] which handles messages from activity.

When the project is set up like this, you can display the layout normally (as with any Activity). Your existing project classes can still be the ones making all the decisions, it's just started in the opposite order.

This is the same concept used for any cross-platform apps (eg libgdx): The app is a native wrapper (Activity) that runs your common code.

Edit:

When you just want to trigger something in the Activity when a method is called in your own class, you can use a callback interface:

public interface OnConnectionMadeListener {
     void onConnectionMade();
}

Your activity can implement it

public class MainActivity extends Activity implements OnConnectionMadeListener {

     private View mOverlay;         

     public void onCreate() {
          ...
          mOverlay = findViewById(R.id.overlay);
          mOverlay.setVisibility(View.GONE);
          new MyConnectionObject(this); //this will be your class that has the madeConnection() method
     }

     ...

     public void onConnectionMade() {
         //show the overlay
         mOverlay.setVisibility(View.VISIBLE);
     }

}

Then inside your object

public class MyConnectionObject {

    private OnConnectionMadeListener mCallback;

    public MyConnectionObject(OnConnectionMadeListener callback) {
         ...
         mCallback = callback;
    }

    public void madeConnection()  {
        ...
        if (mCallback != null) {
            mCallback.onConnectionMade();
        }
    }
}
Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124
  • It's not what you are thinking. I just need to show an animated view over my MainActivity which will start showing when my call reaches in public void madeConnection(){ /* i have MainActivity Context Here*/ } somehow Main Activity will call makeConnection method and make the connection will show a layout but makeConnection method where is defined is not inherited from activity class. – Samrez Ikram Feb 27 '17 at 13:38
  • Ok, that's not remotely what your question says. In that case you should use a callback. I'll edit my answer. – Nick Cardoso Feb 27 '17 at 13:39
  • Can you give me how to perform this. I am new in android previously i have done this in iOS. by just having view i can perform in iOS but don't know how i will achieve this in Android. Thanks for understanding my problem. – Samrez Ikram Feb 27 '17 at 13:43
  • How i will show my overlay or layout inside. //show the overlay thats the main problem sir. Please read my question i have everything but couldn't show layout and pull it back. – Samrez Ikram Feb 27 '17 at 13:51
  • Sorry, that does not fix my problem. I need to hide my Library code and xml from client application. Again i need to show a layout while making connection. and layout is in Library code. and library code do not have activity class. :( – Samrez Ikram Feb 27 '17 at 14:07
  • A library is supposed to be used by a client, not the other way round. One (bad) solution is to create and expose methods on your library like getLayout() and then in onConnectionMade send the inflated layout to your library to do findViewById and setVisibility. But it shows a real problem in your architecture. You need to consider if it's really necessary – Nick Cardoso Feb 27 '17 at 14:49
1

As you said, you've context there in that method. You can use System Level Alert to show full window layout over that activity. Your activity will be the host of that alert and will keep running below but user won't be able to see it because system alert view would have occupied the screen over it.

This is how you can show alert with context: https://stackoverflow.com/a/21182403/2105241

PS. It isn't a good solution to use system alerts :)

Community
  • 1
  • 1
Ahmad Raza
  • 2,850
  • 1
  • 21
  • 37
0

Create a static method in your library class. Create a context object, then with the static method accept the context and set accordingly. Now whenever you first call this library , call this method first and after that you can use the context object anywhere in your code. Tip do not declare the context as static, it will cause bad memory leaks.

Remario
  • 3,813
  • 2
  • 18
  • 25