Friday, October 30, 2015

Android - Creating a Dynamic Grid view

For a sample application, i was trying to include a Dynamic grid view. The git hub project that is in the reference section looked to be 
really useful. noting down few points on this. 

Below are the main things that attracted to look into it. 

- On long tap, the grid enters to edit mode
- when in edit mode, the icons jerk to give an iPhone like animation 
- On tap of the icon, it just does the action as expected. 
- When dragging an icon in edit mode, it adjusts each other nicely
- when dragged by holding, the view scrolls as well. 

Below are the main classes in the project. 

- DynamicGridView => Main View for the Grid view 
- DynamicGridUtils => Utils methods for doing the model level tasks such as reordering, swapping etc. 
- DynamicGridAdapterInterface => interface to be used with grid view 
- AbstractDynamicGridAdapter -> 
- BaseDynamicGridAdapter -> 


Below are the steps to integrate this into a new project. 

1. On the new project right click the project and select new Module 
2. On the new module window, select import existing project 
3. Specify the source directory and add the dynamic grid project from the git hub download 
4. Open module settings, and add the dynamic grid as dependancy to the new app. 


References:

Tuesday, October 20, 2015

Android TimerTask on Background Service

For creating a timer task and running it periodically, say for e.g. making a network ping periodically can be achieved by the below code

class TimeDisplayTimerTask extends TimerTask {
    
    @Override
    public void run() {
        // run on another thread
        mHandler.post(new Runnable() {
            
            @Override
            public void run() {
                
                AppUtils.infoLog("--- Triggering Location Check ---");
                displayTimeInfo();
            }
            
        });
    }
}


if(mTimer != null) {
    mTimer.cancel();
}   // recreate new
mTimer = new Timer();
// schedule task
mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 0, NOTIFY_INTERVAL);

Handler is mainly used to post data from background thread to UI thread. 

References:

Saturday, October 17, 2015

Android Dialog With Radio Buttons

The code is simple 

final CharSequence[] photo = {"Manage Devices","Manage Connections"};
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);

alert.setTitle("Settings For");

alert.setSingleChoiceItems(photo, -1, new
                           DialogInterface.OnClickListener() {
                               @Override
                               public void onClick(DialogInterface dialog, int which) {
                                   Log.v(LOG_TAG,"chosen "+which);
                               }
                           });
alert.show();


References:

Android Custom Adapter for Grid View

The aim was to display a grid icon and the label corresponding to it, just like below screenshot

This can be done by giving a layout for each cell and this layout view is returned as a layout defined in this file

A sample layout for this is

"1.0" encoding="utf-8"?
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="5dp"

android:layout_height="64dp"
android:id="@+id/imageView1"
android:layout_width="64dp"
android:src="@drawable/ic_launcher"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"

android:text="TextView"
android:layout_height="wrap_content"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_below="@+id/imageView1"
android:layout_marginTop="2dp"
android:layout_centerHorizontal="true"
android:textSize="18sp"
android:ellipsize="marquee"



In the getView Method of the Adapter class, below will be the code 

public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ViewHolder view;
    LayoutInflater inflator = activity.getLayoutInflater();
    
    if(convertView==null)
    {
        view = new ViewHolder();
        convertView = inflator.inflate(R.layout.gridview_row, null);
        
        view.txtViewTitle = (TextView) convertView.findViewById(R.id.textView1);
        view.imgViewFlag = (ImageView) convertView.findViewById(R.id.imageView1);
        
        convertView.setTag(view);
    }
    else
    {
        view = (ViewHolder) convertView.getTag();
    }
    
    view.txtViewTitle.setText(listCountry.get(position));
    if(itemStates[position] == 0) {
        view.imgViewFlag.setImageResource(listFlagOff.get(position));
    }
    else
    {
        view.imgViewFlag.setImageResource(listFlag.get(position));
    }
    return convertView;
}
}

It is convenient to have tag as object for each view. This way we can associate arbitrary object. Where as in iOS, the tag has to be always int. 


References:

Thursday, October 15, 2015

Hands On With GitHub Repository - Part II

The main purpose of forking a repository is to propose changes and in other words, work together. Below are the steps to do that 

1. Setup the Git 
2. Clone the repository to the local repository 

To clone, we can get the clone url and enter the following in the terminal 

git clone

after cloning, we can see the details of the remote repository like this 

git remote -v 

 git remote -v
origin https://github.com/mrrathish/TestGitProj.git (fetch)
origin https://github.com/mrrathish/TestGitProj.git (push)


in the above, essentially, it like the format below 

origin https:github.com//

Now we need to add upstream repositories so that we can sync up. 

 git remote add upstream https://github.com/mrrathish/TestGitProj.git

After adding the upstream info, the remote -v command will give the details like below 

git remote -v
origin https://github.com/mrrathish/TestGitProj.git (fetch)
origin https://github.com/mrrathish/TestGitProj.git (push)
upstream https://github.com/mrrathish/TestGitProj.git (fetch)
upstream https://github.com/mrrathish/TestGitProj.git (push)

In the above, the original format of upstream is, 

upstream https://github.com//

Now everything is setup for Synching. Below gives info on how to sync the fork 

to sync the online repository with the local one, below are the steps

1. git fetch upstream (this fetches the commits from master into the local branch upstream/master) 
2. git checkout master (checkout fork’s local branch) 
3. git merge upstream/master (merges the changes from upstream/master into the local master. This brings fork’s master in sync with the upstream master)


Note that Synching the fork only updates the local copy of the repository. To update the fork on github, one must Push the changes 

References:

Tuesday, October 13, 2015

Hands on With GitHub repository - Part I

1. Set up A repo

first of all few configurations we need to do for the git repository. 

1. name  => git config —global user.name = “testgitun”
2. email address => git config —global user.email = “testgitun.gmail.com”

Authentication with GitHub from Git 

When connecting to GitHub repository from Git, we need to authenticate with Github using either HTTPS or SSH
If connecting via HTTPS the password can be cached using a Credential Helper. 
If clone with SSH, we must generate SSH keys on each computer we use to pull or push from GitHub. 

2. Create A Repo 

To put project in GitHub, first we need to create a repository for it to live in. 
The Create New repository option is very easily locatable and To create a new repository, just need to provide the name and description of the project. 
Free accounts can only create public repos while the paid accounts can create private repositories. 

When creating a git repo, we have an option to add a README file. The examples in github is explaining few concepts around this. The github page itself gives option to 
edit the content and preview. Once after preview, we can commit. the commit can be into the same master branch or we can create a new branch for this commit and generate a pull request. 

When the pull request is generated, it appears on the github page and one can merge the changes and delete the branch that came in the pull request. 


3. Forking A Repo 

Fork is a copy of a repository. Forking allows us to do free experiments without affecting the original project. 

Below is the usual practice: 

- Fork Some one else’s project 
- Make bug fix
- Submit a pull request 


References:

Monday, October 12, 2015

Android Bluetooth Scanner - Starter Basics

Below are the tasks intended in this sample 

- Enable bluetooth on a device 
- Display a list of paired devices 
- Discover and list nearest Bluetooth devices 
- Connect and send data to another bluetooth device 

There are two kinds of permissions 

1. BLUETOOTH => used to connect, disconnect and transfer data with another bluetooth device 
2. BLUTOOTH_ADMIN => allows to discover new bluetooth device and change device’s bluetooth settings



Application will need to use Bluetooth adapter for interfacing with the Bluetooth. 

BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();

if the Adapter is returned to be null, then the device does not have the Bluetooth feature

Application can get to know if the Bluetooth is enabled or not by using the API isEnabled. If not 
enabled, it can enable it by using an intent provided by Android

if(!btAdapter.isEnabled())
{
Intent enableBT = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBT,REQUEST_BLUETOOTH);
}

A bluetooth device can be in any of the below states from perspective of Mobile Device 

- Unknown 
- paired 
- connected 

Paired devices know each other’s existence and share a link key, which can be used to authenticate, resulting in a 
connection. Devices are automatically paired once encrypted connection is established. 

Connected devices share RFCOMM channel, allowing them to send and receive data. A device may have many paired devices
but it can have only one connected devices at a time. 

References: