Create your own RelativeLayout
class, in this case MyRelativeLayout
(which extends the RelativeLayout class).
Then implement the onDraw
feature and add your square.
During the Fragment
's onCreateView
method, return the MyRelativeLayout
.
Read the comments.
public class MainFragment extends Fragment
{
protected AlphaAnimation fadeIn = new AlphaAnimation(0.0f, 1.0f);
public MainFragment() { }
/*
* Create a custom RelativeLayout and implement the inherited 'onDraw'
* method
*/
class MyRelativeLayout extends RelativeLayout
{
public MyRelativeLayout(Context context)
{
super(context);
}
@Override
protected void onDraw(Canvas canvas)
{
/*
* Draw your rectangle
*/
Rect rectangle = new Rect(200, 56, 200, 112);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.BLACK);
canvas.drawRect(rectangle, paint);
super.onDraw(canvas);
}
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
/*
* Create the layout
*/
MyRelativeLayout layout = new MyRelativeLayout(getActivity());
layout.setLayoutParams(
new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
/*
* Inflate your xml view
*/
View rootView =
inflater.inflate(R.layout.main_fragment, container, false);
String fontPath = "fonts/roboto.ttf";
Typeface font =
Typeface.createFromAsset(getActivity().getAssets(), fontPath);
TextView txt1 = (TextView) rootView.findViewById(R.id.headerTextView);
txt1.setTypeface(font);
TextView txt2 = (TextView) rootView.findViewById(R.id.instructions);
txt2.setTypeface(font);
txt1.startAnimation(fadeIn);
txt2.startAnimation(fadeIn);
fadeIn.setDuration(1400);
fadeIn.setFillAfter(true);
/*
* Add your view to the MyRelativeLayout you made, 'layout'
*/
layout.addView(rootView);
/*
* Return the 'layout' instead of just the 'rootView'
*/
return layout;
}
}