Sunday, June 16, 2013

Android custom button with server checking animation

This is a demo code which polls sever after every 1 seconds and starts blinking if the repose is gretaer then 1 else stops blinking

Sunday, June 9, 2013

Android custom gallery

Click here to get the sample code custom android gallery with swipe gesture

Thursday, May 16, 2013

Android Edit Text With Different shapes


The code for different edit shapes like rounded corner , oval shape etc can be created using shapes attributes

The code for below can be downloaded from here


http://stackoverflow.com/questions/3646415/how-to-create-edittext-with-rounded-corners

http://letustech.wordpress.com/category/android/

Wednesday, May 15, 2013

Android Slide Toggle button

Android Speech Buuble

Android View Pager

Android custom view pager for disabling default switch activity

Step 1.
PageViewActivity.java



import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager.SimpleOnPageChangeListener;
import android.util.Log;
import android.view.View;

public class PageViewActivity extends FragmentActivity {
    MyPageAdapter pageAdapter;
    int pageId=0;
    CustomViewPager pager;
    PageListener pageListener;
    int currentPage=0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      List<Fragment> fragments = getFragments();
      pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
      pager =(CustomViewPager)findViewById(R.id.viewpager);
      pager.setAdapter(pageAdapter);
      pager.setPagingEnabled(false);
     
      pageListener = new PageListener();
      pager.setOnPageChangeListener(pageListener);
     
    }
   
    private List<Fragment> getFragments(){
     List<Fragment> fList = new ArrayList<Fragment>();
   
     fList.add(MyFragment.newInstance("Fragment 1"));
     fList.add(MyFragment.newInstance("Fragment 2"));
     fList.add(MyFragment.newInstance("Fragment 3"));
     return fList;
    }
   
    public void next(View v){
    if(pageId<=2)
    {
    pager.setCurrentItem(pageId);
    }
    pageId++;
   
    }
   
    private class PageListener extends SimpleOnPageChangeListener{
        public void onPageSelected(int position) {
            Log.i("test", "onPageSelected " + position);
            currentPage = position;
         }
        //Called when the scroll state changes.
        public void onPageScrollStateChanged(int state)
        {
       
        }
       
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
        {
        Log.i("test", "onPageScrolled " + position);
            currentPage = position;
        }
    }
   

  }


Step 2.
MyPageAdapter .java


import java.util.List;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

class MyPageAdapter extends FragmentPagerAdapter {
 private List<Fragment> fragments;

 public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
   super(fm);
   this.fragments = fragments;
 }
 @Override
 public Fragment getItem(int position) {
   return this.fragments.get(position);
 }

 @Override
 public int getCount() {
   return this.fragments.size();
 }
}

Step 3.
MyFragment.java


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MyFragment extends Fragment {
public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";

public static final MyFragment newInstance(String message)
{
  MyFragment f = new MyFragment();
  Bundle bdl = new Bundle(1);
  bdl.putString(EXTRA_MESSAGE, message);
  f.setArguments(bdl);
  return f;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
  Bundle savedInstanceState) {
  String message = getArguments().getString(EXTRA_MESSAGE);
  View v = inflater.inflate(R.layout.myfragment_layout, container, false);
  TextView messageTextView = (TextView)v.findViewById(R.id.textView);
  messageTextView.setText(message);

  return v;
}
}

Step 4.
CustomViewPager.java


import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;

public class CustomViewPager extends android.support.v4.view.ViewPager {
private boolean enabled;
public CustomViewPager(Context context, AttributeSet attrs) {
         super(context, attrs);
         this.enabled = true;
    }

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

return false;
}

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

        return false;
}

/**
 * Custom implementation to enable or not swipe :)
 *
 * @param enabled
 *            true to enable swipe, false otherwise.
 */
public void setPagingEnabled(boolean enabled) {
        this.enabled = enabled;
}

}

Step 5.
activity_main.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent" >

   <com.example.viewpagerdemo.CustomViewPager
     android:id="@+id/viewpager"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent" />

</RelativeLayout>

Step 6.
myfragment_layout.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >
  <TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    />
  <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Next"
      android:onClick="next"
      />

</RelativeLayout>

http://architects.dzone.com/articles/android-tutorial-using
http://blog.evoxmusic.fr/content/android-how-disable-viewpager-page-switch

Android GSON parsing


Step 1.
Download the Gson library from http://code.google.com/p/google-gson/ and add it to your java build path in eclipse.

Step 2.
Create a php file and paste the following json data into it

{"as_of":"Thu, 25 Feb 2010 11:30:17 +0000","trends"
:[{"name":"#nowplaying",
"url":"http://search.twitter.com/search?q=%23nowplaying"}
,{"name":"#nothingworsethan",
"url":"http://search.twitter.com/search?q=%23nothingworsethan"},
{"name":"Dubai Mall",
"url":"http://search.twitter.com/search?q=%22Dubai+Mall%22"},
{"name":"iPad Gets",
"url":"http://search.twitter.com/search?q=%22iPad+Gets%22"},
{"name":"#SuperJuniorTrot",
"url":"http://search.twitter.com/search?q=%23SuperJuniorTrot"},
{"name":"Justin Bieber",
"url":"http://search.twitter.com/search?q=%22Justin+Bieber%22"},
{"name":"Click",
"url":"http://search.twitter.com/search?q=Click"},
{"name":"Jaebum",
"url":"http://search.twitter.com/search?q=Jaebum"},
{"name":"#tosavemoney",
"url":"http://search.twitter.com/search?q=%23tosavemoney"},
{"name":"Protection",
"url":"http://search.twitter.com/search?q=Protection"}]}

Step 3.
Create the following two classes to hold the information of the json data

TwitterTrend .java

public class TwitterTrend {
    public String name;
    public String url;
}

TwitterTrends .java

public class TwitterTrends {

    public String as_of;
    public List<TwitterTrend> trends;
    
    public String getAs_Of() {
        return as_of;
    }
    public void setAs_Of(String asOf) {
        as_of = asOf;
    }
    public List<TwitterTrend> getTrends() {
        return trends;
    }
    public void setTrends(List<TwitterTrend> trends) {
        this.trends = trends;
    }
   
}

Step 4. create the main activity 
MainActivity.java


public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
runJSONParser();
}



public InputStream getJSONData(String url){
        DefaultHttpClient httpClient = new DefaultHttpClient();
        URI uri;
        InputStream data = null;
        try {
            uri = new URI(url);
            HttpGet method = new HttpGet(uri);
            HttpResponse response = httpClient.execute(method);
            data = response.getEntity().getContent();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return data;
    }

public void runJSONParser(){
        try{
        Log.i("MY INFO", "Json Parser started..");
        Gson gson = new Gson();
        Reader r = new InputStreamReader(getJSONData("http://10.0.2.2/gsonTest/index.php"));
        Log.e("MY INFO", r.toString());
        TwitterTrends objs = gson.fromJson(r, TwitterTrends.class);
        Log.e("MY INFO", ""+objs.getTrends().size());
        for(TwitterTrend tr : objs.getTrends()){
            Log.e("TRENDS", tr.name + " - " + tr.url);
        }
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }

}

step 5.
Add the following lines into the xml file
 <uses-permission android:name="android.permission.INTERNET" /> 

Step 6.
run the app , u will see the following log


05-15 22:01:54.623: I/MY INFO(1796): Json Parser started..
05-15 22:01:54.733: E/MY INFO(1796): java.io.InputStreamReader@43e55058
05-15 22:01:54.773: E/MY INFO(1796): 10
05-15 22:01:54.773: E/TRENDS(1796): #nowplaying - http://search.twitter.com/search?q=%23nowplaying
05-15 22:01:54.773: E/TRENDS(1796): #nothingworsethan - http://search.twitter.com/search?q=%23nothingworsethan
05-15 22:01:54.773: E/TRENDS(1796): Dubai Mall - http://search.twitter.com/search?q=%22Dubai+Mall%22
05-15 22:01:54.773: E/TRENDS(1796): iPad Gets - http://search.twitter.com/search?q=%22iPad+Gets%22
05-15 22:01:54.773: E/TRENDS(1796): #SuperJuniorTrot - http://search.twitter.com/search?q=%23SuperJuniorTrot
05-15 22:01:54.773: E/TRENDS(1796): Justin Bieber - http://search.twitter.com/search?q=%22Justin+Bieber%22
05-15 22:01:54.773: E/TRENDS(1796): Click - http://search.twitter.com/search?q=Click
05-15 22:01:54.773: E/TRENDS(1796): Jaebum - http://search.twitter.com/search?q=Jaebum
05-15 22:01:54.773: E/TRENDS(1796): #tosavemoney - http://search.twitter.com/search?q=%23tosavemoney
05-15 22:01:54.773: E/TRENDS(1796): Protection - http://search.twitter.com/search?q=Protection

http://www.softwarepassion.com/android-series-parsing-json-data-with-gson/