Rafael
Rafael

Reputation: 3112

Is there a way to avoid size specifications on android layout?

I work with Android daily, and I would like to avoid the specifications of the views size. For example, if I make a TextView, I got to assign Height and Width properties, like this:

   <TextView
      android:id="@+id/activity_lbl"
      android:layout_width="wrap_content" <!-- Again -->
      android:layout_height="wrap_content" <!--Again...  --> /> 

   <TextView
      android:id="@+id/activity_lbl2"
      android:layout_width="wrap_content" <!-- Again... -->
      android:layout_height="wrap_content" <!--Again, for ever  --> /> 

And for every view I create, I got to assign the same size specifications. Is there a way to avoid them?

Upvotes: 2

Views: 95

Answers (2)

Theuser1234
Theuser1234

Reputation: 15

you can't avoid the

  android:layout_width="wrap_content" 
  android:layout_height="wrap_content"

because it's important but i'm going to give you some codes for some layouts for images you can use this in your class:

ImageView.getLayoutParams().height = YourHeightValue ;
    ImageView.getLayoutParams().width = YourWidthValue;

for the textView use the Textview.setSize(); or TextView.setwidth(int); TextView.setHeight(int);

Upvotes: -2

Ben Weiss
Ben Weiss

Reputation: 17922

Declare it within a style that you'll apply in your layout files like this:

File: res/values/styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
  <style name="wrapall">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
  </style>
</resources>

And then apply it to your layouts like this:

<TextView
      android:id="@+id/activity_lbl"
      style="@style/wrapall" /> 

   <TextView
      android:id="@+id/activity_lbl2"
      style="@style/wrapall" /> 

Upvotes: 5

Related Questions