2014年12月30日 星期二

Android get device ID

import android.provider.Settings.Secure;

private String android_id = Secure.getString(getContext().getContentResolver(),
                                                        Secure.ANDROID_ID); 

2014年12月25日 星期四

android fragment 傳值

public class A   extends Fragment {

B  b= new B();
 Bundle bundle = new Bundle();
 bundle.putString("test","test");
 b.setArguments(bundle);
}



public class  B  extends Fragment {
  Bundle bundle = getArguments();
  String test = bundle.getString("test");
  Log.i("test", test);

}

2014年12月15日 星期一

Android check network status

public class NetworkUtil {
   
    public static int TYPE_WIFI = 1;
    public static int TYPE_MOBILE = 2;
    public static int TYPE_NOT_CONNECTED = 0;
   
   
    public static int getConnectivityStatus(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        if (null != activeNetwork) {
            if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
                return TYPE_WIFI;
           
            if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
                return TYPE_MOBILE;
        }
        return TYPE_NOT_CONNECTED;
    }
   
    public static String getConnectivityStatusString(Context context) {
        int conn = NetworkUtil.getConnectivityStatus(context);
        String status = null;
        if (conn == NetworkUtil.TYPE_WIFI) {
            status = "ok for wifi";
        } else if (conn == NetworkUtil.TYPE_MOBILE) {
            status = "ok for mobule";
        } else if (conn == NetworkUtil.TYPE_NOT_CONNECTED) {
            status = "Not connected to Internet";
        }
        return status;
    }
}

2014年12月11日 星期四

assign padding to Listview item divider line


參考網址:http://stackoverflow.com/questions/14054364/how-to-assign-padding-to-listview-item-divider-line

(list_divider.xml)



<?xml version="1.0" encoding="UTF-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetLeft="50dp"
    android:insetRight="50dp" >

 <shape>
    <solid android:color="@color/orange" />
    <corners android:radius="2.0dip" />
</shape>

</inset>
<ListView
    android:dividerHeight="2dp"
    android:divider="@drawable/list_divider"
    ...
/>

2014年12月10日 星期三

android custom radio button



res/drawable           add      radio_selector.xml


<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:drawable="@drawable/icon_circle"
        android:state_checked="true"
        android:state_pressed="true" />
    <item
        android:drawable="@drawable/icon_select"
        android:state_pressed="true" />
    <item
        android:drawable="@drawable/icon_select"
        android:state_checked="true" />
    <item
        android:drawable="@drawable/icon_circle" />
</selector>







 xml


 <RadioButton
                        android:id="@+id/personal"
                        android:layout_height="35dp"
android:layout_width="35dp"
                        android:layout_marginRight="200dp"
                        android:button="@android:color/transparent"
                        android:background="@drawable/radio_selector"
                        android:textColor="@color/color4"
                        android:textSize="20sp" />


2014年12月8日 星期一

draw a smaller ShapeDrawable inside another shapeDrawable programmatically

  出處:http://stackoverflow.com/questions/13992094/how-to-draw-a-smaller-shapedrawable-inside-another-shapedrawable-programmaticall
ShapeDrawable biggerCircle= new ShapeDrawable( new OvalShape());
        biggerCircle.setIntrinsicHeight( 60 );
        biggerCircle.setIntrinsicWidth( 60);
        biggerCircle.setBounds(new Rect(0, 0, 60, 60));
        biggerCircle.getPaint().setColor(Color.BLUE);

        ShapeDrawable smallerCircle= new ShapeDrawable( new OvalShape());
        smallerCircle.setIntrinsicHeight( 10 );
        smallerCircle.setIntrinsicWidth( 10);
        smallerCircle.setBounds(new Rect(0, 0, 10, 10));
        smallerCircle.getPaint().setColor(Color.BLACK);
        smallerCircle.setPadding(50,50,50,50);
        Drawable[] d = {smallerCircle,biggerCircle};

        LayerDrawable composite1 = new LayerDrawable(d);

        btn.setBackgroundDrawable(composite1);  
enter image description here

2014年12月4日 星期四

Android Facebook like button


// how to use LikeView in Android

before setting , download FBSDK newer version, please google it.

1. create android Project , get PackageName & hashkey & main activty

2. facebook dev page setting by(PackageName & hashkey & main activty) , and join test_user by yourself , get APP_ID.

3. project set APP_ID in manifest like this:

<meta-data
  android:name="com.facebook.sdk.ApplicationId"
  android:value="@string/facebook_app_id" />


4. LikeView setting:

// layout

    <com.facebook.widget.LikeView
   android:id="@+id/like_view"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content">      
</com.facebook.widget.LikeView>

//class

private UiLifecycleHelper uiHelper;
LikeView like_view;
String like_url = "https://www.google.com.tw/";

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

uiHelper = new UiLifecycleHelper(this,mStatusCallback);

like_view = (LikeView)findViewById(R.id.like_view);
like_view.setObjectId(like_url);
like_view.setLikeViewStyle(Style.BOX_COUNT);

}
StatusCallback mStatusCallback = new StatusCallback(){
   @Override
   public void call(Session session, SessionState state, Exception exception) {

   }
};
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   super.onActivityResult(requestCode, resultCode, data);
   uiHelper.onActivityResult(requestCode, resultCode, data, null);
}



PS: change like image method

jump to FBSDK lib_project find the com.facebook.internal.LikeButton => updateForLikeStatus() ,

2014年12月2日 星期二

Android Switch

java

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class MainActivity extends Activity {

private Switch mSwitch;
private TextView mViewShow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewShow = (TextView)findViewById(R.id.tv_show);
mSwitch = (Switch)findViewById(R.id.switch_def);
mSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener(){
       
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
Log.d("dddddd", "hi");
if (isChecked) {
mViewShow.setText(getString(R.string.text_on));
} else {
mViewShow.setText(getString(R.string.text_off));
}
}
});
}

}





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"
    android:orientation="vertical"
>
    <TextView 
        android:id="@+id/tv_show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        
/>

    <Switch
        android:id="@+id/switch_def"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/tv_show"
        android:layout_marginTop="22dp"
        android:textOff="@string/text_off"
        android:textOn="@string/text_on" />
    
</RelativeLayout>





string.xml



<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">SwitchTest</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="text_on">On</string>
    <string name="text_off">Off</string>

</resources>