0
I’m starting to have a lot of buttons in the same Activity and as much as they are simple commands, the code is getting big, and it will get even bigger.
I wonder if there is a way to simplify by decreasing the amounts of lines:
For example to open an Activity have people who do so:
Intendt welcome = new Intent(Dashboard.this,WelcomeScreen.class);
startActivity(welcome);
When you can do it in a single line like this:
startActivity(new Intent(Dashboard.this,WelcomeScreen.class));
Now with buttons, I’m doing like 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;
}
There is a way to simplify all this in a few lines?
Follow my entire class with every button:
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;
}
}
}
In my opinion you’re doing it as simply as possible.
– Rosário Pereira Fernandes
Butterknife. ;)
– itscorey