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();
}
Related
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("");
}
}
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);
}
}
Hi I am trying to add compound view class into my view programmatically. But I don't know how to do that.
I can add directly it in to layout and thats working fine.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainLlt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.example.androidcustomvliveapplication.widget.CustomCardSection
android:id="#+id/cardSection2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</com.example.androidcustomvliveapplication.widget.CustomCardSection>
</LinearLayout>
Above thing working fine. But I want to add above composite layout programmatically.
LIke I want to do like this
mainLlt.add(customcardsection)
Need Some help. Thank you.
public class CustomCardSection extends RelativeLayout
{
int section;
#SuppressLint("NewApi")
public CustomCardSection(Context context, AttributeSet attrs,
int defStyleAttr, int defStyleRes)
{
super(context, attrs, defStyleAttr, defStyleRes);
}
public CustomCardSection(Context context, AttributeSet attrs,
int defStyleAttr)
{
super(context, attrs, defStyleAttr);
}
public CustomCardSection(Context context, AttributeSet attrs)
{
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.custom_card_section, this, true);
}
public CustomCardSection(Context context)
{
super(context);
}
}
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>