user1726619
user1726619

Reputation: 941

How to get the name of textview included in linear layout in OnClickListener?

I am adding textviews dynamically to a linear layout and want to get the name of the textview clicked in OnClickListener of linear layout.This is the code:

m_lvSideIndex = (LinearLayout)ShowTheContacts1.this.findViewById(R.id.sideIndex);
TextView l_tempText = null;

for(int l_a = 0;l_a < m_arrayOfAlphabets.length;l_a++)
{
    l_tempText = new TextView(ShowTheContacts1.this);
    l_tempText.setGravity(Gravity.CENTER);
    l_tempText.setTextSize(15);
    l_tempText.setTextColor(getResources().getColor(R.color.black));
    LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1);
    l_tempText.setLayoutParams(params);;
    l_tempText.setText(m_arrayOfAlphabets[l_a]);
    m_lvSideIndex.addView(l_tempText);
    m_lvSideIndex.setTag(l_a);
}

m_lvSideIndex.setOnClickListener(new OnClickListener() 
{               
    @Override
    public void onClick(View v) 
    {
        String l_itemSelected = (String)v.toString();  //Want to get the name of textview selected here
});

Please help me.Thanks in advance.

Upvotes: 2

Views: 1951

Answers (4)

Bhavdip Sagar
Bhavdip Sagar

Reputation: 1971

((TextView)v.findviewbyTag(R.id.label)).getText();

I hope this work

Upvotes: 1

You can do with it the help of getTag()

first setTag() the value i.e TextName

m_lvSideIndex.setTag(m_arrayOfAlphabets[l_a]);
m_lvSideIndex.setTag(l_a, R.id.sideIndex);

and get the value via getTag()

m_lvSideIndex.setOnClickListener(new OnClickListener() 
{               
    @Override
    public void onClick(View v) 
    {
        String l_itemSelected = (String)v.getTag(); 
        Integer l_position = (Integer)v.getTag(R.id.sideIndex);   
});

Upvotes: 1

Haris ur Rehman
Haris ur Rehman

Reputation: 2663

OnClickListener works on TextView. Make sure you set Clickable property of TextView to true.

Upvotes: 1

Snicolas
Snicolas

Reputation: 38168

Add your click listener to each text view, you will then receive the view as parameter in onClick.

Upvotes: 1

Related Questions