Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9078521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:38:37+00:00 2026-06-16T19:38:37+00:00

I have two RelativeLayouts that I am programmatically adding TextViews to from an ArrayList

  • 0

I have two RelativeLayouts that I am programmatically adding TextViews to from an ArrayList with the intent that, based on the TextView clicked, I can index back into the original ArrayList. For my trivial code example, I’m arbitrarily splitting the items into the two RelativeLayouts.

The code works as expected when the first TextView is clicked. The dialog shows up displaying the correct word and index. However, after closing the dialog, if a different TextView is clicked, the word and index (testWord and testID in my code example) are not updated and only the first TextView’s information is displayed. It seems that onClick is only being called on the first click.

Here is an example Java class (I apologize for any formatting errors, it’s my first time posting here):

package com.test.test;
import java.util.ArrayList;
import java.util.Scanner;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class AndTestActivity extends Activity {
    private final int DIALOG_CASE_ITEM_SELECT=0,CURR_ID=128,P_ID=256,P_HR=512,C_HR=1024;
    private final String TEST="This is a test. Only a test.";
    private int numPast,testID;
    private String testWord;
    private ArrayList<String> test; 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Scanner s=new Scanner(TEST);
        test=new ArrayList<String>();
        while(s.hasNext()){
            test.add(s.next());
        }
        int currID=CURR_ID,pID=P_ID,pHrID=P_HR,cHrID=C_HR,tempLen=3;        
        numPast=0;
        RelativeLayout rlP=(RelativeLayout)findViewById(R.id.caseItemListPastLayout);
        RelativeLayout rlC=(RelativeLayout)findViewById(R.id.caseItemListCurrLayout);
        for(int x=0;x<test.size();x++){                                 
            if(x>tempLen){                      
                addToRL(test.get(x),true,rlC,currID,cHrID);
                currID++;
                cHrID++;
            }else{              
                numPast++;
                    addToRL(test.get(x),false,rlP,pID,pHrID);
                pID++;
                pHrID++;
            }       
        }       
    }

    private void addToRL(String sb, boolean current,RelativeLayout rl,int id,int hrID){
        TextView citv=new TextView(this);
        citv.setText((CharSequence)sb);
        citv.setMovementMethod(new ScrollingMovementMethod());
        citv.setTextSize(15);
        citv.setTextColor(Color.BLACK);
        citv.setGravity(Gravity.CENTER_HORIZONTAL);
        if(current){
            citv.setBackgroundColor(Color.RED);                 
        }else{
            citv.setBackgroundColor(Color.WHITE);
        }
        citv.setPadding(20, 10, 20, 10);
        citv.setWidth((int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 360, getResources().getDisplayMetrics()));
        citv.setId(id);
        RelativeLayout.LayoutParams cilp=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        if(current&&id!=CURR_ID)cilp.addRule( RelativeLayout.BELOW, hrID-1);
        else{
            if(id!=P_ID)cilp.addRule( RelativeLayout.BELOW, hrID-1);
        }
        citv.setLayoutParams(cilp);
        citv.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                TextView tv0=(TextView)v;
                int tvID=tv0.getId()-P_ID;
                if(tvID<0){
                    tvID=(tv0.getId()-CURR_ID)+numPast;
                }

                testID=tvID;
                testWord=test.get(testID);

                showDialog(DIALOG_CASE_ITEM_SELECT); 
            }
        });
        rl.addView(citv);                        
        View hr=new View(this);
        hr.setBackgroundColor(getResources().getColor(R.color.background));
        RelativeLayout.LayoutParams hrlp=new        RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, 5);
        hrlp.addRule(RelativeLayout.BELOW,id);        
        hr.setLayoutParams(hrlp);
        hr.setId(hrID);
        rl.addView(hr);
    }

    protected Dialog onCreateDialog(int id) {
        Dialog dialog;
        AlertDialog.Builder builder;
        switch(id){
        case DIALOG_CASE_ITEM_SELECT:
            builder = new AlertDialog.Builder(this);
            builder.setMessage("word: "+testWord+" index: "+testID)
                   .setCancelable(true)
                   .setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
                       public void onClick(DialogInterface dialog, int id) {
                           dialog.cancel();
                       }
                   });
            dialog = builder.create();
            break;
        default:
            dialog = null;
        }
        return dialog;
    }
}

Here is the associated XML for the layout (nothing special, but I thought I’d include it so there’s a fully working example):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_height="wrap_content" android:layout_width="wrap_content"     android:orientation="vertical"
    android:background="@color/background"
    android:screenOrientation="portrait">       
    <ScrollView 
        android:id="@+id/caseItemCurrScroll"
        android:fillViewport="true"
        android:padding="10dp"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent">
        <RelativeLayout android:id="@+id/caseItemListCurrLayout"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content">
        </RelativeLayout>
    </ScrollView>
    <View
        android:id="@+id/caseItemScrollSpacer"
        android:layout_width="fill_parent"
        android:layout_below="@id/caseItemCurrScroll"
        android:layout_height="5dp"
        android:background="@color/background"/>
    <ScrollView 
        android:id="@+id/caseItemPastScroll"
        android:fillViewport="true"
        android:padding="10dp"
        android:layout_below="@id/caseItemScrollSpacer"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent">
        <RelativeLayout android:id="@+id/caseItemListPastLayout"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content">
        </RelativeLayout>
    </ScrollView>
</RelativeLayout>

That should be a fully “working” example, with the addition of

<color name="background">#3E3E3E</color>

in the strings.xml

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T19:38:38+00:00Added an answer on June 16, 2026 at 7:38 pm

    onCreateDialog() is only called once, while onPrepareDialog() is called each time the Dialog opens.

    Override onPrepareDialog() and call ((AlertDialog) dialog).setMessage() here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say that I have two different types of RelativeLayouts. That is to say
I've got a custom component containing two TextViews that have a custom size-setting method
I have got relative layout, in that i have placed two textviews, one image
I have a RelativeLayout (that I am not married to) and two TextView s.
I have a problem with two Textviews on the same height in a RelativeLayout
I have two classes (MVC view model) which inherits from one abstract base class.
I have two columns say Main and Sub . (they can be of same
I have two relative layouts that are currently displayed one below the other. I
Right now, I have a layout that looks like one of the two images
I have got two images one that i want to display on the top

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.