Search code examples
javafindviewbyid

Is it possible to pass an object parameter as an id in the find view by id


Here is how I would naturally do this, are there any other possible ways that work?

public void error_checker(String Text_ID, int id,int id_2,String greater_num){
    if (id>id_2){


        TextView TextView = findViewById(R.id.Text_ID);

        TextView.setText(Integer.parseInt("You Answer Can't be Greater Than"+(greater_num));
    } else {

    }
}

Solution

  • You can do it like this:

      TextView textView = findViewById(getResources().getIdentifier(Text_ID, "id", getPackageName()););
    

    Where Text_ID is the ID of your view (without the R.id. prefix)