Dim Complete Background When a Fragment is opened in Android - java

How to dim complete background when a fragment is opened likewise when a DialogFragment is created.
Rightnow I am able to dim the background of only the app window using a view with match_parent as height and width. But using it does not dim the background of the status bar which happens with the DialogFragment.
I want to dim complete background including apps window and the status bar when the fragment is created or opened.
Right now this happens with the DialogFragment. But I have to use only Fragment.

That's what I did
I took a relative layout with height and width match_parent, apart from my fragment's main layout
<RelativeLayout
android:id="#+id/rl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/background_transition"/>
This is my background_transition file
<?xml version="1.0" encoding="UTF-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="00FFFFFF" />
<item android:drawable="#B3FFFFFF" />
</transition>
Then in order to dim the background, create TransitionDrawable object like this in your fragment
private TransitionDrawable transition;
and then call
transition.startTransition(300);
to dim the background, and
transition.reverseTransition(300);
to go back to normal background.
Hope that helps.

I tried Nuzha's version and my app crashed as well.
I did find two possible solutions.
1) If you use the following code it will make the back-fragment whiter, not dimmed:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
if (Build.VERSION.SDK_INT >= 23) {
view.alpha = 0.2F
}
Note:
Depending on the value you set, the closer the Float is to 0, the more transparent-white the background will be (only works with 23+)
2) If you would like the back-fragment to be dimmed (dark)
First, you can create a color in your resources folder and make it a bit transparent. I for one used the following one for a darker effect: #7E000000
Next, in the onViewCreated() method, create a variable of the color and add it as a ColorDrawable to the view's foreground.
if (Build.VERSION.SDK_INT >= 23) {
val color: Int = R.color.black_transparent
view.foreground = ColorDrawable(resources.getColor(color, resources.newTheme()))
Note
Make sure to disable any buttons that might be visible in the back-fragment, otherwise, if the user presses it, the app will crash
buttonRegisterEvent.isEnabled = false
As a final step, if you want the user to navigate back to the former fragment, make sure to remove the foreground color and enable any buttons that you might have disabled.

Try this:
fragmentParentLayout.getForeground().setAlpha(215);

Related

How to change Exomedia's default video control bar's color?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:EMVideoView="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#color/MUVCastleRock">
<com.devbrackets.android.exomedia.ui.widget.EMVideoView
android:id="#+id/video_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
EMVideoView:useDefaultControls="true" />
    
How do I change the color of the default controls (where you would see the play button)? I don't want to change the background behind the video, but instead the play bar. This is because my background is grey and so I want to change the color so that the bar is more visible.
EDIT:
#Shank suggested that I replace the images of the buttons in /exomedia/ui/widget/VideoControls.java
Although, I wasn't trying to change the images of these buttons, analyzing this class lead to my answer.
There are several functions within this class to change the settings of the default video controls.
You can set a title, description, subtitle, change out the button images (as Shank suggested), and solve my particular problem by changing the characteristics of the containers that contain the video controls (named controlsContainer).
The default container is initialized in retrieveViews() by the line:
controlsContainer = (ViewGroup) findViewById(R.id.exomedia_controls_interactive_container);
Using this, I simply called this reference from my EMVideoView and changed the color as appropriate:
emVideoView = (EMVideoView) myView.findViewById(R.id.video_view);
ViewGroup textContainer = (ViewGroup) emVideoView.findViewById(R.id.exomedia_controls_interactive_container);
textContainer.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.myColor));
Other useful methods that I discovered were as such:
emVideoView.getVideoControls().setTitle("title");
emVideoView.getVideoControls().setDescription("description");
emVideoView.getVideoControls().setSubTitle("sub");
emVideoView.getVideoControls().setPlayPauseImages(R.drawable.logo,R.mipmap.ic_launcher);

How to customize activities transitions on Lollipop, to work differently on some views?

Background
Lollipop introduces a new way to transition between activities (links here, here, here and here) .
The problem
They said on one of the videos (here) that I can choose exactly how each view will transition, but I can't find how to do it. The only thing I've found is how to set it for all views, except for the "hero" view (which you choose how to transition it to the new activity and back).
For example, let's take the Google Now app, which has this screen:
when you click on the editText, the bottom cards have the "explode" effect, but everything darkens and the imageView behind the editText fade out.
The editText is probably the "hero" view, which transitions between the activities, and because it's on the same location on the screen, this probably doesn't have any visual effect for the user.
What I've tried
I've tried to mimic what Google now did, but as I've written, the imageView also has the "explode" effect, so it goes to the bottom, behind the listView, which is a weird effect (since it gets cropped while animating). I'd prefer it to either animate to a different direction, or just fade out.
Here's a sample code of the transition I'm using:
final Intent intent = new Intent(activity, SearchActivity.class);
ViewCompat.setTransitionName(viewToTransitionFromAndTo, VIEW_TO_TRANSITION_TO);
final ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(activity,
viewToTransitionFromAndTo, VIEW_TO_TRANSITION_TO);
ActivityCompat.startActivityForResult(activity, intent, requestCode, options.toBundle());
in the theme of the activity that starts the other activity, I have this:
<style name="AppTheme.transition" parent="#style/AppTheme">
<!-- transition support -->
<item name="android:windowContentTransitions" tools:targetApi="21">true</item>
<item name="android:windowActivityTransitions" tools:targetApi="21">true</item>
<item name="android:windowEnterTransition" tools:targetApi="21">#transition/explode</item>
<item name="android:windowExitTransition" tools:targetApi="21">#transition/explode</item>
<item name="android:windowSharedElementEnterTransition" tools:targetApi="21">#transition/move_image</item>
<item name="android:windowSharedElementExitTransition" tools:targetApi="21">#transition/move_image</item>
<item name="android:windowAllowReturnTransitionOverlap" tools:targetApi="21">true</item>
<item name="android:windowAllowEnterTransitionOverlap" tools:targetApi="21">false</item>
</style>
and the transition files are:
explode.xml
<?xml version="1.0" encoding="utf-8"?>
<explode />
move_image.xml
<?xml version="1.0" encoding="utf-8"?>
<transitionSet>
<changeBounds />
<changeImageTransform />
</transitionSet>
The question
How can I choose what each view will do upon transition, instead of just saying everything to have the same effect (except for the "hero" view) ?
For example, is it possible to choose "explode" transition for all views except for the "hero" view , and a view that will have a different transition (fade out/in, for example) ?
Please show an example of how to do it. You can use the code I've written above if needed.
Use a TransitionSet and add/exclude certain targets using Transition#addTarget() and Transition#excludeTarget(). For example, let's say you have two views and you want each of them to slide off the screen in different directions. You could create such a transition using the following code:
TransitionSet set = new TransitionSet();
Transition slideUp = new Slide(Gravity.UP);
slideUp.addTarget(view1);
set.addTransition(slideUp);
Transition slideDown = new Slide(Gravity.DOWN);
slideDown.addTarget(view2);
set.addTransition(slideDown);
TransitionSet extends Transition, so the resulting set object can then be used as the window content transition, which will play the two slide transitions simultaneously in parallel.

Functions for Buttons

I'm trying to figure out how to do this thing for an android app.
So I made an array of buttons
Button btn[][] = new Button[10][10];
How do I make it so that once I click a button that it, for instance, turns a different color?
I have had trouble making it because I can create the array, and it looks nice, but how do I assign different functions for individual buttons? Are buttons in the array already labeled and can I use individual ones? Thanks.
Any time you would like to make a view change a different color based on the user actions, you should use a state list drawable
Here is a very simple example of a state list drawable which you would use to trigger only off whether the user had pressed the view or not.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:color="#color/brown" />
<item android:state_pressed="true" android:color="#color/brown_selected" />
</selector>
You would then set this on the view with the following attribute to the view in xml
android:background="#drawable/background"
This though, will only be changed while the user is pressing the button. If you would like it to permenantly change color, use a on click listener. For example if you would like to change the background color to white:
button.setOnClickListener(new View.OnClickListener() {
/**
* Handle a user clicking on the view v
* #param v the view the user clicked on. In this case the button
*/
#Override public void onClick(View v) {
// Set the background color to white
v.setBackgroundColor(Color.WHITE);
}
});

Space between keyboard and EditText in Android

Soft input mode is "SOFT_INPUT_ADJUST_PAN", and when keyboard shown, EditText moves up to stay visible, but the keyboard and txt's bottom are always sticked together as seen on screenshot, I want some space between them, is it possible?
Also input mode must stay as "SOFT_INPUT_ADJUST_PAN".
Sorry form my english, Thanks in Advance...
I applied above methods which worked for me was
Set android:windowSoftInputMode on the Activity to "adjustPan"
than i added the padding to the edittext with gravity bottom
and it works like charm.
Thanks,
May this help you:
If this line is written in your code then remove it:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
OR:
You should not be setting your layout_width and layout_heights with explicit pixel values. Instead, use wrap_parent or fill_parent as appropriate.
OR:
If you have set your minSdkVersion to 3 then change it to minSdkVersion=4
Edit:
Set android:windowSoftInputMode on the Activity to "adjustPan"
OR:
Add this code in your main activity in setOnTouchListener() of your EditText:
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED);
Why don't you try this?
android:windowSoftInputMode="adjustResize"
I know you want to adjust pan but this would solve your problem aswell.
Since you really want to stay with adjustPan, maybe you can try re-designing the XML layout a little bit.
You must also add your required padding to the outer most container element in the layout file.
Simple padding in edit text won't work.
Use drawable with your edit text like below:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="8dp"
android:left="8dp"
android:right="8dp"
android:top="8dp">
<shape android:shape="rectangle">
<corners android:radius="10dp" />
<stroke
android:width="1dp"
android:color="#color/white" />
<solid android:color="#color/transparent" />
</shape>
</item>
</layer-list>
and then use
android:windowSoftInputMode="adjustPan"
in your manifest file.
Just wondering, is your app full screen? does it have the status bar? If it is try to disable full screen mode and try to see if that solves your problem. I remember there was a bug about this some time ago.
try to use GlobalLayoutListener:
private var focusView:View?=null
// Register global layout listener.
decorView?.viewTreeObserver?.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
private val windowVisibleDisplayFrame = Rect()
private var lastVisibleDecorViewHeight: Int = 0
override fun onGlobalLayout() {
// Retrieve visible rectangle inside window.
decorView.getWindowVisibleDisplayFrame(windowVisibleDisplayFrame)
val visibleDecorViewHeight = windowVisibleDisplayFrame.height()
// Decide whether keyboard is visible from changing decor view height.
if (lastVisibleDecorViewHeight != 0) {
if (lastVisibleDecorViewHeight > visibleDecorViewHeight + MIN_KEYBOARD_HEIGHT_PX) {
// Calculate current keyboard height (this includes also navigation bar height when in fullscreen mode).
val currentKeyboardHeight = decorView.height - windowVisibleDisplayFrame.bottom
//keyboardHeight = currentKeyboardHeight
focusView = activity?.currentFocus
focusView.setPadding(0, 0, 0, SET_BOTTOM_PADDING_SPACE) // <---set space here
} else if (lastVisibleDecorViewHeight + MIN_KEYBOARD_HEIGHT_PX < visibleDecorViewHeight) {
focusView.setPadding(0, 0, 0, 0)
}
}
// Save current decor view height for the next call.
lastVisibleDecorViewHeight = visibleDecorViewHeight
}
})

Android - Set Application Background

I am trying to set my applications background to a user chosen image, but I am having some trouble. Is it possible for someone to give me an example of how to do this? I can set the image ok from my resources, but when I try to use an image on the phone I can't seem to make it work.
Assuming you have created a method to let the user choose the path to the image, use this:
// Variable with the path to the background
String bg_path = "/sdcard/bg/background.png" // <-- This path can be whatever you like
//Change background of Activity
getWindow().setBackgroundDrawable(Drawable.createFromPath(bg_path));
Oh Don't forget to set you Layout background color to transparent in the XML file or you won't see the image. (this is valid to anything that fills parent window like a listview, for instance)
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout="#+id/m_layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android.background="Color.TRANSPARENT"
>

Categories

Resources