android - How to load a Custom Java View class to a RelativeLayout programmatically -


i want load custom views within relativelayout don't know how. code i've tried doesn't work hence know how correctly?

xml

<?xml version="1.0" encoding="utf-8"?> <scrollview xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="wrap_content">      <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:paddingleft="@dimen/activity_horizontal_margin"         android:paddingright="@dimen/activity_horizontal_margin"         android:paddingtop="@dimen/activity_vertical_margin"         android:paddingbottom="@dimen/activity_vertical_margin">          <textview             android:id="@+id/textview0"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:gravity="center"             android:layout_marginbottom="20dp"             style="@android:style/textappearance.medium" />          <textview             android:id="@+id/textview1"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:gravity="center"             android:layout_marginbottom="2dp"             style="@android:style/textappearance.medium"             android:layout_below="@id/textview0" />         <view             android:id="@+id/drawing0"             android:layout_width="match_parent"             android:layout_height="40dp"             android:layout_below="@id/textview1" />         <view             android:id="@+id/drawing1"                             android:layout_width="match_parent"             android:layout_height="40dp"             android:layout_margintop="2dp"             android:layout_below="@id/drawing0" />         <textview             android:id="@+id/textview2"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             style="@android:style/textappearance.large"             android:layout_below="@id/drawing1" />         <textview             android:id="@+id/textview3"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             style="@android:style/textappearance.large"             android:layout_marginstart="10dp"             android:layout_below="@id/drawing1"             android:layout_toendof="@id/textview2" />         <textview             android:id="@+id/textview4"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             style="@android:style/textappearance.medium"             android:layout_below="@id/textview3" />         <textview             android:id="@+id/textview5"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             style="@android:style/textappearance.medium"             android:layout_marginbottom="30dp"             android:layout_below="@id/textview4" />     </relativelayout>  </scrollview> 

shapes.java

public class shapes extends android.support.v4.app.fragment {      public shapes() {     }      @override     public view oncreateview(layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) {          return inflater.inflate(r.layout.shapes, container, false);     }      @override     public void onactivitycreated(@nullable bundle savedinstancestate) {         view v = getview();         assert v != null;          //blue shape drawing         view cv0 = (view)v.findviewbyid(r.id.drawing0);         v.addview(new blueshape(getactivity()));          //green shape drawing         view cv1 = (view)v.findviewbyid(r.id.drawing1);         v.addview(new greenshape(getactivity()));          super.onactivitycreated(savedinstancestate);     } } 

blueshape.java

public class blueshape extends view {     private final paint mblue = new paint();      ...  } 

greenshape.java

public class greenshape extends view {     private final paint mgreen = new paint();      ...  } 

refer https://developer.android.com/guide/topics/ui/custom-components.html

specifically under

modifying existing view type

  1. use custom component

you can change xml in relativelayout

<view...  

to

<your.package.greenshape... 

if not know beforehand view going be, change

<framelayout... instead of <view... 

and add child (greenshape or blueshape) programmatically frame layout


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -