How to keep a DialogFragment on top of newly created activities? - android

I have an Activity A, which opens a DialogFragment. In this dialog, a button opens an Activity B.
I would like this Activity B to open below the DialogFragment (which remains open), and I don't want the dialog to be recreated.
How can I achieve this? Is there a way to change the DialogFragment's parent Activity?
Cheers.

You can use a transition to do this.
public void Trans(View v){
Intent intent = new Intent(this,SecondActivity.class);
String transitionName = getString(R.string.transition_album_cover);
ActivityOptionsCompat options =
ActivityOptionsCompat.makeSceneTransitionAnimation(HomeActivity.this,
albumCoverImageView, // The view which starts the transition
transitionName // The transitionName of the view we’re transitioning to
);
ActivityCompat.startActivity(HomeActivity.this, intent, options.toBundle());
}
In the layout of HomeActivity:
<ImageView
android:layout_height="200dp"
android:layout_width="200dp"
android:src="#drawable/pic"
android:id="#+id/transPic"
android:onClick="Trans"
android:transitionName="#string/transition_album_cover" />
And in the layout of SecondActivity
<ImageView
android:layout_height="300dp"
android:layout_width="match_parent"
android:scaleType="centerCrop"
android:id="#+id/transPic"
android:src="#drawable/pic"
android:transitionName="#string/transition_album_cover" />
Here I used an ImageView as a common element in both HomeActivity and Second Activity. I believe you can try something of this sort for the dialogue box too. I have not done it for Dialog box myself though.
EDIT: Changing the parent of the dialogue is not something that is achievable this way. I didn't read that part when I was posting the answer. But this is still worth a try I guess, if you are not obliged to use a Dialog itself.

Related

Stop ImageButton tooltip from appearing on hover through a fragment

Currently I observe a strange behavior of showing a tooltip "through" a fragment and I have no idea how to get rid of it. Any idea or hint would be appreciated!
I have an ImageButton with some contentDescription:
<androidx.appcompat.widget.AppCompatImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="Content description"
android:src="#drawable/ic_image"
/>
This is made, to allow user to see a help text when hovering an Stylus/S-Pen or a mouse pointer over the image. This is a desired behavior:
When an AlertDialog is displayed over the activity, no tooltip is displayed. This is also an expected behavior:
But, when a fragment is displayed over the ImageButton the tooltip is always displayed. This is a non-desired behavior and the great mystery of this question. The fragment has half-transparent green background:
A simple FrameLayout is used in the fragment layout:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#B94EC14A"
/>
As is the user can click through the fragment.
If these lines are added, one can get rid of "click through" behavior, but not from "hover through":
layout1.setClickable(true);
layout1.setFocusable(true);
layout1.setOnTouchListener((v, event) -> true);
layout1.setOnHoverListener((v, event) -> true);
layout1.setOnGenericMotionListener((v, event) -> true);
It looks like some other kind of events is used here.
Which kind of events are used to translate "on hover" event through the view?
How prevent this tooltip from appearing? (similar behavior as with AlertDialog is required)
Unfortunately I could not find the cause of the problem, so I ended up starting a fragment as a Dialog:
MyFragment fragment = new MyFragment();
...
fragment.setArguments(bundle);
fragment.setCancelable(false);
fragment.show(getSupportFragmentManager(), "dialog");
Or for the different use case packing the fragment to an empty activity and starting this activity:
Intent intent = new Intent(this, MyActivity.class);
intent.putExtra(...);
startActivityForResult(intent, REQUEST_CODE);
In this case (as already described in the question) the tooltips are not displayed on hover.

visibility depends on another activity

I'm developing an android app in which I want to apply button visibility functionality in activity_2 and that visibility should depend on button click from activity_1
Ex. In activity_2 I have :
<Button android:id="#+id/button1"
android:text="ABC"
android:visibility="gone"/>
It should visible on button click from activity_1
Activity_1 :
<Button android:id="#+id/button1"
android:text="ABC"/>
Please suggest me, I'm a beginner
Pass the visibility you want from Activity A to Activity B in a Bundle.
Passing a Bundle on startActivity()?
Try this:
On clicking the button in the first activity,send a value to second activity through intent.
Intent intent = new Intent(Activity1.this,Activity2.class);
intent.putExtra("button","clicked");
startActivity(intent);
And then get this value in the onCreate of second activity like this:
String value = getIntent().getStringExtra("button");
then check the value with if statement
if(value.equalsIgnoreCase("clicked")){
//make your button visible here
}
else{
//button not visible
}

Dynamically show Activity as dialog

I have an Activity that I have already implemented sometime ago.
It involves around making a in app purchase, so all the logic is relatively self contained. it doesn't need to care about anything else.
Now, i wish to make that Activity to optionally show up in a dialog in some other activity. Is there a quick way to do that? I still need to keep the old behavior however, where the activity show up as a regular screen.
So is there someway that I could launch the activity with that make it show up as a dialog?
Thanks
You cant show activity as dialog.
Your options are:
1: Open the other activity with some boolean extra like "showDialog", true
Intent intent = new Intent(this, OtherActivity.class);
intent.putExtra("showDialog", true);
and in the other activity in (for example) onCreate:
Boolean showDialog = getIntent().getExtras().getBoolean("showDialog");
if (showDialog) {
// Code to show dialog
}
2: Create a DialogFragment and show it in your original activity. This custom DialogFragment you can use on both activities
https://guides.codepath.com/android/Using-DialogFragment
Probably your cleanest option depending on how complex your Activity is, is to create a new DialogFragment based on your current activity.
A DialogFragment is basically a Fragment, so has a relatively similar set of lifecycle callbacks to your Activity so it shouldn't be too difficult to re-work as a DialogFragment.
If the in-app purchase framework has specific callback requirements with an Activity then you will need to take that into account.
Another separate option would be to mock the appearance of a Dialog, by creating an Activity that may be transparent around the border of the main content.
Just Inflate the layout one button click on onCreate Method.
WhAT I WILL SUGGEST IS try alert box and in place of normal layout inflate you activity layout .
these might help
The easiest way to do that is to apply a dialog theme to the activity:
<activity android:theme="#style/Theme.AppCompat.Dialog" />
Or in the code:
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Theme_AppCompat_Dialog);
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
}
You can customize parameters of the theme in styles.xml, e.g. dim enabled/disabled, click outside behavior.
The crucial point is to perform setTheme() before super.onCreate(), because Theme is immutable, once set through super.onCreate() it cannot be mutated later.

How do I expand CardViews to show more detail like Google Keep cards?

I have some CardViews in my app and I want them to function like the cards in Google Keep. For example, when I click on a card that has text, it expands (with the animation) into another view.
If you're not sure what I mean, create a note on the Google Keep Android app, tap on the card that appears when the note is created. This is exactly what I want to happen in my app.
How do I go about doing this?
New in Lollipop!
Activity + Fragment Transitions
By declaring "shared elements" that are common across two screens you can create a smooth transition between the two states.
album_grid.xml:
<ImageView
…
android:transitionName="#string/transition_album_cover" />
album_details.xml:
<ImageView
…
android:transitionName="#string/transition_album_cover" />
Java:
AlbumActivity.java
Intent intent = new Intent();
String transitionName = getString(R.string.transition_album_cover);
…
ActivityOptionsCompat options =
ActivityOptionsCompat.makeSceneTransitionAnimation(activity,
albumCoverImageView, // The view which starts the transition
transitionName // The transitionName of the view we’re transitioning to
);
ActivityCompat.startActivity(activity, intent, options.toBundle());
Here we define the same transitionName in two screens. When starting the new Activity and this transition is animated automatically. In addition to shared elements, you can now also choreograph entering and exiting elements.

DialogFragment - Implement an OK button and other issues

I am trying to implement a relatively simple DialogFragment that is supposed to contain an image and a OK button and I want to display it on demand from my activity.
I set its layout in onCreateView via inflater.inflate, but I can't figure out how to tell it that the implementation for the OK button event handler is located in my custom DialogFragment class. It seems to try to find it in the activity, which is not what I want. Would calling getDialog().dismiss() be enough to dismiss it?
Here is how I create a dialog in my activity:
ResponseDialog dialog = new ResponseDialog();
dialog.show(getFragmentManager(), "dialog_response_image");
Also, some folks say that my custom DialogFragment should set getDialog().setCanceledOnTouchOutside(true);, but where should I set this. In onActivityCreated?
How can I get access to its view from the activity, if I wish to set the source of the image contained by it?
Also, for some reason, it fills up the entire display, even if I use static width / height. Does anyone know how to fix this? - I managed to fix this by swithching to LinearLayout instead of RelativeLayout in the DialogFragment layout XML...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="#color/background_color"
tools:context=".MainActivity"
android:layout_width="200dp"
android:layout_height="400dp"
android:id="#+id/dialogImageReponse" >
<Button
android:id="#+id/dialogButtonOk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="#string/ok"
android:onClick="Ok" />
</RelativeLayout>
I'll try to answer each of your questions:
1.) About the "how to tell it that the implementation for the OK button event handler is located in my custom DialogFragment class" I'm not sure why you want to do that since you haven't told us.
2.) "Would calling getDialog().dismiss() be enough to dismiss it?". If you wish to dismiss the DialogFragment from your Activity then you just need to call dialog.dismiss() ('dialog' here refers to ResponseDialog dialog = new ResponseDialog(); so obviously you can only call dialog.dismiss() once you've created the ResponseDialog object.
3.) Regarding "getDialog().setCanceledOnTouchOutside(true);" Once again you just need to call
dialog.setCanceledOnTouchOutside(true);
immediately after this:
ResponseDialog dialog = new ResponseDialog();
dialog.show(getFragmentManager(), "dialog_response_image");
4.) You can get access to its view by calling:
View v = dialog.getView();
Although if you just wish to set the source of the image to be used contained by it, and I assume you want to use a photo or a picture stored in your phone then you would have to use a parametrized constructor for ResponseDialog like this and as a parameter you would have to pass the URI OR the filepath of the picture that you would like displayed in the dialogfragment :
ResponseDialog dialog = new Response(String filepath);
and then in your custom dialog class which I understand is ResponseDialog you would have to use this filepath (received in the constructor) to create a bitmap and then set the bitmap as the source of the ImageView in that DialogFragment.

Categories

Resources