In my activity I have a custom made control. In onCreate I inflate the activity's view as normal with:
setContentView(R.layout.image_viewer);
Here is my xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<MyCustomImageView
android:id="#+id/tivImage"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
And here is a reduced piece of code for MyCustomTextView:
public class MyCustomImageView extends ImageView
{
Context context;
public MyCustomImageView(Context context)
{
super(context);
sharedConstructing(context);
}
public MyCustomImageView(Context context, AttributeSet attrs)
{
super(context, attrs);
sharedConstructing(context);
}
}
The exception that gets generated reads:
android.view.InflateException: Binary XML file line #7: Error inflating class MyCustomImageView
you need to add the package name:
<com.my.package.MyCustomTextView
android:id="#+id/tivImage"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
You have to create constructors calling the superclass constructors, because they will be called by the inflater:
public class MyView extends View {
public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public MyView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
}
And you have to put the full package name in your XML:
<com.mypackagename.MyView
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
Related
I'm trying to change different EditText properties using data binding but it doesn't seem to be working.
custom_edittext.xml:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="textVar"
type="String"/>
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/descriptionTextEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:ems="12"
android:inputType="textMultiLine"
android:text="#{textVar}"/>
</RelativeLayout>
</layout>
My class extends RelativeLayout
public class CustomEditText extends RelativeLayout {
public CustomEditText(Context context) {
super(context);
init(context, null);
}
public CustomEditText(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
}
and my init:
private void init(Context context, AttributeSet attrs){
View.inflate(context, R.layout.custom_edittext, this);
CustomEdittextBinding stomEdittextBinding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.custom_edittext, null, false);
Drawable drawableButton = context.getResources().getDrawable(R.drawable.ic_action_name);//context.getResources().getDrawable(R.drawable.ic_action_name);
customEdittextBinding.setTextVar("new text"); //it should change the text, shouldn't it?
}
my main activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
customEditText = findViewById(R.id.custom);
}
and inside the activity_main.xml I've added my CustomEditText like this but there is an error - The following classes could not be found.
<com.example.xxx.app.CustomEditText
android:id="#+id/custom"
android:layout_width="344dp"
android:layout_height="50dp"
android:paddingLeft="100dp"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp"></com.example.xxx.app.CustomEditText >
Ok, I've solved this problem by adding addView(customEdittextBinding.getRoot()); after instantiating CustomEdittextBinding.
I created a custom view with the following code :
public class TestView extends LinearLayout {
public TestView(Context context) {
this(context, null);
}
public TestView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public TestView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context);
}
private void initView(Context context) {
LayoutInflater.from(context).inflate(R.layout.layout_test, this);
setBackgroundResource(R.color.black);
}
}
layout_test.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"/>
It works well, background is black.
But once I added background attribute to xml, setBackgroundResource
not work anymore(layout_test.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"/>
activity_layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TestView
android:id="#+id/testView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
github.com/MummyDing/TestView
try this.
public class TestView extends LinearLayout {
public TestView(Context context) {
this(context, null);
setBackgroundResource(R.color.black);
}
public TestView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public TestView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context);
}
private void initView(Context context) {
LayoutInflater.from(context).inflate(R.layout.layout_test, this);
}
}
try to use with the help of context
LayoutInflater.from(context).inflate(R.layout.layout_test, this).
setBackgroundColor(context.getResources().getColor(R.color.colorAccent));
Try using an id like:
LinearLayout lLayout = (LinearLayout) findViewById(R.layout.my_linear_id);
lLayout.setBackgroundColor(context.getResources().getColor(R.color.green_color));
where my_linear_id is the id of you LinearLayout
Here is my code for class test.java
package com.example.testdroid;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.widget.TextView;
public class test extends TextView{
public test(Context context) {
super(context);
// TODO Auto-generated constructor stub
this.setText("Hello World");
this.setTextSize(20f);
this.setTextColor(Color.RED);
}
protected void onDraw (Canvas canvas) {
super.onDraw(canvas);
}
}
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<com.example.testdroid.test
android:id="#+id/test1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button1"
android:layout_alignRight="#+id/button1"
android:layout_marginBottom="68dp"
/>
</RelativeLayout>
Logcat :
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testdroid/com.example.testdroid.MainActivity}: android.view.InflateException: Binary XML file line #25: Error inflating class com.example.testdroid.test
Create a class for textview like as follow:-
public class TextViews extends TextView{
public TextViews (Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public TextViews (Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public TextViews (Context context) {
super(context);
init();
}
private void init() {
if (!isInEditMode()) {
this.setText("Hello World");
this.setTextSize(20f);
this.setTextColor(Color.RED);
}
}
}
This seems like a simple thing, but it does not run. What am I doing wrong?
I'm subclassing ImageView, but if I put it inside XML layouts, it gives:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myappSubclass/com.example.myappSubclass.MyActivity}:
android.view.InflateException: Binary XML file line #21: Error
inflating class com.example.myappSubclass.ImageViewSubclass
Here is my code:
package com.example.myappSubclass;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;
import java.util.jar.Attributes;
/**
* Created by me on 2014-07-11.
*/
public class ImageViewSubclass extends ImageView {
public Context thisContext;
public ImageViewSubclass(Context context, AttributeSet attrs, Context thisContext) {
super(context, attrs);
this.thisContext = thisContext;
}
public ImageViewSubclass(Context context, AttributeSet attrs, int defStyle, Context thisContext) {
super(context, attrs, defStyle);
this.thisContext = thisContext;
}
public ImageViewSubclass(Context context) {
super(context);
this.thisContext = context;
}
public void changeImage(){
this.setImageLevel(0);
}
And inside the XML I have it simply:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, MyActivity"
/>
<Button android:layout_width="match_parent" android:layout_height="50dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:text="Test"
/>
<com.example.myappSubclass.ImageViewSubclass android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<!--<ImageView android:layout_width="fill_parent" android:layout_height="fill_parent"/>-->
</LinearLayout>
Custom views must have predefined constructors to be able to inflate them from an XML layout. You should remove thisContext from all of them.
public ImageViewSubclass(Context context) {
super(context);
...
}
public ImageViewSubclass(Context context, AttributeSet attrs) {
super(context, attrs);
...
}
public ImageViewSubclass(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
...
}
Why did you add that extra parameter anyway? All views have a getContext() method if that's what you needed.
I write a simple extend view class,the code is following:
public class MyView extends View
{
public MyView(Context context)
{
super(context);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
protected void OnDraw(Canvas canvas)
{
super.onDraw(canvas);
}
}
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.main.sufaceview.MyView
android:id="#+id/myview"
android:layout_width="300dp"
android:layout_height="300dp"
android:background="#drawable/tt"/>
</RelativeLayout>
When entry the virtual machine. It shows the breakdown:
why this is? i think the custom view should very easy.I did not add other code,it has the error.
Edit:the logcat displays:
I think you need another constructor, at least, thats what I read in other posts.
public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}