I'm starting to have many buttons in the same Activity and however simple they are, the code is getting big, and it gets even bigger.
I wonder if you have a way to simplify by reducing the number of lines:
For example to open an activity there are people who do this:
Intendt welcome = new Intent(Dashboard.this,WelcomeScreen.class);
startActivity(welcome);
When you do it on a single line like this:
startActivity(new Intent(Dashboard.this,WelcomeScreen.class));
Now with buttons, I'm doing this:
--- no onCreate
Button btn_welcome = findViewById(R.id.btn_welcome);
btn_welcome.setOnClickListener(this);
--- no onClick
switch (view.getId()) {
case R.id.btn_welcome:
startActivity(new Intent(Dashboard.this,WelcomeScreen.class));
break;
}
Is there a way to simplify all this in a few lines?
Follow my entire class with all the buttons:
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class Dashboard extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
Button btn_welcome = findViewById(R.id.btn_welcome);
btn_welcome.setOnClickListener(this);
Button btn_perfil = findViewById(R.id.btn_perfil);
btn_perfil.setOnClickListener(this);
Button btn_popup = findViewById(R.id.btn_popup);
btn_popup.setOnClickListener(this);
Button btn_intromd01 = findViewById(R.id.btn_intromd01);
btn_intromd01.setOnClickListener(this);
Button btn_spinners = findViewById(R.id.btn_spinners);
btn_spinners.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_welcome:
startActivity(new Intent(Dashboard.this,WelcomeScreen.class));
break;
case R.id.btn_perfil:
startActivity(new Intent(Dashboard.this,perfil.class));
break;
case R.id.btn_popup:
startActivity(new Intent(Dashboard.this,popup.class));
break;
case R.id.btn_intromd01:
startActivity(new Intent(Dashboard.this,InstroSlidesMd01.class));
break;
case R.id.btn_spinners:
startActivity(new Intent(Dashboard.this,SpinnerLayouts.class));
break;
}
}
}