Showing posts with label android. Show all posts
Showing posts with label android. Show all posts

Sunday, August 10, 2014

Android: Implementing a One-Way Lockable View Pager

While implementing a fun Android app, I came across a scenario where the ViewPager should only be swiped right-to-left. Visiting back should be restricted. Also at some points, I needed to lock the whole view pager so that it cannot be swiped at any direction.

The following code shows how I did it by extending the ViewPager class. Note that I was using the support library.


 
 import android.content.Context;
 import android.support.v4.view.ViewPager;
 import android.util.AttributeSet;
 import android.view.MotionEvent;

 public class OneWayLockableViewPager extends ViewPager {  
   private boolean enabled;  
   private float lastX;  

   public OneWayLockableViewPager(Context context, AttributeSet attrs) {  
     super(context, attrs);  
     this.enabled = true;  
   }  

   @Override  
   public boolean onTouchEvent(MotionEvent event) {  
     if (this.enabled) {  
       boolean lockScroll;  
       switch (event.getAction()) {  
         case MotionEvent.ACTION_MOVE:  
           lockScroll = lastX <= event.getX();  
           break;  
         default:  
           lockScroll = false;  
           lastX = event.getX();  
           break;  
       } 
       return lockScroll ? false : super.onTouchEvent(event);    
     }  
     return false;  
   }  

   @Override  
   public boolean onInterceptTouchEvent(MotionEvent event) {  
     if (this.enabled) {  
       return super.onInterceptTouchEvent(event);  
     }  
     return false;  
   }  

   public void lock() {  
     this.enabled = false;  
   }  

   public void unlock() {  
     this.enabled = true;  
   }  
 }  

It is quite easy to change it if you want to reverse the allowed direction of swiping. Just change the less than or equal sign to a greater than sign in line number 21.

Also when you want to lock down the whole view pager, just call the lock() method and swiping will be disabled.

Monday, February 3, 2014

Android: How to embed Google Map

Well here I am specifically talking about getting it done with Android Studio and with Maps API v2.

First of all you need the Google Play services downloaded using the SDK Manger.


Then add the Google Play services version to your app's manifest file.
<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />
Get your Google Maps API key.

  • Go here: https://code.google.com/apis/console/?noredirect and click 'Cloud Console' link.
  • Register a project there and turn on your Google Maps Android App v2 Service
  • Create a new key under APIs & auth -> Credentials
  • Add the key information to the manifest. See below.
<meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="AasdJHJHKSDHfcFJLbZhndBC_fdsd1KEHk"/>
Add permissions in the manifest to access Internet.
<uses-permission android:name="android.permission.INTERNET"/>
Add a map fragment to your layout. Note that I have used SupportMapFragment. That comes with Android Support Repository.
<fragment 
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          class="com.google.android.gms.maps.SupportMapFragment"/>
Check device compatibility. If Google Play Services are not up-to-date on the phone, this will let the user to update then and there.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
int mapResult = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (mapResult != ConnectionResult.SUCCESS) {
    GooglePlayServicesUtil.getErrorDialog(mapResult, this, 0).show();
} else {
    mapFragment.getMap().animateCamera(CameraUpdateFactory.zoomIn());
}
That's about it. Now the map should be visible on your app. If not, patiently go through the above check-list again and see what you have missed :)

Sunday, December 29, 2013

Connecting your Android phone to your Windows WiFi HotSpot

Now this actually something I thought would be a piece of cake, until the day I actually wanted to do it.

This is how I tried:

I went to 'Network and Sharing Center' and selected 'Set up a Connection or Network' and clicked on 'Set up a wireless ad hoc (computer-to-computer) network'.


Then gave the following details.



Windows said my network is ready to use but it was not showing up on my Android phone.

Googled a bit, and got the bad news. Android does not support WiFi ad hoc networks! Well at least if your phone is not rooted. In my case I was trying with a Nexus running Vanilla Android. Doh!

Again, Google to the rescue. I found out that we can use 'netsh' administrative commands to host a wireless network in the access point mode instead of the ad hoc mode.

So on an administrative command prompt, you type:
netsh wlan set hostednetwork mode=allow ssid=MyAdHocNetwork
key=12345678



Then you start it:
netsh wlan start hostednetwork



Now you will see 'MyAdHocNetwork' in your Android phone's WiFi networks list!

PS: Later I found out this can be done with ease using 'Virtual Router Plus', but it doesn't hurt to get a little grease under the nails once in a while :)

Sunday, July 7, 2013

The Android+Azure Love Story

Once upon a time, there was this beautiful princess named ‘Android’. ‘Azure’ was the most powerful warrior at that time. One sunny day… Ok, let’s cut the crap and get down to business.

If you haven’t tried out Windows Azure yet, well then you’ve been missing out a lot. Windows Azure is amazing! If you’ve been living under a rock for the last couple of years, Azure is Microsoft’s flexible, open & rock solid cloud platform. And by any chance if you haven’t heard of Android, stop reading right now and go kill yourself!

With the introduction of the Mobile Services SDK for Android, Windows Azure supports developers with super easy access to structure data storage, authenticate and push notifications using GCM. Now you are going to learn how to get started.

Prerequisites:

Let’s build a simple ‘Contacts’ app which saves its content on the cloud. Sounds pretty lame, but we gotta start somewhere right?

Step 1: Create a new mobile service in Azure.


Step 2: Go to the DATA tab and create a new table named ‘Contact’. You do not need to create the columns. They are automatically generated when the data is inserted.


Step 3: Create a new Android project.

Step 4: Extract the .jar files in Mobile Services SDK to the project’s lib folder.

Step 5: Create a new class to hold the contact data.


The class name has to be the same name as of the table in mobile service database. Case doesn't matter.


Did you notice the annotations? They specify the mapping between the DTO fields and the back end Azure table columns. They even work with private fields.

Step 6: Modify onCreate method of your main activity to insert a random contact.


Client creation takes the mobile service URL and the application key, which can be found in the portal.

Note: `getRandomContact’ and  ‘showMessage’ method implementation details are omitted for brevity.

Step 7: Run the app couple of times.


Step 8: Browse the Contact table in Azure.


So there you have it. An Android app hookup up with an Azure backend in no time! And that’s not all; you can perform all your CRUD operations with ease. You can even intercept requests to the database using the ‘Script’ tab. This is particularly useful for hooking up push notifications for the GCM clients, like illustrated below.


The only thing you have to do is to enter your API key in the ‘Push’ tab. You can get your API key by registering the app in Google APIs console.

Also you can setup authentication for your app using Google, Facebook, Microsoft or Twitter accounts very easily. All you gotta do is add the credentials under the ‘Identity’ tab.

So, hopefully you figured how easy it is to use Azure mobile services with Android. Remember that you get 10 free mobile services with your free trial, so start experimenting now. I’m sure you are going to love it.

Inspired by a Colombo Mobile Developer Meetup session.

Arduino Line Following Robot: Cracking the shortest/fastest path on a grid with loops

I'm sure you've created a line following robot at some point, if you've been exposed to Arduino (or any other programmable elec...