Kotlin: How to change background color of the toggle button? - kotlin

I hope to change the background of the Toggle Button.
There would be two ways, in xml and in code, I think..
I have made using xml like this, successfuly;
<ToggleButton
android:id="#+id/downloadButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="Download"
android:textOn="Downloaded"
android:background="#drawable/toggle_bg_sector"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
toggle_bg_sector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#drawable/toggle_download"
android:state_checked="false"/>
<item
android:drawable="#drawable/toggle_downloaded"
android:state_checked="true"/>
</selector>
toggle_download.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/blue" />
</shape>
toggle_downloaded.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/gray" />
</shape>
It works very well..
However, I want to know to make this Programmatically.
I made this inside of the onCreate.
downloadButton.setOnCheckedChangeListener { _: CompoundButton?, isChecked ->
if (isChecked)
downloadButton.setBackground(R.drawable.toggle_download)
else
downloadButton.setBackground(R.drawable.toggle_downloaded)
}
However, I got and error message on the R.drawable.toggle_download.
Can someone explain it?

You also change the color in MainActivity like This
downloadButton.setOnCheckedChangeListener { _: CompoundButton?, isChecked ->
if (isChecked)
downloadButton.setBackground(R.drawable.red)
else
downloadButton.setBackground(R.drawable.blue)
}
In this way you can easily change the background color of toggle button

Related

Custom Toast inflater must be initialized

i have this fun
private fun testToast(){
val inflater:LayoutInflater
val view = inflater.inflate(R.layout.custom_toast_message)
val toast = Toast(this.context)
toast.setGravity(Gravity.TOP,0,70)
toast.duration = Toast.LENGTH_LONG
toast.view =view
toast.show()
}
the main idea its to change the background color of the toast without getting a square toast
in this code i get an error in inflater.inflate
(inflater must be initialized)
this is the test xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/custom_toast_container"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#color/background_blue"
android:padding="16dp"
android:weightSum="1"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_toasticon"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"/>
<TextView
android:id="#+id/TextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="14sp"
android:layout_gravity="center_vertical"
android:layout_weight="0.8"
android:textColor="#color/gauge_red"
android:fontFamily="#font/roboto"
android:text="Prueba prueba PRUEBA"
/>
</LinearLayout>
con someone help me or annother way to do a toast with custom color.
change the color of a toast with rounded corners
You declared the inflater variable but never assigned anything to it (using =). You can’t use a variable’s value if you haven’t assigned anything to it yet.
If this function is in an Activity, you can delete the line that declares val inflater and replace inflater. with layoutInflater. to use the property of the Activity. If it’s in a Fragment, use requireActivity().layoutInflator..
By the way, the latest version of Android has deprecated setting a custom view for your toast. On Android R and later if you set a view on your Toast, it won’t show. They want you to use SnackBars instead. There might be a third party library available that could help create something more similar to Toasts.
you can make a drawable resource to achieve it
toast_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/teal_200" />
<corners android:radius="48dp" />
</shape>
and use this drawable for background of toast view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/custom_toast_container"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#drawable/toast_background"
android:orientation="horizontal"
android:padding="16dp"
android:weightSum="1">
<ImageView
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:src="#drawable/ic_launcher_background" />
<TextView
android:id="#+id/TextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="0.8"
android:text="Prueba prueba PRUEBA"
android:textColor="#color/purple_700"
android:textSize="14sp" />
</LinearLayout>
the result

Appearing two splash screen

I configured the splash screen and two are showing.
How do I remove it first?
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/primary" />
<item
android:drawable="#mipmap/logo"
android:height="300dp"
android:width="300dp"
android:gravity="center"
/>
</layer-list>
This is my splash screen and before she appears another one with the ic_launcher_round.

How Change toolbar color?(xamarin.forms)

How Change toolbar color?(xamarin.forms)
how change blue to green ?
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:pages="clr-namespace:News.Pages"
x:Class="News.MainPage"
BarBackgroundColor="Green">
<TabbedPage.ToolbarItems>
<ToolbarItem x:Name="btnSetting" Text="Setting" IconImageSource="Setting.png" Clicked="btnSetting_Clicked"/>
</TabbedPage.ToolbarItems>
<TabbedPage.Children>
<pages:WorldPage Title="World" IconImageSource="world.png"/>
<pages:TeredingPage Title="Trending" IconImageSource="trending.png"/>
<pages:GamePage Title="Game" IconImageSource="xbox.png"/>
</TabbedPage.Children>
</TabbedPage>
screen of app
In Android Resource/Layout/Toolbar xaml, there is one property
android:background="?attr/colorPrimary"
Changing background property like following code:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff99cc00"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:popupTheme="#style/ThemeOverlay.AppCompat.Light" />

Chage Lottie attributes for each keyframe, MotionLayout, Android Animation

How I could change Lottie and any other attributes inside MotionLayout?
I have start and end layouts and I one to add middle point(KeyFrame) in which I want to change lottie_rawRes="#raw/lottie_animation(json)" or any other attribute.
My code is here:
motion_scene(xml\activity_app_splash_screen_scene.xml):
<?xml version="1.0" encoding="utf-8"?>
<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:motion="http://schemas.android.com/apk/res-auto">
<Transition
motion:autoTransition="animateToEnd"
motion:constraintSetEnd="#layout/motion_splash_screen_end"
motion:constraintSetStart="#layout/motion_splash_screen_start"
motion:duration="5000">
<KeyFrameSet>
<KeyAttribute <-------- that one
motion:framePosition="50"
motion:motionTarget="#+id/splashSun" />
<KeyAttribute
android:alpha="0"
motion:framePosition="85"
motion:motionTarget="#+id/splashAppName"
/>
<KeyAttribute
android:alpha="1"
motion:framePosition="95"
motion:motionTarget="#+id/splashAppName" />
</KeyFrameSet>
</Transition>
<ConstraintSet android:id="#layout/motion_splash_screen_end">
<Constraint
android:id="#+id/splashClouds1"
>
<CustomAttribute
motion:attributeName="lottie_rawRes"
motion:customStringValue="#raw/weather_moon_clear" />
</Constraint>
</ConstraintSet>
Part of my start/end layout(layout\motion_splash_screen_start.xml):
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/splash_end"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/splashSun"
android:layout_width="0dp"
android:layout_height="0dp"
android:rotation="0"
android:scaleType="fitXY"
app:layout_constraintBottom_toTopOf="#+id/splashAppName"
app:layout_constraintDimensionRatio="1.4:1"
app:layout_constraintEnd_toStartOf="#+id/guideline2"
app:layout_constraintStart_toStartOf="#+id/guideline1"
app:layout_constraintTop_toTopOf="parent"
app:lottie_autoPlay="true"
app:lottie_loop="true"
app:lottie_rawRes="#raw/splash_sun" />
app:lottie_rawRes="#raw/splash_sun"
CustomAttribute calls the name based on bean conventions
so
<CustomAttribute motion:attributeName="lottie_rawRes"
motion:customStringValue="#raw/weather_moon_clear" />
Would look for a method setLottie_rawRes(String str)
And would not find it.
Look in the Lottie api and find the method you want
setAnimation(final String assetName)
Would you use motion:attributeName="animation"
setAnimationFromJson(String jsonString)
would mean you use motion:attributeName="animationFromJson"
Traditionally there is a match between attribute foo and method setFoo

Android RatingBar below Lollipop renders incorrectly

I'm using my custom rating bar. The top image with 5 stars is on Lollipop where my ratingbar works fine. However, below Lollipop (Kitat, JellyBean, ICS) versions the rating bar is goofed up (check second image with a single star)
What can be the problem? I tried all possible methods, changing theme, managing height width...nothing seems to work :(
This is the style
<style name="customRatingBar" parent="#android:style/Widget.RatingBar">
<item name="android:progressDrawable">#drawable/rating_custom_bar</item>
</style>
This is the rating bar xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+android:id/background"
android:drawable="#drawable/star_blank"/>
<item
android:id="#+android:id/secondaryProgress"
android:drawable="#drawable/star_blank"/>
<item
android:id="#+android:id/progress"
android:drawable="#drawable/star_filled"/>
</layer-list>
This is star blank
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/rating_empty" android:state_pressed="true" android:state_window_focused="true"/>
<item android:drawable="#drawable/rating_empty" android:state_focused="true" android:state_window_focused="true"/>
<item android:drawable="#drawable/rating_empty" android:state_selected="true" android:state_window_focused="true"/>
<item android:drawable="#drawable/rating_empty"/>
</selector>
This is star filled
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/rating_full" android:state_pressed="true" android:state_window_focused="true"/>
<item android:drawable="#drawable/rating_full" android:state_focused="true" android:state_window_focused="true"/>
<item android:drawable="#drawable/rating_full" android:state_selected="true" android:state_window_focused="true"/>
<item android:drawable="#drawable/rating_full"/>
</selector>
I have the same problem and spend a lot of time in it, tried setting all parameters at the ratingBar but none work, but there was something weird. I was working with a friend in a project and he compiles the exact same code as I and doesn't have this issue, so I change the IDE to his and all starts working.
I was using "Android Developer Tools", the eclipse you used to download at https://developer.android.com/training/index.html which is an eclipse with the ADT already embedded.
I change to Java EE and download ADT following this instructions http://developer.android.com/sdk/installing/installing-adt.html
I hope this helps you with your problem.
Has been a while but never hurts to answer. Here is my 3 step solution regardless of SDK version.
1- Add this as your RatingBar in your xml layout:
<RatingBar
android:id="#+id/new_ratingbar"
android:progressDrawable="#drawable/custom_drawable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="0.5"/>
If needed, you can give the RatingBar minHeight and maxHeight or add other attributes as required.
2- Here is the drawable you would refer the above progressDrawable to:
custom_drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#android:id/background"
android:drawable="#drawable/rating_bar_empty" /> <!--this is your .png file-->
<item
android:id="#android:id/secondaryProgress"
android:drawable="#drawable/rating_bar_fill" /> <!--this is your .png file-->
<item
android:id="#android:id/progress"
android:drawable="#drawable/rating_bar_fill" /> <!--this is your .png file-->
</layer-list>
3- The last thing is to provide the .png files in your various drawable folders.