Should we make changes directly in Android external libraries? - android

I am using an external library in my Android project. So if i want to customize it according to my needs( color, layout), shall i make direct changes in the library or is it a bad practice to do so?
One other way would be to extend its classes and make changes in my project's code itself. So which is a better way. The first method is easier and i dont see any problem with that in future.
The below code is just that SO is not letting me to submit the question without code
The library that i am using provide some functions
public void setBackgroundResourceForDates(HashMap<Date, Integer> backgroundForDateMap);
public void setBackgroundResourceForDateTimes(HashMap<DateTime, Integer> backgroundForDateTimeMap);
public void setBackgroundResourceForDate(int backgroundRes, Date date);
public void setBackgroundResourceForDateTime(int backgroundRes, DateTime dateTime);

Related

Android's module with hidden classes

I'm creating a new module in android studio, and I want some of the classes to be hidden to outside of the module, I mean, that the classes could just be used internally in the module, but not externally. Is it possible? How could I achieve that?
Thanks in advance!
EDIT: I doubt it's possible to have module-visibility, but the closest you can use is package-visibility, for which you do the following:
Don't make the classes you intend to hide 'public'. Keep the default visibility, which is only seen within classes of the same package. Other public classes within this same package can act as your external interface to your module.
class PrivateToPackageInModule {
}
public class InterfaceOfModule {
private PrivateToPackageInModule ptpim;
}
For anyone that happens to stumble upon this post, there is now a keyword called internal which offers exactly the functionality that OP was looking for.
Documentation link

how to set date in android WeekView library?

I want to implement android WeekView in my calendar application.For that i used this https://github.com/alamkanak/Android-Week-View library. Now the problem is i cant set date in that WeekView .
Never tried it because I never used this lib, but looking at the code, it seems this does what you want :
public void goToDate(Calendar date)
There is also a convenience method to go straight to today :
public void goToToday()
goToDate is deprecated use scrollToDate

Handling a core project and multiple derived from it in Android

I've searched around SO for this and found a few things, but I'm still not sure I fully understand, so I ask you for clarifications.
Here is what I need:
Have a project that has specific function: interrogate web service, display results in different views
Have a second, third and forth project that has exactly the same functionality as the first one, but only different graphic elements like splash screen image, icon, name, package name.
So, I have ProjectCore with activities and functionality. Project1 with a car icon and car image for splashscreen. Project2 with airplane icon and airplane image for splashscreen. Something like that. Each projects has a class with constants like'appId, appName, appServerURL"... All the web service call, data display is in Core as it's the same for all prohects, only the read is made from Constants class.
I was thinking of this approach
Make ProjectCore a Library project with a package like com.domain.core and dummy images
Make Project1, add reference to ProjectCore in it and with a package like com.domain.code.project1 and in resources folder, put images with same name as in core project
Make Project2 on the same principle like project1
Will this approach work ?
Thanks.
Later Edit. I've tried as mentioned before. For instance in Core project I had in drawable a file called splash.png. In Project1's and Project2's drawable folder I've put spash.png file with other images. This works fine. Running the Project1 and Project2 on my phone, started each app with it's own image. So far so good.
Then, because I have different constants I need to use in my App, I went into Core library project and added:
public class C {
public static String SomeConstant = "Project core!";
}
Here comes the problem, I need to have different constant values across Project1 and Project2. Because on Core project, the class is in com.domain.core.utils for instance... I can't add the same package in Project1 and Project2. How do I add the classes so I can update their values and be used on each project with particlar values ?
public class C {
public static String SomeConstant = "Project 1 constant!";
}
public class C {
public static String SomeConstant = "Project 2 constant!";
}
Thank you!
You want to create your functionality in a Library project and then have all of your Branded/OEM/3rdParty projects extend from this, overriding images and string resources where necessary.
When you need to use "Constants" you should instead have a single "run once" portion of your code (such as a splash screen) load these strings from resource files:
public static final String CONSTANT_ONE;
public void onCreate() { CONSTANT_ONE = getResources().getString(R.String.CONSTANT_ONE); }
EDIT
I'm unsure on how initialising a final value on onCreate() will perform. If final doesn't work well and you're worried about changing the variable during program execution then make the variable private (so only that class can assign to it) and then create a public static String getConstantOne() function.
Yes. Library projects are ideal for this, especially if only resources differ. I've used the exact approach that you've outlined with success...
Yes this should work fine. I did something a bit similar and I found occasionally you may have some circumstances where you want to call out from your library project to your application project. In these cases I used interfaces/abstract classes defined in the library project but implemented in application project...

Robolectric Custom Shadow Object

OOTB, Robolectric does not support Locales that well. Therefore, if your app is dependent on locales (which a lot of apps are if they are i18n'nd properly) this can be a royal pain. Long story short, I created my own ShadowFooGeocoder and ShadowFooAddress that allow me to simulate the locale I want. They're basically re-implementations of the existing shadows.
However, when I bind my class as such: bindShadowClass(ShadowFooGeocoder.class), this works great. At runtime, the correct shadow is returned. The problem is that I want to set up the simulations on this object and I'm not sure how. shadowOf(instance) where instance is an injected GeoCoder returns ShadowGeoCoder. I've tried working directly with the ShadowWrangler, but that also returns a ShadowGeocoder.
How can I get at my shadowed class that I've bound through the bindShadowClass(...) call so I can set my expectations (simulations)?
Note: This is a repost of the same question on the Robolectric group here. I posted here because my success rate of getting anyone to answer questions on the group is fairly low. I'm hoping for a better result here.
What I've basically done here is extend ShadowGeocoder like this:
#SuppressWarnings({"UnusedDeclaration"})
#Implements(Geocoder.class)
public class ShadowFooBarGeocoder extends ShadowGeocoder {
// implementation stuff
}
Then I would bind it using the bindShadowClasss(...) and when I retreive the shadow via the static shadowOf(...) call I get back a "ShadowGeocoder" which is an instance of ShadowFooBarGeocoder. I then cast it to that type and perform whatever work I need to.

Android data-binding (similar to WPF)?

I have searched for some similar questions before posting - however I have a general question when it comes to Android and data binding (and the other answers I check did not really get me much further...). Assume you have a class Vehicle:
public class Vehicle {
private Owner owner;
private String brand;
//getter and setter for above attributes...
}
and here is the Owner class ....
public class Owner {
private String name;
}
Now - I was just recently looking into MVVM (ModelView-ViewModel) pattern as employed by Microsofts WPF. Which got me wondering: Assuming I would want to bind the name property of my owner object which is a child of the Vehicle object - would there be some standard way in Android to achieve this? Also presuming, that I might have to validate input before I can have the Model updated?
I was imagining the following components (assuming MVVM):
The View (an Activity) contains no application logic - so its more or less empty
A ViewModel would handle the instance of the Vehicle object and perform actions on it
The Model itself would look as the code I posted before - totally oblivious to the View
and the ViewModel
Now when I add my EditTexts, TextViews and so on to the view, I want them to bind to certain the properties of my context object (Vehicle in this case) ... Mhhh if my question is not clear or you need further informatio do let me know :) thanks in advance.
P.s. I think people familiar with WPF might now what I mean? I myself just read about WPF and found it's a nice way to handle stuff.
P.P.s I am aware of the android binding project but I was wondering if there is a sort of build-in approach in Android or maybe some convention someone is following :) this really is more of a binding-theory question I guess ...
Native Data Binding
Google has launched its native Data Binding Library!
https://developer.android.com/tools/data-binding/guide.html
Which lets you write your views as such
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{user.firstName}"/>
But its event wiring mechanism at this time (rc1 version) can't bind to methods on a class extending from Observable (an exception is thrown when compiling; this is a known bug which will be solved).
And sure the lack of two-way binding (which is not expected to be on the first release) is a big drawback.
Anyway, here's a good sample project to play around with https://github.com/saleeh93/data-binding-samples
There is nothing "baked" into the Android SDK which provides equivalent databinding functionality found in MS WPF. Google is providing a lower level interface for the various devices running Android. To date, higher level application frameworks have not emerged from the Android development community. (IMHO, it would take a monster company like Google to create such a thing, given all the different constraints on a framework for all the various Android devices.)
You could create a set of databinding classes in your own application to handle your needs for the MVVM pattern. The "Databinders" would set the relevant event handlers on Android View objects and run some appropriate method on your ViewModel objects in response. They would also translate change events (that you define) on the ViewModel into the appropriate property assignments on the View objects.
Whether that turns out to be worthwhile for your application is a judgment call. My guess is that it would require more effort to write the Databinder classes than just to hook the lower level View event handlers directly. Frameworks are useful primarily when you didn't have to write them yourself.
Well, my Android-Binding project is trying to do data-binding via XML layout. Because there's no build-in method provided by Google (and I can't foresee that Google will do so), that's the reason I started this project.
Android M will provide powerful library for data binding!
It's available now in dev-preview version.
It looks amazing inside xml and java files:
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{user.firstName}"
/>
Java bean:
public class User {
private final String firstName;
private final String lastName;
public User(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return this.firstName;
}
public String getLastName() {
return this.lastName;
}
}
Binding:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.main_activity);
User user = new User("Test", "User");
binding.setUser(user);
}
Reading a bit about this topic, I just found RoboBinding, a "data-binding Presentation Model framework" for Android.
Similar to the Android-binding project, you can bind properties (one-way or two-way) and events to your views in XML using an extra namespace.
Although it is no built-in approach either, it might be a great help for you.
Since you first asked your question, the landscape has changed a lot.
Most importantly Stuart Lodge gave us MVVMCross.
This project provides a cross-platform mvvm mobile development
framework built on top of Silverlight for WP7, Mono for Android and
MonoTouch for iOS, and the WinRT XAML framework for Windows 8 Store
applications
This project makes extensive use of Portable Class Libraries to
provide maintainable cross platform C# native applications.
It provides data binding in your Views against ViewModels
For example, it enables the following:
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
local:MvxBind="{'Text':{'Path':'Query','Mode':'TwoWay'}}" />
Resources:
Github Page:
https://github.com/slodge/MvvmCross
Presentation:
http://slodge.blogspot.co.uk/2012/12/mvvmcross-video-presentation-xaminar.html
And a very good introductional tutorial: Building Android Apps with MVVM and Data Binding
In addition to Oleksii's answer, for those who want to see a sample project (it seems Google hasn't provided any sample project yet), I just made one and pushed it to GitHub.
A few notes:
classpath "com.android.databinding:dataBinder:1.0-rc0" doesn't work for me so I use classpath group: 'com.android.databinding', name: 'dataBinder', version: '1.0-rc0'
Need Java 7 +
If you get errors, try clean/rebuild.
Don't forget to implement android.databinding.Observable or extends BaseObservable if you need the ability to notify when data changes.
minSdkVersion is 7. It's a support lib.
My project doesn't follow MVVM, it just uses data binding. Added MVVM sample.
I realize this is some years later but faced with the same issues I ran across the following which may help lessen the load.
RoboBinding - handles binding - as mentioned above
RoboGuice - does dependency injection
There is a very nice video presentation of RoboBinding that will help explain what and how.
I am not affiliated with either effort but they do look promising for those folks still trying to resolve the binding issues, especially on involved code. RoboBinding also handles bidirectional updates.

Categories

Resources