Start a new activity with a normal class method

I don't know how to write a method in the class that will start another activity.

I have a footer with 5 buttons and each button needs to start a new activity. And I would like to create a class with 5 methods that trigger actions.

I would like to do something like this:

My Footer_buttons

class:

public class Footer_buttons{

//Back to Home activity
    public static void home_footer(Context context) {   

        Intent intent = new Intent(context, Home_page.class);
        context.startActivity(intent);
    }
}

      

In one of my actions, I would like to call something like this:

    private static Context context;
....
        context = this;
....

    public void home_footer(View view) {    
        Footer_buttons.home_footer(context);
    }

      

+3


source to share


1 answer


You can specify the behavior a button should perform in several ways.

xml onClick attribute First, buttons have an xml attribute called onClick. You can assign a method name to this attribute:

<Button
    android:id="@+id/btnMyButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/lbl_click_here"
    android:onClick="goToActivity" />

      

This button calls the goToActivity method in the Activity that this layout belongs to.

public void goToActivity(View view) {
 Intent i = new Intent(this,NewActivity.class);
 startActivity(i);

      

}

onClickListener in fragment The following example applies an onClickListener to a button in the fragment layout during the fragment's onCreateView event.

Here is the button in the xml snippet:

<Button
    android:id="@+id/btnMyButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/lbl_click_here" />

      

Please note that we are no longer using the onClick xml attribute for the button.

OnClickListener is an interface and can be implemented as an anonymous class inside a fragment class:

View rootView = inflater.inflate(R.layout.fragment_main, container, false);

// Find your button in the layout.
Button btnMyButton = (Button) rootView.findViewById(R.id.btnMyButton);

btnMyButton.setOnClickListener(new OnClickListener() {

  @Override
  public void onClick(View v) {
        Intent i = newIntent(getActivity(),NewActivity.class);
        startActivity(i);
  }

});

      

onClickListener in action The following example applies an onClickListener to a button in an activity layout during the onCreate event.

Here is the button in the xml snippet:

<Button
    android:id="@+id/btnMyButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/lbl_click_here" />

      

Once again, we are not using the button's onClick xml attribute.



The onClickListener interface is now implemented as an anonymous class inside the activity class:

    // Find your button in the layout.
Button btnMyButton = (Button)findViewById(R.id.btnMyButton);

btnMyButton.setOnClickListener(new OnClickListener() {

  @Override
  public void onClick(View v) {
        Intent i = newIntent(this,NewActivity.class);
        startActivity(i);
  }

});

      

Finding xml elements at runtime

Finding xml elements at runtime, as shown in the previous two examples, requires that the elements be assigned an ID:

android:id="@+id/btnMyButton"

      

and that this identifier is specified in the calling code:

R.id.btnMyButton

      

When an object searches for items in its layout, it can call the findByView method directly, as shown below:

Button btnMyButton = (Button)findViewById(R.id.btnMyButton);

      

When a fragment searches for items in its layout, it must first call findViewByID on its own view, as shown below:

Button btnMyButton = (Button) rootView.findViewById(R.id.btnMyButton);

      

Casting

Note that in both examples, the return value of findViewByID is passed to the declared type - in this case Button.

Button btnMyButton = (Button)...

      

findViewByID returns the default view - the view is the parent of the Button and is the most common type.

+2


source







All Articles