Java Question Custom Notification Library (incomplete)

thedesolatesoul

Expert
Licensed User
Longtime User
This library does not work completely, I do not know why and that is why I am requesting help.
It does do SOMETHING, in that it blanks out the notifcation and gives you and empty canvas, which gives me reason to believe there is some life in this log.
There is nothing shown on the notification and there is nothing in the unfiltered logs either.

Anyone who can spot something please raise an event in this thread :D :D :D

This requires a RelativeLayout cm.xml file to be resided as read-only in your res/layout folder.

The idea is to create a remoteview with the layout cm.xml (which warwound kindly created for me since it was so hard to make an xml file :D )

B4X:
@ShortName("setCustomView")
   public void setCustomView(BA ba, NotificationWrapper n)
   {
   
      RemoteViews rv = new RemoteViews(ba.packageName,getResourceId("layout", "cm"));
      
      LayoutInflater mInflater  = ((LayoutInflater)ba.context.getSystemService("layout_inflater"));
      
      View container = null;
      container =  mInflater.inflate(getResourceId("layout", "cm"), null);
      TextView text = (TextView)container.findViewById(getResourceId("id", "title"));
      text.setTextColor(Colors.Black);
      text.setText("Yay!");
      
      n.getObject().contentView = rv;

   }
   
   @BA.Hide
     public static int getResourceId(String type, String resourceName)
     {
       int ret = 0;
       try
       {
         ret = ((Integer)GetStaticField(BA.packageName + ".R$" + type, resourceName)).intValue();
       }
       catch (Exception e) {
         e.printStackTrace();
       }

       return ret;
     }
   @BA.Hide
    public static Object GetStaticField(String classname, String fieldName) throws Exception {
        Class<?> myClass = Class.forName(classname);
        try {
            Field field = myClass.getDeclaredField(fieldName);
            field.setAccessible(true);
            return field.get(null);
        }
        catch (Exception e) {
            Class<?> superClass = myClass.getSuperclass();
            if (superClass == null) {
                throw e;
            } else {
                Field field = (Field) GetStaticField(superClass.getName(), fieldName);
                field.setAccessible(true);
                return field.get(null);
            }
        }
    }
 

Attachments

  • cm.zip
    380 bytes · Views: 245
Last edited:

barx

Well-Known Member
Licensed User
Longtime User
We all know that I am no position really to comment on this BUT, do you need LayoutInflater when using RemoteViews. I can't find anything anywhere that says yes...
 

thedesolatesoul

Expert
Licensed User
Longtime User
I need the inflater in order to reference the views in the xml layout.
Maybe I should try to set the text in the layout itself.

EDIT:
So if I put the text in the layout file with
B4X:
android:text="Type here:"
it actually shows the text.

This means that the remoteview is working with the layout.
What I am not sure is if I am getting the view id and setting the text later properly or not.
 
Last edited:

barx

Well-Known Member
Licensed User
Longtime User
Well, looking at the link in my post / request for this.

http://www.b4x.com/forum/bugs-wishlist/16078-custom-notification-library.html

You use contentview like this?

B4X:
contentView.setTextViewText(R.id.title, "Custom notification");

I don't think it would be very easy to allow the user to create their own layouts. If I ever figured this out myself I was going to create multiple layout file. 1 for 2 text + image, 1 for 2 texts + progress bar, etc. Then some how use a method to select the layout. But, lets just get it working with 1 first eh?
 

thedesolatesoul

Expert
Licensed User
Longtime User
Well, looking at the link in my post / request for this.

http://www.b4x.com/forum/bugs-wishlist/16078-custom-notification-library.html

You use contentview like this?

B4X:
contentView.setTextViewText(R.id.title, "Custom notification");
You can only use this method when the resources are added to your eclipse project. (I dont know it completely). But the R resources are not available when referencing an external file, like I am doing with the xml.
So I need to get the static fields from the xml file in the other way.

What I dont understand is that it never throws an exception if it doesnt find the view. And if it does find the view, then it doesnt update the text.


I don't think it would be very easy to allow the user to create their own layouts. If I ever figured this out myself I was going to create multiple layout file. 1 for 2 text + image, 1 for 2 texts + progress bar, etc. Then some how use a method to select the layout. But, lets just get it working with 1 first eh?
Completely agree!
 

barx

Well-Known Member
Licensed User
Longtime User
I still don't the xml file content or a link to it...
 

thedesolatesoul

Expert
Licensed User
Longtime User
Okay, thanks to many many people here but especially thanks to Barx for his constant persistence and throwing ideas at me like rotten eggs :D

This line will set a text view with some text:

B4X:
@ShortName("setCustomView")
   public void setCustomView(BA ba, NotificationWrapper n)
   {
      RemoteViews rv = new RemoteViews(ba.packageName,getResourceId("layout", "cm"));
      rv.setTextViewText(getResourceId("id", "title"), "yay");
      n.getObject().contentView = rv;
   }

So in short your layout is designed in xml. Each view will have an id.
Using this id, you can get a resource, and set various attributes.
This includes text, images and progress.
These views however are only customizable in xml and not by code.

I will post something more usable later.
 
Last edited:

JonPM

Well-Known Member
Licensed User
Longtime User
Okay, I have got this working except for PendingIntents.
The app force closes, when I hand over it an intent to open for a view id.
That is necessary to have buttons that open different intents on the phone.
It dies with a null pointer exception.

Did you set min API 11 in manifest?

Sent from my DROIDX
 

thedesolatesoul

Expert
Licensed User
Longtime User
Maybe I'm reading it wrong, but I believe it says you would need setPendingIntentTemplate for custom views, which requires API 11.
Hi JonPM,
I am trying to use setOnClickPendingIntent and not setPendingIntentTemplate.
setPendingIntentTemplate seems to be for collections like listview etc.
setOnClickPendingIntent is available from API 3.
Does that sound reasonable?
 

Inman

Well-Known Member
Licensed User
Longtime User
Looks like this library is what I need to design a notification layout like the one in the bottom part of the screenshot. Could you guys please restart this project?

nlfzH.jpg
 
Top