0

Hi i am trying to make a public function in order to change activities/changes in my android application.

In a external class (Utl.java) I have this code.

public class Utl extends Activity{
public void onBtnClicked(View v) {
    Intent i;
    Integer data=0;

    switch(v.getId()){
        case R.id.btnStart:
            i=new Intent(getApplicationContext(), LevelChoose.class);                   
            startActivity(i);
        break;
        case R.id.btnEasy:
            i=new Intent(getApplicationContext(), PlayGame.class);
            startActivity(i);
        break;
        case R.id.btnMedium:
            i=new Intent(getApplicationContext(), PlayGame.class);
            data=48;
            i.putExtra("extra", data);
            startActivity(i);
        break;
        case R.id.btnHard:
            i=new Intent(getApplicationContext(), PlayGame.class);
            data=96;
            i.putExtra("extra", data);
            startActivity(i);
        break;
        }

    }
}

and in my xml's files i use the on click function of the buttons...

    <Button
    android:id="@+id/btnStart"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="42dp"
    android:minHeight="110dp"
    android:text="Start Game"
    android:onClick="cls2.onBtnClicked(this)" />

I get that the function can't be found...

I wish to dynamically change screens.... Thank you for your time!!

4
  • 1
    where is onCreate for the activity? Jan 28, 2014 at 16:46
  • you need to chnge like android:onClick="onBtnClicked" and add overridden onCreate method to Actvt
    – kalyan pvs
    Jan 28, 2014 at 16:48
  • If this is not an actual Activity then you shouldn't be extending it. Because it sounds to me like that's what is going on. Can you explain a little better what you are doing?
    – codeMagic
    Jan 28, 2014 at 16:48
  • I have three screens. my first screen has a button which, when clicked the user moves to the second screen. On the second screen there are three more buttons(options for the user). Buttons of the second screen lead to the third after clicked. At first i used setOnClicklistenter for each button but i wished to make it more simple but adding a function so it can be done without adding the id of the buttons every time
    – Cupcake
    Jan 28, 2014 at 16:56

3 Answers 3

0

You can use the constructor where you can assign the current context. Try this example

public void sampleMethod(final Activity cont)
{
Intent intent = new Intent(cont, XYZ.class);
        cont.finish();
        cont.startActivity(intent);
}

also move this method to a util class and call it from activity and pass the activity reference as follows

class ActivityB extends Activity
{
  Util.sampleMethod(this);
}
0

You have multiple issues in your code. I'll try to list them here:

  1. You cannot pass a method of any class to android:onClick.

    You'll have to set the onClickListener for your Buttons in code, using

    yourButton.setOnClickListener(new Utl());
    
  2. Don't make Utl a subclass of Activity. Use Activity only for different "screens". You'll want to use Object instead here. If you need a Context in Utl, add a class member and take a Context in the constructor, like this:

    private Context mContext;
    
    public Utls (Context c){
        mContext = c;
    }
    

    Now, instantiate Utls like this:

    yourButton.setOnClickListener(new Utl(this));
    
0

Just change this line

android:onClick="cls2.onBtnClicked(this)"

to

android:onClick="onBtnClicked"

View will be passed to function automatically.

Happy coding..:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.