CustomView based Android interview questions and answers
When we need to create any custom view in Android, we will extend our class from the View parent class. In this scenario, as we need to create a custom Text view so we will create a subclass of TextView class. And then we can our Custom textview in a layout.xml file.
For example please follow below code where I am trying to create Custom TextView
CustomTextView extends TextView { private String title; private boolean color; title = tarry.getString(R.styleable.CustomTextView_settitle); if(title == null) setText("Custom Message"); else setText("Welcome to the Class"); color = tarry.getBoolean(R.styleable. CustomTextView _setColor, false); if(color == true) setTextColor(Color.RED); }
We need to define our custom textview in \res\layout folder and write the code like as shown below.
<?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" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical"> <com.example.CustomTextView android:layout_width="wrap_content" android:layout_height="wrap_content" app:settitle="Welcome" app:setColor="true" android:layout_marginTop="200dp" android:layout_marginLeft="120dp" android:textStyle="bold" android:textSize="18dp"/> </LinearLayout>
2)
Comments
Post a Comment