I am looking for a spec or reference of all the possible options for the various XML layout attribute settings that typically come with an android UI. Google seem to be good at burying it. This is similar to this question but remains in-effectively answered.
Such as what are my options available to me for the TextView layout_width definition ? There must be a complete definition published ... somehwere....
layout_* attributes aren't directly part of the view they appear on, which is why you won't find them in TextView's documentation. (TextView is not a ViewGroup.) They are arguments to the parent view, also known as LayoutParams. Take a look at the "Known Subclasses" sections at the top of the linked page for a list of them. They're instructions about how a ViewGroup should arrange each child view, and each parent type can recognize different ones depending on what kinds of layout options it supports.
For example, LinearLayout.LayoutParams supports the android:layout_weight parameter. Children of a LinearLayout can specify weight to request a proportion of the remaining space after all children have been measured. You can give equal weight to two sibling TextViews with a base width of 0 to give them each half of the available space within the parent.
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Hello" />
<TextView android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="World" />
</LinearLayout>
Normally developer.android.com is your site. Maybe this helps:
http://developer.android.com/reference/android/view/View.html
If you use Eclipse, then the autocomplete suggestions may help you as well in adding the right parameter.
...and the options you have for layout_width are
wrap_content (as large as the content of the View)
fill_parent (extends to the whole size - width or height - of its parent)
Layout parameters are pretty well described in the documentation for ViewGroup.LayoutParams and its subclasses. For the truly strong of heart, you can always browse the source for attr.xml.
Related
I am currently learning the one activity + multiple fragments approach, which is recommended by Google. I want to share some layouts between the fragments: toolbar, scrollbar, app bar.
What is the best way of doing this?
for example this app in GitHub:
https://github.com/mitchtabian/Open-API-Android-App
You need to have a separate layout file for each of the layouts you're trying to reuse. You can then include those in your fragments' layouts - you do this via the <include> tag.
Example layout for a toolbar (toolbar.xml):
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
example of reusing this:
<include layout="#layout/toolbar"/>
There's good documentation on how to achieve this here.
I am new to Android and working with Relative Layout. Now I am trying to change the ID of controls but as soon as I change the ID, other controls get re-positioned as they are relative to each other. Is their any way to change ID of controls without re-positioning them?
Because every object in your relative layout is arranged base on object ID. Once you changed one of the object ID, you will need to change its object ID on the other side as well. If not the layout will be pretty messed up and need to rearrange.
The best way to avoid this is to declare every object ID before you proceed.
My advice is that you changed the object ID one by one. Change all the field of the object ID to the latest.
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textView2"
android:layout_below="#+id/login_email"
android:layout_marginTop="46dp"
android:text="#string/password_text" />
<EditText
android:id="#+id/login_password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView3"
android:layout_alignBottom="#+id/textView3"
android:layout_alignRight="#+id/login_email"
android:layout_toRightOf="#+id/textView3"
android:ems="10"
android:password="true"
android:singleLine="true" />
<Button
android:id="#+id/register_button"
android:layout_width="100dip"
android:layout_height="40dip"
android:layout_alignRight="#+id/login_password"
android:layout_below="#+id/login_password"
android:text="#string/signup_text" />
For example, based on the code above, once you change the TextView id from #+id/textView3 to #+id/password_text. You would need to change one by one in those object that contains the of #+id/textView3 to #+id/password_text. Which is in
<EditText
android:layout_alignBaseline="#+id/textView3"
android:layout_alignBottom="#+id/textView3"
By the way try tag Android in your question as well. More people will notice your question that way.
I am new to Android and I am trying to design my Android application like the interface of the Android RealPlayer but I have no idea how!
What I exactly want to design is: Break the page into 4 equal grid-like sections such that the whole page is covered and no extra empty space is left at the end of the page (exactly like the RealPlayer interface).
<?xml version="l.0" encoding="utf—8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android“
android:id=“#+id/gridview“
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_gravity="center"
android:layout_weight="l.0"
android:columnWidth="90dp"
android:horizontalSpacing="l0dp"
android:numColumns="2"
android:orientation="horizontal"
android:paddingTop="50dp"
android:stretchMode=“columnWidth"
android:verticalSpacing="l0dp" >
</GridView>
I have tried manipulating GridView attributes but it seems that it is not possible only using GridView elements. I hope my problem statement is clear enough! Can somebody give me a hint on how to do that?
I have found out that it is possible using buttons of different sizes!
When my activity receives a new intent via onNewIntent, it updates the data of three Views, an ImageView, a TextView, and a VideoView. The problem is, the two other views just flash, then disappear when my VideoView comes on. After scattering a few breakpoints, I discovered that they appear when their content is set, but disappear when VideoView.onStart() is called in my MediaPlayer.onPrepared() method. I also have an AlertDialog show up when the menu button is pressed. After pressing the menu button, it shows up. I'm on Android API 9, as this is the API on the device I'm working on. I really need help, so I'd appreciate any advice.
Here's the layout. I don't think it's the issue though:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:keepScreenOn="true" >
<TextView
android:id="#+id/marquee"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:ellipsize="marquee"
android:focusable="false"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:textAppearance="#android:style/TextAppearance.Large"
android:textColor="#android:color/white" >
</TextView>
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_above="#id/marquee"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_gravity="center_vertical" />
<VideoView
android:id="#+id/video"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/marquee"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:layout_toLeftOf="#+id/image" />
</RelativeLayout>
Take note that the TextView is meant to be a marquee; it's supposed to keep scrolling sideways, until it is disposed of. I discovered that when the text is too short for it to start the marquee, the TextView AND the ImageView disappear (as stated earlier, they appear for a split second, then disappear). However, when the text causes the marquee feature to activate, everything works.
CURRENTLY:
I got it to work by calling postInvalidateDelayed(500) on my ImageView and TextView after calling VideoView.start(). I think the start() method is causing the problem, and requires that other views call invalidate(). Also, for some reason, there needs to be a small delay in-between the call to start() and the call to invalidate().
The layout is the issue.
Your VideoView is declared as match_parent, match_parent, allowing it to consume the whole width and height of the screen. Since you declared it last on the xml file (You used RelativeLayout. Ordering matters), the TextView and the ImageView would be covered by the VideoView.
If you're confused,
match_parent is basically the same as fill_parent. It's just another name for fill_parent in android 2.3+
Now what can you do about it?
Reorder your views in such a way that the largest is declared first. In this case, VideoView, then ImageView, then TextView.
Also note that your ImageView has height set to fill_parent - you may not want that.
I created a method, but it seems very bad:
private void startVideo() {
this.videoView.start();
if (this.imageView != null)
this.imageView.postInvalidateDelay(500);
if (this.textView != null)
this.textView.postInvalidateDelay(500);
}
It works, but it makes me feel dirty.
I'm trying to build and app that shows organized data so I'm thinking that basically a TableLayout would be a good idea, just to keep them in rows, so I'm wondering whats the correct way to do it? I have this in my xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
android:id="#+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/hello" >
</TextView>
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="233dp"
android:layout_marginTop="44dp" >
</ListView>
</TableRow>
</TableLayout>
</LinearLayout>
then it's just that the TableLayout tag and TableRow tag displays a warning that says:
"This TableLayout layout or its LinearLayout parent is possibly useless"
so what I understand is that is not picking up the TableLayout. how can I fix this? is there another way to do this easly?
so what i understand is that is not picking up the tablelayout. how can i fix this? is there another way to do this easly?
Not quite, the renderer is taking account of the TableLayout but because the lint tool has detected that you have two views when you only need one (A LinearLayout and TableLayout) it is warning you that you should remove one of these layouts as it will boost performance while not effecting the functionality of the page.
I will add, you have a TableLayout with just a single row. TableLayout's are designed to allow their contents to format their columns based upon all of their rows. With a single Row the TableLayout & Row together are acting as a LinearLayout:
<LinearLayout>
<LinearLayout>
<ListView>
<TextView>
Since your TableLayout isn't adding any addition layout in that case it becomes obsolete and you will gain a resource boost from removing it and changing the orientation of LinearLayout to horizontal.
If you're planning to add more TableRow's later, then you should instead remove the LinearLayout for the same reason - it will then become the view which is not adding extra information: your table layout will be entirely responsible for the layout so you might as well make it the parent of the layout file (remember to add xmlns:android="http://schemas.android.com/apk/res/android" attribute to it if you do.)
In general I've seen the stronger warning:
This LinearLayout layout or its RelativeLayout parent is useless
when this is not the case. For example, this can happen if I've nested a linear layout inside a relative layout. The relative layout positions the linear layout exactly where I want it, the linear layout takes up space. Both are non-useless uses to me.
1) it says possibly so avoid drawing conclusions, trust yourself young padawan! But yes, the parent looks useless to me too :)
2) Using Table layout outside of your ListView wont change the rows' layout in the list, in case this is what you want to achieve.
You might have seen this but the Developers page offers a really good tutorial that can help you create a good base for your ListView (this example uses a ListActivity). Then on you can modify the rows' layouts using TableLayout ...etc.