Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Click Listener on a text view in side a viewFlipper in Android?

In my sample news application I have a view Flipper that display news title, you can see XML code of the ViewFlipper below.

<ViewFlipper android:id="@+id/pushFlipper"
        android:layout_width="fill_parent"
        android:layout_height="25dp"
        android:background="#90333333"
        android:flipInterval="4000"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="10dip"
        >

    </ViewFlipper>

Dynamically I add TextViews to this ViewFlipper in order to display the news title in animated form. Here is the code where I add TextViews to the Flipper.

Now, the issue I am facing is how to set listener on the dynamic TextViews, that I could display the appropriate news descriptionin new Activity of the title that has been displayed on the textview. Please help me in this respect, your help would be greatly appreciated.

pushFlipper = ((ViewFlipper) this.findViewById(R.id.pushFlipper));
            pushFlipper.startFlipping();
try
            {
                RSSdata = new RSSHandler();
                for (int i = 2; i < RSSdata.getTitle().size(); i++)
                {
                    TextView tvNewsRSS = new TextView(this);
                    tvNewsRSS.setText(RSSdata.getTitle().get(i).toString());
                    tvNewsRSS.setTextColor(Color.parseColor("#FFFFFF"));
                    tvNewsRSS.setTextSize(14);
                    tvNewsRSS.setGravity(Gravity.CENTER_VERTICAL);

                    pushFlipper.addView(tvNewsRSS);

                }     
            }
            catch(Exception e)
            {
            }
like image 517
user1703737 Avatar asked Dec 31 '25 10:12

user1703737


1 Answers

One of the solution can be getting newsId on click of item from textview tag and then using this newsId in next activity to fetch and show appropriate news.

Code:

try
    {

        for (int i = 0; i < 10; i++)
        {
            TextView tvNewsRSS = new TextView(this);
            tvNewsRSS.setText(RSSdata.getTitle().get(i));
            tvNewsRSS.setTextColor(Color.parseColor("#FFFFFF"));
            tvNewsRSS.setTextSize(14);
            tvNewsRSS.setTag(RSSdata.getId().get(i));
            tvNewsRSS.setGravity(Gravity.CENTER_VERTICAL);
            tvNewsRSS.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    String newsId = v.getTag().toString();

                    // Pass this newsId to next activity via intent putExtra.

                }
            });
            pushFlipper.addView(tvNewsRSS);

        }     
    }
    catch(Exception e)
    {
    }
like image 108
Sourab Sharma Avatar answered Jan 02 '26 02:01

Sourab Sharma