How to call or open a fragment when a button is pressed

I am using an android app. I want to call or pop up a fragment when I click the rating button.

For example, here I attached a screenshot. When I press the speed button, how to trigger a popup or fragment similar to this

enter image description here

enter image description here

Please help me what to name this fragment?

+5
source share
2 answers

You only need to create another layout for the popup that will be shown and make it alignparenttop. Here is an example layout

Create a new dialog.xml XML file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mainLyt" android:layout_width="match_parent" android:layout_height="match_parent" > <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:background="#FFFFFF" android:paddingTop="10dp" > <TextView android:id="@+id/close" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:text="Fill in your design in this place" android:textSize="10dp" /> </RelativeLayout> </RelativeLayout> 

Once you have created your design. You need to call your onbutton click. Below is an example dialog

 ratingButton.setOnCLickListener(new view.OnCLickListener(){ @Override public void onCLick(View v){ final Dialog fbDialogue = new Dialog(ProfileActivity.this, android.R.style.Theme_Black_NoTitleBar); fbDialogue.getWindow().setBackgroundDrawable(new ColorDrawable(Color.argb(100, 0, 0, 0))); fbDialogue.setContentView(R.layout.facebook_dialogue); fbDialogue.setCancelable(true); fbDialogue.show(); } }); Hey I have put the code in onclicklistener. Check it . The above code works perfectly fine for me. As per my requirement I wanted the dialogue on the bottom of the screen . I have just made few lines of code change above to meet your requirement. My requirement was something like this 

enter image description here

+2
source

You can implement this as an overlay on top of the current layout. It can vary from implementation to implementation.

I implemented this using the following approach:

 <RelativeLayout> <- This is the parent <RelativeLayout id="actual_layout"> <- This is where you will implement the layout <!--Implement your layout here--> </RelativeLayout> <RelativeLayout id="rating_container" visibility="gone" background="#BB000000"> <- This is to set the translucent black background <RelativeLayout id="rating_dialog" alignParnetTop="true"> <- This is the rating dialog <!--Your implementation here--> </RelativeLayout> </RelativeLayout> </RelativeLayout> 

Now switch the visibility of "rating_container" to onCLick of your "Rating" button.

 ratingButton.setOnCLickListener(new view.OnCLickListener(){ @Override public void onCLick(View v){ toggleRatingContainer(true); } }); 

You can follow this approach to fulfill your requirement. Let me know if you need more help.

NOTE. I have not written the full code. In addition, I did not check, but directly typed everything.

EDIT

You can implement toggleRatingContainer as follows:

 public void toggleRatingContainer(boolean showRatingDialog){ ratingContainer.setVisibility((showRatingDialog)?View.VISIBLE:View.GONE); } 

Here, "ratingContainer" is a "RelativeLayout" with the identifier "rating_container", which you can get in your onCreate function.

And then you can switch its visibility to “View.GONE” when the “Send” or “Cancel” button is pressed, calling the same method that passes “false” as an argument.

0
source

Source: https://habr.com/ru/post/1237541/


All Articles