How to open a Dialog on android

2

I would like to know how to open a dialog so that the user can enter text, as soon as he clicks the button, the box to type is opened and as soon as he type it will have a save and cancel button, how can I do that?

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 do not know how to go to the layout through Intent , I am lay in android

My onActivityResult() is already being used for something else, has problem on 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();

    }
}
    
asked by anonymous 25.06.2015 / 19:49

2 answers

1

I searched 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();

    }
}
    
26.06.2015 / 02:19
1

To open a dialog box you can use the setOnClickListener method in the 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 file of 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 Activity of your dialog you can work the way you want by returning to your Activity or by continuing the flow of the app.

    
25.06.2015 / 19:57