Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android EditText blocks its parent's OnClickListener

1) I create a number of controls dynamically. I have a medium-sized RelativeLayout with an EditText that fills most of its parent visible space. The rest is covered by another view and the layout is supposed to slide up on click. The problem is that the layout does not receive onClick event when user clicks on the EditText. I can't use TextView because I will need user to enter some text into it later. I tried this code, without success

private void addTopView() {
    RelativeLayout midView = new RelativeLayout(this);
    ImageView imageView = new ImageView(this);
    imageView.setImageResource(R.drawable.wallet_top);
    imageView.setBackgroundColor(0xffff0000);
    RelativeLayout.LayoutParams lp = new LayoutParams(scale(320), scale(heightTop));
    midView.addView(imageView, lp);
    RelativeLayout topCard = populateTPasses ? addTpass(0) : addCard(0);
    topCard.setOnClickListener(topCardClick);
    midView.addView(topCard);
    lp = new LayoutParams(scale(320), scale(cardTopOffsetY + cardHeight));
    lp.topMargin = scale(upperOffset);
    walletScrollView.addView(midView, lp);
}

private RelativeLayout addCard(int indexVal) {
    String cardName = "Add Credit Card";
    String cardNum = "Max 16 digits";
    String cardCVC = "Max 6 digits";
    boolean switchStatus = NO;
    String cardExpiry = dateFormatter.format(new Date());
    if(indexVal < creditcards.size())
    {
        cardName = creditcards.get(indexVal).name;
        cardNum = String.format("........%s", creditcards.get(indexVal).display);
        cardCVC = "NOT STORED";
        cardExpiry = creditcards.get(indexVal).expiry;
        switchStatus = creditcards.get(indexVal).isDefault;
    }
    LayoutParams lp = new LayoutParams(scale(cardWidth), scale(cardHeight));
    lp.leftMargin = scale(cardTopOffsetX);
    lp.topMargin = scale(cardTopOffsetY);
    RelativeLayout layout = new RelativeLayout(this);
    layout.setLayoutParams(lp);
    layout.setTag(indexVal);
    addCardElements(layout, cardName, cardNum, cardExpiry, cardCVC, switchStatus);
    return layout;
}

private ImageView addCardElements(RelativeLayout whichCard, String cardName, String cardNumber, String cardExpiry, String cardCVC, boolean defaultStatus) {
    whichCard.setBackgroundResource(R.drawable.credit_card_tpay);

    //Add card name
    EditText cardNameField = addCardName(cardName);

    whichCard.addView(cardNameField);
    return null;
}

private EditText addCardName(String cName) {
    EditText cardName = new EditText(this);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(scale(cardWidth - 90), LayoutParams.WRAP_CONTENT);
    params.leftMargin = scale(32 + 50);
    params.topMargin = scale(15);
    cardName.setBackgroundColor(Color.TRANSPARENT);
    cardName.setLayoutParams(params );
    cardName.setTextColor(Color.WHITE);
    if(isLast == NO || (_selectedIndex >= creditcards.size() && creditcards.size() != 0))
        cardName.setText(cName);
    else
    {
        cardName.setHint(cName);
    }
    isLast = NO;
    cardName.setTag(kTag_CardName);
    if(_selectedIndex >= creditcards.size())
        cardName.setEnabled(YES);
    else {
        cardName.setEnabled(NO);
        cardName.setFocusable(NO);
        cardName.setFocusableInTouchMode(NO);
        cardName.setClickable(NO);
        cardName.setBackgroundColor(Color.YELLOW);
    }
    cardName.setGravity(5);
    return cardName;
}

2) This one is solved. When my layout slides up with the help of TranslateAnimation I have to remove it from its parent and to put onto another ViewGroup or else all the user input gets broken. Then it slides back with similar change of parent. In the process I set the layout's OnClickListener to null and back to my listener. The problem is that after that the listener never fires and the layout can only slide once. I wonder what could be causing this.

like image 523
Anton Duzenko Avatar asked Oct 31 '13 09:10

Anton Duzenko


1 Answers

My experience is that a click on EditText does not propagate to it's parent, unlike a click on a TextView. You have to listen to the children themselves.

like image 52
Lee Avatar answered Nov 01 '22 16:11

Lee