Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sample code which extends View class and using layout xml file

I am new to the Android world.... it will be a great help if someone correct me... what am I doing wrong the below code...

  • Requirement : Need to create a custom View(using xml layout file) so that the same view should be used inside in my application activities. Here i go with the sample code which i am working on,

cutomviewxml.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>

Extended View class ...code...

mycustomTextview.java

public class mycustomTextview extends View {

    private View mView;
    Context mycontext;

    public mycustomTextview(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub

    this.mycontext = context;

    LayoutInflater inflater;
    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mView = inflater.inflate(R.layout.cutomviewxml, null);
    }

Activity main.xml file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


     <com.motorola.mycustomTextview
         android:id="@+id/customtextview"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_below="@+id/textView2"
         android:layout_marginLeft="143dp"
         android:layout_marginTop="69dp"
         android:layout_toRightOf="@+id/textView1" />

</RelativeLayout>

Activity class sample.java ..

public class sample extends Activity{

    private static final String LOG_TAG = "sampleActivity";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(LOG_TAG,"OnCreate of sampleActivity");
        setContentView(R.layout.main);
}
}
like image 254
Satish Bellapu Avatar asked Nov 08 '11 15:11

Satish Bellapu


4 Answers

I think you have done a little mistake in mycustomTextview, when you inflate your layout you have to pass ViewGroup too, you can use this line

mView = inflater.inflate(R.layout.cutomviewxml, this);
like image 145
Ging3r Avatar answered Oct 13 '22 11:10

Ging3r


To get access to views inside of custom view override onFinishInflate() method and use findViewById(). For the problem at hand the solution will be as follows.

public class mycustomTextview extends LinearLayout {

    Context mycontext;
    private TextView textView1;

    public mycustomTextview(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mycontext = context;
    }

    @Override
    protected void onFinishInflate() {
         super.onFinishInflate();
         textView1=(TextView) findViewById(R.id.textView1);
    }
}
like image 28
bohlool.mohammadi Avatar answered Oct 13 '22 10:10

bohlool.mohammadi


Use this in your xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
        <yourpackagename.viewclassname android:id="@+id/myView1" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content"></yourpackagename.viewclassname>
</LinearLayout>
like image 36
Shalini Avatar answered Oct 13 '22 10:10

Shalini


public class ExampleView extends FrameLayout {

        ImageView mImage;

        public ExampleView(Context context) {
            super(context);
            init(context);
        }

        public void init(Context context) {
            View view = inflate(context, R.layout.my_view_layout,this);
            view.findViewById(R.id.image)
        }
}
like image 28
Pablo Cegarra Avatar answered Oct 13 '22 10:10

Pablo Cegarra