How to open a Dialog on android

Asked

Viewed 515 times

2

I wanted to know how to open a dialog so the user can insert text, as soon as he click the button, the box to type is opened and as soon as he type will have a save and cancel button, how can I do this ?

private ImageView imgsavesom;

imgsavesom = (ImageView) findViewById(R.id.imgsavesom);
imgsavesom.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            //Criar um objecto File com o caminho actual
            Intent intent = new Intent("grav_som"); //grav_som eh o nome do  layout

            startActivity(intent);

    }
});

i don’t know how to go to the layout through the Intent, I am layman on android

My onActivityResult() is already being used for something else, has problem about startActivity(Intent); ?

@Override
public void onActivityResult(int reqCode, int resCode, Intent data) {
    super.onActivityResult(reqCode, resCode, data);
    if(resCode == RESULT_OK)    {
        if (reqCode == 1)
            contactImgView.setImageURI(data.getData());
        Uri imageUri = data.getData();
        imagePath = getImagePath(imageUri);
        Toast.makeText(MainActivity.this, imagePath, Toast.LENGTH_LONG).show();

    }
}
  • Don’t you just need to open the dialog? this "grav_som" is the layout that has the dialog? your concern and when you click "cancel"?

  • I just want to open the dialog, the grav_som would be the layout you gave me, and my concern is when you click the button, it opens the dialog where is the layout you gave me in your reply

  • Already tested? dialog opened?

  • he doesn’t open, he closes the program

  • Put the Log Cat error to analyze! @Gabriel Santana Bonatto

  • sorry for the delay, but I’m not able to get the right information from the logcat of android studio, it is different from the eclipse that I was using, could help about this?

  • I never used Android Studio. The log Cat, does not show the released errors?

  • show, but there’s a lot of strange thing, if you want I take all this and put here, want?

Show 3 more comments

2 answers

1

To open a dialog box you can use the method setOnClickListener id of your ex button: btFinalizar.setOnClickListener(new View.OnClickListener()

It would look something like this :

//seu button 
private Button                      Abredialog;


//fica dentro do seu @OnCreate esse bloco

Abredialog= (Button) findViewById(R.id."seu id do botao que vem do layout");
//aqui você atribui ao seu botão o metodo setOnClickListener
Abredialog.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {

            //Aqui dentro do OnClick, você faz da mesma forma que você trabalha para abrir uma Activity
        Intent intent = new Intent("sua activity a ser aberta");
        startActivity(intent);

        }
    });

Your dialog can be created as follows in another layout:

 <EditText
    android:id="@+id/edtOservacaoPedRota"
    android:layout_width="fill_parent"
    android:layout_height="150dp"
    android:layout_marginTop="5dp"
    android:gravity="top"
    android:inputType="textMultiLine" >

    <requestFocus />
</EditText>

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/edtRota"
    android:orientation="horizontal"
    android:paddingBottom="10dp" >

    <Button
        android:id="@+id/btCancelarObservaca"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="7dp"
        android:layout_weight="1"
        android:drawableLeft="@android:drawable/ic_menu_revert"
        android:text="@string/lblCancelar" />

    <Button
        android:id="@+id/btGravarObservacao"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="7dp"
        android:layout_weight="1"
        android:drawableLeft="@android:drawable/ic_menu_save"
        android:text="@string/lblGravar" />
</LinearLayout>

In the Activity from your dialog you can work the way you want by returning to your Activity or by continuing the app flow.

  • I put the Onclick part you put just above, could you comment on each line on the Onclick part to see if I understand? because the program here is closing when I click the :( I would also like to know that my onActivityResult is already being used for another purpose, if I send as in the case of that part of the startActivity(Intent) code will not give problem being that it is receiving other parameters because it is being used for another purpose?

  • I made an issue, see if it fits. @Gabrielsantanabonatto

  • edited my question

1


I looked around and found

ExibeDialog();

private void ExibeDialog(){
        final Dialog dialog = new Dialog(this);
        
        //layout para o dialog
        dialog.setContentView(R.layout.dialog_som);

        //define o título do Dialog
        dialog.setTitle("Nomear");

        //instancia os objetos que estão no layout customdialog.xml
        final Button confirmar = (Button) dialog.findViewById(R.id.btnConfirmar);
        final Button cancelar = (Button) dialog.findViewById(R.id.btnCancelar);
        fianl EditText nome = (EditText) dialog.findViewById(R.id.edtnome);


        confirmar.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
 

                //finaliza o dialog
                dialog.dismiss();
            }
        });

        cancelar.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //finaliza o dialog
                dialog.dismiss();
            }
        });

        //exibe na tela o dialog
        dialog.show();

    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.