Android: set view style programmatically

Technically you can apply styles programmatically, with custom views anyway: private MyRelativeLayout extends RelativeLayout { public MyRelativeLayout(Context context) { super(context, null, R.style.LightStyle); } } The one argument constructor is the one used when you instantiate views programmatically. So chain this constructor to the super that takes a style parameter. RelativeLayout someLayout = new MyRelativeLayout(new ContextThemeWrapper(this,R.style.RadioButton)); … Read more

Differences between ConstraintLayout and RelativeLayout

The intention of ConstraintLayout is to optimize and flatten the view hierarchy of your layouts by applying some rules to each view to avoid nesting. The Rules are similar to RelativeLayout, for example setting the bottom edge to the bottom of some other view. app:layout_constraintBottom_toBottomOf=”@+id/view1″ Unlike RelativeLayout, ConstraintLayout offers a bias value that is used … Read more

Android: How to create a Dialog without a title?

FEATURE_NO_TITLE works when creating a dialog from scratch, as in: Dialog dialog = new Dialog(context); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); But it doesn’t work when creating an AlertDialog (or using the Builder), because it already disables the title and use a custom one internally. I have looked at the SDK sources, and I think that it can’t be worked … Read more

How to center the elements in ConstraintLayout

There is a simpler way. If you set layout constraints as follows and your EditText is fixed sized, it will get centered in the ConstraintLayout: app:layout_constraintLeft_toLeftOf=”parent” app:layout_constraintRight_toRightOf=”parent” app:layout_constraintTop_toTopOf=”parent” app:layout_constraintBottom_toBottomOf=”parent” The left/right pair centers the view horizontally and top/bottom pair centers it vertically. This is because when you set the left, right or top,bottom constraints bigger … Read more

Android LinearLayout Gradient Background

Ok I have managed to solve this using a selector. See code below: main_header.xml: <?xml version=”1.0″ encoding=”utf-8″?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”50dip” android:orientation=”horizontal” android:background=”@drawable/main_header_selector”> </LinearLayout> main_header_selector.xml: <?xml version=”1.0″ encoding=”utf-8″?> <selector xmlns:android=”http://schemas.android.com/apk/res/android”> <item> <shape> <gradient android:angle=”90″ android:startColor=”#FFFF0000″ android:endColor=”#FF00FF00″ android:type=”linear” /> </shape> </item> </selector> Hopefully this helps someone who has the same problem.

Android list view inside a scroll view

For any Child view to scroll inside a ScrollView. Anything like ListView, RecyclerView, etc. You just have to replace ScrollView with androidx.core.widget.NestedScrollView in your current xml and then magic happens. Below is a sample xml code : <?xml version=”1.0″ encoding=”utf-8″?> <androidx.core.widget.NestedScrollView xmlns:android=”http://schemas.android.com/apk/res/android” xmlns:app=”http://schemas.android.com/apk/res-auto” android:layout_width=”match_parent” android:layout_height=”match_parent”> <androidx.appcompat.widget.LinearLayoutCompat android:layout_width=”match_parent” android:layout_height=”wrap_content” android:orientation=”vertical” android:padding=”16dp” android:paddingBottom=”20dp”> <TextView android:layout_width=”match_parent” android:layout_height=”wrap_content” android:text=”Recycler … Read more

How to change the color of a CheckBox?

You can change the color directly in XML. Use buttonTint for the box: (as of API level 23) <CheckBox android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:buttonTint=”@color/CHECK_COLOR” /> You can also do this using appCompatCheckbox v7 for older API levels: <android.support.v7.widget.AppCompatCheckBox android:layout_width=”wrap_content” android:layout_height=”wrap_content” app:buttonTint=”@color/COLOR_HERE” />