This library offers some barebone code for android common to most applications. It provides simple classes and pre-written functions for:
- Internet Access
- SharedPreferences storage and retrieval
- ImagePicker and Bitmap operations
- File read and write
- Recycler View
- Image Downloading
- Some other basic functions like dp2pixel, etc.
- Database support
- Json Parsing
- Marshmallow Permissions Support (coming soon...)
The library is on Jcenter, so usage is really simple. Add the following dependency in your app's build.gradle
:
dependencies {
...
compile 'com.github.bijoysingh:android-basics:0.8.19'
...
}
You might need to also include these in case you use the corresponding dependencies
dependencies {
...
// For Image Downloader
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.4'
// For internet access
compile 'com.mcxiaoke.volley:library:1.0.17'
// For Timestamp utility
compile 'net.danlew:android.joda:2.8.1'
// For basic features from Google
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:support-v4:23.1.1'
// For Recycler View
compile 'com.android.support:recyclerview-v7:23.1.1'
...
}
Extend the AccessManager
class. Use the Access
class to send and get requests by using objects of AccessItem
.
public class Access extends AccessManager {
....
}
Use the class and the built in functions such as get
and send
.
Access access = new Access(context);
access.get(new AccessItem(link, filename, access_type, is_authenticated));
access.send(new AccessItem(link, filename, access_type, is_authenticated), data);
Extend the PreferenceManager
class.
public class Preferences extends PreferenceManager {
...
@Override
public String getPreferencesFolder() {
return "YOUR_PREFERENCE_FOLDER_NAME";
}
}
Use the class and built in functions using save
and load
.
Preferences preferences = new Preferences(context);
preferences.save(KEY, your_variable);
preferences.load(KEY, your_default_variable);
ImageManager imageManager = new ImageManager();
imageManager.showFileChooser(this);
Handle the response for this using handleResponse
in onActivityResult
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bitmap bmp = imageManager.handleResponse(requestCode, resultCode, data);
...
}
You can perform a number of Bitmap operations
ImageManager.getScaledBitmap(bitmap, scale);
ImageManager.resizeBitmap(bitmap, width, height)
ImageManager.getScaledBitmapWithHeight(bitmap, height);
ImageManager.getScaledBitmapWithWidth(bitmap, width);
To store and retreive some text, some basic support code is available. This is needed if you want to save some file/ json you receive from the server to act as cache.
FileManager.write(context, filename, text_to_write);
String text_read = FileManager.read(context, filename);
This library uses the Universal Image Loader library. To use this some basic configuration is pre-built. You can do this as follows
ImageLoaderManager.displayImage(context, image_url, image_view);
or
ImageLoaderManager loader = new ImageLoaderManager(context);
loader.displayImage(image_url, image_view);
You can also customize the image loader using
ImageLoader imageLoader = ImageLoaderManager.getImageLoader(context);
ImageLoader imageLoader = ImageLoaderManager.getImageLoader(context, diskCacheInMB, memoryCacheInMB);
and use it as follows:
ImageAware imageAware = new ImageViewAware(image_view, false);
imageLoader.displayImage(image_link, imageAware);
These are some common useful functions. These will expand with time.
Functions.makeToast(context, message);
Functions.dpToPixels(context, dp)
LocaleManager.toString(Character/Float/Double/Integer/Boolean variable)
This function will convert your variable to the String to these using the Locale. This functions is a wrapper around the code String.format
. The function will prevent Lint Warning for the same.
ResourceManager.getColor(Context context, Integer colorId)
This was previously meant to handle version support. Now it is simply a wrapper around ContextCompat.getColor();
Using Recycler View cannot be easier!
Extend the Recycler View Holder
public class YourViewHolder extends RVHolder<YourItem> {
...
@Override
public void populate(final YourItem data) {
// Populate your view. You can set on click listeners etc.
}
}
Extend the Recycler View Adapter
public class YourAdapter extends RVAdapter<YourItem, YourViewHolder> {
...
}
Setup your recycler view
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(false);
layoutManager = new LinearLayoutManager(context);
recyclerView.setLayoutManager(layoutManager);
YourAdapter adapter = new AppAdapter(context, R.layout.your_layout_item);
recyclerView.setAdapter(adapter);
A full fledged example can be seen in my TutorialApp.
Adding database setup is super simple. You have to do very little work!
Just add a simple model like
public class YourDatabaseItem extends DatabaseModel {
@DBColumn(primaryKey = true, autoIncrement = true)
public Integer id;
@DBColumn
public String title;
@DBColumn
public String description;
}
Using @DBColumn
you can add custom arguments like
fieldType = DBColumn.Type.INTEGER
unique = True
primaryKey = true, autoIncrement = true
fieldName = "custom_field_name"
You can create a custom class for your databases, or you can simply use the default database:
DatabaseManager db = new DatabaseManager(this, new DatabaseModel[]{new YourDatabaseItem()});
// To add an item
YourDatabaseItem your_item = ...;
db.add(your_item);
// To get all items for a custom class
List<YourDatabaseItem> items = db.get(YourDatabaseItem.class);
A full fledged example can be seen in my TutorialApp.
Making a JSON Parser is simple to work
public class YourItem extends JsonModel {
@JsonField
public Integer integer_field;
@JsonField(field = "alternate_json_field_name")
public String string_field;
@JsonField
public Double real_field;
@JsonField(field = Type.BOOLEAN)
public Boolean boolean_field;
@JsonField(field = Type.JSON)
public JSONObject json_field;
}
The method automatically detects the type of the JSON field, you can still choose to override it. Further, it assumes that the name of the field is the JSON field, you can yet again choose to override it
You can also, quickly serialize your item into a JSON Object
JSONObject json = item.serialize();
Handling your permissions for Marshmallow made simpler, and cleaner
// Could be more than one permissions here
String[] permissions = new String[]{Manifest.permission.ACCESS_FINE_LOCATION};
// Initialise the manager object, with required permissions
PermissionManager manager = new PermissionManager(context, permissions);
/*
* Or set them as you need them
* PermissionManager manager = new PermissionManager(context);
* manager.setPermissions(permissions);
*/
Now checking for permission is really simple
manager.hasPermission(Manifest.permission.ACCESS_FINE_LOCATION)
And requesting for permissions too
// Using an access code fixed in the library
manager.requestPermissions();
// Using a custom access code, for more control
manager.requestPermissions(SOME_REQUEST_CODE);
It will automatically detect which permissions are already allowed, and will request the missing permissions.
To handle a response, the procedure is same as that in the usual case. You override the onRequestPermissionsResult
listener.
Copyright 2016 Bijoy Singh Kochar
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Apache 2.0 License
Copyright (C) 2014,2015,2016 Xiaoke Zhang
Copyright (C) 2011 The Android Open Source Project
Apache 2.0 License
https://github.com/dlew/joda-time-android/blob/master/LICENSE
Apache 2.0 License
Copyright 2011-2015 Sergey Tarasevich