Custom View subclass lose references - android

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);
}

Related

Android custom checkbox for set custom font

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("");
}
}

in custom TextInputLayout, setHint() is not working

I am trying to subclass TextInputLayout, in a CustomTextInputLayout. This is the code:
public class CustomTextInputLayout extends TextInputLayout {
public CustomTextInputLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CustomTextInputLayout(Context context) {
super(context);
}
public View getComponent(){
return this.getChildAt(0);
}
public void setHint(String hint){
super.setHint(hint);
}
}
I am using it to wrap another custom component, with a button and an EditText:
public class ComponentesSeleccion extends RelativeLayout {
private EditText editText;
private Button button;
public ComponentesSeleccion(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public ComponentesSeleccion(Context context) {
super(context);
inicializar();
}
public ComponentesSeleccion(Context context, AttributeSet attrs) {
super(context, attrs);
inicializar();
}
private void inicializar() {
String infService = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li =
(LayoutInflater)getContext().getSystemService(infService);
li.inflate(R.layout.seleccionlayout, this, true);
editText=(EditText)findViewById(R.id.editText);
button=(Button)findViewById(R.id.button2);
}
public Button getButton() {
return button;
}
public EditText getEditText() {
return editText;
}
public void setInputType(int type){
editText.setInputType(type);
}
public void setImagen(Drawable drawable){
button.setBackgroundDrawable(drawable);
}
}
With this, I am not able to do in my code:
CustomTextInputLayout ctil=(CustomTextInputLayout)view.findViewById(R.id.ctil);
ctil.setHint(“a hint");
No exception, no errors, but the hint is not drawn. i can set the normal hint on the EditText, but I want the animated hint of the TextInputLayout.
How could I get it working?
Thank you.

Public Method 'xxx' is never used

I have a class which extends LinearLayout. This class has a method called addSubview and the warning message "Method 'actionXYZ' is never used" shows, but the method have public modifier!
Furthermore, this method does not appear when this class is instantiated in a activity, for example.
Why?
public class dummyLayout extends RelativeLayout {
LayoutInflater mInflater;
RelativeLayout rootItem;
public dummyLayout(Context context) {
super(context);
init(context);
}
public dummyLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public dummyLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context ctx) {
mInflater = LayoutInflater.from(ctx);
rootItem = (RelativeLayout) mInflater.inflate(R.layout.dummyLayout, this, true);
}
public void addSubView(View v) {
rootItem.addView(v);
}
}

Android + Inflate layout within Custom Button

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

Removing extra view created when using a custom view

Using the following code:
public class CustomView extends RelativeLayout {
public CustomView(Context context) {
this(context, null, 0);
}
public CustomView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView();
}
private void initView() {
inflate(getContext(), R.layout.custom_view, this);
}
}
The layout is simple:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/custom_view_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f00"/>
The hierarchyviewer shows the following:
The CustomView hierarchy is useless and I would like to remove it.
Is there a way to create a custom view extending a ViewGroup without adding that additional View?
If your CustomView is already a RelativeLayout in you XML layout you can just delete the RelativeLayout with "#+id/custom_view_id" and use as father the tag
That will merge the children with the CustomView without using an extra RelativeLayout.
The CustomView will setBackgroundColor
public class CustomView extends RelativeLayout {
public CustomView(Context context) {
this(context, null, 0);
}
public CustomView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView();
}
private void initView() {
setBackgroundColor(Color.parseColor("#f00"));
inflate(getContext(), R.layout.custom_view, this);
}}
and the layout xml file will be:
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<!-- children here -->
</merge>

Categories

Resources