Link to home
Start Free TrialLog in
Avatar of jetbet
jetbetFlag for New Zealand

asked on

onClick function for android app

I am attempting to write my first android app but cannot get the onClick method working for a sub form. I am using Android Studio.
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/GridLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:columnCount="3"
    android:rowCount="4"
    android:orientation="horizontal"
    tools:context=".GridLayoutActivity">

    /* Player 1 */
    <TextView android:text="@string/player_1" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <bool android:name="running_1">false</bool>
    <Button
        android:id="@+id/button1"
        android:layout_gravity="left|top"
        android:text="Button"
        android:onClick="updateTimer"/>

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

Open in new window


public class GridActivity extends ActionBarActivity {


 /*   Stopwatch timer = new Stopwatch();*/
    final int REFRESH_RATE = 100;

    public void updateTimer(View view) {
        Resources res = getResources();
        boolean running_1 = res.getBoolean(R.bool.running_1);
        Button p1_button = (Button)findViewById(R.id.button1);
        if (running_1)
        {
            p1_button.setText("Start");
        }
        else
        {
            p1_button.setText("Stop");
        }
    }


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

    }

Open in new window


I am a developer but my main skill sets are C#, Delphi, Perl, XML

Some of the sites I have been to show that this approach will work but it crashes the app every time.
Without the onClick added to the button there is no problem.

Most sites advise to add a onClickListener to the onCreate method, but this does not seem right to me as there will be multiple lines.

What I want to do in the long run is have refactored code that I can call on each line as this app will have stop watch timers on each line.
Avatar of jetbet
jetbet
Flag of New Zealand image

ASKER

basic pseudocode for stage 1 of what I am trying to do

Class PlayerLine
{
Boolean running = false;
TextView name;
Button toggleStartStop;
TextVew timeElapsed;

function onClick()
{
  SetTimer(this)
}
}

SetTimer(playerLine line)
if (line.running)
{
line.button.text = start;
line.running = false;
}
else
{
line.button.text = stop;
line.running = true;
}


At this stage though getting the onClick method would be a winner





}
Avatar of Chris Harte
You need to implement the either onClickListener, which is what the onClick method talks to or an onTouchListener and use the onTouch method. That is how the app talks to the screen.
Avatar of jetbet

ASKER

Thanks for that.

Do you know why this does not work (cast to button crashes app
public void updateTimer(View view) {
        Resources res = getResources();
        Button b = (Button)view;
        int value = (int)b.getTag();

        switch(value) {
            case '1':
                if (running_1 == false) {
                    p1_button.setText("Start");
                    running_1 = true;
                } else {
                    p1_button.setText("Stop");
                    running_1 = false;
                }
                break;
            case '2':
                if (running_1 == false) {
                    p1_button.setText("Start");
                    running_1 = true;
                } else {
                    p1_button.setText("Stop");
                    running_1 = false;
                }
                break;
            default:
                if (running_1 == false) {
                    p1_button.setText("Start");
                    running_1 = true;
                } else {
                    p1_button.setText("Stop");
                    running_1 = false;
                }
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grid);
        Button button= (Button) findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateTimer(v);
            }
        });

    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Chris Harte
Chris Harte
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jetbet

ASKER

package com.example.redkatipo.myfirstapp;

import android.content.Intent;
import android.content.res.Resources;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;



public class GridActivity extends ActionBarActivity implements onClickListener
{


 /*   Stopwatch timer = new Stopwatch();*/
    final int REFRESH_RATE = 100;

    boolean running_1 =false;
    Button p1_button = (Button)findViewById(R.id.button1);
 
    boolean running_2 =false;
    boolean running_3 =false;
    boolean running_4 =false;
    boolean running_5 =false;



    public void updateTimer(View view) {
        Resources res = getResources();
        Button b = (Button)view;
        int value = (int)b.getTag();

        switch(value) {
            case '1':
                if (running_1 == false) {
                    p1_button.setText("Start");
                    running_1 = true;
                } else {
                    p1_button.setText("Stop");
                    running_1 = false;
                }
                break;
            case '2':
                if (running_1 == false) {
                    p1_button.setText("Start");
                    running_1 = true;
                } else {
                    p1_button.setText("Stop");
                    running_1 = false;
                }
                break;
            default:
                if (running_1 == false) {
                    p1_button.setText("Start");
                    running_1 = true;
                } else {
                    p1_button.setText("Stop");
                    running_1 = false;
                }
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grid);
        Button button= (Button) findViewById(R.id.button1);
        
        button.setOnClickListener(this);

    }

    @Override public void onClick(View view) {
updateTimer(view);
    }
    



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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Open in new window


Errors
Adding the implements gives error "cannot resolve symbol 'onClickListener'
The import statement shows as being unused
Moving the onClick function from being nested gives error "Method does not override the method on its superclass."

The one thing that was not working before was getting the .tag value from the view (casting to button failed)
I want to do this so I can use one updateTimer method to deal with each line by using it as a parameter for the sending button.
Avatar of jetbet

ASKER

I was wondering if the issue before may have been not instantiating (creating) the button in the onCreate method. It is just defined in the xml file.
Avatar of jetbet

ASKER

Have got it working. Thanks for your help

package com.example.redkatipo.myfirstapp;

import android.content.Intent;
import android.content.res.Resources;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


public class GridActivity extends Activity implements OnClickListener {


 /*   Stopwatch timer = new Stopwatch();*/
    final int REFRESH_RATE = 100;

    boolean running_1 =false;
    Button button_1;
 
    boolean running_2 =false;
    Button button_2;

    boolean running_3 =false;
    Button button_3;

    boolean running_4 =false;
    Button button_4;





    public void updateTimer(View view) {
        Button b = (Button)view;
        int value = (int)b.getTag();

        switch(value) {
            case 1:
                running_1 = setTimer(button_1, running_1);
                break;
            case 2:
                running_2 = setTimer(button_2, running_2);
                break;
            case 3:
                running_3 = setTimer(button_3, running_3);
                break;
            case 4:
                running_4 = setTimer(button_4, running_4);
                break;
            default:

        }
    }

    private boolean setTimer(Button _button, boolean _boolean)
    {
        if (_boolean == false) {
            _button.setText("Start");
            return true;
        } else {
            _button.setText("Stop");
           return false;
        }
    }


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

        button_1 = (Button) findViewById(R.id.button1);
        button_1.setTag(1);
        button_1.setOnClickListener(this) ;

        button_2 = (Button) findViewById(R.id.button2);
        button_2.setTag(2);
        button_2.setOnClickListener(this) ;

        button_3 = (Button) findViewById(R.id.button3);
        button_3.setTag(3);
        button_3.setOnClickListener(this) ;

        button_4 = (Button) findViewById(R.id.button4);
        button_4.setTag(4);
        button_4.setOnClickListener(this) ;

    }


    public void onClick(View v) {
        updateTimer(v);
    }



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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Open in new window