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.
Related
In my app, I am creating views with extending relative class(For text view i am extending native textview in my java class) because I want to give custom font and don't want to apply font in every individual text view. For this I am using below code
public class LoyaltyTextView extends TextView {
private final static String DROID_SANS = "droid_sans";
public LoyaltyTextView(Context context) {
super(context);
}
public LoyaltyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
parseAttributes(context, attrs);
}
public LoyaltyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
parseAttributes(context, attrs);
}
private void parseAttributes(Context context, AttributeSet attrs) {
TypedArray values = context.obtainStyledAttributes(attrs,R.styleable.LoyaltyTextView);
String typeface = values.getString(R.styleable.LoyaltyTextView_customTypeFace);
if(typeface.equalsIgnoreCase(DROID_SANS)) {
setTypeface(Typeface.createFromAsset(context.getAssets(), "DroidSans.ttf"));
}
values.recycle();
}
}
and i am using above custom control in xml as mentioned below
<?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"
android:background="#drawable/bg_splash">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_above="#+id/tv_logo"
android:layout_centerHorizontal="true"
android:src="#drawable/logo_m" />
<customconstrols.LoyaltyTextView
android:id="#+id/tv_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:text="#string/loyalty_logo"
android:textSize="#dimen/logo_text_size"
android:textStyle="bold"
app:customTypeFace="DROID_SANS" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.customconstrols.LoyaltyTextView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="#string/login"
/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
but when I use simple text views (as native android widget) it works fine but when i use above custom control preview section starts giving me error as mentioned in picture
I have tried number of solution which were mentioned in different post like clear cache/invalidate option and refresh layout and cleaning project and rebuilding it but nothing worked for me. Anything I am missing?
Replace your code as following line in your xml
xmlns:font="http://schemas.android.com/apk/res-auto"
Run your app,It will work fine.
public class MyFontTextView extends TextView {
private Typeface typeface;
public MyFontTextView(Context context) {
super(context);
}
public MyFontTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setCustomFont(context, attrs);
}
public MyFontTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setCustomFont(context, attrs);
}
private void setCustomFont(Context ctx, AttributeSet attrs) {
TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.app);
String customFont = a.getString(R.styleable.app_customFont);
setCustomFont(ctx, customFont);
a.recycle();
}
private boolean setCustomFont(Context ctx, String asset) {
try {
if (typeface == null) {
typeface = Typeface.createFromAsset(ctx.getAssets(),
"fonts/OpenSans-Regular.ttf");
}
} catch (Exception e) {
AndyUtils.generateLog("Exception in component"+e);
return false;
}
setTypeface(typeface);
return true;
}
}
And in values -> attr.xml
<declare-styleable name="app">
<attr name="customFont" format="string" />
</declare-styleable>
Try this
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
What is correct way to extend EditText?
The problem is following:
I have empty application from template with two EditText:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="one"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="two"/>
</LinearLayout>
It works fine:
Then I create my custom view from EditText:
public class CuteEditText extends EditText {
public CuteEditText(Context context) {
this(context, null);
}
public CuteEditText(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CuteEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// some special initialization will be here
}
}
And when I change EditText to my CuteEditText, interface works incorrectly:
The problem is not only with view ot UI. If I type something in first EditText and than touch the second one, nohing happens: input will continue in first.
The same behaviour if I inherite CuteEditText from AppCompatEditText.
What is wrong?
Sources for experiment are available at https://github.com/tseglevskiy/EditTextExperiment
Your construtors are broken. This is how it should look:
public class CuteEditText extends EditText {
public CuteEditText(Context context) {
super(context);
}
public CuteEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CuteEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
}
You don't need the third constructor overload. The first one is for creating the view programmatically and the second one is for creating the view from xml. Those two should be enough for most cases.
public class CuteEditText extends EditText {
public CuteEditText(Context context) {
this(context, null);
}
public CuteEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
}
textView.setText is not working properly in a compound view.
Means Both hello and jack are appearing...
and jack is displayed below layout not appearing if I put some background color,...
Could somebody help... Thanks and regards...
Here is my code...
Layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/chatLayout"
android:layout_width="300dp"
android:layout_height="400dp"
>
<TextView
android:id="#+id/chatNamed"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Hello"
android:textColor="#ffffff"
android:textSize="20dp"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
/>
</RelativeLayout>
CompoundView.java
public class ChatCompoundView extends RelativeLayout {
private static TextView zoneName;
public ChatCompoundView(Context context) {
this(context, null);
setView();
}
public ChatCompoundView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
setView();
}
public ChatCompoundView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setView();
}
public void setView()
{
inflate(getContext(), R.layout.chat_layout, this);
zoneName = (TextView) findViewById(R.id.chatNamed);
}
zoneName.setText("jack");
}
try this..
LayoutInflater mInflater;
public ChatCompoundView (Context context) {
super(context);
mInflater = LayoutInflater.from(context);
setView();
}
setView():
public void setView(){
mInflater.inflate(R.layout.custom_view, this, true);
TextView zoneName = (TextView) findViewById(R.id.chatNamed);
zoneName .setText("jack");
}
Yeah Now I got the Answer... Actually my constructors are called with this(context),.. and I need to call super(context)...
Code Changed to
public ChatCompoundView(Context context) {
super(context);
setView();
}
public ChatCompoundView(Context context, AttributeSet attrs) {
super(context, attrs);
setView();
}
public ChatCompoundView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setView();
}
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.