I am trying to create controls with code, I was able to create them but my problem is that when creating them they do not adopt my custom style that I am using with material design properties. I am attaching an image where I have created controls manually with xml design and they adopt the material design style without problems, but at the bottom I created 3 controls (Textview, EditText and RadioButton) with java code which have not adopted the style. The problem happens in Api 19 or lower, in Api 21 and up I don't have this problem.
I did everything they told me and I don't find results, it's still the same, I leave my code that I'm using.
package com.ejemplo.probandoo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewGroup hourButtonLayout = (ViewGroup) findViewById(R.id.radio_group);
final RadioButton[] rb = new RadioButton[5];
RadioGroup rg = new RadioGroup(this);
//rg.setOrientation(RadioGroup.VERTICAL);
for(int i=0; i<5; i++){
rb[i] = new RadioButton(this);
rb[i].setText("foo bar");
rb[i].setId(i + 100);
rg.addView(rb[i]);
}
hourButtonLayout.addView(rg);
}}
That's my code and the result is the same it doesn't adopt my custom style:
The user originally had a layout similar to :
In which I wanted to change the colors of the layout and controls.
To achieve this, you have to change the theme colors that define the application, the theme is defined in the
AndroidManifest.xml
:This topic must be defined within
\values\styles.xml
and the colors defined inside
\values\colors.xml
should be (I got RGB colors similar to your image attached in the question):Editing the values for
colorPrimary
,colorPrimaryDark
andcolorAccent
, results in:* Important note: The colors shown in this image are taken from the image that was originally in the question, and was removed by the OP.
How to change color of RadioButtons and CheckBox created programmatically in API 19 or lower.
Apparently there is a problem changing the color of the controls in API 19 or lower through the theme, this if the controls are created programmatically .
I add this method which actually works for all versions as we use AppCompatRadioButton and AppCompatCheckBox classes
With this you will not have any problem changing the color of our programmatically created controls:
I attach the code to perform the example shown in the image:
In the layout we add the
RadioGroup
:Without doing anything, that is, from a project generated by android studio,
Shows where the controls will be created
Java to create dynamic controls
It has created 5 radiobuttons for me with the one
colorAccent
defined asstyle
when they are activated.If you want to force the controls to have another color different from the one
colorAccent
instyles.xml
addExtracted from SO
The activity must have the theme assigning.
Tested on real device:
Dear thanks to all for your collaboration, in the end my solution was to use the Appcompat widgets directly so that there are no incompatibility problems with versions prior to api 19; I leave you my code:
I leave you the reference link: https://developer.android.com/reference/android/support/v7/widget/AppCompatRadioButton.html