Daiwik Daarun
Daiwik Daarun

Reputation: 3974

Android: How do you set default view attributes?

I have multiple buttons that all have the same textSize, layout_width, etc. and I do not want to copy and paste these attributes over and over as it makes for hard to edit code. In other words, I am looking to take many of these:

<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="@drawable/round_button_normal"
    android:text="@string/button1"
    android:textSize="50sp" />

And instead be able to use a preset button (where the layout_width, layout_height, layout_weight, background, and textSize are all set to the above values, by default):

<PresetButton
    android:id="@+id/button1"
    android:text="@string/button1" />

Upvotes: 0

Views: 1249

Answers (3)

mohammed momn
mohammed momn

Reputation: 3210

in your values -> style.xml file put the xml attribute

 <style name="buttonConfirm" parent="@android:style/Widget.Button">
    <item name="android:textColor">#FFFFFF</item>
    <item name="android:textSize">15dip</item>
    <item name="android:focusable">true</item>
    <item name="android:clickable">true</item>
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>

</style>

and in your layout call it by this xml

   <Button
    android:id="@+id/button1"
    style="@style/buttonConfirm"
    android:text="Button"
    />

Upvotes: 1

Steven Spasbo
Steven Spasbo

Reputation: 646

You can use styles.xml to create a style, then you can apply that styling to each button.

http://developer.android.com/guide/topics/ui/themes.html

Upvotes: 1

Androiderson
Androiderson

Reputation: 17083

Just use styles.

Styles and Themes

Example:

In: res/values/styles.xml

<style name="Numbers">
  <item name="android:inputType">number</item>
</style>

Use like this:

<EditText
    style="@style/Numbers" />

Upvotes: 2

Related Questions