Question

enter image description hereI have a string generator function in my Android app. I want the string generator function to be called every time there is a "tap" or "touch". The string generator function will then change the string in a text label. I don't have any errors of any kind, but when I test the app, both on Android emulator and on a physical device, the string in the text label isn't changing, which means the string generator function is not being called.

Here's what my code looks like:

public class MainActivity extends Activity{

    //member variables
    private ExcuseGenerator mExcuseGenerator = new ExcuseGenerator();
    private ImageView mMrExcuse;
    private TextView mExcuse;
    private RelativeLayout rl;

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


        mMrExcuse = (ImageView) findViewById(R.id.imageView1);
        mExcuse = (TextView) findViewById(R.id.textView1);
        rl = (RelativeLayout) findViewById(R.id.rl1);

        rl.setOnTouchListener(
                new View.OnTouchListener(){
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        if(event.getAction() == MotionEvent.ACTION_UP){

                            //here is the string generator function
                            excuseGenerator();
                            return true;
                        }
                        return false;
                    }
                }
                );
    }

NOTE: I want the taps/touches to be detected anywhere on the screen, that is why I am using the relative layout for the OnTouchListener.

EDIT: screen shot was added to show XML file.

EDIT 2: Here is my excuseGenerator function:

private void excuseGenerator(){
        String excuse = mExcuseGenerator.getExcuse();
        mExcuse.setText(excuse); //mExcuse is a text label.
    }

Here is the getExcuse function from the class mExcuseGenerator:

    /*
     * I want to create a "real random experience".
     * This function will have 2 lists. Every time an excuse is generated, 
     * it is deleted from its original array and then put into a temporary array
     * so that the same excuses are not to be seen over and over again. 
     * when the original array is emptied, it will be reassigned the set of excuses
     * and the temporary array will be emptied again. 
    */
    public String getExcuse(){

        if(stringArray.isEmpty()){
            stringArray = Arrays.asList(mExcuseList);
            tempHolder = null;
        }
        String excuse;
        Random randomGenerator =  new Random();

        // get the position of the element from the array
        int randomElement = randomGenerator.nextInt(stringArray.size());
        excuse = stringArray.get(randomElement);

        tempHolder.add(stringArray.get(randomElement));
        stringArray.remove(stringArray.get(randomElement));



        //return the element 
        return excuse;
    }
Was it helpful?

Solution

If you want to enable touchListener anywhere on the Screen then use method OnTouchEvent(MotionEvent event) like this.

public class MainActivity extends Activity {

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


@Override
public boolean onTouchEvent(MotionEvent event)
{
    switch(event.getAction() & MotionEvent.ACTION_MASK)
{
    case MotionEvent.ACTION_DOWN : //Do something
                                    break;
    case MotionEvent.ACTION_UP : //Do something
                                    break;
    }
    return true;
}
@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;
}

Now you don't have to detect touchEvents for particular view.

OTHER TIPS

Please try to return true when ACTION_DOWN (http://developer.android.com/reference/android/view/MotionEvent.html#ACTION_DOWN) event occurs. This is the event that is first fired, and if you return false in that case, all further events won't be triggered if I remember that right.

add

android:clickable="true"

to your RelativeLayout in XML of your Layout.

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/clicks"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
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=".MainActivity" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

</RelativeLayout>

and in your java code, MainActivity.java

package com.test.testing;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            RelativeLayout click = (RelativeLayout) findViewById(R.id.clicks);

            click.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                            Toast.makeText(getApplicationContext(), "Testing", Toast.LENGTH_SHORT).show();
                    }
            });
    }

    @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;
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top