Michalsx
Michalsx

Reputation: 3621

How to create method which will find view by Id and set it into variable?

There are several switches, which must be set same way:

private SwitchCompat switch1,switch2,...,switch10;

private void initSwitch(@NonNull SwitchCompat switchCompat) {
    switchCompat.setOnCheckedChangeListener(this);
    switchCompat.setTypeface(...);
}

How to pass switch id to initSwitch, so it would set all variables switch1, switch2, ..., switch10?

Because this will not work:

private void init(){
initSwitch(switch1, R.id.switch1)
initSwitch(switch1, R.id.switch2)
...
initSwitch(switch1, R.id.switch10)
}

private void initSwitch(@NonNull SwitchCompat switchCompat,int id) {
    switchCompat.findById(id)
    switchCompat.setOnCheckedChangeListener(this);
    switchCompat.setTypeface(...);
}

Upvotes: 0

Views: 595

Answers (4)

Michalsx
Michalsx

Reputation: 3621

I didn't say it clearly, so this is my fault, but I can't add any libraries or use Kotlin because my great team won't let me.

It is simple, I just had "a window".

switch1= initSwitch(R.id.switch1);
switch2= initSwitch(R.id.switch2);
switch3= initSwitch(R.id.switch3);
....
switch10= initSwitch(R.id.switch10);


private SwitchCompat initSwitch(@IdRes int id) {
    final SwitchCompat switchCompat = findViewById(id);
    switchCompat.setOnCheckedChangeListener(this);
    switchCompat.setTypeface(...);
    return switchCompat;
}

Thank you all for your help.

Upvotes: 0

AskNilesh
AskNilesh

Reputation: 69689

Try this way

Create a method like this

@SuppressWarnings("unchecked")
public <T extends View> T $(int id) {
    return (T) findViewById(id);
}

Change your init() like this

switch1=$(R.id.switch1);
switch2=$(R.id.switch2);
switch10=$(R.id.switch10);

You can also use Butter Knife

Also other option is Data Binding

if you want to use kotlin then no need to findViewById()

Upvotes: 1

ASHKARAN
ASHKARAN

Reputation: 365

As an answer to your question, I agree with Nilesh Rathod But for this purpose a suggest you use something like ButterKnife

class ExampleActivity extends Activity {

  @BindView(R.id.switch1) SwitchCompat switch1;
  @BindView(R.id.switch2) SwitchCompat switch2;
  @BindView(R.id.switch3) SwitchCompat switch3;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.simple_activity);
    ButterKnife.bind(this);
    // TODO Use fields...
  }
}

or if you want to listen on setOnCheckedChangeListener you don't need to define them

for example, we define onClickListener at the following code without any definition of views

  @OnClick({R.id.switch1,R.id.switch2,R.id.switch3}) 
  public void onClick(View view) {
     switch(view.getId) {
        case R.id.switch1: {
          //do something here
          break;
        } 
     } 
  }

Upvotes: 1

Arka Prava Basu
Arka Prava Basu

Reputation: 2560

You can use databinding.

Another way would be to traverse your layout like this and set the properties to your Switch.

LinearLayout layout = (LinearLayout)findViewById(R.id.root); // whatever layout you are using
setPropToSwitch(layout);

public void setPropToSwitch(LinearLayout layout) {
    for (int i = 0; i < layout.getChildCount(); i++) {
        View v = layout.getChildAt(i);
        if (v instanceof SwitchCompat) {
            //set properties
        }
    }
}

Upvotes: 0

Related Questions