Relatively new to the whole javascript scene -trying to create my first app! So far I have managed to create the base layout, link button to next .xml, etc. however I’d really like a button that when clicked, opens a pop-up with some information on (a few lines describing the app’s purpose). Unfortunately, all tutorials, although helpful, don’t explain where in the mainactivity.java (e.g.) I need to place the code (i.e. before what tag? what do I need to keep/replace?) I’ll paste my full code for activity.xml and full for mainactivity.java – hopefully, you legends will be able to give me a steer!
Java:
package com.example.newapp; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { openactivity_tasks(); } }); } public void openactivity_tasks(){ Intent intent = new Intent(this,activity_tasks.class); startActivity(intent); } }
.xml:
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:background="@color/black" tools:ignore="ExtraText"> <ImageView android:id="@+id/imageView2" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.09" app:srcCompat="@drawable/shortlogo" tools:ignore="ContentDescription" /> <Button android:id="@+id/button" android:layout_width="300dp" android:layout_height="300dp" android:background="@drawable/circlebutton" android:text="@string/hit_me" android:textColor="#000" android:textSize="28dp" android:textStyle="bold" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.711" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="4dp" android:paddingBottom="5dp" android:text="Design and What2Do owned by Armourtal Ltd 2021" android:textColor="@color/grey" android:textSize="8dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.092" app:layout_constraintStart_toStartOf="parent" /> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="4dp" android:paddingBottom="5dp" android:text="Version 0.0.1" android:textColor="@color/grey" android:textSize="8dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.963" app:layout_constraintStart_toStartOf="parent" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_margin="30dp" android:textSize="22sp" android:text="This is a popup window." android:textColor="@color/white"/> </androidx.constraintlayout.widget.ConstraintLayout>
Advertisement
Answer
You can use this code in Java File
private void showInfo() { AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AlertDialogStyle); builder.setMessage("Put your piece of information HERE"); // Create and show the AlertDialog final AlertDialog alertDialog = builder.create(); alertDialog.show(); }
Add this code in your styles.xml to change text and background color
<style name="AlertDialogStyle" parent="Theme.AppCompat.Light.Dialog"> <!-- textColor. --> <item name="android:textColorPrimary">#000</item> <!-- background Color. --> <item name="android:background">#fff</item> </style>
Use the method showInfo() inside your Button
button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showInfo(); } });