Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get reference to a Button created in a custom Dialog, using a xml layout?

I have an Activity in which user can update a specific information clicking in a button near a label. This buttons than triggers a Dialog where I have some fields to get user input and a button to finish editing.

My problem is that I am not able to get a reference to the button declare in the dialog specific xml layout. The button reference returns null. Follow some code snippet to ilustrate.

Button which fires the event to build the dialog is declared as a instance variable in the activity as follows:

private Button bConfigurarCarro;

than onCreate method:

bConfigurarCarro = (Button)findViewById(R.id.bConfigurarCarro);
 bConfigurarCarro.setOnClickListener(configuraCarroListener);

this correctly fires the event to create the dialog:

protected OnClickListener configuraCarroListener = new OnClickListener(){
public void onClick(View v) {
showDialog(CARRO_DIALOG_ID);
Log.d(TAG, "Executando evento do botão de configuração de carro no abastecimento.");
            }
        };

than to create the dialog Overrides onCreateDialog method like this:

@Override
protected Dialog onCreateDialog(int id) {
  switch (id) {
    case TIME_DIALOG_ID:
        return new TimePickerDialog(this, mTimeSetListener, hora, minuto, false);
    case DATE_DIALOG_ID:
        return new DatePickerDialog(this, mDateSetListener, ano, mes, dia);
    case CARRO_DIALOG_ID:
        Log.d(TAG, "Criando dialog de cadastro de carro.");
        dialogCarro = new Dialog(this);
        dialogCarro.setContentView(R.layout.novo_carro_dialog);
        bSalvarCarro = (Button)findViewById(R.id.botaoSalvarCarro);
        bSalvarCarro.setOnClickListener(salvarCarroListener);
        dialogCarro.setTitle("Carro");
        dialogCarro.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        Log.d(TAG, "Dialog de cadastro de carro criado retornando...");
        return dialogCarro;
    }
        return null;
}

The specific line where the NullPointer triggers is the one where after trying to get Button reference above, I than try to setOnClickListener..

bSalvarCarro = (Button)findViewById(R.id.botaoSalvarCarro);
bSalvarCarro.setOnClickListener(salvarCarroListener);

the bSalvarCarro is null.

The xml layout for the Dialog which I try to set above using the line of code:

dialogCarro.setContentView(R.layout.novo_carro_dialog);

Is this one(novo_carro_dialog.xml):

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="fill_parent">
            <TableRow>
                <TextView android:id="@+id/marcaCarro"
                    android:layout_width="wrap_content" android:layout_height="wrap_content" 
                    android:text="@string/marcaCarro"/>
                <EditText android:id="@+id/tMarcaCarro" android:singleLine="true" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:width="130px"/>
            </TableRow>
            <TableRow>
                <TextView android:id="@+id/nomeCarro"
                    android:layout_width="wrap_content" android:layout_height="wrap_content" 
                    android:text="@string/nomeCarro"/>
                <EditText android:id="@+id/tNomeCarro" android:singleLine="true" android:layout_width="fill_parent"
                android:layout_height="wrap_content"/>
            </TableRow>
            <TableRow>
                <TextView android:id="@+id/anoCarro"
                    android:layout_width="wrap_content" android:layout_height="wrap_content" 
                    android:text="@string/anoCarro"/>
                <EditText android:id="@+id/tAnoCarro" android:singleLine="true" android:layout_width="fill_parent"
                android:numeric="integer" android:maxLength="4" android:layout_height="wrap_content" />
            </TableRow>
            <TableRow>
                <TextView android:id="@+id/modeloCarro"
                    android:layout_width="wrap_content" android:layout_height="wrap_content" 
                    android:text="@string/modeloCarro"/>
                <EditText android:id="@+id/tModeloCarro" android:singleLine="true" android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
            </TableRow>
            <Button android:id="@+id/botaoSalvarCarro" android:layout_width="wrap_content"
                    android:layout_height="wrap_content" android:text="@string/bSalvarCarro" />
</TableLayout>

As you can see the Button is declared with the id botaoSalvarCarro, but trying to get a reference to it returns null. I am a bit confused by this as if I take out the line which sets the listener the dialog is correctly showed, so. How do I get the reference to this button correctly?

like image 838
groo Avatar asked Oct 25 '10 00:10

groo


People also ask

How do I style a button in XML?

Two things you need to do, if you want to make a custom button design. 1st is: create a xml resource file in drawable folder (Example: btn_shape_rectangle. xml) then copy and paste the code there. You can change shape color radius what design you want can do.


1 Answers

dialogCarro = new Dialog(this);
dialogCarro.setContentView(R.layout.novo_carro_dialog);
bSalvarCarro = (Button)dialogCarro.findViewById(R.id.botaoSalvarCarro);
like image 158
Cristian Avatar answered Sep 28 '22 08:09

Cristian