Search code examples
androidclassandroid-layoutandroid-alertdialogandroid-custom-view

access layout text view (no class) from another class


I have a custom dialog layout with a text view but it doesn't have a class. in my main activity in onOptionsItemSelected, i have an "about" (in toolbar) which shows a dialog with that custom layout. I want to access and set that textView to show version name. how should i do it? The following didn't work: Main activity:

public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_settings) {
        Intent i = new Intent(this,Settings.class);
        startActivity(i);
    } else if (id == R.id.action_about){
        **TextView tvvn = (TextView)findViewById(R.id.tvvn);
        tvvn.setText(BuildConfig.VERSION_NAME);**

        AlertDialog dialog = new AlertDialog.Builder(this)
                .setView(getLayoutInflater().inflate(R.layout.custom_dialog, null))
                .create();
        dialog.show();
    }

Solution

  • Do this:

                View v = getLayoutInflater().inflate(R.layout.custom_dialog, null);
                TextView tvvn = (TextView)v.findViewById(R.id.tvvn);
                tvvn.setText(BuildConfig.VERSION_NAME);
                AlertDialog dialog = new AlertDialog.Builder(this)
                    .setView(v)
                    .create();
                dialog.show();