How to simplify the clicks on buttons in Java for Android?

0

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;
        }
    }
}
    
asked by anonymous 25.02.2018 / 18:42

2 answers

1

Considering that its structure looks like this:

YoucanusethetagattributeonyourButtonelementandthenuse Reflection . With this you can capture the class, just passing as a parameter, its name.

To do this simply set the tag attribute with the activity name to which the button will redirect the user.

XML Example:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="br.com.valdeirpsr.stackoverflow.MainActivity"
    android:id="@+id/container"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button #1"
        android:tag=".ProfileActivity"/> <!-- Atributo TAG -->

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button #2"
        android:tag=".WelcomeActivity"/> <!-- Atributo TAG -->

    <Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button #2"
        android:tag=".AnotherActivity"/> <!-- Atributo TAG -->

</LinearLayout>

In Java, to capture the class by name, just use Class.forName . This will return the Class object that can be used in Intent .

Example in Java:

package br.com.valdeirpsr.stackoverflow;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

public class MainActivity extends Activity implements View.OnClickListener {

    private ViewGroup container;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.btn1).setOnClickListener(this);
        findViewById(R.id.btn2).setOnClickListener(this);
        findViewById(R.id.btn3).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn1:
            case R.id.btn2:
            case R.id.btn3:
                try {
                    startActivity(new Intent(MainActivity.this, Class.forName(
                            getPackageName().concat(v.getTag().toString())
                    )));
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
                break;
        }
    }
}
    
25.02.2018 / 19:39
0

For each Button you can assign the .setOnClickListener directly to your onClick method within onCreate as in the example:

@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(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(Dashboard.this,WelcomeScreen.class));
        }
    });

    Button btn_perfil = findViewById(R.id.btn_perfil);
    btn_perfil.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(Dashboard.this,perfil.class));
        }
    });
} 

This decreases the distance of the Button reference with its onClick method, leaving the code smaller and more objective.

    
03.03.2018 / 00:10