Wednesday, March 5, 2014

Set color of Android button, programmatically

Here's a little problem that had Nirmal frustrated. If there is a layout with multiple buttons to switch layouts or views, how can the user be shown which layout he/she/ze is currently viewing? One idea: change the color of the text on the button which corresponds to the current layout. To do that is so easy!

Button whateverButton = (Button) findViewById(R.id.whateverButton);
whateverButton.setTextColor(Color.parseColor("#00a2ff")); //set text color

of course, you will need to import Button and Color if you haven't already, in Eclipse

How to swipe between layouts in Android apps

Is it easy to open a new layout by swiping horizontally? Yes, it is. When you want to drag your fingers across the screen to switch view layouts, just follow this code!

First, import the Android support library. Right click on your project in ecplise, go to Android Tools, then select Add Support Library. If you don't see that, go to your SDK manager under the Window menu, and find and download it! Now we have all the resources we need.

Paste this in your oncreate to set up the horizontal swipe:
   MyPagerAdapter adapter = new MyPagerAdapter();
   ViewPager pager = (ViewPager) findViewById(R.id.pager);
   pager.setAdapter(adapter);
   pager.setCurrentItem(0);
//set the opening screen
      
Add this class somewhere after the oncreate:
 public class MyPagerAdapter extends PagerAdapter {
    @Override
    public int getCount() {
        return 2; //set  number of swipe screens here 
    }
    @Override
    public Object instantiateItem(final View collection, final int position) {
        LayoutInflater inflater = (LayoutInflater collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        int resId = 0;
        switch (position) {
        case 0:
            resId = R.layout.coffee; //set which layout will show on load
            break;
        case 1:
            resId = R.layout.subway; //what layout swiping shows                 break;
        }
        View view = inflater.inflate(resId, null);
        ((ViewPager) collection).addView(view, 0);
        return view;
     }
    @Override
    public void destroyItem(final View arg0, final int arg1, final Object arg2) {
         ((ViewPager) arg0).removeView((View) arg2);
    }
    @Override
    public boolean isViewFromObject(final View arg0, final Object arg1) {
        return arg0 == ((View) arg1);
    }

}

And put this in the default layout, which is actually never seen because it gets replaced with the first swipe screen
    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    </LinearLayout>

Thursday, February 27, 2014

Easiest way to get current location on an android

The newest update of the NY Coffee Map app has a feature to launch navigation to the nearest coffee shop on the map. Android makes location-finding easy. Every device can find its coordinates through GPS  and NETWORK (includes cell tower triangulation and WiFi) It has it's own class "Criteria" to determine which of these is best, taking into account battery, accuracy, whats on, etc.
Here's code (nestled in onCreate) to find the Location we've cleverly named 'devicelocation'

Criteria criteria = new Criteria(); provider = locationManager.getBestProvider(criteria, true); 
Location devicelocation = locationManager.getLastKnownLocation(provider);

n.b. The true in the .getBestProvider tells the method to only select a provider that is currrently enabled.
Once you have that location, getting the latitude an longitude is as easy as requesting it! We save them as doubles called 'currentlat' and 'currentlong'


currentlat = location.getLatitude();
currentlong = location.getLongitude();

And, bingo, you have your latitude and longitude coordinates saved. Note that since we put the Location code in onCreate, we only get a new set of coordinates when the Activity is launched. Adding this method to your code, outside of onCreate obviously, will give you more updates:

@Overrideprotected void onResume() {
  super.onResume();

  if (provider != null){    
    locationManager.requestLocationUpdates(provider, 400, 1, this);
  }

}

@Override 
public void onLocationChanged(Location location) { 
  currentlat = location.getLatitude(); 
  currentlong = location.getLongitude(); 
}

In the next post, I will show how to launch navigation to get to these coordinates!

Thursday, February 13, 2014

The best coffee in Brooklyn and Queens!

In response to the popularity of the original map/app, the entire team at Butterfruit Labs has been investigating the coffee in Brooklyn and Queens (the birthplace of NY's coffee scene may it be argued). There has never been such a rampant, highly-caffeinated, group of geeks. Dozens of shops encountered mysterious interviewers demanding specifics about their coffee. 

Ok, so it's not all of Brooklyn and Queens, but it has the areas with good coffee. A few lines, such as the L and the G, had fierce competition. Other stops (ahem, Woodside and Corona - and come on Barclays Center) had our researchers struggling to find one good cup. Overall, this map should not disappoint, and hopefully, will incite some passionate (yet hiply nonchalant) rage! Without further ado, the map:


Wednesday, February 12, 2014

release date!

Tomorrow morning, Butterfruit Labs will be releasing the newest coffee map: a revision of Manhattan, addition of WiFi symbols, and the much-requested, Brooklyn + Queens. Follow @butterfruitlabs on twitter, check this blog, or watch the nyc subreddit! The app will be updated overnight!

Sunday, February 9, 2014

Lucky Nirms

Nirmal had the very fortunate opportunity of having his dinner last night cooked by done other than Steph, head chef of Steph's Apartment Kitchen. Kofta, baba ganoush, shepherd's salad... oh my Ghandi!

Saturday, February 8, 2014

Map updates

Our coffee map has become very popular, and we've received a ton of suggestions and feedback! And, it's official, the map will be expanding to other boroughs. I also think that WiFi icons will be added. WiFi is a little bit of a paradox for 2014 - there shouldn't be any coffeeshops that can't provide WiFi, but then again, there shouldn't be anyone who needs to rely on a coffee shop to provide it. There are some (looking at you, Juliano) that don't allow laptops at all, but that's a different story. Anyway, stay tuned, I'll post the new stuff right here. You can also follow @rickymikeabono or @butterfruitlabs on twitter, and the NYC subreddit is highly recommended. If you have a favorite spot of your own, please tweeit it, share it below, or email butterfruitlabs@gmail.com