package com.example.tutorial;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;
import android.widget.TextView;
/**
* Created by Shiburagi on 26-Jan-15.
*/
public class MainLayout extends RelativeLayout {
/**
* Constructor
*
* @param context
*/
public MainLayout(Context context) {
super(context);
// call init function
init(context, null);
}
public MainLayout(Context context, LayoutInflater inflater) {
super(context);
// call third-party font from assets/fonts folder
Typeface typeface = Typeface.createFromAsset(context.getAssets(),
"fonts/Square Unique Thin.ttf");
// call init function
init(context, typeface);
// call and receive view from the activity_main.xml
// View view = inflater.inflate(R.layout.activity_main, null);
// obtain TextView from activity_main.xml(view) via R.id
// TextView textView = (TextView) view.findViewById(R.id.hello_world_textview);
// set custom font for a view
// textView.setTypeface(typeface);
// add the view into layout
// addView(view);
}
/**
* to initialize component been used in the application
*
* @param context
* @param typeface
*/
private void init(Context context, Typeface typeface) {
// declare textview component
TextView textView = new TextView(context);
// set value of the textview.
textView.setText("Hello World!");
// set custom font for a view
textView.setTypeface(typeface);
// This line to set view's dimension
LayoutParams layoutParams=new
LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
// This line to set view's alignment
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP,TRUE);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
// This line to set top margin, can also use marginTop
// The value in pixel unit
layoutParams.setMargins(0,50,0,0);
//This line to set LayoutParams
textView.setLayoutParams(layoutParams);
// add textview into this layout
addView(textView);
}
}