No visible button feedback after setEnabled(false) -> (true)/ selectableItemBackground as drawable? - java

With one button i'm disabling three other button's with button.setEnabled(false).
That works fine.
When this button is pressed again, the tree button's will be enabled with button.setEnabled(true).
They are still doing what they shoud and are fine responding to the onClickListener.
But since the enabling they do not visible respond when they get pressed.
How do i reactivate them right?
(i already searcht in google but didn't find anything).
private void startSleeping()
{
editorState.putBoolean("SLEEPING", true);
editorState.commit();
buttonDrink.setEnabled(false);
buttonEat.setEnabled(false);
buttonWash.setEnabled(false);
buttonDrink.setBackgroundColor(getResources().getColor(R.color.darkgray));
buttonEat.setBackgroundColor(getResources().getColor(R.color.darkgray));
buttonWash.setBackgroundColor(getResources().getColor(R.color.darkgray));
buttonSleep.setBackgroundColor(getResources().getColor(R.color.orange));
buttonWash.setTextColor(getResources().getColor(R.color.lightgray));
buttonDrink.setTextColor(getResources().getColor(R.color.lightgray));
buttonEat.setTextColor(getResources().getColor(R.color.lightgray));
buttonSleep.setTextColor(getResources().getColor(color.black));
}
private void stopSleeping()
{
editorState.putBoolean("SLEEPING", false);
editorState.commit();
buttonDrink.setEnabled(true);
buttonEat.setEnabled(true);
buttonWash.setEnabled(true);
buttonDrink.setBackgroundColor(getResources().getColor(R.color.transparent));
buttonEat.setBackgroundColor(getResources().getColor(R.color.transparent));
buttonWash.setBackgroundColor(getResources().getColor(R.color.transparent));
buttonSleep.setBackgroundColor(getResources().getColor(R.color.transparent));
buttonWash.setTextColor(getResources().getColor(R.color.white));
buttonDrink.setTextColor(getResources().getColor(R.color.white));
buttonEat.setTextColor(getResources().getColor(R.color.white));
buttonSleep.setTextColor(getResources().getColor(R.color.white));
}
<Button
android:id="#+id/buttonEat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:paddingBottom="#dimen/padding_size"
android:paddingTop="#dimen/padding_size"
android:text="#string/button_eat"
android:textColor="#color/white"
android:textColorHint="#color/white"
android:textSize="#dimen/text_size" />

This is because you are setting the background resource as transparent instead of what you applied in the layout as the background drawable to those buttons. Re-apply those background drawables and you'll get the touch feedback again. That should solve your problem.
On a side note for future implementations you can directly provide the enabled/disabled background resource in the drawable's list-selector itself. use state_enabled = true/false. This eliminates the need of doing all of that in the code.

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);

android change imageview programmatically

I am making an android app where the same imageview is displayed every time the user enters a value. but I want the imageview to be changed every time the user enters a value. I wrote a code to do this but the problem is after the first image is displayed I face black screen and it takes me to the first page in the app, it doesn't crash just shows black screen.
here is the imageview code in xml file:
<ImageView
android:id="#+id/ImageSuccess"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_marginBottom="20dip"
android:layout_marginTop="20dip"
android:scaleType="centerInside"
android:src="#drawable/image1" />
and here where I changed the imageview in java file:
ImageView myImage= (ImageView) findViewById(R.id.ImageSuccess);
if(userVlue.equals("SCHOOL")){
myImage.setImageResource(R.drawable.image1);
}
else if(userVlue.equals("CAR")){
myImage.setImageResource(R.drawable.image2);
}
else if(userVlue.equals("TOY")){
myImage.setImageResource(R.drawable.image3);
}
One of myImage or userVlue is not set properly and probably is null.
Debug and share logs. Your partcular activity is getting distroyed probably because of nullpointexception hence returning to main activity in backtrack.
I tried with a sample app and your logic is working perfectly.
You are handling user action on some UI element, when you do findViewById, cotext is that UI element, so this method searches ImageSuccess in children of it. Try giving page layout as rootlayout and do rootlayout.findViewById("ImageSuccess");
I found the solution finally, the black screen appeared because the images were only in the drawable file, so I needed to add the images in all drawbridge folders such as drawable-ldp, drawable-ldpi etc...., so that the app can work in different phone sizes.

Removing "Persistent Footer Button" When Keyboard is Called - Material Design

My question is the following, how do I go about removing a RelativeLayout containing buttons every time the user calls up the soft keyboard to type on an editText on the same layout. As you can see on my images below, the buttons within the red box need to disappear every time the user is inputing information and reappear after the keyboard has been called off.
FYI - The RelativeLayout needs to be fixed at the bottom when the keyboard is not in view so "layout_alignParentBottom='false' is not a solution for me. I think this most likely needs to be done programatically.
Any suggestions on how to tackle this problem would be highly appreciated.
<RelativeLayout
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="#dimen/activity_margin_zero"
android:background="#color/background"
android:minHeight="48dp"
android:padding="#dimen/activity_margin_zero">
Here is what Google Android Developer site describes as "Persistent Footer Button"
This is what I have but the so called "Persistent Footer Button" should disappear when the keyboard is on screen.
Buttons should be behind keyboard when it shows or disappear so that the user can input information with more screen real state.
Have you tried modifying the windowSoftInputModeproperty of your activity?
In your manifest, for the related activity, set the property like so:
<activity android:name="MyActivity"
...
android:windowSoftInputMode="adjustPan"
... >
</activity>
RelativeLayout footer = (RelativeLayout) findViewById(R.id.footer);
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks whether a hardware keyboard is available
if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
Toast.makeText(this, "keyboard visible", Toast.LENGTH_SHORT).show();
footer.setVisibility(View.VISIBLE)
} else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
Toast.makeText(this, "keyboard hidden", Toast.LENGTH_SHORT).show();
footer.setVisibility(View.INVISIBLE)
}
}
Note: The above code will only called for virtual keyboard also check out this Listen for keyboard show or hide event in android

Problems When Changing Visibility and Background of a View

I have a LinearLayout in which a TextView's visibility and background (GradientDrawable) changes according to a function myFunction():
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:animateLayoutChanges="true"
android:gravity="center"
android:orientation="vertical" >
(...Some views...)
<TextView
android:id="#+id/currentPercentage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:gravity="center"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:singleLine="true"
android:text="XX.X%"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/White" />
</LinearLayout>
The currentPercentage TextView has GONE visibility by default and, according to some events in the fragment that contains it, may have it changed to Visible (or back to GONE) and its background to a different GradientDrawable like so:
myFunction(...){
TextView currentPercentage= (TextView) getView().findViewById(R.id.currentPercentage);
if(condition ... ) {
Color color = // Color based on the condition...
GradientDrawable gd = new GradientDrawable(
GradientDrawable.Orientation.TL_BR,
new int[] {color,color});
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
int dp = Math.round(10f / (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
gd.setCornerRadius(dp);
currentChartPercentage.setBackground(gd);
} else if( otherCondition...)
{
// ... similar stuff ...
} else {
// Hide the currentPercentage TextView
currentPercentage.setVisibility(View.GONE);
}
}
This works fine as the TextView's background and visibility change smoothly whenever myFunction() is called and the other elements in the LinearLayout move (with animation) to make room for the TextView.
My issue is that whenever the visibility goes from GONE to VISIBLE, the View fades in nicely (as expected because of android:animateLayoutChanges="true") but right in the end shows up with an annoying black background where the GradientDrawable round corners are (see image).
Right after that, if I touch something else or drag the fragment (it's inside a ViewPager), the black background disappears. If myFunction() is called again, with the View already VISIBLE, then the background color changes as desired with no black background.
Also, if I disable the animation, I don't get the black background too. Thus, the problem seems to be related to the visibility going from GONE to VISIBLE with an animation.
This behavior is obtained on an emulator with Android 4.3 and I haven't tested it on my real device yet.
Does anybody have a clue on why this may be happening?
EDIT:
Just tested this on a real device with ICS without any issue.
EDIT 2:
I once had a problem with an undesirable background showing up in a ListView similar to Background ListView becomes black when scrolling but in this case, setting a cacheColorHint does nothing.

How to change the color of intern Android drawables?

I've tried to put the intern icons of Android (anrdoid.R.drawable.bla) into an ImageButton
and I wanted to change the color of Icon (not the Background!), but it doesn't work like I want to.
Here is what I've tried:
my ImageButton from the Layout:
<ImageButton
android:id="#+id/imageButton1"
android:layout_marginTop="100dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/background"
android:src="#android:drawable/ic_lock_silent_mode" />
what I've tried in my Activity:
Drawable myIcon = getResources().getDrawable( android.R.drawable.ic_lock_silent_mode);
ColorFilter filter = new LightingColorFilter( R.color.blue, R.color.blue);
myIcon.setColorFilter(filter);
No matter what Values I've tried for the LightingColorFilter it always gives the same result. The icon inside the Imagebutton gets dark. But thats not what I wnated. I just wanted to apply a color from my colors.xml, it somehow doesnt work out.
Is this even the right direction I'm going? Or is there another opertunity (And I don't mean coloring myself in Photoshop and putting them into the drawables folder)
this worked for me, for example it will paint in dark gray:
ImageButton imgBtn = (ImageButton) findViewById(R.id.imageButton11); // image button
imgBtn.setColorFilter(getResources().getColor(android.R.color.darker_gray), Mode.SRC_ATOP);

Categories

Resources