I know this is a simple question but I'm a beginner and I have this homework where we
have to make an image show when the radio button is clicked but they didn't teach us anything about that
because of coronavirus so I would really appreciate it if someone could tell me how to do it.
For creating two radio buttons with simple image, and control visibility of image when check one of the radio buttons, I will give some simple steps to build that in java.
After build a new empty activity project:
You can open the wanted files from project explorer as shown :
go to activity_main.xml which is the layout file, I will create Linear layout with image and radio group contains two radio buttons,and I used imageview android:src attribute as a color (you can change it to image):
<ImageView
android:id="#+id/imageView"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="#color/colorPrimary"
android:text="Hello World!"
android:visibility="gone" />
<RadioGroup
android:id="#+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:orientation="horizontal">
<RadioButton
android:id="#+id/radio_button_choice1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="choice-1" />
<RadioButton
android:id="#+id/radio_button_choice2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="choice-2" />
</RadioGroup>
Second, we need to go to java file named as MainActivity which is java file, and we will get the views and create a listener on the RadioGroup which we will use to show and hide the image (some comments help you understands the java code):
package com.your.packagename.here;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;
public class MainActivity extends AppCompatActivity {
private ImageView image;
private RadioGroup radioGroup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//intit views
image = findViewById(R.id.imageView);
radioGroup = findViewById(R.id.radioGroup);
//create the listener for radio group
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
//check if the choice-1 is selected
if (radioGroup.getCheckedRadioButtonId() == R.id.radio_button_choice1) {
//show image wen select choice-1
image.setVisibility(View.VISIBLE);
} else {
//hide image when choice-1 not selected
image.setVisibility(View.GONE);
}
}
});
}
}
Finally, and you will get similar to this.
I hope this was helped you.
Related
how can i use lottie animation in custom progress dialog.
i know how implement custom progress bar with lottie but i want progress dialog.
i have my animation in json format. but i dont know how make custom progress dialog.
in gradle i write this line
implementation 'com.airbnb.android:lottie:3.4.4'
and new class
import android.app.ProgressDialog;
import android.content.Context;
import android.view.animation.Animation;
public class MyProgressDialog extends ProgressDialog {
public MyProgressDialog(Context context) {
super(context,R.style.NewDialog);
// TODO Auto-generated constructor stub
}
}
i found this video but it is coded by kotlin. i want use java
https://www.youtube.com/watch?v=ZcR6AMNIagU
i found solution
1- make one xml layout for new design. "loading.json" is json format of animation in asset folder.
<?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"
android:orientation="vertical"
android:layout_width="100dp"
android:layout_height="100dp"
>
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/progressAnimationView"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:lottie_autoPlay="true"
app:lottie_fileName="loading.json"
app:lottie_loop="true" />
</androidx.constraintlayout.widget.ConstraintLayout>
2-create new java class
public class lottiedialogfragment extends Dialog {
public lottiedialogfragment(Context context) {
super(context);
WindowManager.LayoutParams wlmp = getWindow().getAttributes();
wlmp.gravity = Gravity.CENTER_HORIZONTAL;
getWindow().setAttributes(wlmp);
getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
setTitle(null);
setCancelable(false);
setOnCancelListener(null);
View view = LayoutInflater.from(context).inflate(
R.layout.dialog_lottie, null);
setContentView(view);
}
}
3-call dialog in main activity
final lottiedialogfragment lottie=new lottiedialogfragment(this);
lottie.show();
You can create it easily using the LottieDialog library
Code will be like this
LottieDialog dialog = new LottieDialog(this)
.setAnimation(R.raw.ripple)
.setAutoPlayAnimation(true)
.setDialogHeightPercentage(.2f)
.setAnimationRepeatCount(LottieDialog.INFINITE)
.setMessage("Loading...")
.setMessageColor(orangeColor);
dialog.show();
Github Repo for examples and documentation : LottieDialog
End result will be like this
I have a simple android project where I'm populating a list of items in a database. To do this I'm using RecyclerView showing in my activity_main.xml
These are my two (and only two that exist) xml files
activity_main.xml
package com.twocrows.foretoldapp;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import com.twocrows.foretoldapp.adapter.ChartAdapter;
import com.twocrows.foretoldapp.entity.Chart;
import com.twocrows.foretoldapp.viewmodel.ChartViewModel;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ChartViewModel chartViewModel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
final ChartAdapter adapter = new ChartAdapter();
recyclerView.setAdapter(adapter);
chartViewModel = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(this.getApplication())).get(ChartViewModel.class);
chartViewModel.getAllCharts().observe(this, new Observer<List<Chart>>() {
#Override
public void onChanged(List<Chart> charts) {
adapter.setCharts(charts);
}
});
}
}
which shows in the preview like this
chart_item.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp">
<TextView
android:id="#+id/text_view_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:text="location"
android:textAppearance="#style/TextAppearance.AppCompat.Medium" />
<TextView
android:id="#+id/text_view_name"
android:layout_width="139dp"
android:layout_height="38dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="248dp"
android:layout_marginRight="248dp"
android:layout_below="#+id/text_view_name"
android:text="Chart Name"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
tools:ignore="NotSibling" />
<TextView
android:id="#+id/text_view_dateTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/text_view_name"
android:text="MM/DD/YYYY"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
MainActivity.java
package com.twocrows.foretoldapp;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import com.twocrows.foretoldapp.adapter.ChartAdapter;
import com.twocrows.foretoldapp.entity.Chart;
import com.twocrows.foretoldapp.viewmodel.ChartViewModel;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ChartViewModel chartViewModel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
final ChartAdapter adapter = new ChartAdapter();
recyclerView.setAdapter(adapter);
chartViewModel = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(this.getApplication())).get(ChartViewModel.class);
chartViewModel.getAllCharts().observe(this, new Observer<List<Chart>>() {
#Override
public void onChanged(List<Chart> charts) {
adapter.setCharts(charts);
}
});
}
}
The issue is that whenever I try to run the app, it's showing me an old version of the main activity xml file. I no longer even have these components in the xml so I don't understand where they are coming from.
#Things I've tried
Invalidate cache/ restart
clean build
edit configuration, uncheck 'skip installation if apk has not changed'
Nothing so far has worked. Any help would be appreciated. Thanks!
The issue of your app showing an old version of the activity_main.xml, even though you no longer have those components in the file, may be due to having two different layout resource files for a single activity.
You can check your layout resource directory to see if you have two different versions of the main activity layout file, such as "main.xml" and "main.xml(v21)", which may be causing the confusion. You can see the reason for having multiple files in this answer.
In this case, the solution would be to make sure that you're editing the correct layout file. If you edited an XML file but your emulator is running the v21 file, you can copy your code and paste it into the other file.
If you've made changes to the correct file but are still seeing the old version in the emulator, you can try cleaning and rebuilding the project, or even deleting the app from the emulator and reinstalling it.
If you continue to experience the issue, you may also want to check the AndroidManifest.xml file to ensure that the correct layout file is being used for the main activity. This file can be found in the "app" directory of your project.
I've been using the new Navigation Component since shortly after it has been announced at Google I/O, and also started to embrace the single-activity as much as possible.
The Single Activity allowed me to share ViewModels between view for an awesome experience and I really don't want to go back to multi-activity if I'm not forced to.
But there's something that gets in the way: AppBar / Themes (status bar) to the single activity concept.
This is part of the design I'm working in:
As you can see there are different requirments for how the Actionbar / status bar should look.
It's a simple drawer with standard actionbar
Classic detail with image going under the translucent status bar, supposed to use CollapsingToolbarLayout to turn into a standard actionbar when scrolling up
In this case it is non-standard actionbar, I'd call it a "floating toolbar" cause it doesn't expand to the full with of the screen and contains an already expanded SearchView / EditText
Fairly standard AppBar with tabs
List of issues that arise from leaving the single activity:
can't share ViewModels between activities
complex navigations which re-use parts already defined in another activity navigation graph have to be duplicated / moved into a dedicated activity
back navigation "re-construction" doesn't work between activities
Those are issues I want to avoid if possible, but how do you guys manage these kind of situation on a single-activity with navigation component. Any idea?
As mentioned here, the developer document said
Adding the top app bar to your activity works well when the app bar’s layout is similar for each destination in your app. If, however, your top app bar changes substantially across destinations, then consider removing the top app bar from your activity and defining it in each destination fragment, instead.
I was also thinking the same but never got time to do some experiment. So it's not a solution, it's an experiment, where I want to replace a view with another, here, the toolbar with a toolbar that contains an ImageView.
So I created a new Application using "Basic Activity" template. Then created two destinations within the graph, Home and destination. And lastly, created another layout for Toolbar:
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="?actionBarSize">
<ImageView
android:id="#+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#mipmap/ic_launcher_round" />
</androidx.appcompat.widget.Toolbar>
The activity_main.xml has:
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
...
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
...
And then within Activity, of-course depends on the setup, but let's say that I want to setup an support-actionbar with toolbar:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
Toolbar toolbar2 = (Toolbar) getLayoutInflater().inflate(R.layout.destination_toolbar, null);
AppBarLayout appBarLayout = findViewById(R.id.appbar_layout);
navController = Navigation.findNavController(this, R.id.nav_host_fragment);
appBarConfiguration = new AppBarConfiguration.Builder(navController.getGraph())
.build();
navController.addOnDestinationChangedListener((controller, destination, arguments) -> {
switch (destination.getId()) {
case R.id.homeFragment:
appBarLayout.removeAllViews();
appBarLayout.addView(toolbar);
setSupportActionBar(toolbar);
toolbar.setTitle("Home Fragment");
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
break;
case R.id.destinationFragment:
appBarLayout.removeAllViews();
appBarLayout.addView(toolbar2);
setSupportActionBar(toolbar2);
toolbar2.setTitle("");
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
break;
}
});
}
And thus, this works, making it somewhat ugly as destination grows and new Toolbar/any other view is being added.
P.S. As I told earlier, this is just an experiment, if anyone has a better solution, please do post a new answer.
Disclaimer
Based of #Rajarshi original experiment, I made a working solution for this problem. I'm not sure is the most elegant, or if there are better ways. But after hours of research and investigation, this is the best solution I found.
Solution
Inflate the toolbars separately and store their references so they are not picked by the garbage collector.
Then load each on demand in your main AppBarLayout inside a custom OnDestinationChangedListener defined for your navController
Example
Here's an example I've written in Kotlin.
On your activity.xml layout, define an AppBarLayout that is empty.
layout/activity.xml
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
...
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay" />
...
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Define the toolbars that your app needs to have in separate layout files.
layout/toolbar_defaul.xml
<com.google.android.material.appbar.MaterialToolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/default_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:menu="#menu/menu_default"
app:popupTheme="#style/AppTheme.PopupOverlay" />
layout/toolbar2.xml
<com.google.android.material.appbar.MaterialToolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar2"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:menu="#menu/menu2"
app:popupTheme="#style/AppTheme.PopupOverlay" />
In your main (and only) activity, declare AppBar related components as class properties, so that they are not picked up by the garbage collector.
Activity.kt
class Activity : AppCompatActivity() {
private lateinit var appBarConfiguration: AppBarConfiguration
private lateinit var appBarLayout: AppBarLayout
private lateinit var defaultToolbar: MaterialToolbar
private lateinit var toolbar2: MaterialToolbar
...
And finally, in the onCreate method, define a OnDestinationChangedListener for the navController. Use it to load on demand each toolbar.
Activity.kt
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_ryvod)
// Set up AppBar
appBarLayout = findViewById(R.id.appbar)
appBarConfiguration = AppBarConfiguration(setOf(R.id.StartFragment))
defaultToolbar = layoutInflater.inflate(R.layout.toolbar_default, appBarLayout, false) as MaterialToolbar
toolbar2 = layoutInflater.inflate(R.layout.toolbar2, appBarLayout, false) as MaterialToolbar
val host =
supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment?
?: return
val navController = host.navController
navController.addOnDestinationChangedListener { _, destination, _ ->
when (destination.id) {
R.id.locationPickerFragment -> {
appBarLayout.removeAllViews()
appBarLayout.addView(toolbar2)
setSupportActionBar(toolbar2)
}
else -> {
appBarLayout.removeAllViews()
appBarLayout.addView(defaultToolbar)
setSupportActionBar(defaultToolbar)
}
}
setupActionBarWithNavController(navController, appBarConfiguration)
}
}
That should do the trick
I confronted this problem a while ago, with similar UX/UI as yours:
Sidenav Navigation Drawer
A "normal" Appbar with back arrow
Translucent Appbar/status bar
My solution was having a different .xml Appbar for each case and using the <include/> tag inside every fragment xml:
<include
android:id="#+id/include"
layout="#layout/default_toolbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
The window config for case 1 and case 2 was the same, but for the translucent Appbar, the window config changed, see case 3.
So I had to do a config change every time the fragment showed up/replaced:
public static void transparentStatusBar(Activity activity, boolean isTransparent, boolean fullscreen) {
if (isTransparent){
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}else {
if (fullscreen){
View decorView = activity.getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
} else {
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
| View.SYSTEM_UI_FLAG_VISIBLE);
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
}
}
And then use this method in translucent appbar/status bar fragment's lifecycle:
#Override
public void onResume() {
super.onResume();
UtilApp.transparentStatusBar(requireActivity(), true, true);
}
#Override
public void onStop() {
super.onStop();
UtilApp.transparentStatusBar(requireActivity(), false, false);
}
I am basic. in android studio, I want to start a basic project.
I want to type "any word" in a plain text view.
Only whenever I click on a button ,Just we can see that word as a TOAST.
this case I need : a plan text view, a button , and a toast.
How should i do this?
Thanks for the replies.
In your xml file you need to set an id for both textviews and button
In your activity:
//get the button
button = (Button) findViewById(R.id.but);
txt1= (text1) findViewById(R.id.text1);
txt2 = (text2) findViewById(R.id.text2);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
txt2.setText(txt1);
}
Your XML code should look like so. Note the id's of the EditText and the TextView which are crucial for extracting your strings and setting them respectively.
Also note the 'onClick' function on your button which is necessary to call upon the method in your MainActivity (Java Class) with the same name.
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/enterText"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="changeText"
android:text="Show Now"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/newText"/>
</LinearLayout>
Now for your MainActivity (Java) Class method:
public void changeText(View view){
EditText editText = findViewById(R.id.enterText);
String res = editText.getText().toString();
TextView textView = findViewById(R.id.newText);
textView.setText(res);
}
Again take note of the declaration of the EditText object using your 'id', "enterText" and "newText" respectively.
Hope this helps.
In Android Alert Dialog : dialog.getButton is not available
How to change the background of the Positive button in laert dialog
You need to write it after
dialog.show();
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(NewApplication.this);
alertDialogBuilder.setCancelable(false).setNegativeButton("Yes",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
alertDialog.getButton(Dialog.BUTTON_NEGATIVE).
setBackgroundColor(Color.parseColor("#3399ff"));
I think this has been addressed here before, IRC.
Check these answers posted earlier on a similar issue:
Android Button modify Question.
Good links and answers there.
First off, the
'
`DialogInterface.OnClickListener`'
listener — must not be NULL .
Second make sure you imported the correct Android classes, there are a lot of them, and some are version specific.
For example:
import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.os.Handler;
import android.util.Log;
Here is a quick and dirty sample of code in Java that illustrates the use of some special Android imports you need for an application. Remember Android uses many special classes and objects all its own.
final AlertDialog d = new AlertDialog.Builder(this)...create();
//final Button b1 = d.getButton(Dialog.BUTTON_POSITIVE);
editName.addTextChangedListener(new TextWatcher() //
{
public void afterTextChanged(Editable ed) {
Button b1 = d.getButton(Dialog.BUTTON_POSITIVE);
//comment out sections that may or may not work //
b1.setEnabled(ed.length() > 0);
private static final class [More ...] ButtonHandler extends Handler {
// Button clicks have Message.what as the BUTTON{1,2,3} constant//
private static final int MSG_DISMISS_DIALOG = 1;
private WeakReference<DialogInterface> mDialog;
public void // whatever classes you want //
ButtonHandler(DialogInterface dialog) {
mDialog = new WeakReference<DialogInterface>(dialog);
}
Use override to set replacement buttons or new button messages in a new section. It is one way to use the same button positioning for a new function. For example:
#Override
public void [More ...]
handleMessage(Message msg) {
switch (msg.what) {
case DialogInterface.BUTTON_POSITIVE:
case DialogInterface.BUTTON_NEGATIVE:
case DialogInterface.BUTTON_NEUTRAL:
((DialogInterface.OnClickListener)
msg.obj).onClick(mDialog.get(), msg.what);
break;
case MSG_DISMISS_DIALOG:
((DialogInterface) msg.obj).dismiss();
}
Hope this helps answer your question.
Code illustrated for illustration only, it is not the actual code, you need to write your own.
As an additional note: If you are using eclipse or other GUI interface, it tends to add a lot of additional and unnecessary code. Try coding on a text editor like Notepad++ and you will find it to be cleaner and smoother.
U try to Custom Alert dialog
<LinearLayout
android:id="#+id/ll_head"
android:layout_width="match_parent"
android:layout_height="30dp"
android:orientation="horizontal"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:background="#color/colorPrimaryDark"
app:layout_constraintRight_toRightOf="parent">
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="63dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_below="#+id/ll_head"
android:layout_alignParentStart="true"
android:background="#color/colorAccent"
android:layout_marginLeft="36dp"
android:layout_marginStart="36dp"
android:layout_marginBottom="36dp"
android:id="#+id/button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button"
android:layout_alignBottom="#+id/button"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="#android:color/holo_blue_bright"
android:layout_marginRight="53dp"
android:layout_marginEnd="53dp" /> </RelativeLayout>
public class MainActivity extends AppCompatActivity {
Dialog alertDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView=(TextView) findViewById(R.id.text);
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alertDialog=new Dialog(MainActivity.this);
alertDialog.setContentView(R.layout.dialog);
alertDialog.show();
}
});
}
}