Ankush
Ankush

Reputation: 6927

access a different layouts parameter from current activity

Suppose my current activity is Main.java and I have already declared its layout through setContentView(R.layout.layout1) from its onCreate method. Now, is it in any way possible for me to access a different layout? For e.g., assuming there is another layout - layout2 which has TextView with id tv, then I won't be able to execute the following code from Main.java :

TextView text = (TextView) findViewById(R.id.tv);
text.setText("blah blah");

Is there any way that I can set tv's value from Main.java.

My actual code is the following

setContentView(R.layout.layout);
Button button = (Button) findViewById(button);
    button(buttonListener);
Dialog dialog;

Inside the listener, I have the following code:

TextView dialogTitle = (TextView) findViewById(R.id.dialog_title);
        dialogTitle.setText("Email");

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View customView = getLayoutInflater().inflate(R.layout.dialog, null);
        builder.setView(customView);            

        dialog = builder.create();
                    dialog.show();

The problem that I am facing is that dialog_title is in dialog.xml and not in layout.xml

Upvotes: 0

Views: 1548

Answers (3)

Tobias Moe Thorstensen
Tobias Moe Thorstensen

Reputation: 8981

You can use Bundles

In Activity 1

String your_string = "Hello, World!";
Bundle bundle = new Bundle();
bundle.putString("The key for this string", your_string );

Intent ActivityToLaunch= new Intent(this, ActivityB.class); 
ActivityToLaunch.putExtras(bundle);
this.startActivity(ActivityToLaunch);

In Activity 2

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout2); //Setup some layout, set to your own

    String content = getIntent().getExtras().getString("The key for this string");
    TextView text = (TextView) findViewById(R.id.tv);
    text.setText(content);     
}

The thread starter said that he wanted to raise a custom dialog, so here goes the edit

This is my class which will generate a custom Dialog:

public class ErrorDialog {

    TextView msgTextView;
    Button toSettings;
    final Context c;
    Dialog errorDialog;


   /**
     * @param c The Context
     * @param title Title of the Dialog
     * @param msg Message og the Dialog
     * @param textOnButton The text on the button
     */

    public ErrorDialog(final Context c, String title, String msg, String textOnButton) {

        this.c = c;
        errorDialog = new Dialog(c);
        errorDialog.setContentView(R.layout.error_dialog);
        errorDialog.setTitle(title);

        msgTextView = (TextView) errorDialog.findViewById(R.id.errorMSG);
        msgTextView.setText(msg);

        toSettings = (Button) errorDialog.findViewById(R.id.toSettings);
        toSettings.setText(text);
        toSettings.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

               //doing operations when the user clicks my button in the dialog. 
            } 
        });

        errorDialog.show();
        errorDialog.setCancelable(true);
    }
}

Use this class this way:

new ErrorDialog(getApplicationContext(), "My Title", "My Message to the user", "Text on the button"); 

Upvotes: 2

Just Variable
Just Variable

Reputation: 877

AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View customView = getLayoutInflater().inflate(R.layout.dialog, null);
        builder.setView(customView);      
TextView dialogTitle = (TextView) customView.findViewById(R.id.dialog_title); 
dialogTitle.setText("Email");

Upvotes: 1

sdabet
sdabet

Reputation: 18670

You can always inflate any XML layout you want at any time:

    View layout2 = LayoutInflater.from(this).inflate(R.layout.layout2, null);

Upvotes: 3

Related Questions