Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. package com.example.tutorial;
  2.  
  3. import android.content.Context;
  4. import android.graphics.Typeface;
  5. import android.view.LayoutInflater;
  6. import android.widget.RelativeLayout;
  7. import android.widget.TextView;
  8.  
  9.  
  10. /**
  11.  * Created by Shiburagi on 26-Jan-15.
  12.  */
  13. public class MainLayout extends RelativeLayout {
  14.  
  15.     /**
  16.      * Constructor
  17.      *
  18.      * @param context
  19.      */
  20.     public MainLayout(Context context) {
  21.         super(context);
  22.  
  23.         // call init function
  24.         init(context, null);
  25.     }
  26.  
  27.     public MainLayout(Context context, LayoutInflater inflater) {
  28.         super(context);
  29.  
  30.         // call third-party font from assets/fonts folder
  31.         Typeface typeface = Typeface.createFromAsset(context.getAssets(),
  32.                 "fonts/Square Unique Thin.ttf");
  33.  
  34.         // call init function
  35.         init(context, typeface);
  36.  
  37.  
  38.         // call and receive view from the activity_main.xml
  39.         // View view = inflater.inflate(R.layout.activity_main, null);
  40.  
  41.         // obtain TextView from activity_main.xml(view) via R.id
  42.         // TextView textView = (TextView) view.findViewById(R.id.hello_world_textview);
  43.  
  44.         // set custom font for a view
  45.         // textView.setTypeface(typeface);
  46.  
  47.         // add the view into layout
  48.         // addView(view);
  49.     }
  50.  
  51.     /**
  52.      * to initialize component been used in the application
  53.      *
  54.      * @param context
  55.      * @param typeface
  56.      */
  57.     @SuppressWarnings("ResourceType")
  58.     private void init(Context context, Typeface typeface) {
  59.  
  60.          /*
  61.         FIRST VIEW
  62.          */
  63.         // declare textview component
  64.         TextView textView1 = new TextView(context);
  65.  
  66.         // This line set ID for textview1
  67.         textView1.setId(12345);
  68.  
  69.         // set value of the textview1.
  70.         textView1.setText("Hello World!");
  71.  
  72.         // set custom font for a view
  73.         textView1.setTypeface(typeface);
  74.  
  75.         // This line to set view's dimension
  76.         LayoutParams layoutParams1=new
  77.                 LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
  78.  
  79.         // This line to set view's alignment
  80.         layoutParams1.addRule(RelativeLayout.ALIGN_PARENT_TOP, TRUE);
  81.         layoutParams1.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);
  82.  
  83.         // This line to set top margin, can also use marginTop
  84.         // The value in pixel unit
  85.         layoutParams1.setMargins(0, 50, 0, 0);
  86.  
  87.         //This line to set LayoutParams
  88.         textView1.setLayoutParams(layoutParams1);
  89.  
  90.         // add textview1 into this layout
  91.         addView(textView1);
  92.  
  93.         /*
  94.         SECOND VIEW
  95.          */
  96.  
  97.         // declare textview component
  98.         TextView textView2 = new TextView(context);
  99.  
  100.         // set value of the textview2.
  101.         textView2.setText("My first program...");
  102.  
  103.         // set custom font for a view
  104.         textView2.setTypeface(typeface);
  105.  
  106.         // This line to set view's dimension
  107.         LayoutParams layoutParams2=new
  108.                 LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
  109.  
  110.         // This line to set view's alignment, below textView1
  111.         layoutParams2.addRule(RelativeLayout.BELOW, textView1.getId());
  112.         layoutParams2.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);
  113.  
  114.         //This line to set LayoutParams
  115.         textView2.setLayoutParams(layoutParams2);
  116.  
  117.         // add textview2 into this layout
  118.         addView(textView2);
  119.  
  120.     }
  121. }