Sunday, September 23, 2012

Back to basic

Let's look at activity basic - How we can maintain a view as android recycle it's view either when it switch from vertical view to horizontal view or vice-versa.

Within activity at java code level
public class UIACTIVITY extends Activity {

//There are two views we have in this app
int activityLayoutA = R.layout.ViewA;
int activityLayoutB = R.layout.ViewB;
int currentActivity = R.layout.ViewA; //defaultview

//single method to view which view is to be constructed
public void switchAppWideViews(int LayoutView){
setContentView(LayoutView);
if(LayoutView == activityLayoutA){
//LAYOUT ACTIVITY A SETTINGS HERE
}

if(LayoutView == activityLayoutB){
//LAYOUT ACTIVITY B SETTINGS HERE
}
currentActivity = LayoutView; //set a current view

}


@Override
    public void onCreate(Bundle savedInstanceState) {
//Standard create view on switch
           super.onCreate(savedInstanceState);

//savedInstanceState will be null if it is launched for the first time
        if(savedInstanceState==null){       
        switchAppWideViews(authenLayout);
        }else{

//activity already launched

                int cLayout = (Integer) savedInstanceState.get("CURRENTLAYOUT");
        switchAppWideViews(cLayout);


        }


@Override
    protected void onSaveInstanceState(Bundle outState) {
//Save current layout before view is destroyed
    outState.putInt("CURRENTLAYOUT", currentActiveLayout);
    super.onSaveInstanceState(outState);
    }



}

Sunday, September 16, 2012

Statelistdrawables 2

A simpler mechanism to custom statelistdrawables

private Button newbutton(){
Button new = new Button(context);
new.setBackgroundDrawable(custombtnstate());

}



private StateListDrawable custombtnstate(){
StateListDrawable draw = new StateListDrawable();
Drawable bkgd = new Drawable(){

@Override
public void draw(Canvas canvas) {
// TODO Auto-generated method stub
canvas.drawColor(Color.rgb(22, 33, 44)); //Custom color 1
}

@Override
public int getOpacity() {
// TODO Auto-generated method stub
return 0;
}

@Override
public void setAlpha(int alpha) {
// TODO Auto-generated method stub
}

@Override
public void setColorFilter(ColorFilter cf) {
// TODO Auto-generated method stub
}

};

Drawable bkgd1 = new Drawable(){

@Override
public void draw(Canvas canvas) {
// TODO Auto-generated method stub
canvas.drawColor(Color.rgb(150, 180, 190));//Custom color 2
}

@Override
public int getOpacity() {
// TODO Auto-generated method stub
return 0;
}

@Override
public void setAlpha(int alpha) {
// TODO Auto-generated method stub
}

@Override
public void setColorFilter(ColorFilter cf) {
// TODO Auto-generated method stub
}

};
//Essential
 
int stateFocused = android.R.attr.state_focused;
int statePressed = android.R.attr.state_pressed;
int stateSelected = android.R.attr.state_selected;

draw.addState(new int[] {statePressed}, bkgd1);
draw.addState(new int[] {stateSelected},  bkgd1);
draw.addState(new int[] {stateFocused},  bkgd1);
draw.addState(new int[] {-stateFocused,  -statePressed, -stateSelected}, bkgd);


return draw;
}