[Solved] TextView Listener

Put your problem here if it does not fit any of the other categories.

[Solved] TextView Listener

Postby rohan » Mon Apr 07, 2008 3:42 pm

Hi all,
I want to add a OnClickListener to a TextView . I have tried this code but its not working.

//java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.TEXT;
  2.  
  3.  
  4.  
  5. import android.app.Activity;
  6.  
  7. import android.os.Bundle;
  8.  
  9. import android.view.View;
  10.  
  11. import android.view.View.OnClickListener;
  12.  
  13. import android.widget.TextView;
  14.  
  15. import android.widget.Toast;
  16.  
  17.  
  18.  
  19. public class texttry extends Activity {
  20.  
  21.     /** Called when the activity is first created. */
  22.  
  23.         TextView t;
  24.  
  25.     @Override
  26.  
  27.     public void onCreate(Bundle icicle) {
  28.  
  29.         super.onCreate(icicle);
  30.  
  31.         setContentView(R.layout.main);
  32.  
  33.         t=new TextView(this);
  34.  
  35.         t.setClickable(true);
  36.  
  37.         t=(TextView)findViewById(R.id.TextTRY);
  38.  
  39.         t.setOnClickListener(clickListener);
  40.  
  41.        
  42.  
  43.     }
  44.  
  45.     public OnClickListener clickListener = new OnClickListener() {
  46.  
  47.  
  48.  
  49.         public void onClick(View v) {
  50.  
  51.                 try
  52.  
  53.                 {
  54.  
  55.                         Toast.makeText(texttry.this, "TextClicked", 50000).show();
  56.  
  57.                 }
  58.  
  59.                 catch(Exception e)
  60.  
  61.                 {
  62.  
  63.                         Toast.makeText(texttry.this, "Exception::"+e, 50000).show();
  64.  
  65.                 }
  66.  
  67.         }
  68.  
  69.     };    
  70.  
  71. }
  72.  
  73.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4

//main.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.    android:orientation="vertical"
  6.  
  7.    android:layout_width="fill_parent"
  8.  
  9.    android:layout_height="fill_parent"
  10.  
  11.    >
  12.  
  13. <TextView
  14.  
  15.         android:id="@+id/TextTRY"
  16.  
  17.    android:layout_width="fill_parent"
  18.  
  19.    android:layout_height="wrap_content"
  20.  
  21.    android:text="Hello World, texttry"
  22.  
  23.    android:clickable="true"
  24.  
  25.    />
  26.  
  27. </LinearLayout>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4



Can anyone edit this and send me the working code
Plz Help

Thanks in advance,
Rohan
rohan
Developer
Developer
 
Posts: 34
Joined: Wed Mar 19, 2008 2:26 pm

Top

Postby cadlg » Mon Apr 07, 2008 4:01 pm

Hi rohan,

maybe you could tell us if you get runtime errors or what do you mean with 'is not working'

regards,


cadlg
cadlg
Experienced Developer
Experienced Developer
 
Posts: 84
Joined: Wed Feb 20, 2008 12:33 am
Location: Guatemala

Postby cadlg » Mon Apr 07, 2008 4:13 pm

try adding:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. android:focusable="true"
Parsed in 0.000 seconds, using GeSHi 1.0.8.4

to your TextView in the layout file.

it should work.

regards,

cadlg
cadlg
Experienced Developer
Experienced Developer
 
Posts: 84
Joined: Wed Feb 20, 2008 12:33 am
Location: Guatemala

Postby rohan » Mon Apr 07, 2008 4:26 pm

Thanks cadlg,

its workin with
android:focusable="true"

Thanks
rohan
Developer
Developer
 
Posts: 34
Joined: Wed Mar 19, 2008 2:26 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Exabot [Bot] and 14 guests