I'm fairly new to Android development and planning to build a simple news reader app to further familiarize myself to the platform.
The problem is; I want each of the news-item to look like Google+'s post item below:
Well, basically a big headline picture with a title and some description underneath.
I tried to re-create it using a Button (because the whole thing needs to be clickable by the user) while setting a drawable at the top of the text. It worked quite nicely.. until I decided to add more drawables to it:
Here's my XML code (although I doubt anyone would need it):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/ic_launcher"
android:drawablePadding="8dp"
android:drawableTop="#drawable/temp_jakarta"
android:text="Button" />
</RelativeLayout>
Can you tell me why is this happening or maybe, suggest me another way to accomplish this?
Thanks in advance!
p.s., I'm pretty sorry about the link; as a new user, SO didn't allow me to post images just yet. :(
Put everything inside a RelativeLayout and place one relative to others (event on top, this doesn't matter at all) using the layout_below/above/left_of/right_of/... etc.
See http://developer.android.com/guide/topics/ui/layout/relative.html for usage, http://developer.android.com/reference/android/widget/RelativeLayout.html for a complete reference of the RelativeLayout class and "Android Layout Tricks" series in the Developer Blog for some tips_
http://android-developers.blogspot.com.es/2009/02/android-layout-tricks-1.html
http://android-developers.blogspot.com.es/2009/02/android-layout-tricks-2-reusing-layouts.html
http://android-developers.blogspot.com.es/2009/03/android-layout-tricks-3-optimize-with.html
Related
Recently, while working on some projects, I've had a rather annoying problem when coding the user interface of my application. The problem is, some controls (usually a floating action button or a regular button) render strangely. It's hard for me to explain exactly how it looks - but here's an example of how it shows the floating action button to look like in the designer:
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context=".MainActivity">
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:src="#drawable/ic_baseline_add_24"
android:layout_height="wrap_content"/>
</LinearLayout>
Now, the thing is, I don't have any weird or special code added, the XML code is as pure as it gets, so I have no idea why I am having these strange rendering problems.
Here is how it rendered the following button:
Code:
<com.google.android.material.button.MaterialButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_gravity="bottom"
android:textAllCaps="false"
android:layout_margin="30dp"
app:cornerRadius="30dp"
android:id="#+id/submitButton"
android:backgroundTint="#color/turquoise"
/>
Image:
A solution would be greatly appreciated!
Cheers,
Tom
Solution is simple u just need to restart your android rendering engine. I provided Image for your context . In android studio top right corner above layout display there is Toggle issue panel click on that and u will see one box in the bottom showing rendering issue restart rendering engine and that will restart your android studio. and u good to go. try this solution and let me know your issue is solved or not.
note: In my Image I already restarted so there is no option for restart rendering engine .but in yours's U may be find hope so.
Image
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context="com.bignerdranch.android.geoquiz.CheatActivity">
<TextView
android:id="#+id/answer_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
tools:text="Answer"/>
I am a novice in programming. I started with Big Nerd Ranch programming book. I came across this code. In the book it is stated as:
"This namespace allows you to override any attribute on a widget for the
purpose of displaying it differently in the Android Studio preview. Since TextView has a text attribute,
you can provide a literal dummy value for it to help you know what it will look like at runtime. The
value “Answer” will never show up in the real app. Handy!"
What does that actually mean? I am completely new. I know this is foolish question, please help me in this.
Thanks to that line
xmlns:tools="http://schemas.android.com/tools"
you can use in all your XML something like that
tools:text="Answer"
Thanks to that line
xmlns:android="http://schemas.android.com/apk/res/android"
you can use in all your XML element the android attribute, for example
android:id="#+id/answer_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
The book you're reading is a good book, keep continue and don't give up!
Tools is basically a collection of extra properties you can add to your TextView that assist you when designing layouts in Android Studio. In this particular example, tools:text allows you to put a fake value into your TextView which will only show up in the the layout preview in Android Studio.
This will allow you to see what a TextView looks like when designing your layout in Android Studio, but you don't have to worry about removing that dummy text from your layout when you build a "real" version of your app for a phone.
See also: Tools Attribute Reference
I'm trying to make an Android layout like the one below. I have a couple of questions:
1 - what is the element called that FB uses for posts? Ie, it doesn't look like a text view, but the element looks like it separates each post with a divider line. Also, the text style is different for a person's name and how long ago they posted. I'm looking to duplicate this (minus pictures) but I can't find the right UI elements.
What is the element called at the bottom? It's like a static menu. IE, it's the same as a menu but instead of pressing "menu" to access it, it's on the page at all times.
Finally, are there good tutorials/examples on how to make nice looking, professional layouts like the apps on the market? The tutorials that I've found on layouts are really basic. I'd like to understand what elements exist, what all of the attributes mean and see examples, etc. So far I'm only able to see the capabilities from other applications. I'd like to have a handbook or some type of some type of reference manual to go to.
For the "fancy" text views you can make a linear layout that hosts a <RelativeLayout>:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="0">
<ImageView
android:id="#+id/userPhoto"
android:layout_height="64dip"
android:layout_width="64dip"
/>
<TextView
android:id="#+id/userFullName"
android:layout_height="25dp"
android:layout_width="fill_parent"
android:layout_marginLeft="70dp"
/>
</RelativeLayout>
Once you have a relative layout you can add different views inside of that to create a sort of customeized view.
As far as good examples I would look at this book. It's easy to understand and very helpful on such things.
I found a really helpful tutorial to solve a problem in ListView Row design a bit like yours. It goes a bit further explaining how to do Async Image loading but the first part should help you.
Also, I might be wrong (I am still a bit new to this) but I think the answer above lacks a TextView for the actual message besides the userName and the relative positions of the elements since it is a relative layout. Something like:
<TextView
android:id="#+id/userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#id/userPhoto"
android:layout_toRightOf="#id/userPhoto"
android:textSize="17dp"
android:textStyle="bold" />
<!-- actual message -->
<TextView
android:id="#+id/message"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/userName"
android:layout_marginTop="1dip"
android:layout_toRightOf="#id/userPhoto"
android:textSize="15dp" />
The key in organizing a relative layout is:
android:layout_alignTop="#id/userPhoto"
android:layout_toRightOf="#id/userPhoto"
and
android:layout_below="#id/userName"
android:layout_toRightOf="#id/userPhoto"
I might be wrong but if it helps, great! Just adding my bit to the other answer.
Cheers
I have a layout menu which contains this:
<RelativeLayout android:id="#+id/computersMenu"
android:layout_width="50dp"
android:layout_height="50dp">
<include layout="#layout/component_add_button"
android:id="#+id/imagebutton_add_client"/>
</RelativeLayout>
component_add_button.xml:
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/ic_menu_add"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="center"/>
Looking at the preview (EDIT: I mean the "Graphical Layout" tab of the xml editor in eclipse which uses the same rendering engine as all devices AFAIK) of component_add_button.xml you can see the image. Great. On the page which is including it it isn't displaying (although it does give you a selectable space where the image should be).
This seems like a really simple example that "should just work". The include tag has always seemed really flaky to me - I'm wondering if I'm missing something in terms of how it actually works...?
As your answer states, the eclipse preview does not display included layouts.
The simplest way to avoid doubt or mistakes in markup when "including" layout's is to design in the original layout then right click the view you wish to extract and select Extract include... from the menu
On a device this functionality worked fine. This seems like a bug in the "Graphical Layout" tab of the layout xml editor shipped with android-sdk.
I have a background in iPhone development, which may be a cause of some of my confusion with Android, which I am very new at developing.
My question is this: How to I create two TextViews, and specify their exact location on screen? For example, on the iPhone, I would create a UILabel, generate a rectangular frame that specified the label's size and position, and then set this rectangle to the frame property of the UILabel.
If you can help me understand the similarities with Objective C and iOS' UILabel, that would be most helpful.
On Android, we don't use absolute screen positions. This is highly discouraged. It's pretty understandable that you think this way if you are coming from iOS. But you need to revise your habits.
Instead of absolute positions, we use layouts, such as LinearLayout, RelativeLayout or FrameLayout. All of these allow you to arrange your views dynamically. And in many cases, it will automagically adapt to the screen size, which vary a lot from device to device.
Actually, there's nothing exotic about dynamic layouts. Many major UI toolkits, such as GTK, or Qt, work similarly. Pixel position are a bad idea in my opinion, except maybe in the Apple world, where the OS and the hardware are tightly coupled, but this is an exception actually.
So, in your case, all that you need is to put your text views into the appropriate layout. Please read the documentation and tutorials about the different types of layouts mentioned above to decide which one is best. The question is how you want your views to be placed relatively to each other.
Create a basic Android project in eclipse. You will be having a main.xml layout file in your project. You can open it in Eclipse using Ctrl+Shift+r and keying in main.xml
copy paste this in your xml after clearing its content.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:text="TextView One"
android:layout_height="fill_parent"
android:layout_weight="1"></TextView>
<TextView
android:id="#+id/textView2"
android:layout_width="fill_parent"
android:text="TextView Two"
android:layout_height="fill_parent"
android:layout_weight="1"></TextView>
</LinearLayout>