Saturday, October 6, 2012

View and layoutparams

View Hierarchy

When creating views programmatically, there are rules to remember:

1. Each view has it's own layoutparams settings
2. layoutparams has different classes, depending on the parent of the view

For example, the layoutparams for a textview within a linearlayout and a textview with a relativelayout use different class - linearlayout.layoutparams and relativelayout.layoutparams.

Using incompatible layoutparams for views will crash the graphic creation engine.

Layoutparams are sometimes created automatically or are requires definition.

Example,

TextView tv = new TextView(context);
CurrentLayout.add (tv); //adds view and automatically assign currentlayout layoutparams into tv

or

TextView tv = new TextView(context);
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(100,100); //set width and height to 100
tv.setLayoutParams( lparams);
CurrentLayout.add (tv); //Current Layout MUST be a linearlayout or an exception will be raise

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;
}

Wednesday, May 16, 2012

Making a circle with different reactive arcs


This post will summarize the creation of a circle with arcs

1. Class that contains a single pieItem
2. method that bind multiple pieItems into 1 single layout
3. Insert the layout into your main view as a single element

1.Class that contains a single pieItem
This class will extend Views, and override OnTouchEvent.
2 things will happen to this custom view. First, it will draw a circle arc based on start point on a plane. Second, it intercept a touchevent and check that the angle of the touch to it's center point matches the angle of the circle arc created (boundary = start point, end point = start point + sweep point). If true, we can then react/set an action

2. Method that bind muliple pieItems - this have to be in the UI
Create a relativelayout, and house all pieItems view into the relativelayout without positioning. This will create a illusion that the views are a circle, since they are all position as 1,1 - where the non-arc portion is transparent.