1. 2015 is going to be a great year for Android! Why wait??
    Samsung Galaxy Note 5 | Samsung Galaxy S6 | HTC One M9
  2. New Forum Created: Samsung Gear VR!

Desperate for help - Link TextView to another page


  1. RED_

    RED_ Well-Known Member

    This is driving me up the wall, i keep getting different answers from everyone so was hoping I can get a definite answer on here. Looking for the simplest way for me to have the text in my app to link to the another page within my app.

    What's the easiest way this can be done?

    I'm using lots of nested LinearLayouts with a TextView and ImageView in each. I only need my text to be linkable so just TextView.

    Is it easier to have just the image linking? I.e. I can have the ImageView linking instead of my TextView? Whatevers easier really as this is getting tiring now.

    i've been told to use an OnTouchListener, OnClickListener, Android:Clickable etc and nothing has worked.


    Any help would really be appreciated.

    Advertisement
    :
  2. vasanthj

    vasanthj Member

    I'm sure you should be able to achieve this with an OnClickListener. Can you paste the code where you register your listener and call a new activity in the onClick() method?
  3. RED_

    RED_ Well-Known Member

    This is what i was told to add but it didn't work as i got a force close when running it and i had nowhere to put the url for the new page.

    Code (Text):
    LinearLayout layout = (LinearLayout)findViewById(R.id.layoutId);

    layout.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });
  4. RED_

    RED_ Well-Known Member

    Big fat breakthrough with this:

    Code (Text):
    LinearLayout layout = (LinearLayout)findViewById(R.id.layout1);

       layout.setOnClickListener(new View.OnClickListener() {

               @Override
               public void onClick(View v) {
                   // TODO Auto-generated method stub
                   startActivity(new Intent(SellScreen.this, Linktext.class));
               }
           });
    However, i get a force close when clicking the area :(. The code above makes a view clickable, since im using nested LinearLayouts in this case i'm clicking on one of the LinearLayouts

    Maybe i need to add something to the xml files or something into the new class it goes to? Been working 8hours+ on this now and getting nowhere. Manifest maybe..? No idea.
  5. JiMMaR

    JiMMaR Well-Known Member

    don't link to the layout in the code
    have a textview , and link it inside the code then put View.OnClickListener [the same way you did up there]

    I haven't tried it , but it should work

    you can also use this
    ImageButton | Android Developers
    RED_ likes this.
  6. RED_

    RED_ Well-Known Member

    What do you mean?

    As in don't have this bit: "LinearLayout layout = (LinearLayout)findViewById(R.id.layout1);"?

    I have a textview in my main.xml so should i change the above bit to Textview?

    Thing is though that the code that i wrote allows a whole view to be a clickable area, so just like the android market see here: http://img269.imageshack.us/img269/691/09droid3295x440.jpg

    Any chance of that being done? No problem if not, just an option if possible.
  7. RED_

    RED_ Well-Known Member

    Sorry to keep bumping and posting but this is a bit long. I'm trying the textview option as mentioned above, this is what i changed the code too still getting force closes:
    Code (Text):
    TextView text = (TextView)findViewById(R.id.text1);

       text.setOnClickListener(new View.OnClickListener() {

               @Override
               public void onClick(View v) {
                   // TODO Auto-generated method stub
                   startActivity(new Intent(SellScreen.this, Linktext.class));
               }
           });

    This is the code on the page that the onClick links too:
    Code (Text):
    package com.android.sellscreen;

    import android.app.Activity;
    import android.os.Bundle;

    public class Linktext extends Activity {

        public void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               setContentView(R.layout.main);
        }
    }
  8. JamTheMan

    JamTheMan Well-Known Member

    Are you running this from Eclipse and with the emulator? = What error messages do you get with the force close? They are almost always the fastest way to the answer...
    RED_ likes this.
  9. RED_

    RED_ Well-Known Member

    The console doesn't show any errors when a force close takes place. Just the usual dialog in the emulator:

    "The application SellScreen(process com.android.sellscreen) has stopped unexpectedly. Try again later"

    All i know is when i remove the this line: startActivity(new Intent(SellScreen.this, Linktext.class)); i get no force close or errors, however nothing happens when clicking that area/text. So therefore it must be something in that line.
  10. JamTheMan

    JamTheMan Well-Known Member

    Did you try looking at the LogCat instead of the Console?

    The most common error when a startActivity results in a ForceClose is that you forgot to add the activity you want to start to the Manifest.
    RED_ likes this.
  11. RED_

    RED_ Well-Known Member

    I did have a problem with that to be fair. This is my manifest:

    Code (Text):
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.android.sellscreen"
          android:versionCode="1"
          android:versionName="1.0">
        <application android:icon="@drawable/icon" android:label="@string/app_name">
            <activity android:name=".SellScreen"
                      android:label="@string/app_name">  
            <activity android:name=".Linktext"
                      android:label="@string/app_name">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            </activity>
        </application>


    </manifest>
    How is the new activity added properly? Its called Linktext but when i add it under SellScreen (like i have above) and run it, it comes up with a ''no launcher found'' error.
  12. JamTheMan

    JamTheMan Well-Known Member

    Well... Your main (the first one you want to see when you start the app) activity should have the intent filter you have there. Like this:

    Code (Text):

    <activity
      android:name=".MainActivity"
      android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
     

    And any new activity, which should only be used by your app can be added simply like this:

    Code (Text):

    <activity
      android:name=".SecondActivity"
      android:label="@string/second_activity_name"
    >
    </activity>
     
    New activities should be added after ending the main activity, and are not to be nested!
    RED_ likes this.
  13. RED_

    RED_ Well-Known Member

    Works like a charm! Really appreciate your help mate, spent all of last night and the day before trying to get it to work and you pop along and fix it in one go :D Top stuff, just glad its done.
  14. JamTheMan

    JamTheMan Well-Known Member

    ;-)

    Always glad to help
  15. vasanthj

    vasanthj Member

    Excellent work JamTheMan!
Loading...

Share This Page