Advertisement
yo2man

Adding the gradient

Jul 13th, 2015
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
XML 8.40 KB | None | 0 0
  1. // Android List and Adapters: adding a gradient:
  2. // Make the background a gradient instead of a solid color to look more awesome:
  3. // One way we can do it is to use an image of a gradient.
  4.  
  5. // Or we could create a drawable in xml format.
  6. // Rightclick on drawable>new>drawable resource file> bg_gradient for the file name in the new window that pops up> click okay >
  7. // with the result, change 'selector' to 'shape' like this:
  8.  
  9. //from this:
  10. <?xml version="1.0" encoding="utf-8"?>
  11. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  12. </selector>
  13.  
  14. //to this:
  15. <?xml version="1.0" encoding="utf-8"?>
  16. <shape xmlns:android="http://schemas.android.com/apk/res/android">
  17. </shape>
  18.  
  19. //simple.
  20.  
  21. //------------------------------------------------------------------------------------------
  22. bg_gradient.xml
  23.  
  24. <?xml version="1.0" encoding="utf-8"?>
  25. <shape xmlns:android="http://schemas.android.com/apk/res/android"
  26.    android:shape="rectangle"> <!-- this will gradient will be a rectangle-->
  27.     <gradient
  28.        android:startColor="#f9d34c"
  29.        android:endColor="#f25019"
  30.        android:angle="270"
  31.        android:type="linear"/>
  32.     <!-- Basically
  33.        Color that the gradient begins with
  34.        Color that the gradient ends with
  35.        Angle of the gradient
  36.        Type of gradient spread: radial, linear, etc (hit ctrl+space inside "" to see options)
  37.        Simple.
  38.           -->
  39.  
  40.  
  41. </shape>
  42.  
  43.  
  44. //------------------------------------------------------------------------------------------
  45. activity_main.xml
  46.  
  47. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  48.                xmlns:tools="http://schemas.android.com/tools"
  49.                android:layout_width="match_parent"
  50.                android:layout_height="match_parent"
  51.                android:paddingLeft="@dimen/activity_horizontal_margin"
  52.                android:paddingRight="@dimen/activity_horizontal_margin"
  53.                android:paddingTop="@dimen/activity_vertical_margin"
  54.                tools:context=".MainActivity"
  55.                android:background="@drawable/bg_gradient"> <!--background @drawable -->
  56.  
  57.     <TextView
  58.        android:layout_width="wrap_content"
  59.        android:layout_height="wrap_content"
  60.        android:text="--"
  61.        android:id="@+id/temperatureLabel"
  62.        android:layout_centerVertical="true"
  63.        android:layout_centerHorizontal="true"
  64.        android:textColor="@android:color/white"
  65.        android:textSize="150sp"/>
  66.  
  67.     <ImageView
  68.        android:layout_width="wrap_content"
  69.        android:layout_height="wrap_content"
  70.        android:id="@+id/degreeImageView"
  71.        android:layout_alignTop="@+id/temperatureLabel"
  72.        android:layout_toRightOf="@+id/temperatureLabel"
  73.        android:layout_toEndOf="@+id/temperatureLabel"
  74.        android:src="@drawable/degree"
  75.        android:layout_marginTop="50dp"/>
  76.  
  77.     <TextView
  78.        android:layout_width="wrap_content"
  79.        android:layout_height="wrap_content"
  80.        android:text="..."
  81.        android:id="@+id/timeLabel"
  82.        android:layout_above="@+id/degreeImageView"
  83.        android:layout_centerHorizontal="true"
  84.        android:textColor="#80ffffff"
  85.        android:textSize="18sp"/>
  86.  
  87.     <TextView
  88.        android:layout_width="wrap_content"
  89.        android:layout_height="wrap_content"
  90.        android:text="Alcatraz Island, CA"
  91.        android:id="@+id/locationLabel"
  92.        android:layout_above="@+id/timeLabel"
  93.        android:layout_centerHorizontal="true"
  94.        android:layout_marginBottom="60dp"
  95.        android:textColor="@android:color/white"
  96.        android:textSize="24sp"/>
  97.  
  98.     <ImageView
  99.        android:layout_width="wrap_content"
  100.        android:layout_height="wrap_content"
  101.        android:id="@+id/iconImageView"
  102.        android:layout_alignBottom="@+id/locationLabel"
  103.        android:layout_alignParentLeft="true"
  104.        android:layout_alignParentStart="true"
  105.        android:src="@drawable/cloudy_night"/>
  106.  
  107.     <LinearLayout
  108.        android:orientation="horizontal"
  109.        android:layout_width="match_parent"
  110.        android:layout_height="wrap_content"
  111.        android:layout_below="@+id/temperatureLabel"
  112.        android:layout_centerHorizontal="true"
  113.        android:layout_marginTop="10dp"
  114.        android:weightSum="100"
  115.        android:id="@+id/linearLayout">
  116.  
  117.         <LinearLayout
  118.            android:orientation="vertical"
  119.            android:layout_width="wrap_content"
  120.            android:layout_height="match_parent"
  121.            android:layout_weight="50">
  122.  
  123.             <TextView
  124.                android:layout_width="match_parent"
  125.                android:layout_height="wrap_content"
  126.                android:text="HUMIDITY"
  127.                android:id="@+id/humidityLabel"
  128.                android:textColor="#80FFFFFF"
  129.                android:gravity="center_horizontal"/>
  130.  
  131.             <TextView
  132.                android:layout_width="match_parent"
  133.                android:layout_height="wrap_content"
  134.                android:text="--"
  135.                android:id="@+id/humidityValue"
  136.                android:textColor="@android:color/white"
  137.                android:textSize="24sp"
  138.                android:gravity="center_horizontal"/>
  139.         </LinearLayout>
  140.  
  141.         <LinearLayout
  142.            android:orientation="vertical"
  143.            android:layout_width="wrap_content"
  144.            android:layout_height="match_parent"
  145.            android:layout_weight="50">
  146.  
  147.             <TextView
  148.                android:layout_width="match_parent"
  149.                android:layout_height="wrap_content"
  150.                android:text="RAIN/SNOW?"
  151.                android:id="@+id/precipLabel"
  152.                android:textColor="#80ffffff"
  153.                android:gravity="center_horizontal"/>
  154.  
  155.             <TextView
  156.                android:layout_width="match_parent"
  157.                android:layout_height="wrap_content"
  158.                android:text="--"
  159.                android:id="@+id/precipValue"
  160.                android:textColor="@android:color/white"
  161.                android:textSize="24sp"
  162.                android:gravity="center_horizontal"/>
  163.         </LinearLayout>
  164.     </LinearLayout>
  165.  
  166.     <TextView
  167.        android:layout_width="wrap_content"
  168.        android:layout_height="wrap_content"
  169.        android:text="Getting current weather..."
  170.        android:id="@+id/summaryLabel"
  171.        android:layout_below="@+id/linearLayout"
  172.        android:layout_centerHorizontal="true"
  173.        android:layout_margin="40dp"
  174.        android:textColor="@android:color/white"
  175.        android:textSize="18sp"
  176.        android:gravity="center_horizontal"/>
  177.  
  178.     <ImageView
  179.        android:layout_width="wrap_content"
  180.        android:layout_height="wrap_content"
  181.        android:id="@+id/refreshImageView"
  182.        android:layout_alignParentTop="true"
  183.        android:layout_centerHorizontal="true"
  184.        android:src="@drawable/refresh"/>
  185.  
  186.     <ProgressBar
  187.        android:layout_width="wrap_content"
  188.        android:layout_height="wrap_content"
  189.        android:id="@+id/progressBar"
  190.        android:layout_alignParentTop="true"
  191.        android:layout_centerHorizontal="true"
  192.        android:layout_alignBottom="@+id/refreshImageView"/>
  193.  
  194.     <LinearLayout
  195.        android:orientation="horizontal"
  196.        android:layout_width="match_parent"
  197.        android:layout_height="wrap_content"
  198.        android:layout_alignParentBottom="true"
  199.        android:layout_alignParentLeft="true"
  200.        android:layout_alignParentStart="true">
  201.  
  202.         <Button
  203.            android:layout_width="match_parent"
  204.            android:layout_height="wrap_content"
  205.            android:text="HOURLY"
  206.            android:id="@+id/hourlyButton"
  207.            android:layout_weight="1"
  208.            android:background="#40ffffff"
  209.            android:textColor="#ffffffff"/>
  210.  
  211.         <Button
  212.            android:layout_width="match_parent"
  213.            android:layout_height="wrap_content"
  214.            android:text="7 DAY"
  215.            android:id="@+id/dailyButton"
  216.            android:layout_weight="1"
  217.            android:background="#40ffffff"
  218.            android:textColor="#ffffffff"
  219.            android:layout_marginLeft="2dp"/>
  220.     </LinearLayout>
  221. </RelativeLayout>
  222.  
  223.  
  224. //Also added some space between the two buttons with: android:layout_marginLeft="2dp
  225.  
  226. // Simple.
  227. // https://teamtreehouse.com/library/android-lists-and-adapters/standard-listviews/adding-a-gradient
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement