1

For a better UI, I thought of using TextViews in place of Buttons. When a TextView is clicked, the next page of the application should appear. I have defined onClick functions in the XML file, but it's not working.

Here is the XML file.

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="16dp"
    android:onClick="serve"
    android:text="@string/Service"
    android:textSize="20dp" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="87dp"
    android:onClick="complain"
    android:text="@string/Comp"
    android:textSize="20dp" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView2"
    android:layout_below="@+id/textView2"
    android:layout_marginTop="121dp"
    android:onClick="feed"
    android:text="@string/Feedback"
    android:textSize="20dp" />

Java file:

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    public void serve(View v)
    {
        Intent i=new Intent();
        i.setClass(this,Second.class);
        startActivity(i);
    }

    public void complain(View v)
    {
        Intent in=new Intent();
        in.setClass(this,Third.class);
        startActivity(in);
    }

    public void feed(View v)
    {
        Intent inn=new Intent();
        inn.setClass(this,Fourth.class);
        startActivity(inn);
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shreya
  • 39
  • 1
  • 4
  • 9
  • Yes, but what do you want to display on "the next page"? Where is this code? – Sam Aug 23 '12 at 16:23
  • thats the secondary thing...what matters is this xml file n even the d java file... I have used appropriate functions like serve, complain and feed in the java file..onClick will go to these java files :) – Shreya Aug 23 '12 at 16:31
  • Can you be more specific? Not working doesn't tell me much. Is it throwing an error or not responding to touch events or... – bytebender Aug 23 '12 at 16:41
  • i get all the textviews on the screen but they are not responding to the touch. it doesnt show any error... – Shreya Aug 23 '12 at 16:42
  • See my answer... Don't see any code in your activity that tells the textViews to respond to touch events. – bytebender Aug 23 '12 at 16:50
  • @bytebender yeah but how it will understand which page it has to go to?? like nothing is mentioned in the code about the next java file.. – Shreya Aug 23 '12 at 17:02
  • @bytebender yeah my bad...am sorry...anyway thanks :) i used sam's method n it worked :) thanks a lot for ur help too:) – Shreya Aug 23 '12 at 17:09

3 Answers3

9

A TextView is not clickable by default so the onClick action can never be called... Simply add: android:clickable="true" to each of your TextViews:

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="16dp"
    android:clickable="true"
    android:onClick="serve"
    android:text="@string/Service"
    android:textSize="20dp" />

Buttons and many other Views expect user interaction so they are already clickable, but not a TextView.


Also since your onClick() methods perform the same basic action, consider a generic method instead:

public void click(View v) {
    Intent intent;
    switch(v.getId()) {
    case R.id.serve: // R.id.textView1
        intent = new Intent(this, Second.class);
        break;
    case R.id.complain: // R.id.textView2
        intent = new Intent(this, Third.class);
        break;
    case R.id.feed: // R.id.textView3
        intent = new Intent();
    }
    startActivity(intent);
}

You need to modify your id and onClick attributes in each TextView, like so:

<TextView
    android:id="@+id/serve"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="16dp"
    android:clickable="true"
    android:onClick="click"
    android:text="@string/Service"
    android:textSize="20dp" />
Sam
  • 86,580
  • 20
  • 181
  • 179
0

Make sure you have the following methods (with matching signatures) in your Activity. They should each take a parameter that is a View.

public void serve(View v) { }
public void complain(View v) { }
public void feed(View v) { }
wsanville
  • 37,158
  • 8
  • 76
  • 101
0

Your activity (based on your code doesn't have any onClickListeners() You need to obtain a reference to your textViews and then add an event handler and then inside your event handler is where you would call serve(), complain(), feed() See this SO question. Here is an example of changes in your activity class.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    TextView textView1 = (TextView) findViewById(R.id.TextView1);
    textView1.setOnClickListener(new View.OnClickListener() {

        @Override
        protected void onClick(View view) {

            serve(view);
        }

    });

    //Repeat for each text view...
}

Each TextView will have to have the following to attributes set in xml:

android:onClick="onClick"
android:clickable="true"

Edit:

I still don't see where you Activity implements OnclickListener if you are going to do it that way...

public class MainActivity extends Activity implements OnclickListener {


    protected void onClick(View view) {
        // borrowed from @Sam
        Intent intent;

        switch(v.getId()) {
            case R.id.serve: // R.id.textView1
                intent = new Intent(this, Second.class);
                break;
            case R.id.complain: // R.id.textView2
                intent = new Intent(this, Third.class);
                break;
            case R.id.feed: // R.id.textView3
                intent = new Intent();
            }

            startActivity(intent);
    }

}
Community
  • 1
  • 1
bytebender
  • 7,371
  • 2
  • 31
  • 54
  • 1
    The OP's `OnClickListeners` are called `serve`, `complain` and `feed` and are clearly present in the code posted in the question. The issue is purely that `TextView` isn't clickable by default - Sam already answered that. – Squonk Aug 23 '12 at 16:54
  • need not use intent??public void serve(View v) { Intent i=new Intent(); i.setClass(this,Second.class); startActivity(i); } – Shreya Aug 23 '12 at 16:56
  • 1
    @Shreya : All that should be needed is to add `android:clickable="true"` to your `TextViews` and your existing code should work. Sam already answered that in his answer. – Squonk Aug 23 '12 at 17:00