0

i put click event on this click_screen

<TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:onClick="click_screen" />

another activity's .java file (click_screen)

public void click_screen(View v)
{
    Intent click_screen=new Intent(this, MainActivity.class);
    startActivity(click_screen);
}

MainActivity.java

package com.amcct.amcostapp;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity 
{

    @SuppressLint("NewApi")
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

/*  @Override
    public boolean onTouchEvent(MotionEvent event) 
    {
        if (event.getAction() == MotionEvent.ACTION_DOWN) 
        {
            Intent click_screen=new Intent(this, MainActivity.class);
            startActivity(click_screen);
        }

        return super.onTouchEvent(event);
    }*/

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void click_screen(View v)
    {
        Intent click_screen=new Intent(this, MainActivity.class);
        startActivity(click_screen);
    }
}

===================================================================== .xml when i click anywhere it generates ^^ above click_screen

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:onClick="click_screen"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Click_Page"
    android:background="@drawable/img_1" >

</RelativeLayout>

See this my All Files....

Kartik Shah
  • 143
  • 2
  • 5
  • 14

5 Answers5

0

Try setting onClick for the parent view of your xml instead. Something like.-

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:onClick="click_screen" >

...

</RelativeLayout>

Or just override onTouchEvent in your Activity like this.-

public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        Intent click_screen=new Intent(this, MainActivity.class);
        startActivity(click_screen);
    }

    return super.onTouchEvent(event);
}
ssantos
  • 16,001
  • 7
  • 50
  • 70
  • when i do this my application stopped working :( and my function coding is true ?? and i need another activity when i click on anywhere on screen – Kartik Shah Sep 13 '13 at 17:32
  • Mmh how stopped working? Which is the exception? Have you tried just overriding onTouchEvent? – ssantos Sep 13 '13 at 17:34
  • ssantos thanks for help i dont put any exception in application but when i clik anywhere on screen it stopped working...and i dont know how to override onTouchEvent :( – Kartik Shah Sep 13 '13 at 17:39
  • Ok, forget about `onClick` attribute. Just added an example of how overriding `onTouchEvent` – ssantos Sep 13 '13 at 17:42
  • hay nothing is happen so i added my MainActivity.java and .xml file ^^ – Kartik Shah Sep 13 '13 at 17:54
0

Myabe you can try this code, i make textview.setClickable(true)

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final TextView textView = (TextView)findViewById(R.id.textView1);
    textView.setClickable(true);
    textView.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    startActivity(new Intent(this, MainActivity.class));
}
Jhohannes Purba
  • 576
  • 1
  • 5
  • 16
0

You want to move to a different activity when clicked anywhere on screen?then why are you setting the onclick on that textview?set the onclick attribute on the top relative layout/Linear layout instead of setting on the textview.

khubaib
  • 535
  • 4
  • 12
0

Your code is in MainActivity and through intent you are again trying to go to the MainActivity.class? specify the activity to which you want to go to for eg:

Intent click_screen=new Intent(MainActivity.this, nextActivity.class);
    startActivity(click_screen);
khubaib
  • 535
  • 4
  • 12
0
## Please add clickable property ##
 <TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="Click Me"
    android:clickable="true" 
    android:onClick="click_screen" />
Sino
  • 886
  • 7
  • 21