Other questions say that the style cannot be set programmatically, but a View can be initialised with a style such as when it is loaded from XML.
How can I initialise a View with a particular style programmaticly (not in XML)? I tried using View(Context context, AttributeSet attrs, int defStyle), but I don't know what to parse in for the second argument. Passing in null results in the View not being displayed
I'm having the same problem, but haven't found any practical way to directly set a style programmatically, so far. I would like to populate my screen with a lot of widgets, of a given type, let's say buttons. It is impractical to define them all in the layout file. I would like to create them programmatically, but I would also like to define their style in a style xml file.
The solution I have devised consists in defining just one of those widgets in the layout file, create all the others programmatically, and clone the style info from the first one to the other ones.
An example follows.
In the style file, define the style for your buttons. For example:
<style name="niceButton">
<item name="android:layout_width">160dip</item>
<item name="android:layout_height">60dip</item>
<item name="android:gravity">center</item>
<item name="android:textSize">18dip</item>
<item name="android:textColor">#000000</item>
</style>
Then subclass class "Button", by deriving a class "NiceButton". Define the constructor that will be needed by the inflater:
public NiceButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
Then define another constructor, which purpose is to clone an existing button:
public NiceButton(int id, NiceButton origButton) {
super(origButton.getContext());
setId(id);
setLayoutParams(origButton.getLayoutParams());
setGravity(origButton.getGravity());
setPadding(origButton.getPaddingLeft(),
origButton.getPaddingTop(),
origButton.getPaddingRight(),
origButton.getPaddingBottom());
setTextSize(TypedValue.COMPLEX_UNIT_PX, origButton.getTextSize());
setTextColor(origButton.getTextColors());
// ... also copy whatever other attributes you care about
}
In your layout file, define just the first one of your buttons. Suppose for example that you want to put your buttons in a table:
<TableLayout android:id="#+id/button_table"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow android:id="#+id/button_row_0">
<com.mydomain.mypackage.NiceButton
style="#style/niceButton" android:id="#+id/button_0" />
<!-- More rows/buttons created programmatically -->
</TableRow>
</TableLayout>
Notice that the full qualified name of the widget class is used; obviously, you will have to replace com.mydomain.mypackage with the actual package name.
In your activity, you may want to define an array which is going to hold a reference to all of the buttons, and a common listener to be called when any of the buttons is pressed:
NiceButton[] mButtonViews = new NiceButton[10];
private View.OnClickListener mNiceButtonClickListener = new View.OnClickListener() {
public void onClick(View view) {
int i = view.getId();
mButtonViews[i].setText("PRESSED!");
}
};
Notice how the view id is used as an index in the array of buttons. So you will need your buttons to have an id from 0 to n-1.
Finally, here is the way you can create your buttons in the onCreate method:
// Retrieve some elements from the layout
TableLayout table = (TableLayout)findViewById(R.id.button_table);
TableRow row = (TableRow)findViewById(R.id.button_row_0);
NiceButton origButton = (NiceButton)findViewById(R.id.button_0);
// Prepare button 0
origButton.setId(0);
origButton.setText("Button 0");
origButton.setOnClickListener(mNiceButtonClickListener);
mButtonViews[0] = origButton;
// Create buttons 1 to 10
for (int i = 1; i < 10; i++) {
if (i % 2 == 0) {
row = new TableRow(this);
table.addView(row);
}
NiceButton button = new NiceButton(i, origButton);
button.setText("Button " + i);
button.setOnClickListener(mNiceButtonClickListener);
mButtonViews[i] = button;
row.addView(button);
}
Here's how the screen appears after you have pressed some buttons:
Well, there's some code involved, but in the end you can create as many widgets you want programmatically, and still have their attributes defined as a style.
If you want to style a view you have 2 choices: the simplest one is to just specify all the elements in code:
button.setTextColor(Color.RED);
button.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
The other option is to define the style in XML, and apply it to the view. In the general case, you can use a ContextThemeWrapper for this:
ContextThemeWrapper newContext = new ContextThemeWrapper(baseContext, R.style.MyStyle);
button = new Button(newContext);
To change the text-related attributes on a TextView (or its subclasses like Button) there is a special method:
button.setTextAppearance(context, R.style.MyTextStyle);
This last one cannot be used to change all attributes; for example to change padding you need to use a ContextThemeWrapper. But for text color, size, etc. you can use setTextAppearance.
AttributeSet contains the list of attributes specified in xml (ex. layout_width, layout_height etc).
If you are passing it as null, then you should explicitly set the height/width of view.
Related
hi guys I'm new to styling android layouts and I want to ask if there is a way to apply a drawable background to a widget (ex: all buttons in a layout) without having to type the android:drawable in each widget.
You can create class extend Button :
public class CustomButton extends Button {
public CustomButton(Context context, AttributeSet attrs) {
super(context, attrs);
//set drawable here
}
}
and in xml file call CustomButton :
<yourpackage.name.CustomButton
android:id="#+id/xxx"
android:text="CustomButton" />
You can add it in every layout but you can also add it programmatically by iterating every control on the page. I did this recently. I didn't like the result because it was impossible (literally) to check the existing styling to see if I needed to replace it -I didn't want to restyle labels- but it was something like this:
//change linerlayout to whatever viewgroup type you have
LinearLayout layout = (LinearLayout) findViewById(R.id.name_of_your_linearlayout);
for(int count = 0; count < layout.getChildCount(); count ++) {
if (layout.getChildAt(count)) instanceOf EditText) {
/*do your work here. Note you don't have to limit yourself
to theming. You could add an event to every button or textbox
at the same time */
}
}
I've created a custom control which is a subclass of LinearLayout. I have also created a layout file on which this control is based. Finally I have defined attributes which I parse in the constructor to set my custom properties on. As an example, one of those properties is called 'text'.
Here's a simplified version of my code (I've stripped out a lot of the other properties and such so we can just focus on the one property 'text'):
First, the class (our custom version of a RadioButton)...
public class RadioButton extends LinearLayout
{
private TextView textView;
public RadioButton(Context context, AttributeSet attrs)
{
super(context, attrs);
initAttributes(attrs, 0);
}
private void initAttributes(AttributeSet attrs, int defStyle)
{
final TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CheckBoxView, defStyle, 0);
text = a.getString(R.styleable.RadioButton_text);
if(text == null)
text = "Not set";
a.recycle();
}
#Override
protected void onFinishInflate()
{
super.onFinishInflate();
textView = (TextView)findViewById(R.id.textView);
textView.setText(text);
}
private String text;
public String getText() { return text; }
public void setText(String newValue)
{
text = newValue;
if(textView != null)
textView.setText(text);
}
}
Here's the attrs.xml file...
<resources>
<attr name="text" format="string" />
<declare-styleable name="RadioButton">
<attr name="text" />
</declare-styleable>
</resources>
And here's the 'reusable_radiobutton.xml' layout file (Note: the internal RadioButtonView is a custom-rendered View and works fine):
<?xml version="1.0" encoding="utf-8"?>
<com.somedomain.reusable.ui.RadioButton
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<com.somedomain.reusable.ui.RadioButtonView
android:id="#+id/radioButtonView"
style="#style/DefaultRadioButtonView" />
<TextView
android:id="#+id/textView"
style="#style/DefaultRadioButtonText" />
</com.somedomain.reusable.ui.RadioButton>
With the above, users of my control can simply include it in their own layout files, like so...
<include android:id="#+id/someRadioButton"
layout="#layout/reusable_radiobutton" />
The in their code, using the following, they can get that instance and do with it what they wish, like so...
RadioButton someRadioButton = (RadioButton)findViewById(R.id.someRadioButton);
someRadioButton.text = "Woot!";
This works as expected.
However, this doesn't...
<include android:id="#+id/someRadioButton"
layout="#layout/reusable_radiobutton"
app:text="Hello World!" />
It gives me a warning, but otherwise ignores it.
I then tried this...
<com.somedomain.reusable.ui.RadioButton
app:text="Hello World!" />
While this does instantiate my control and does pass 'Hello World!' to my property via the attributes, nothing actually loads or even associates the layout file to my class so nothing appears on the screen!
So how can I create a custom view, based on a layout, which other developers can simply reference in their own layout files while also allowing them to set custom attributes?
Hope that all made sense! :)
Note: The Android documentation talks about exactly what I'm after, 'Compound Controls' as referenced here, but they don't give an example of using a layout to define the compounded control. However, I feel that was pretty close.
Found it here on SO. I didn't realize you could inflate something into yourself. That and using the Merge tag took care of it for me.
Using Theme.AppCompat in Gingerbread (API 10), programmatically added buttons do not match buttons added through XML. It works fine in all newer APIs, its only an issue with Gingerbread. This image shows the issue.
Here is the code that adds the buttons:
for (int i = 0; i < btnFiles.length; i++) {
btnFiles[i] = new Button(this);
btnFiles[i].setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
btnFiles[i].setGravity(Gravity.CENTER);
btnFiles[i].setId(100 + i);
btnFiles[i].setText(fileList.get(i).replace(".xml", ""));
btnFiles[i].setTag(fileList.get(i));
registerForContextMenu(btnFiles[i]);
btnFiles[i].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Continue(v); //Start next activity when button is pressed
}
});
l.addView(btnFiles[i]);
setTitle(getString(R.string.title_activity_load_menu));
}
Make a layout file with just the Button and use a LayoutInflater to inflate it.
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
... />
LayoutInflater inflater = getLayoutInflater();
for (int i = 0; i < btnFiles.length; i++) {
btnFiles[i] = (Button) inflater.inflate(R.layout.button, l, false);
// everything else, except the LayoutParams stuff because that's in the layout file
}
Just to clarify for anyone passing by, the issue is probably that <Button> tags from XML get replaced by instances of AppCompatButton and <item name="buttonStyle">...</item> (assuming that's what had been used) applies to that. Same thing happens for many other Views.
So, an alternative possibility would be either to put both <item name="buttonStyle">...</item> and <item name="android:buttonStyle">...</item> into the style, so that Button and AppCompatButton can be combined, which would be quite a mess.
Somewhat better option would be to instantiate AppCompatButton for the APIs using AppCompat, but using the XML layout with just a single Button seems like the safest and most portable solution, so go for that, assuming you have no reason not to do that.
I want to create a custom Android View (MyCustomView). In this View I want to have a property of a custom Type (MyCustomType). Similar to this:
MyCustomView extends LinearLayout {
private MyCustomType prop1;
public MyCustomType getProp1()
{
return this.prop1;
}
public void setProp1(MyCustomType value)
{
this.prop1 = value;}
}
}
So far so good. But now I want to be able to set the value of this property from XML. I can create a custom attribute with string, int, reference format, but I do not see how to define this attribute to be of MyCustomType format. I image something similar to this:
<declare-styleable name="MyCustomView">
<attr name="prop1" format="MyCustomType"/>
</declare-styleable>
Is this possible somehow? Or custom type attributes are possible to be set only from code behind?
Thank you!
I don`t really understand why you need this. but you can use format="String" and write full class name in property field in your layout. For example:
custom:prop1="com.example.MyCustomType"
then in constructor of your View:
TypedArray a = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.MyCustomView,
0, 0);
String className = a.getString(R.id.prop1);
Class<MySustomType> c = Class.forName(className);
MySustomType prop = c.newInstance();
setProp1(prop);
You cannot use own property types with android framework. You can come with own proprties based on available types but that's it. Not sure what type you got in mind in your case, but in most cases whatever that custom thing is, it could be solved by available primitives.
Is it possible to obtain styled attributes values from particular Theme without setting the theme up to application/activity?
(I mean before invoking context.setTheme(..))
For example, to get editTextColor attribute's value of a theme called MyTheme:
TypedArray a = getTheme().obtainStyledAttributes(
R.style.MyTheme,
new int[] { R.attr.editTextColor });
// Get color hex code (eg, #fff)
int intColor = a.getColor(0 /* index */, 0 /* defaultVal */);
String hexColor = Integer.toHexString(intColor);
// Don't forget to recycle
a.recycle();
JavaDoc:
method TypedArray
android.content.res.Resources.Theme.obtainStyledAttributes(int[]
attrs)
Return a TypedArray holding the values defined by Theme which are
listed in attrs.
Be sure to call TypedArray.recycle() when you are done with the array.
if you need it in the xml file, you can use something like this:
style="?android:attr/panelTextAppearance"
for example:
<TextView
style="?android:attr/panelTextAppearance"
android:paddingTop="?android:attr/paddingTop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/your_text"
/>
if you're using eclipse, control+click on the item, to see other possible values (a file attrs.xml will open).