Custom RelativeLayout children ignoring all positioning "android:layout_" attributes - android

I'm going completely mad.
I'm attempting to create a RadioGroup that extends RelativeLayout instead of LinearLayout. Seems easy enough. I'm following [this] exactly. Everything's double and triple checked. This seems to have worked for a bunch of other people just fine.
Upon actually using it, the RelativeLayout attributes specified by the children are completely ignored. Everything piles up in the top left and nothing I can do will move them.
If I change the view from RadioGroupRelative to a regular RelativeLayout, everything is suddenly following the 'android:layout_' attributes perfectly. Looks exactly the way I want it to look.
So. Somewhere along the lines, and I've redone this like 5 times now, I'm losing those xml attributes. I don't understand how though. RadioGroup doesn't do anything to it's children's layout parameters from what I can tell. Why aren't they getting applied when android inflates the layout?

There is no link for [this] - could you add it? Also, mind posting your source code for your custom RadioGroup that inherits from RelativeLayout. From what i can tell, RadioGroup is-a LinearLayout so I'm assuming you've copied and customized the Android source code to do this...
EDIT: as a test, what does the following code do in your environment?
YourCustomRadioGroup radioGroup = new YourCustomRadioGroup(this);
if (radioGroup instanceof RelativeLayout){
//do you end up in here?
Log.d("yourapp","I'm a RelativeLayout!!");
}else{
Log.d("yourapp","I'm NOT a RelativeLayout! :( ");
}

Related

Using Layout - Android

I have a layout, but I need put more views (Buttons, EditText, etc), but how you know, the layout of the ADT is a bit small and I can't to set more views or know where is it, Somebody know How can I set more of these?
Thank you
Android isn't like iOS how you can just position elements with drag & drop.
RelativeLayout will position elements relative to each other and LinearLayout will lay out elements sequentially in a linear fashion, either vertically or horizontally. Both methods are better executed by actually writing the View XML yourself.
I suggest reading this: http://developer.android.com/guide/topics/ui/declaring-layout.html
If you want to actually see what you are adding to your layout without messing with the XML you could maybe change the device that the renderer is using to preview your layout.
I don't know what IDE you're using but in Eclipse and Android Studio you can change the device that your layout preview is rendered on. That way you can see what you're adding as you add it.
Then just make sure to put everything in a scroll view so users can access all the views and widgets you've put in that layout for your activity or fragment or dialog or whatever else it is.

How to place UI elements to the position we want in Android?

I have some buttons, textboxes etc. in my android application, but when i drag them with my mouse in the xml file, their place doesn't change, or changes but they are not placed where i exactly wanted. How can i adjust their positions in the screen?
Thanks
Unfortunatly there is no such thing as absolute positionning in android ( RIP AbsoluteLayout deprecated since years.)
instead you have to position views according to their parents and according to other views in the same parent.
first you have to define wich parent you need ( if you want some viens in a single line go for a LinearLayout. a more custom layout: use a RelativeLayout ...)
then you can drag and drop views inside, but they will always snap a position relative to their parent and/or relative to the other views.
you can of course play with margins.
A list of layout type with some advanced techniques can be found on this page
Hope that helps.
You RelativeLayout as a group layout for your layout so positioning can somewhat easy using mouse.
Best is to arrange them from the xml code. Just Learn about using the Relative layout, LinearLayout and TableLayout
Learn how the XML works. For a LinearLayout, the items come in the order listed. For a RelativeLayout, the items are related by the values of their layout_XXX properties. Then you don't have to worry about the WYSIWYG tool not working.
FYI, the tool bundled with eclipse is extremely buggy. Don't count on whats on there being what's on your phone for anything non-trivial.
Like the others wrote it is easier to edit layout using xml editor. You can read more here http://developer.android.com/guide/topics/ui/declaring-layout.html.

Aligning widget in RelativeLayout with a widget in nested LinearLayout

So I've been looking around for awhile now and can't seem to find an answer to my question
I have tried a few things, but nothing seems to work so I'm wondering if what I'm trying to do is even possible.
Basically I have a RelativeLayout at the base of my XML Layout, and within that RelativeLayout I have some nested LinearLayouts, what I want to do is align a widget in the RelativeLayout to a TextView nested within a LinearLayout, so something like below (unnecessary XML markup has been removed so you can see what I'm trying to do easier)
&ltRelativeLayout&gt
&ltLinearLayout&gt
&ltTextView
android:id="#+id/TextView1"/&gt
&ltTextView
android:id="#+id/TextView2"/&gt
&lt/LinearLayout&gt
&ltTextView
android:layout_alignLeft="#id/TextView2" /&gt
&lt/RelativeLayout&gt
At this point I've simply given up attempting to do this and removed the linear layouts from my XML and gone strictly with just the RelativeLayout.
However I'd just like to know if what I originally attempted is possible, and if I was just doing it wrong, or because of the way the widget is nested within another layout that it just doesn't work.
Thanks in advance
I'm not sure if this is missing the point (lack of context, but that doesn't matter if I manage to help), but it sounds like you should be able to replace TextView2 with a horizontal LinearLayout containing both TextView2 and the external TextView. But, yes, sticking totally with a RelativeLayout will do a similar thing with more control (I just like nested LinearLayouts, and personally, I'd probably replace the entire RelativeLayout with them, depending on what it's for).
Elements in a RelativeLayout will only align with other elements in the same layout at the same level (siblings).
However, you may be able to achieve something like this with a little work. You could put an ID on the LinearLayout. You could then align the outer TextView with the LinearLayout and use padding and margin attributes (on both TextView2 and the outer TextView) to make it truly line up at display time.

TextView's text doesn't change?

i'm facing a problem that i never came accross before:
well i successfully applyed the 3D transition between a listview and someother layout container some info about the selected item. well the probelm is: some of the textviews are displayed correctly while others display nothing, even the imageview is shown as expected...
any suggestions?
thanks in advance..
I guess you had same collision in android:name tags. Acting wierd could happen by many reasons, but one of the most liklly to happen is when you have some collision in names, I mean more views defined with same name
#+id/something definition and #id/something reference is okey
but having #+id/something twice sometimes could be a problem.
This kind of references is usually used in relative layaout when you say I want this view to be to the left (or above, or below) some other view. Then you must give a reference to other view...
I fixed this: well i don't know the exact reason but it solved the problem
i changed the textview id in the layout and it worked fine. Weird why it didn't work until this..if any one can explain it would be great^^
I have 2 text view with the same id, one of them is inside the parent layout and the other one was in the viewpager which is child of the parent layout. I changed the latter one is and the problem is solved.

onClickListener on a LinearLayout

Got a little problem with a functionality I'd like to implement.
I've got a LinearLayout, containing 2 TextViews. I'd want to set a onClickListener on it, so that it would behave just like an item in a ListView.
I've tried some possible implementation of this, and got it working using the xml attribute android:onClick of the LinearLayout.
As I want the application to visually react to a click on the LinearLayout (as in a ListView), I put this in the listener callback method :
l.setBackgroundColor(android.R.drawable.list_selector_background);
l is a LinearLayout object, initialised with findViewById();
I get an application not responding whenever I try to click on the layout. I tried to replace the code in the callback method by a Toast message show up, and it works, so I guess the problem is in the setBackgroundColor.
Is anyone familiar with this?
PS: sorry, I can't post more code right now, I'm at work.
You have to set LinearLayout attribute android:clickable="true" in the xml layout file or setClickable(true) in the java activity code.
i noticed that all the advices above don't helpt as long as any of the child elements inside the LinearLayout has the attribute android:textIsSelectable="true".
I found that that setClickable(true) would still cause clicks to go to children of the linearlayout. I found that to have the LinearLayout capture the touch instead of it's children I had to override the dispatchTouchEvent method so I created a subclass of LinearLayout for just this purpose. Seems like an ugly solution though.
you should set the LinearLayout's focusable to true and set all the children view's focusable to false, don't use the android:clickable="true", but you can't see the effect of the click of the linelayout. BTW, the best way is to implement the onTouchEvent api.
android:clickable="true" works perfectly under one condition.
Youhave to put the childs inside the LilnearLayout to android:clickable="false".
Had the same problem, I've been trying for an hour all the answers in SO but none worked.
Then I realized I just had to promote my LinearLayout to the bottom of the layout, since other views blocked it, and then it worked, without adding ANY special attributes to the layout.
Putting it here in case it might help someone someday.

Categories

Resources