I'm facing problems when creating custom LinearLayout
I have defined my custom layout in xml as follows
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal">
<TextView
android:id="@+id/textViewIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="48sp"
android:text="abc" />
<TextView
android:id="@+id/textViewMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="abc" />
<Button
android:id="@+id/btnAction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp" />
</LinearLayout>
Then I defined some attributes in attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomEmptyListView">
<attr name="visualIcon" format="string"/>
<attr name="message" format="string"/>
<attr name="textColor" format="color"/>
<attr name="iconColor" format="color"/>
<attr name="buttonColor" format="color"/>
<attr name="buttonVisibility" format="boolean" />
<attr name="buttonText" format="string" />
</declare-styleable>
</resources>
Here is the code for my custom class
public class CustomEmptyListView extends LinearLayout {
private TextView txtIcon;
private TextView txtMessage;
private Button btnAction;
public CustomEmptyListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomEmptyListView(Context context) {
super(context);
LayoutInflater.from(context).inflate(R.layout.empty_listview, this);
}
public CustomEmptyListView(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs);
initViews(context, attrs);
}
private void initViews(Context context, AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomEmptyListView, 0, 0);
String iconText = a.getString(R.styleable.CustomEmptyListView_visualIcon);
String message = a.getString(R.styleable.CustomEmptyListView_message);
int buttonColor = a.getColor(R.styleable.CustomEmptyListView_buttonColor, ContextCompat.getColor(context,R.color.blueGreen));
int textColor = a.getColor(R.styleable.CustomEmptyListView_textColor, ContextCompat.getColor(context,R.color.textCard));
int iconColor = a.getColor(R.styleable.CustomEmptyListView_textColor, ContextCompat.getColor(context,R.color.light_grey));
boolean btnVisible = a.getBoolean(R.styleable.CustomEmptyListView_buttonVisibility,true);
String buttonText = a.getString(R.styleable.CustomEmptyListView_buttonText);
a.recycle();
LayoutInflater.from(getContext()).inflate(
R.layout.empty_listview, this);
txtIcon = (TextView)findViewById(R.id.textViewIcon);
txtIcon.setText(iconText);
txtIcon.setTextColor(iconColor);
txtMessage = (TextView)findViewById(R.id.textViewMessage);
txtMessage.setText(message);
txtMessage.setTextColor(textColor);
btnAction = (Button)findViewById(R.id.btnAction);
btnAction.setText(buttonText);
btnAction.setBackgroundColor(buttonColor);
btnAction.setTextColor(ContextCompat.getColor(context,R.color.white));
if(btnVisible){
btnAction.setVisibility(View.GONE);
}else{
btnAction.setVisibility(View.VISIBLE);
}
}
}
And after adding it to my layout xml file as follows
<com.package.views.CustomEmptyListView
android:id="@+id/emptyListView"
android:visibility="visible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
custom:message="@string/no_hay_tareas_registradas"
custom:visualIcon="@string/icon_tasks"
android:layout_centerInParent="true"
custom:buttonText="@string/anadir_tarea">
</com.package.views.CustomEmptyListView>
In my xml editor I only can see a 0 width and height component and nothin shows on runtime. What I'm doing wrong? Thank you in advance!
You must define a correct namespace for your attributes. See this post.