Code example for RelativeLayout

Methods: addView, setBackgroundColor, setId, setLayoutParams, setPadding

0
    setBackgroundColor(Color.WHITE);
    setLayoutParams(params);
  } 
 
  private void loadHeaderView(Context context) {
    headerView = new RelativeLayout(context);
    headerView.setId(HEADER_VIEW_ID);
     
    LayoutParams params = null;
    if (layoutHorizontally) {
      params = new LayoutParams((int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float)250.0, getResources().getDisplayMetrics()), LayoutParams.MATCH_PARENT);
      params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);
      headerView.setPadding(0, 0, 0, 0);
    } 
    else { 
      params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
      headerView.setPadding(0, 0, 0, (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float)20.0, getResources().getDisplayMetrics()));
    } 
    headerView.setLayoutParams(params);
    headerView.setBackgroundColor(Color.rgb(230, 236, 239));
     
    loadTitleLabel(headerView, context);
    loadVersionLabel(headerView, context);
    loadUpdateButton(headerView, context);
     
    addView(headerView);
  } 
 
  private void loadTitleLabel(RelativeLayout headerView, Context context) {
    TextView textView = new TextView(context);
    textView.setId(NAME_LABEL_ID);
 
    LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    int margin = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float)20.0, getResources().getDisplayMetrics());
    params.setMargins(margin, margin, margin, 0);
    textView.setLayoutParams(params);
    textView.setEllipsize(TruncateAt.END);
    textView.setShadowLayer(1, 0, 1, Color.WHITE);
    textView.setSingleLine(true);
    textView.setTextColor(Color.BLACK);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
    textView.setTypeface(null, Typeface.BOLD);
     
    headerView.addView(textView);
  } 
 
  private void loadVersionLabel(RelativeLayout headerView, Context context) {
    TextView textView = new TextView(context);
    textView.setId(VERSION_LABEL_ID);
 
    LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    int marginSide = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float)20.0, getResources().getDisplayMetrics());
    int marginTop = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float)10.0, getResources().getDisplayMetrics());
    params.setMargins(marginSide, marginTop, marginSide, 0);
    params.addRule(RelativeLayout.BELOW, NAME_LABEL_ID);
    textView.setLayoutParams(params);
    textView.setEllipsize(TruncateAt.END);
    textView.setShadowLayer(1, 0, 1, Color.WHITE);
    textView.setLines(2);
    textView.setLineSpacing(0.0f, 1.1f);
    textView.setTextColor(Color.BLACK);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
    textView.setTypeface(null, Typeface.BOLD);
     
    headerView.addView(textView);
  } 
 
  @SuppressWarnings("deprecation") 
  private void loadUpdateButton(RelativeLayout headerView, Context context) {
    Button button = new Button(context);
    button.setId(UPDATE_BUTTON_ID);
 
    int margin = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float)20.0, getResources().getDisplayMetrics());
    int width = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float)120.0, getResources().getDisplayMetrics());
     
    LayoutParams params = new LayoutParams(width, LayoutParams.WRAP_CONTENT);
    params.setMargins(margin, margin, margin, margin);
    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);
    params.addRule(RelativeLayout.BELOW, VERSION_LABEL_ID);
    button.setLayoutParams(params);
    button.setBackgroundDrawable(getButtonSelector());
    button.setText("Update");
    button.setTextColor(Color.WHITE);
    button.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
     
    headerView.addView(button);
  } 
 
  private Drawable getButtonSelector() {
    StateListDrawable drawable = new StateListDrawable();
    drawable.addState(new int[] {-android.R.attr.state_pressed}, new ColorDrawable(Color.BLACK));
    drawable.addState(new int[] {-android.R.attr.state_pressed, android.R.attr.state_focused}, new ColorDrawable(Color.DKGRAY));
    drawable.addState(new int[] {android.R.attr.state_pressed}, new ColorDrawable(Color.GRAY));
    return drawable;
  } 
 
  @SuppressWarnings("deprecation") 
  private void loadShadow(RelativeLayout headerView, Context context) {
    int height = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float)3.0, getResources().getDisplayMetrics());
    LayoutParams params = null;
     
    ImageView topShadowView = new ImageView(context);
    if (layoutHorizontally) {
      params = new LayoutParams(1, LayoutParams.MATCH_PARENT);
      params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);
      topShadowView.setBackgroundDrawable(new ColorDrawable(Color.BLACK));
    } 
    else { 
      params = new LayoutParams(LayoutParams.MATCH_PARENT, height);
      params.addRule(RelativeLayout.ALIGN_PARENT_TOP, TRUE);
      topShadowView.setBackgroundDrawable(ViewHelper.getGradient());
    } 
    topShadowView.setLayoutParams(params);
     
    headerView.addView(topShadowView);
     
    ImageView bottomShadowView = new ImageView(context);
    params = new LayoutParams(LayoutParams.MATCH_PARENT, height);
    if (layoutHorizontally) {
      params.addRule(RelativeLayout.ALIGN_PARENT_TOP, TRUE);