Skip to content
Advertisement

Open New Activity from Android Button Click

In my app I’m developing in android studio, I’m making a button to redirect from one activity to another but it does not give me anything even though I do not have any errors in the code.

package com.example.shreeganesha.splash;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_beatness);
    init();
}
    public Button btn2;

 public void init(){
         btn2= (Button)findViewById(R.id.btn2);
         btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

               Intent inf= new Intent(beatness.this,Informacoes.class);

              startActivity(inf);
            }
      });
}

Advertisement

Answer

Check your id’s on your xml layout, also check there is no onClick XML property being used that can be conflicting with your button, check that your new activity name and that your new activity is correctly linked to the intended layout

package com.example.shreeganesha.splash;

    private Button changeActivity; //Try more explicit names on your variables instead of just btn2, 
    // also declare your variables on top for better organized code

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_beatness);
        init();
    }


    public void init(){
        changeActivity = (Button)findViewById(R.id.changeActivity);
        changeActivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(this, YourNewActivityClass.class);
                startActivity(i);
            }
        });
    }

Hope it helps

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement