Today I am going to show you that how
you can create Book Shelf View in android. I research soo many and
finally i reached to one best and simple way to create shelf View in
Android.
In this example i have implemented the
Custom Shelf View which will create a shelf. The list of the books
will come in the form of JSON response and by parsing it show the
details of books in shelfview along with its thumbnails. I wrote a
class that extends AdapterViewto create
a customized GridViewthat I called
"shelf view".
Below is the code of ShelView:
BookShelfView.java
public class BookshelfView extends GridView {
private Bitmap background;
public BookshelfView(Context context) {
super(context);
init();
}
public BookshelfView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public BookshelfView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
protected void init() {
//Set the background image of the ShelfView panel.
background = BitmapFactory.decodeResource(getResources(), R.drawable.shelf_panel_new);
}
//Draw a background in the screen and create multiple panels using height & width.
@Override
protected void dispatchDraw(Canvas canvas) {
int top = getChildCount() > 0 ? getChildAt(0).getTop() : 0;
for (int y = top; y < getHeight(); y += background.getHeight()) {
for (int x = 0; x < getWidth(); x += background.getWidth()) {
canvas.drawBitmap(background, x, y, null);
}
}
super.dispatchDraw(canvas);
}
}
In the demo please change the Url according to your needs to get the list of books.
Graphic designers aren't programmers and sometimes don't know how to properly prepare graphic assets for developers. This simple cheatsheet should help them to do their job better, and to simplify developers' lives.
A Nine-patch drawable is a stretchable bitmap image, which Android will automatically resize to accommodate the contents of the view in which you have placed it as the background, e.g. nine-patch background for button, which must stretch to accommodate strings of various lengths. The rules for nine-patch image are following:
Standard PNG image with alpha
Filename suffix is ".9.png", e.g. "btn_login_normal.9.png"
Image has an extra 1 pixel wide border, used to define the stretchable/static/padding areas
Stretchable sections are indicated by 1 px wide black line(s) in the left and top part of the border
Static sections are indicated by fully transparent or white pixels
Padding area (optional) is indicated by 1 px wide black line in the right and bottom part of the border
Use color primarily for emphasis. Blue is the standard accent color in Android's color palette. Note that red and green may be indistinguishable to color-blind users. Primary colors are as follows:
File names must contain only lowercase a-z, 0-9, or _.
Drawables for the specific views (ListView, TextView, EditText, ProgressBar, CheckBox etc.) should be named like this views keeping the naming rules, e.g. drawable for CheckBox should be named "checkbox_on_bg.png".
Asset Type
Prefix
Example
Action bar
ab_
ab_stacked.9.png
Button
btn_
btn_send_pressed.9.png
Dialog
dialog_
dialog_top.9.png
Divider
divider_
divider_horizontal.9.png
Icon
ic_
ic_star.png
Menu
menu_
menu_submenu_bg.9.png
Notification
notification_
notification_bg.9.png
Tabs
tab_
tab_pressed.9.png
Sources and useful links: naming conventions taken from the
Android SDK
One drawable must have the same file name for all screen densities (ldpi, mdpi, hdpi etc.) and all these files must be organized according to density into the following directories. Here's the resources directory structure for drawables:
Once again I am back with the article which will guide for managing android device's Basic keys i.e. HOME,MENU,BACK,SETTINGS,SEARCH keys which comes common in most of the devices. And sometimes its needed to manage this buttons click events in our application.
There are various Android Application where we need to control the android bottom key panel. This bottom key panel is organised by five keys
1. Back 2. Menu 3. Home 4. Search 5. Settings
If we take any example like online payment Application then we can’t allow a user to press the back button in the middle of payment. So we have to override the back press event and managed the click event.
There are many cases where we provide certain action of the application like action bar menu will be appeared when menu button user will pressed.
This key panel button events are identified by android in following ways
1. Back – KEYCODE_BACK 2. Menu -KEYCODE_MENU 3. Home – KEYCODE_HOME 4. Search – KEYCODE_SEARCH 5. Settings – KEYCODE_SETTINGS
For handling this keys we need to override this method of the Activity
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//Do Code Here
// If want to block just return false
return false;
}
if (keyCode == KeyEvent.KEYCODE_MENU) {
//Do Code Here
// If want to block just return false
return false;
}
if (keyCode == KeyEvent.KEYCODE_HOME) {
//Do Code Here
// If want to block just return false
return false;
}
if (keyCode == KeyEvent.KEYCODE_SEARCH) {
//Do Code Here
// If want to block just return false
return false;
}
if (keyCode == KeyEvent.KEYCODE_SETTINGS) {
//Do Code Here
// If want to block just return false
return false;
}
return super.onKeyDown(keyCode, event);
}
From Android 4.0 (ICS) onward KEYCODE_HOME has been deprecated. As Android believed that it is wrong to give all the option blocked for user. User can pressed the Home button to navigate to other application. But android keep the current application as it is state in the background. So that user can back to the previous application.
Below i am showing code to manage HOME_KEY in android.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tvInfo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hi! This is the testing of override home button" />
<ToggleButton
android:id="@+id/btnLock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="UnLocked"
android:textOn="Locked" />
</LinearLayout>
There are another instance user can accidentally press back button and exit the application. In that case we have to give alert to the user before quit the application. For that we need to identify for the last activity in the stack. For that we need to identify and checked for the last activity form the stack.
private boolean isLastActivity() {
final ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
final List tasksInfo = am.getRunningTasks(1024);
final String ourAppPackageName = getPackageName();
RunningTaskInfo taskInfo;
final int size = tasksInfo.size();
for (int i = 0; i < size; i++) {
taskInfo = tasksInfo.get(i);
if (ourAppPackageName
.equals(taskInfo.baseActivity.getPackageName())) {
return taskInfo.numActivities == 1;
}
}
return false;
}
Today i am going to show you the tutorial of drawing or creating your own signature or simply draw/paint a line using finger with different colors and save it as image. You will be able to draw,clear,save your signature in this tutorial. You will be able to draw a picture as well.
Here i am going to show you the steps to create finger touch view.
Step 1: Create Custom View
The first thing we need to do is to create CustomView by creating a class and extends View in it.
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
public class Signature extends View {
public Signature(Context context, AttributeSet attrs)
{
super(context, attrs);
}
}
This view you can add in your Activity or you can create separate class called Signature . You can add it inside of a layout and, if you wish to expand on this example, you could add some buttons for colors, etc…
onTouch method is called when a touch event is dispatched to a view. This allows a listeners to get chance to respond before the target view.
public boolean onTouch(MotionEvent event)
event -is object of MotionEvent containing full information about event
In the code above, is first store the X and Y coordinates of the event (the touch or the moving of your finger) in variables. We then put in a switch statement to identify the type of event that was fired. In this case we need to create a new starting point for the path if the event is of type MotionEvent.ACTION_DOWN, which means we placed our finger on the screen, and connect points on MotionEvent.ACTION_MOVE which is when we moved our finger.
Next, We save the drawing into sdcard as an image.
In Android GUI is always an important part of any application, because ordinary users don't know and don't care about what's behind the scene; they want something easy to work with and now a days attractive GUI is a must for most applications.
Although making an appealing and innovative interface needs something more than just programming skills and knowledge, every programmer should know how to customize different GUI components within whatever framework and environment they are working.When designing GUIs, most of the times you want to change the appearance of buttons, input Fields, menus etc..
Android Selectors have been provided to solve all these kind of problems, they enable us to decide what to show and how to show based on different states of each components.
For example. You can tell a button to have black background color with red text color when it is in pressed state or whatever else.
So today i am going to show you how you can create selectors for Buttons with single color,two color and three color of layers.
Step 1: Create Project and add resources.
First of all create a simple android project in your workspace. After creating project Prepare/select any two images for Button states and put it into res/drawable folder. 1. "btn_selector_img.png" -Default image of Button 2. "btn_selector_hv_img.png" - Display when the Button is Pressed.
Step 2: Create xml resources for the different types of selectors in res/drawable folder
1. Simple Image selector for showing images for pressed and unpressed states
image_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/btn_selector_hv_img" android:state_pressed="true"/>
<item android:drawable="@drawable/btn_selector_img"/>
</selector>
2.Simple Color selector to show different colors on Button when pressed or unpressed.
color_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/yellow" android:state_pressed="true"/>
<item android:drawable="@color/pink"/>
</selector>
2.Simple Color selector to show different colors on Button when pressed or unpressed.
You must have understood from the title only what i am going to share with you now. I think you must have checked the Tinder App on play store. Tinder app is a social application which will allow you to find the people nearby and chat with them.
This application contains the nice control of like/dislike the people by swipe right or left. That animated control is already available ready for the iOS Tinder animation, but for android its not available as far as my knowledge.
So i have tried to create similar kind of control for android and that is what today i am going to share with you.
Here are the screenshots of the view which i have developed.
Today i am going to show you that how you can customize Dialog in Android using custom layout file.
Dialog box is mainly a popup or we can say a prompt that opens in front of the current activity to perform some operation or functionality. You can use a dialog box to ask the user to confirm an action, notify the user of an event, or prompt the user for additional information. Because dialog boxes disrupt the flow of the UI, you should use them only as a last resort. In most cases, you should integrate confirmation, feedback, and prompts directly into your app.
Sometimes in our applications we want to alert the user for an event and/or ask user about taking a decision. For this purpose AlertDialog class can be used, where a message and one, two or three buttons are displayed in a popup window.
When a dialog box opens the current activity by which we open the dialog box goes to the background and the dialog box comes in the foreground. After performing the operation on the dialog box we dismiss the dialog box and the background activity comes back to the foreground.
To create custom dialog first of all create simple Android application in eclipse.
Create a project with the following details:
ProjectName: CustomDialog
PackageName:com.demo.customdialog
ActivityName: CustomDialogActivity
In your CustomDialogActivitywrite the code as below:
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Toast;
import com.wli.framework.model.ClearActivityObjectListner;
import com.wli.framework.view.ActivityHelper;
public class CustomDialogActivity extends Activity implements OnClickListener
{
private static final int SIMPLE_DIALOG = 0;
private static final int BORDER_DIALOG = 1;
private static final int ROUNDE_CORNER_DIALOG = 2;
private static final int ROUNDE_CORNER_BORDER_DIALOG = 3;
private Dialog m_dialog; //Dialog instance.
private Button m_btnSimpleDialog, m_btnBorderDialog, m_btnRoundeCornerDialog, m_btnRoundeCornerBorderDialog;
private ScrollView m_svMain;
private LinearLayout m_llMain;
@Override
protected void onCreate(Bundle p_savedInstanceState)
{
super.onCreate(p_savedInstanceState);
setContentView(R.layout.maindialog_layout);
m_btnSimpleDialog = (Button) findViewById(R.id.dlbtnSimpleDialog);
m_btnBorderDialog = (Button) findViewById(R.id.dlbtnBorderDialog);
m_btnRoundeCornerDialog = (Button) findViewById(R.id.dlbtnRoundeCornerDialog);
m_btnRoundeCornerBorderDialog = (Button) findViewById(R.id.dlbtnRoundeCornerBorderDialog);
m_svMain = (ScrollView) findViewById(R.id.dlsvMain);
m_btnSimpleDialog.setOnClickListener(this);
m_btnBorderDialog.setOnClickListener(this);
m_btnRoundeCornerDialog.setOnClickListener(this);
m_btnRoundeCornerBorderDialog.setOnClickListener(this);
}
/**
* This is method to show customize dialog.
*
* @param p_index
* - index of customize dialog
*/
public void showCustomDialog(int p_index)
{
m_dialog = new Dialog(CustomDialogActivity.this, R.style.Dialog_No_Border);
m_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
LayoutInflater m_inflater = LayoutInflater.from(CustomDialogActivity.this);
View m_view = m_inflater.inflate(R.layout.custom_dialog, null);
m_llMain = (LinearLayout) m_view.findViewById(R.id.cadllMain);
//Change the background of the dialog according to the layout.
if (p_index == BORDER_DIALOG)
{
m_llMain.setBackgroundResource(R.drawable.btn_style_border);
}
else if (p_index == ROUNDE_CORNER_DIALOG)
{
m_llMain.setBackgroundResource(R.drawable.btn_style_roundcorner);
}
else if (p_index == ROUNDE_CORNER_BORDER_DIALOG)
{
m_llMain.setBackgroundResource(R.drawable.btn_style_border_roundcorner);
}
Button m_btnOk = (Button) m_view.findViewById(R.id.cadbtnOk);
Button m_btnCancel = (Button) m_view.findViewById(R.id.cadbtnCancel);
OnClickListener m_clickListener = new OnClickListener(){
@Override
public void onClick(View p_v)
{
Toast.makeText(CustomDialogActivity.this, "Pressed " + ((Button) p_v).getText(), Toast.LENGTH_SHORT).show();
switch (p_v.getId())
{
case R.id.cadbtnOk:
m_dialog.dismiss();
break;
case R.id.cadbtnCancel:
m_dialog.dismiss();
break;
default:
break;
}
}
};
m_btnOk.setOnClickListener(m_clickListener);
m_btnCancel.setOnClickListener(m_clickListener);
m_dialog.setContentView(m_view);
m_dialog.show();
}
/**
* Common click listener for the buttons.
*/
@Override
public void onClick(View p_v)
{
switch (p_v.getId())
{
case R.id.dlbtnSimpleDialog:
showCustomDialog(SIMPLE_DIALOG);
break;
case R.id.dlbtnBorderDialog:
showCustomDialog(BORDER_DIALOG);
break;
case R.id.dlbtnRoundeCornerDialog:
showCustomDialog(ROUNDE_CORNER_DIALOG);
break;
case R.id.dlbtnRoundeCornerBorderDialog:
showCustomDialog(ROUNDE_CORNER_BORDER_DIALOG);
break;
default:
break;
}
}
public void clearView()
{
m_dialog = null;
m_btnSimpleDialog = null;
m_btnBorderDialog = null;
m_btnRoundeCornerDialog = null;
m_btnRoundeCornerBorderDialog = null;
m_svMain = null;
m_llMain = null;
}
@Override
protected void onDestroy()
{
super.onDestroy();
clearView();
}
}
Here is the layout code. Create the layouts for the activity and dialog as below:
Android uses more than one file system (think of "multiple drives/partitions" when comparing with your computer, while sharing a common base, directory structures might differ between manufacturers.
What is a file system?
A file system (or filesystem) is an abstraction to store, retrieve and update a set of files. The term also identifies the data structures specified by some of those abstractions, which are designed to organize multiple files as a single stream of bytes, and the network protocols specified by some other of those abstractions, which are designed to allow files on a remote machine to be accessed.
Android supports different file systems:
FAT: Used mainly for SDCards. This system is supported by most operating systems (Windows, Linux, Mac, etc.), which is why it's used for these "exchangeables". But it is also quite restricted, which is why it is rarely used somewhere else.
exFAT: sometimes used instead of FAT -- but not generally supported
extfs: The extended file system already has seen several generations. Android devices usually support ext2, ext3, and in most cases also ext4.
YAFFS: A Log structured file system designed for NAND flash, but also used with NOR flash. This was often used for e.g. the /data partition with many devices up to Android 2.x
There are also some pseudo-filesystems used on Unix/Linux system in general and on Android devices in special:
devfs: Virtual file system for managing devices on-the-fly
procfs: Pseudo-file system, used to access kernel information about processes
sysfs: Virtual file system holding information about buses, devices, firmware, filesystems, etc.
Internal storage of Android devices is devided into several partitions, each dedicated to a special "task".
Some of the more important ones include:
/system: This is where the Android system and the pre-installed apps reside. It usually is mounted read-only, so on non-rooted devices you cannot write here (with the exception of applying a system update, e.g. OTA, or flashing a new ROM)
/data: Here the downloaded apps and all app's data are located. Additionally, some system settings are stored here, log files, and more
Your sdcard usually has a single partition only -- but also could be split into multiple partitions.
Below are some of the ways i am showing you to access the various files and folders in android file system: