Im pretty pretty new to Dynamic-Jasper, but due to work i had to add a new feature to our already implemented solution.
My Problem
The Goal is to add a Column to a report that consists only out of a background-color based on some Information. I managed to do that, but while testing I stumbled upon a Problem. While all my Columns in the html and pdf view had the right color, the Excel one only colored the fields in the last Color.
While debugging i noticed, that the same colored Fields had the same templateId, but while all Views run through mostly the same Code the Excel one showed different behavior and had the same ID in all fields.
My Code where I manipulate the template
for(JRPrintElement elemt : jasperPrint.getPages().get(0).getElements()) {
if(elemt instanceof JRTemplatePrintText) {
JRTemplatePrintText text = (JRTemplatePrintText) elemt;
(...)
if (text.getFullText().startsWith("COLOR_IDENTIFIER")) {
String marker = text.getFullText().substring(text.getFullText().indexOf('#') + 1);
text.setText("ID = " + ((JRTemplatePrintText) elemt).getTemplate().getId());
int rgb = TypeConverter.string2int(Integer.parseInt(marker, 16) + "", 0);
((JRTemplatePrintText) elemt).getTemplate().setBackcolor(new Color(rgb));
}
}
}
The html view
The Excel view
Temporary Conclusion
The same styles uses the same Objects in the background and the JR-Excel export messes something up by assigning the same Object to all the Fields that I manipulated there. If anyone knows of a misstake by me or potential Solutions to change something different to result the same thing please let me know.
Something different I tried earlier, was trying to set the field in an evaluate Method that was called by Jasper. In that method we assign the textvalue of each field. It contained a map with JRFillFields, but unfortunatelly the Map-Implementation denied access to them and just retuned the Value of those. The map was provided by dj and couldn't be switched with a different one.
Edit
We are using JasperReports 6.7.1
I found a Solution, where I replaced each template with a new one that was supposed to look exactly alike. That way every Field has its own ID guaranteed and its not up to chance, how JasperReports handles its Data internaly.
JRTemplateElement custom =
new JRTemplateText(((JRTemplatePrintText) elemt).getTemplate().getOrigin(),
((JRTemplatePrintText) elemt).getTemplate().getDefaultStyleProvider());
custom.setBackcolor(new Color(rgb));
custom.setStyle(((JRTemplatePrintText) elemt).getTemplate().getStyle());
((JRTemplatePrintText) elemt).setTemplate(custom);
So I'm pretty new to the whole Android Studio thing and I've been using the internet to help me with a lot of the things I am doing and needed help on something.
I'm not sure if it's possible to connect this to either a string or an SQL database but I have a Main Layouts with a bunch of buttons that allow me to click on them and choose what external player I would like to use to watch the video. In my MainActivity java class, this is how it finds the button.
case R.id.button3:
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("http://videoname.mp4"), "video/*");
startActivity(Intent.createChooser(intent, "Choose an External Player"));
break;"
I wanted to know if "http://videoname.mp4" url can be connected to like a string where I can always update or change the URL instead of manually going to find the URL in the MainActivity java and changing it. As of now I have to manually do it but a different way to do it would be helpful.
I'm sorry if it's all confusing, but if you know, please let me know as soon as.
Thank you.
you just need write your string URL :
open your directory folder /res/values/strings.xml -> write your string : <string name="yourStringName>yourStringURL</string>. to use your string do this
getContext().getString(R.string.yourStringName) in fragment
getString(R.string.yourStringName) in Activity
or you can write directly on your code, put your cursor on your string, then press key alt + enter choose extract string resource fill the resource name with your string name
wherever you need to use it, just do point 1 or 2. also in your Intent
hope this help you, never stop learning!
Add it to strings.xml in your project. That is the recommended way of using strings anyway.
Right now I have 3 projects:
Library with all my code
Pro empty shell
Lite empty shell
I need to be able to do four things:
Change an int in library based on if user has pro or lite version
Change a long in library based on if user has pro or lite version
Add ads in the lite shell
Change the launch icon. Not sure where to put this. Leave both icons out of library or put both in library?
I have 2 Google Analytic codes -- one to track pro and other to track lite
I have a TextView in library right now that has a link to the pro version. Should I leave this in the library or just make it not visible in the pro or take it out the library and only have it in the lite?
I am having trouble finding a good example of how to set up code for this sort of thing. Do I just put logic statements somewhere in the library or do I create some java/xml files in the shells? So right now the empty pro/lite projects just reference the library but have no jars, xml files, java files, pngs or anything else. I have changed the two shells AndroidManifest package name to be unique to each project.
How about creating an Enum?
public enum LibType
{
PRO(
0,
1L,
"PRO"
),
LITE(
10,
20L,
"LITE"
);
public final int intVal;
public final long longVal;
public final String analyticsCode;
...
private LibType(
int intVal,
long longVal,
String analyticsCode
)
{
this.intVal = intVal;
...
}
}
I am writing my first Android app. It will have hundreds of menu links that will change with each version released. I am looking for a way to process the onclicks based on the Resource ID.
Menus are defined in XML like so:
<item android:id="#+id/Spring_2013" android:title="Spring 2013" android:showAsAction="ifRoom|withText" />
The onclicks will load a URL stored in strings.xml:
<string name="Summer_2013">file:///android_asset/catalog/current/genadmissionreq.html</string>
So, seems like all I need is a simple, single onclick method that loads whatever string is needed based on the selected Resource ID. The MenuItem ID is ALWAYS equal to the URL string ID in my code. So, without using hundreds of if() or switch/case statments, cant I just say:
String url = getString("R.string." + menuItem.getItemId());
Obviously that assignment wont work for a number of reasons. But you get the idea right? So, how CAN I make this work?
Im sorry for the newb question. I know it should be simple but I've been reading for days and cannot find any way to do this without an if() statement for EVERY menu item. That method would make the weekly updates a nightmare!
Try using getIdentifier()
String title = menuItem.getTitle().toString().replace(' ', '_');
id = getResources().getIdentifier(title, "string", getPackageName());
String string = getString(id);
The first line fetches Spring 2013 and converts it to Spring_2013.
The second line builds "R.string." with whatever the title is, in this case "R.string.Spring_2013".
The third line simply fetches the String resource.
Take accessing contacts in android
android.jar for versions 1.6 has People.CONTENT_URI for invoking contacts related info whereas in later versions we need to have api support for RawContacts.CONTENT_URI.
Same thing is true for accessing calendar for instance as its URI is changed in android 2.2.
Is there a best practice to manage all different changes without adding additional application or build separately for each version of changes?
For my money, a very good answer is at http://android-developers.blogspot.co.uk/2010/07/how-to-have-your-cupcake-and-eat-it-too.html. However, the example there is a little more complicated than need be, so based on that, here is an example of how to cope with it when building notifications. The underlying reason this works is a consequence of how java engines interpret classes: it only looks at them when needed, so if you wrap version specific code up in a class and only create it when you know you are using that version, it all works ...
There are, as far as I can tell, two generations of approaches to creating notification, and a naming change along the way in the second. So that gives three ways to do it. For each way, create a class with the notification generation in it:
The first approach (used through to Gingerbread):
public class MyNotificationBuilderToGingerBread {
Notification notification = null;
MyNotificationBuilderToGingerBread(Context myContext, int icon, String ticker, String title, String info, Long timeStamp, PendingIntent pendingIntent, int flags) {
notification = new Notification(R.drawable.ic_sb, ticker, timeStamp);
notification.setLatestEventInfo(myContext, title, info, pendingIntent);
notification.flags |= flags;
}
Notification get() {
return notification;
}
}
The second approach, Honeycomb to IceCreamSandwich:
public class MyNotificationBuilderHoneyCombToIceCreamSandwich {
Notification.Builder mb = null;
MyNotificationBuilderHoneyCombToIceCreamSandwich(Context myContext, int icon, String ticker, String title, String info, Long timeStamp, PendingIntent pendingIntent, boolean onGoing) {
mb = new Notification.Builder(myContext);
mb.setSmallIcon(icon);
mb.setContentIntent(pendingIntent);
mb.setContentTitle(title);
mb.setContentText(info);
mb.setWhen(timeStamp);
if (ticker != null) mb.setTicker(ticker);
mb.setOngoing(onGoing);
}
Notification get() {
return mb.getNotification();
}
}
The second generation, with the name change, Jellybean (onwards, so far ...):
public class MyNotificationBuilderJellyBean {
Notification.Builder mb = null;
MyNotificationBuilderJellyBean(Context myContext, int icon, String ticker, String title, String info, Long timeStamp, PendingIntent pendingIntent, boolean onGoing) {
mb = new Notification.Builder(myContext);
mb.setSmallIcon(icon);
mb.setContentIntent(pendingIntent);
mb.setContentTitle(title);
mb.setContentText(info);
mb.setWhen(timeStamp);
if (ticker != null) mb.setTicker(ticker);
mb.setOngoing(onGoing);
}
Notification get() {
return mb.build();
}
}
Then, you just need to pick which class to instantiate on the fly:
// System information
private final int sdkVersion = Build.VERSION.SDK_INT;
// If you want to go really old:
// (actually, there is a question about how this issue should be handled
// systematically. Suggestions welcome.)
// final int sdkVersion = Integer.parseInt(Build.VERSION.SDK);
// This is for a permanent notification. Change the final argument (flags or boolean) if it isn't meant ot be
// For meaning of other variable, see notification documentation on the android website.
if (sdkVersion < Build.VERSION_CODES.HONEYCOMB) {
MyNotificationBuilderToGingerBread mnb = new MyNotificationBuilderToGingerBread(myContext, R.drawable.notification_icon, ticketText, title, infoText, timeStampMillis, pendingIntentForTapOnFullNotitifcation, Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR);
notification = mnb.get();
}
else if (sdkVersion < Build.VERSION_CODES.JELLY_BEAN) {
MyNotificationBuilderHoneyCombToIceCreamSandwich mnb = new MyNotificationBuilderHoneyCombToIceCreamSandwich(myContext, R.drawable.notification_icon, ticketText, title, infoText, timeStampMillis, pendingIntentForTapOnFullNotitifcation, true);
notification = mnb.get();
}
else {
MyNotificationBuilderJellyBean mnb = new MyNotificationBuilderJellyBean(myContext, R.drawable.notification_icon, ticketText, title, infoText, timeStampMillis, pendingIntentForTapOnFullNotitifcation, true);
notification = mnb.get();
}
// Send the notification.
notificationManager.notify(idForNotificationManager, notification);
Hope this helps!
There are many resources for you to utilize to help support multiple versions of android.
Read this blog post here and
then read this one here, they
will help you address API level
version support issues.
Read this blog post on multiple
screen support, especially how the
asset hierarchy in parsed in res
folder. This will help you
understand and design how to do
asset folder structure to support
different screen size/densities and
android versions.
Lastly write your own custom ant build
scripts so that you can compile with
all versions of android.
Quite Honestly, it's a pain.
I usually, just isolate parts of code that are different and access them using abstract classes. So technically creating different version for different OS.
But there are other ways. The best one i've seen involves using reflection.
If you don't really need the new functionnalities, and really have to support old Android versions, drop it. Build your app for the oldest version, and don't bother with this kind of thing.
In the other case, you can detect the version using Build, and use reflection to load the classes you need. An example of that can be found in the source code of the K9Mail app
There's a nice article on android.com about it:
http://developer.android.com/resources/articles/backward-compatibility.html
Personally I would suggest the wrapper class or wrapper library solution. But in small cases the reflection should be fine (and in case performance is not a problem for you).
If you need more info, ask in comments.
This is a great article for when you have to do reflection in Android (to support multiple API levels).
And when you have to have different resources for different API Levels, this is the reference to use (see the section on "Platform Version (API level)").
If on Eclipse, from ADT version 17 you can specify code to run with some version simply as described in Lint API Check.
The code word is #TargetAPI(XX)
Hope it helps
Best practice (though not for Android, but for J2ME) to my knowledge is to use preprocessing C/C++ styled statements, like:
//#if S40
...
//#else
...
//#endif
Some IDE's support these kind of preprocessing, e.g. Netbeans. To my knowledge Eclipse has some plugins to enable preprocessing also. I don't really know are they applicable to Android development. Try to google yourself.