Show a dialogue on android

4

I have a problem: I want to show a dialogue that I made from a layout that has a text and a button but I do not understand because it tells me that the application stopped.

Code:

public class MainActivity extends AppCompatActivity {

    Button b1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        b1 = (Button) findViewById(R.id.b1);
    }

    public void b1(View view) {

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);

        View child = getLayoutInflater().inflate(R.layout.dialogo6, null);
        alertDialogBuilder.setView(child);

        final AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);

        Button dismissButton = (Button) alertDialog.findViewById(R.id.boton);
        dismissButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });

        alertDialog.show();
    }
}

The dialogue that I want to show:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:text="Alto Titulo Ameo"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/titulo"
    android:textAlignment="center"
    android:textSize="30sp" />

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="340dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:srcCompat="@mipmap/ic_launcher"
            android:id="@+id/imagen" />

        <TextView
            android:text="Descripcion del dialogo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/desc"
            android:textSize="20sp" />

    </LinearLayout>
</ScrollView>

<Button
    android:text="Button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/boton"/>
</LinearLayout>
    
asked by Agustin Val 18.12.2016 в 17:36
source

3 answers

1

Inside your onCreate method where it says b1 = (Button) findViewById (R.id.b1); you have to replace with:

b1 = (Button) findViewById(R.id.boton);

Inside the b1 method where it says View child = getLayoutInflater (). inflate (R.layout.dialogo6, null); you have to replace with:

View child = getLayoutInflater().inflate(R.layout.activity_main, null);

The errors were only because of the reference to your eyes. With this it is solved. I tried it from my Samsung S5 device

    
answered by 18.12.2016 в 18:26
1

If you do not add the LogCat the problems can be several, I notice 2 things to check:

1) Inside your layout activity_main.xml you must have a Button with id b1, make sure so, since this is the code that does what I say:

...
 setContentView(R.layout.activity_main);
 b1 = (Button) findViewById(R.id.b1);
...

2) The public void b1(View view) { method is called from the view in the layout, ensures this defined in this way:

<Button
 ...
 ... 
 android:onClick="b1" />

and 3) check that within your layout dialogo6.xml you have defined a button with id boton :

<Button
 android:id="@+id/boton
 ...
 ... />

which is used here:

View child = getLayoutInflater().inflate(R.layout.dialogo6, null);
alertDialogBuilder.setView(child);

final AlertDialog alertDialog = alertDialogBuilder.create();
...
Button dismissButton = (Button) alertDialog.findViewById(R.id.boton);
    
answered by 19.12.2016 в 20:23
-1

There is another option that is to create a Activity (blank) with whatever you want inside and Manifest assign the attribute

android:theme="@style/Theme.AppCompat.Light.Dialog"

Note that all activity will be in Dialog mode. and you do not need to import any liberia from third parties since it is part of Android Studio. Greetings

    
answered by 18.12.2016 в 22:47