Custom view with Typedarray returns null all parameters



I have to create a custom linear layout for repeated use. Linear layout has imageview and textview.


I am following current creating custom layout


Please find my code



MainActivity.java




package com.customview.compoundview;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Toast;
import com.customview.compoundview.R;

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
}

public void onClicked(View view) {
String text = view.getId() == R.id.view1 ? "Background" : "Foreground";
Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
}



}



ColorOptionsView.java




/**
*
*/
package com.customview.compoundview;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.customview.compoundview.R;

/**
* @author gaurav
* Feb 18, 2015
*/
public class ColorOptionsView extends LinearLayout {

ImageButton imageButton_icon;
TextView textView_caption;

public ColorOptionsView(Context context) {
super(context);
}

public ColorOptionsView(Context context, AttributeSet attrs) {
super(context, attrs);
Log.d("ColorOptionsView",""+1);
LayoutInflater inflater = LayoutInflater.from(context);
Log.d("ColorOptionsView",""+2);
inflater.inflate(R.layout.view_color_options, this);
Log.d("ColorOptionsView",""+3);

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CaptionButton);
Log.d("ColorOptionsView",""+4);
Drawable icon;
String caption;
try{
icon = a.getDrawable(R.styleable.CaptionButton_button_icon);
Log.d("ColorOptionsView",""+5);
caption = a.getString(R.styleable.CaptionButton_caption);
Log.d("ColorOptionsView",""+6 +caption );
}
finally
{
a.recycle();
}

setIcon(icon);
setCaption(caption);
}

public void setIcon(Drawable icon) {
try{
Log.d("ColorOptionsView",""+7 +icon );
imageButton_icon.setImageDrawable(icon);
}
finally{
System.out.println("error in seticon "+icon);
}
}

public void setCaption(String caption) {
try{
Log.d("ColorOptionsView",""+8 +caption );
textView_caption.setText(caption);
}
finally
{
System.out.println("error in setCaption"+caption);
}
}
}



fragment_main.xml




<LinearLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
xmlns:myapp="http://ift.tt/1G1DfPV"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:showDividers="middle"
android:divider="?android:attr/listDivider"
tools:context=".MainActivity" >

<com.customview.compoundview.ColorOptionsView

android:id="@+id/view1"
android:layout_width="match_parent"
android:layout_height="40dp"
myapp:caption="background color"
myapp:button_icon="@drawable/handy_man"
/>

<com.customview.compoundview.ColorOptionsView

android:id="@+id/view1"
android:layout_width="match_parent"
android:layout_height="40dp"
myapp:caption="Foreground color"
myapp:button_icon="@drawable/history"
/>


</LinearLayout>



view_color_options.xml




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/linearLayout"
android:orientation="horizontal">

<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/handy_man"/>

<TextView
android:layout_marginLeft="50dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Caption fdafasfs"
android:id="@+id/textView_caption"
android:gravity="center_horizontal"/>

</LinearLayout>



attrs.xml




<?xml version="1.0" encoding="utf-8"?>
<resources>

<declare-styleable name="CaptionButton">
<attr name="button_icon" format="reference" />
<attr name="caption" format="string" />
</declare-styleable>

</resources>


i always get error during setting text and icon due to null value received. This is logcat output



02-18 17:25:26.057: E/AndroidRuntime(16164): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.customview.compoundview/com.customview.compoundview.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class com.customview.compoundview.ColorOptionsView
02-18 17:25:26.057: E/AndroidRuntime(16164): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class com.customview.compoundview.ColorOptionsView
02-18 17:25:26.057: E/AndroidRuntime(16164): at com.customview.compoundview.ColorOptionsView.setIcon(ColorOptionsView.java:61)
02-18 17:25:26.057: E/AndroidRuntime(16164): at com.customview.compoundview.ColorOptionsView.<init>(ColorOptionsView.java:54)


any help will be appreciated. Thanks in advance.


No comments:

Post a Comment