I'm trying to implement a custom view with textview children added dynamically. But it isn't showing any result. there are no error, just nothing shows up:
My Class:
public class CustomPasscodeEntryView extends LinearLayout {
private Context mContext;
private CustomPasscodeEntryView thisView;
public CustomPasscodeEntryView(Context context) {
super(context);
/* same as below */
}
public CustomPasscodeEntryView(Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
/*same as below*/
}
public CustomPasscodeEntryView(Context context, #Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
thisView = (CustomPasscodeEntryView) inflater.inflate(R.layout.view_passcode_entry,this);
}
public void addDigit(int digit){
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final pinView pin = (pinView) inflater.inflate(R.layout.password_bullet_view,null);
pin.setDigit(digit);
thisView.addView(pin, thisView.getChildCount());
pin.setVisibility(VISIBLE);
}
public void deleteDigit(){
if(getChildCount() > 0)
thisView.removeView(thisView.getChildAt(getChildCount()-1));
}
}
class pinView extends TextView {
int digit;
public pinView(Context context) {
super(context);
}
public pinView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public pinView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setText('•');
}
public void setDigit(int digit) {
this.digit = digit;
this.setText(Integer.toString(digit));
}
}
and my XMLs:
view_passcode_entry:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:animateLayoutChanges="true">
</LinearLayout>
password_bullet_view:
<com.github.lockpin.lollipin.lib.views.pinView
xmlns:android="http://schemas.android.com/apk/res/android"
android:textColor="#android:color/white"
android:text="•"
android:textSize="20sp"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2.5dp"
/>
EDIT: the view in my activity:
<com.github.lockpin.lollipin.lib.views.CustomPasscodeEntryView
android:id="#+id/custom_passcode_entry_view"
android:layout_width="300dp"
android:layout_below="#id/pin_code_text_view"
android:layout_centerHorizontal="true"
android:layout_height="40dp"/>
I cant see what's wrong, and I've followed tutorials online and made a few changes. Am I missing something here? Or is this plainly wrong?
You use wrong view id view_passcode_entry when inflating view, which is instance of LinearLayout, and typecast it to CustomPasscodeEntryView.
You only need to use this when call addView() in addDigit() because CustomPasscodeEntryView is also instance of LinearLayout. So layout view_passcode_entry can be removed because it is not needed anymore.
this.addView(pin, this.getChildCount());
But if you want to add view view_passcode_entry as child of CustomPasscodeEntryView instance then you need to add thisView also
private ViewGroup thisView;
...
thisView = (ViewGroup) inflater.inflate(R.layout.view_passcode_entry, this);
this.addView(thisView, 0);
and then if you want to use view_passcode_entry as parent of pinView then you can use
thisView.addView(pin, thisView.getChildCount());
Is it because of you have already mentioned the height of CustomPasscodeEntryView in the xml (android:layout_height="40dp")?
Can you try wrap_content instead?
Related
I am really worried about the .findViewById() method and its use in a custom compound view creation. I am not sure about the exact place that it is guaranteed that it will never return null.
Let's say I have this custom compound view and that it is added to some .xml like this:
<com.app.path.TwoButtonsView
android:id="#+id/ok_cancel_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
two_buttons_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<merge>
<LinearLayout 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:orientation="horizontal">
<TextView
android:id="#+id/first_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:clickable="true"
android:gravity="center"
android:textAllCaps="true"/>
<TextView
android:id="#+id/second_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:clickable="true"
android:gravity="center"
android:textAllCaps="true"/>
</LinearLayout>
</merge>
TwoButtonsView.java
public class TwoButtonsView extends LinearLayout {
// views
private TextView mFirstButtonView;
private TextView mSecondButtonView;
public TwoButtonsView(Context context) {
super(context);
init(context, null);
}
public TwoButtonsView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public TwoButtonsView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public TwoButtonsView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.two_buttons_view, this);
// retrieve views
mFirstButtonView = (TextView) findViewById(R.id.first_button); // is there a chance it will return null?
mSecondButtonView = (TextView) findViewById(R.id.second_button); // is there a chance it will return null?
}
}
My question is: is there any chance that .findViewById(RESOURCE_ID) will return null right after calling inflater.inflate(R.layout.two_buttons_view, this);, or should I call my init() method on onFinishInflate() callback?
My question is: is there any chance that .findViewById(RESOURCE_ID)
will return null right after calling
inflater.inflate(R.layout.two_buttons_view, this)
No, there is not, as long as the views you are looking for are in the layout and you explicitly added to the view's hierarchy. In the end, findViewById loops on View[], filled up by addView.
A small note about
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.two_buttons_view, this);
ViewGroup has the static method inflate, so you don't need to retrieve the inflater calling getSystemService
I have a Custom View "CustomLayout" subclass of RelativeLayout.
public class CustomLayout extends RelativeLayout implements View.OnClickListener{
private String titleText;
private Context context;
private AttributeSet attrs;
private ImageView iv1,iv2;
private TextView title,tv2;
private TextView textView;
private Button button;
public CustomLayout(Context context) {
this(context, null);
}
public CustomLayout(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
this.attrs = attrs;
init();
}
public CustomLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
this.attrs = attrs;
initAttributes(context,attrs,defStyle);
}
private void initAttributes(Context context, AttributeSet attrs, int defStyleAttr) {
// declare styleable here to programatically set custom view style attributes
final int[] styleable = new int[] {
android.R.attr.src,
android.R.attr.textAppearance,
android.R.attr.text,
android.R.attr.textSize
};
Arrays.sort(styleable);
TypedArray a = context.obtainStyledAttributes(attrs, styleable, defStyleAttr,0);
...
}
I don't know how to set the properties pro-grammatically by passing arguments in the last constructor ,which takes three arguments, from my Activity. I know to do it from layout.xml file as shown in code below.Please help
<com.example.customview.CustomLayout
android:id="#+id/view1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:selectableItemBackground"
app:titleText="HappyTrips Editors"
app:descriptText="#string/content"
app:titleTextColor="#FF0000"
app:descriptTextColor="#0000FF"
app:titleTextSize="8sp"
app:descriptTextSize="6sp"
app:bgColor="#FFFF00"/>
In my ListAdapter
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View result=convertView;
if (convertView==null){
result=inflater.inflate(R.layout.layout_list_items,null);
CustomLayout object = (CustomLayout)result.findViewById(R.id.view1);
How to pass arguments containing AttributeSet in CustomLayout constructor from here
in layout.xml you can user style,it will work or
CustomLayout object=new CustomLayout(new ContextThemeWrapper(this,R.style,customStyle));
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);
}
}
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();
}
I am extending a frame layout
and when in the constructor (after it loaded the view from xml) i call getChildCount()
I get 0. How to fix this ?
public class DisabledFrameLayout extends FrameLayout {
public DisabledFrameLayout(Context context) {
super(context);
init(context, null,0);
}
public DisabledFrameLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context,attrs,defStyle);
}
public DisabledFrameLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init(context,attrs,0);
}
private void init(Context context, AttributeSet attrs, int defStyle) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DisabledFrameLayout, 0, 0);
if(a.hasValue(R.styleable.DisabledFrameLayout_disable_descendants)) {
boolean disable = a.getBoolean(R.styleable.DisabledFrameLayout_disable_descendants, false);
if(disable) {
disableDescendants(this);
}
}
a.recycle();
}
private void disableDescendants(ViewGroup v) {
for (int i=0; i<v.getChildCount();i++) {
if(v.getChildAt(i) instanceof ViewGroup) {
disableDescendants((ViewGroup)v.getChildAt(i));
}
v.setEnabled(false);
v.setFocusable(false);
v.setFocusableInTouchMode(false);
}
}
}
xml
<com.lenabru.DisabledFrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:disable_descendants="true"
>
<include
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
layout="#layout/fragment_p" />
</com.lenabru.DisabledFrameLayout>
In state of constructing your FrameLayout, children are still not fully attached to the view. So, calling getChildCount() will return you 0.
If you want to iterate over child views and update them, do it inside onLayout() or onMeasure().
Refs:
http://blog.denevell.org/android-custom-views-onlayout-onmeasure.html
ViewGroup - check this example code.