Advertisement
yo2man

Creating A Custom Layout part 1

Jul 16th, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.57 KB | None | 0 0
  1. // We have the data we need and
  2. // we're displaying a little bit of it in a list, great.
  3. // But we can do so much more.
  4. // Right now we are using a list item layout and
  5. // list adapter already provided by the Android SDK.
  6. // We can build on top of those or create totally new ones to match our needs.
  7. // Now don't worry, it's not as hard as it sounds although it can be a little
  8. // confusing the first time through.
  9. // But, with this course and the code we're working on, you should have the knowledge
  10. // and a model to create any kind of list layout and adapter you need in the future.
  11.  
  12.  
  13. // Right click>layout folder>new>layout resource file> name it "daily_list_item" and change it from 'LinearLayout' to 'RelativeLayout' --because we are going to position things relative to each other.
  14. //Click OK to finish it, and okay our layout file is created and if we switch
  15. // over to the text view, we see that it has a relative layout at the root, cool:
  16.  
  17. Step 1.
  18. <?xml version="1.0" encoding="utf-8"?>
  19. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  20.                 android:layout_width="match_parent"
  21.                 android:layout_height="match_parent">
  22.  
  23. </RelativeLayout>
  24.  
  25. // So the background of any layout is transparent by default, but in this view, it's showing as white.
  26. // This is actually going to be a problem since we're going to use white text.
  27. // But we don't wanna specify a background color in here
  28. // because we want the gradient from our activity background to show through.
  29.  
  30.  
  31. // Fortunately, the Android Tools contain a useful little trick for setting attributes
  32. // that only show when working on a layout in Android Studio, not in the actual app.
  33.  
  34. // So let's check out activity_main.xml to see what I'm talking about.
  35. // Make sure you're in the text view like this, and then here at the top, we have
  36. // two lines that are for XML namespaces, that's what this xmlns prefix stands for.
  37. // The first is the android namespace and the second is the tools namespace.
  38.  
  39. // Now, we don't really care about the details about what XML namespaces are or how they're used.
  40. // All we care is that this one allows us to use those special tools attributes.
  41. // So copy it from this activity main layout and then let's go back to
  42. // daily_list_item.xml, and switch to the text view and we can paste it in here.
  43.  
  44. Step 2.
  45. <?xml version="1.0" encoding="utf-8"?>
  46. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  47.                 xmlns:tools="http://schemas.android.com/tools" // <--this line right here, copied from activity_main.xml allows us to add special attributes that begin with tools instead of Android.
  48.                 android:layout_width="match_parent"
  49.                 android:layout_height="match_parent">
  50.  
  51. </RelativeLayout>
  52.  
  53.  
  54.  
  55.  
  56. Let's add a background color to our preview using 'tools:' so we can see the white font.  
  57. Step 3.
  58. <?xml version="1.0" encoding="utf-8"?>
  59. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  60.                xmlns:tools="http://schemas.android.com/tools"
  61.                android:layout_width="match_parent"
  62.                android:layout_height="match_parent"
  63.                tools:background="#ffaa00">
  64.  
  65. </RelativeLayout>
  66.  
  67. // notice that the background has changed to a yellowish orange color.
  68. // But it's only this color in our tools.
  69. // The final app will still be transparent.
  70. // Simple.
  71.  
  72.  
  73.  
  74. <?xml version="1.0" encoding="utf-8"?>
  75. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  76.                 xmlns:tools="http://schemas.android.com/tools"
  77.                 android:layout_width="match_parent"
  78.                 android:layout_height="wrap_content" // Since this is a list, change layout from 'match_parent' to 'wrap_content'
  79.                 tools:background="#ffaa00">
  80.  
  81. </RelativeLayout>
  82.  
  83. // You will need to download a new image for the circle that will hold the temperature.
  84. // Step 5. Download the icons. extract them, then copy and paste them into the Res>Drawable folders.
  85.  
  86. /*
  87. Then go to design tab of the .xml in Android studio. Drag in ImageView to the top left ( android:layout_alignParentTop="true" android:layout_alignParentLeft="true" ) then go to Id in the properties tab, call it circleImageView. Then go to src. click [...] button, scroll down to the drawables folder and you will see "bg_temperature" that you had just extracted/copy&pasted into the drawable folder. Select it and hit okay.
  88. */
  89. // Drag and drop andother image view in, to the right of the circleImageView. Then do the same thing again except name the id iconImageView and for 'src' use "clear_day".
  90.  
  91. // Drag and drop Plain Text View in and align it to the right of the circleImageView. Set "id" to dayNameLabel and set textColor to #FFFFFFFF . Make textSize 20dp.
  92. // Add some placeholder text by clearing the text in the Text. Then in the XML tab in <TextView> add "tools:text="Wednesday" <- as we said before, this won't show up in the app when we run it but it will show up in the preview. *
  93.  
  94.  
  95. // The result:
  96.  
  97.  
  98. <?xml version="1.0" encoding="utf-8"?>
  99. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  100.                 xmlns:tools="http://schemas.android.com/tools"
  101.                 android:layout_width="match_parent"
  102.                 android:layout_height="wrap_content"
  103.                 tools:background="#ffaa00">
  104.  
  105.     <ImageView
  106.         android:layout_width="wrap_content"
  107.         android:layout_height="wrap_content"
  108.         android:id="@+id/circleImageView"
  109.         android:layout_alignParentTop="true"
  110.         android:layout_alignParentLeft="true"
  111.         android:layout_alignParentStart="true"
  112.         android:src="@drawable/bg_temperature"/>
  113.  
  114.     <ImageView
  115.         android:layout_width="wrap_content"
  116.         android:layout_height="wrap_content"
  117.         android:id="@+id/iconImageView"
  118.         android:src="@drawable/clear_day"
  119.         android:layout_centerVertical="true"
  120.         android:layout_toRightOf="@+id/circleImageView"
  121.         android:layout_toEndOf="@+id/circleImageView"/>
  122.  
  123.     <TextView
  124.         android:layout_width="wrap_content"
  125.         android:layout_height="wrap_content"
  126.         android:id="@+id/dayNameLabel"
  127.         android:layout_centerVertical="true"
  128.         android:layout_toRightOf="@+id/iconImageView"
  129.         android:layout_toEndOf="@+id/iconImageView"
  130.         android:textColor="#ffffffff"
  131.         android:textSize="20sp"
  132.         tools:text="Wednesday" //*
  133.         />
  134. </RelativeLayout>
  135.  
  136. // Now we need a text view to hold the temperature, but
  137. // we want it to be in the center of the first circle we added.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement