can anyone explain me these codes what are they doing exactly? I could not understand how it is adding two buttons (OK and Cancel). I expect some button creation code like new Button() or something like that It is accessing the buttons with id but there are no button with these ids in any xml file. I can just see the R.id.okcancelbar_ok defination in R file.
Thanks.
Original source : http://developer.android.com/resources/articles/layout-tricks-merge.html
Source Code : http://progx.org/users/Gfx/android/MergeLayout.zip
public class OkCancelBar extends LinearLayout {
public OkCancelBar(Context context, AttributeSet attrs) {
super(context, attrs);
setOrientation(HORIZONTAL);
setGravity(Gravity.CENTER);
setWeightSum(1.0f);
LayoutInflater.from(context).inflate(R.layout.okcancelbar, this, true);
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.OkCancelBar, 0, 0);
String text = array.getString(R.styleable.OkCancelBar_okLabel);
if (text == null) text = "Ok";
((Button) findViewById(R.id.okcancelbar_ok)).setText(text);
text = array.getString(R.styleable.OkCancelBar_cancelLabel);
if (text == null) text = "Cancel";
((Button) findViewById(R.id.okcancelbar_cancel)).setText(text);
array.recycle();
}}
this line inflate “this” with the layout R.layout.okcancelbar
it means that there is a button with id “okcancelbar_ok” in the layout okcancelbar ( inflated earlier)
Next we assigned to it the text “Ok”
same as above, there is a button with id “okcancelbar_cancel” in the layout okcancelbar
So this code do :
1) inflate the view R.layout.okcancelbar
2) get the button (declared in the previous layout) with id “okcancelbar_ok” and set the text to “Ok”
3) idem with the button “okcancelbar_cancel” and text “Cancel”
The “layout/okcancelbar.xml” layout should be like that :
There also should be a “values/attrs.xml” that looks like :
And finally the “layout/okcancelbar_button” should looks like :
Hope it helps