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.     private void init(Context context, Typeface typeface) {
  58.  
  59.         // declare textview component
  60.         TextView textView = new TextView(context);
  61.  
  62.         // set value of the textview.
  63.         textView.setText("Hello World!");
  64.  
  65.         // set custom font for a view
  66.         textView.setTypeface(typeface);
  67.  
  68.         // This line to set view's dimension
  69.         LayoutParams layoutParams=new
  70.                 LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
  71.  
  72.         // This line to set view's alignment
  73.         layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP,TRUE);
  74.         layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
  75.  
  76.         // This line to set top margin, can also use marginTop
  77.         // The value in pixel unit
  78.         layoutParams.setMargins(0,50,0,0);
  79.  
  80.         //This line to set LayoutParams
  81.         textView.setLayoutParams(layoutParams);
  82.  
  83.         // add textview into this layout
  84.         addView(textView);
  85.  
  86.     }
  87. }