I want to create a custom view a thing like a power switch ( a switch that switches between ON and OFF). When I have started to implement it I faced 3 constructors for View class:
public CusatomView(Context context) {
super(context);
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
Now my question is: Which one of these constructors I should complete it to retrieve my own XML attribute (for instance: textOn and textOff)?
And what is the role of each?
Ideally, you should do your stuff in a separate method and call this from all three constructors, because you never know which of the constructor will be called. Here are the roles:
CusatomView(Context context) creates a new view with no attributes initialized.
CustomView(Context context, AttributeSet attrs) is invoked when you set attributes like layout_height or layout_width in your layout.xml
CustomView(Context context, AttributeSet attrs, int defStyle) is used when you set styles to your view.
You should create another funciton init and call it in all.
public CusatomView(Context context) {
super(context);
init();
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
The thing is any of this constructors can be used to instantiate your custom view. As in when you create a view in java code you just provide context and when it is created from xml attrs is also supplied.
Related
i created simple widget as custom checkBox as this below code, in this code setting custom font work fine but i can't check or uncheck CheckBox
public class CustomFontCheckBox extends AppCompatCheckBox {
public CustomFontCheckBox(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setTypeface(FontManager.getInstance(getContext()).loadFont("fonts/my_font.ttf"));
}
public CustomFontCheckBox(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
#SuppressWarnings("unused")
private void internalInit(Context context, AttributeSet attrs) {
}
}
xml layout:
<com.myapp.test.Widgets.CustomFontCheckBox
android:id="#+id/ch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/sickness_depth_marginRight"
android:button="#null"
android:buttonTint="#color/colorAccent"
android:drawableRight="?android:attr/listChoiceIndicatorMultiple"
android:text="#string/diabetes"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/body_text_color"/>
Well I think you are messing with its constructor and its style use it like this
public class CustomFontCheckBox extends AppCompatCheckBox {
public CustomFontCheckBox(Context context) {
super(context);
init();
}
public CustomFontCheckBox(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CustomFontCheckBox(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init(){
//set your typeface here.
// setTypeface("");
}
}
I am facing one exception in my Custom Linear layout. Please see the sample code below.
public class ParentView extends LinearLayout {
private Menu mMenu;
public DontPressWithParentView(Context context, AttributeSet attrs, Menu menu) {
super(context, attrs);
mMenu = menu;
}
}
When I am using this layout in xml like below
com.android.ParentView
android:id="#+id/call_icon"
android:layout_width="91dip"
android:layout_height="83dip"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="22dip"
android:gravity="center" >
</com.android.hParentView>
It is giving me xml inflate exception. Please help me.
You have to implement the following constructors in your Viewcode:
public ParentView(Context context) {
super(context);
}
public ParentView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ParentView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#TargetApi (Build.VERSION_CODES.LOLLIPOP)
public ParentView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
Furthermore please check if your package declaration in your xml is correct:
com.android.ParentView
It sounds a bit weird to me.
I need every instance of a custom Button to use a FrameLayout as it's layout. How do I do this from my custom Button class, as to avoid having to wrap every xml defined CustomButton in a FrameLayout?
public CustomButton(Context context) {
super(context);
init(context);
}
public CustomButton(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public CustomButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
private void init(Context context) {
//this doesn't work...
FrameLayout layout = new FrameLayout(context);
inflate(context, R.layout.frame_layout, null);
}
just put your FrameLayout arount your Custom button in frame_layout and later in other xml you can put it in as and this view will be as it is in frame_layout
I wonder if there is a way to change the font of all the text views in an android application in single time ? By all the text views i mean programmatically or dynamically created text views and the text views created separately using XML layout files (Drag n drop)?
I know i can create a new theme with different required fonts and use it . But i can only see the theme applies to dynamically created text views within the program but not the one's in XML layout .
Can you please let me know if there is any solution for this or the only option is to change the font of each text view manually.
The easiest way is to extend the TextView widget:
public class FontTextView extends TextView {
private String mTypefacePath;
public FontTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setAttrs(context, attrs, defStyle);
init(context);
}
public FontTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setAttrs(context, attrs, 0);
init(context);
}
public FontTextView(Context context) {
super(context);
init(context);
}
private void setAttrs(Context context, AttributeSet attrs, int defStyle) {
if (isInEditMode())
return;
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FontTextView, defStyle, 0);
mTypefacePath = a.getString(R.styleable.FontTextView_typeface);
a.recycle();
}
private void init(Context context) {
if (!TextUtils.isEmpty(mTypefacePath)) {
try {
setTypeface(Typeface.createFromAsset(context.getAssets(),
mTypefacePath));
} catch (Exception ex) {
// could not create the typeface from path
}
}
}}
You also need to define your typeface attribute.
Take a look on this to see a useful explanation about.
I'm trying something like this
public class CustomViewSubclass extends HorizontalScrollView{
private LinearLayout layout;
public CustomViewSubclass(Context context) {
this(context,null,0);
}
public CustomViewSubclass(Context context, AttributeSet attrs) {
this(context,attr,0);
}
public CustomViewSubclass(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
layout = new LinearLayout(context);
}
// This is called from the `Activity`
public void startAsyncTask() { // code }
// This method is called in the `onPostExecute()` of an `AsyncTask` subclass
public void doSomething(Context context) {
ImageView image = ImageView(context);
layout.addView(image); // NullPointerException here, layout seems to be null
}
but it seems that layout on doSomething() is null. How is that even possible? I'm initializing it on the constructor... and I never re-initialize it again;
I'm adding my custom view via XML
<com.mypackage.CustomViewSubclass
android:layout_width="wrap_content"
android:layout_width="match_parent" />
Ok I fixed it, it was an stupid mistake made by me:
I used super() on the 3 methods, instead of using this().
public CustomViewSubclass(Context context) {
super(context,null,0);
}
public CustomViewSubclass(Context context, AttributeSet attrs) {
super(context,attr,0);
}
public CustomViewSubclass(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
layout = new LinearLayout(context);
}
Solution:
public CustomViewSubclass(Context context) {
this(context,null,0);
}
public CustomViewSubclass(Context context, AttributeSet attrs) {
this(context,attr,0);
}
public CustomViewSubclass(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
layout = new LinearLayout(context);
}