findViewById in a different xml file? - android

I am editing csipsimple app and I have setContentView to this file, in_call_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/mainFrame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ViewStub
android:id="#+id/dropZones"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout="#layout/in_call_drop_zones" />
<LinearLayout
android:id="#+id/inCallContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="3"
android:orientation="horizontal" >
<!--
Can we do better than using two linear layouts here?
The problem is that RelativeLayout doesn't allow to use weight and
plays bad with hidden childs and above/below only support one id
-->
<!-- Grid for active calls -->
<com.csipsimple.ui.incall.InCallInfoGrid
android:id="#+id/activeCallsGrid"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="2"
android:paddingBottom="4dip"
android:paddingTop="4dip"
tools:ignore="NestedWeights" />
<!-- Grid for held calls -->
<com.csipsimple.ui.incall.InCallInfoGrid
android:id="#+id/heldCallsGrid"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/in_call_held_background"
android:padding="4dip"
android:visibility="gone" />
</LinearLayout>
<!-- Answer slider/buttons -->
<com.csipsimple.ui.incall.locker.InCallAnswerControls
android:id="#+id/inCallAnswerControls"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="2"
android:visibility="gone" />
<!-- Wraps split action bar manually managed -->
<com.csipsimple.ui.incall.InCallControls
android:id="#+id/inCallControls"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/incall_bottom_bar_bg" />
</LinearLayout>
<com.csipsimple.ui.incall.locker.ScreenLocker
android:id="#+id/lockerOverlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#8000"
android:visibility="gone" >
</com.csipsimple.ui.incall.locker.ScreenLocker>
</RelativeLayout>
There is another layout file, in_call_card.xml; with this code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/call_card_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:padding="2dip"
tools:ignore="Overdraw" >
<com.csipsimple.widgets.MaxScaleImageView
android:id="#+id/contact_photo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/call_action_bar"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:contentDescription="#string/empty_description"
android:scaleType="centerCrop"
android:src="#drawable/ic_contact_picture_180_holo_light" />
<RelativeLayout
android:id="#+id/contact_name_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#9e808080"
android:gravity="center_vertical" >
<!--
<EditText
android:id="#+id/editText1"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
-->
<com.csipsimple.widgets.MarqueeTextView
android:id="#+id/contact_name_display_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:padding="5dip"
android:singleLine="true"
android:textColor="#android:color/white"
android:textSize="25sp" />
<com.csipsimple.widgets.MarqueeTextView
android:id="#+id/contact_name_sip_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/contact_name_display_name"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:padding="2dip"
android:singleLine="true"
android:textColor="#android:color/white"
android:textSize="15sp" />
<Chronometer
android:id="#+id/elapsedTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:textSize="20sp" />
</RelativeLayout>
<TextView
android:id="#+id/call_status_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/contact_name_bar"
android:layout_marginTop="1dip"
android:background="#c818617b"
android:gravity="center_vertical|right"
android:padding="3dip"
android:text="#string/call_state_calling"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textSize="15sp" />
<RelativeLayout
android:id="#+id/call_secure_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/call_status_text"
android:layout_marginTop="1dip"
android:background="#c8d4aa00" >
<ImageView
android:id="#+id/call_secure_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_margin="2dip"
android:contentDescription="#string/empty_description"
android:src="#drawable/stat_sys_vp_phone_call" />
<TextView
android:id="#+id/call_secure_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="#+id/call_secure_icon"
android:padding="2dip"
android:textColor="#android:color/white"
android:textSize="15sp" />
</RelativeLayout>
<FrameLayout
android:id="#+id/call_action_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/end_call_bar"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignWithParentIfMissing="true"
android:layout_marginTop="2dip"
android:background="#000000" />
<LinearLayout
android:id="#+id/end_call_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginTop="2dip"
android:background="#drawable/end_call_background"
android:orientation="horizontal" >
<!-- android:onClick="pushtotalk3" -->
<ToggleButton
android:id="#+id/PTT_button5"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:text="#string/ptt5"
android:layout_weight="50"
android:textOn="Push To Talk On"
android:textOff="Push To Talk Off"
android:background="#drawable/btn_lightblue_glossy"
android:textColor="#android:color/white"
android:textSize="15sp"
/>
<ImageButton
android:id="#+id/endButton"
style="?attr/actionButtonStyle"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="50"
android:contentDescription="#string/done"
android:minHeight="64dip"
android:src="#drawable/ic_end_call" />
</LinearLayout>
</RelativeLayout>
When I setContentView to the other xml file, I can still see elements from this file, I am not sure how this works. As in I can see the PTT_button5 button etc, my problem is that if I want to set a listener on this button, how do I do it? If I try to reference it I get a Null Pointer Exception:
findViewById(R.id.PTT_button5).setOnTouchListener(this);
Is this because I have setContentView to a file that does not contain a reference to this button, so how do I get a reference to this button? I am also not sure how this works as in the button is displayed on screen without settingContentView to that file.
EDIT: as the button is on screen, can anybody see how it is done from the code?
https://code.google.com/p/csipsimple/source/browse/trunk/CSipSimple/src/com/csipsimple/ui/incall/?r=2170

I think you need to understand the following basics:
1) Activity provides a window for you in which you can place your UI with setContentView(View)
In your case, this View (lets call it contentView) is inflated using in_call_main.xml
2) The contentView contains other views.
In your case, the contentView contains views, which are defined in the XML layout. This is the reason why they are shown when the activity starts. (If xml file is empty, you don't see anything)
3 You can add and remove views from the contentView.
This means that, using code, we can create a new view (lets say ToggleButton) and add it to the contentView. This ToggleButton is now visible on the screen. (because it is a child of the contentView). Similarly, removing a child view will cause it to disappear.
4) Any view, (not just contentView) can be inflated (created) from a layout file.
In your case, InCallCard is a view, which is being inflated from the in_call_card.xml.
When the getView() method of the CallsAdapter is called.. a new instance of InCallCard (view) is created and returned. It is then attached to the contentView and therefor all the UI elements (views) inside the InCallCard (in_call_card.xml) are now visible on the screen. (Including the ToggleButton that has troubled you so much)
Therefore, if you try to access the ToggleButton from onCreate() it will be null. It has not been added to the contentView as yet. But if you do the same in the getView method, as I have explained in another answer. You will see that it is no more null, and you can attach listeners, change background etc..

Is this because I have setContentView to a file that does not contain
a reference to this button.
Yes. findviewById looks for a view with the id mentioned in the current inflated layout. So if you set the content of the layout to the activity, it looks for a view with the id mentioned in that layout. If not your initialization fails and you get NullPointerException.
You say
I have setContentView to this file, in_call_main.xml
So
setContentView(R.layout.in_call_main);
findViewById(R.id.PTT_button5).setOnTouchListener(this);// gives NPE
//in_call_main.xml does not have a toogle button with id button5
In in_call_card.xml you have
<ToggleButton
android:id="#+id/PTT_button5"
But if you have
setContentView(R.layout.in_call_card);
findViewById(R.id.PTT_button5).setOnTouchListener(this);// works
// in_call_card.xml has a toogle button with id button5
so how do I get a reference to this button?
You can't without inflating the layout.

Related

view sliding over layout

I have relative layout. Within which I have many views like edit text, text views and so on. I do lots of animations in that. Everything was perfectly fine. But at one point, I have a requirement.
It is the password page. Create and confirm password. Two edittexts one after another. If the password doesnt meet the requirement, a plain orange view should slide up with error message "Invalid password".
So I put create and confirm password in linear layout. The linear layout will slide up, letting user type in passwords. After validation, if there is an error, the linear layout should slide down and the plain view will slide up. I kept the view invisible and when required I made it visible and wrote the code to slide up. The same code works when I just use single edit text instead of linear layout. But it is not working when using linear layout. Am I doing something wrong?
Adding code to it. The code specific to this issue starts with till the end of coding
I declared linear layout as view and NOT VIEWGROUP.
EDIT: After doing analysis, I found that the view I am trying to slide up after sliding down the layout is hidden somewhere in the view hierarchy.
view.bringToFront() helps to bring the error view to the front and slides up.
But after that the problem is, after my linear layout slide down, the error view is not sliding up straight away. Because I am using bringToFront(), it displays the view first on the whole screen (with background color as orange) and then the sliding starts.
I am not sure whether it is correct but I fixed this issue like this.
no background color has been given to the view
I used errorView.bringToFront () and errorView.setVisibility(true) in the animationEnd() of a Layout slidingdown animation listener.
I defined sliding up animation for the errorView in the same animationEnd() of layout sliding down listener
In the animationStart() of errorView sliding up animation listener, I defined the background color of the errorView. Now its working as expected.
But let me know better way of doing this.
//Declaration
View greyView, orangeView, yellowView, blueView, orangeErrorView, letsGetStartedView, welcomeBckView, passwordMasterLayout, orangePwdErrorView;
TextView welcomeText,errorTextView, letsGetStartedTextView, welcomeBckTxtView, errorPwdMsg;
//Password Error Handling
orangePwdErrorView = v.findViewById(R.id.orangePasswordErrorView);
errorPwdMsg = (TextView) v.findViewById(R.id.errorPasswordMessage);
//Layout code
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/sample_main_layout">
<FrameLayout
android:id="#+id/sample_content_fragment"
android:layout_weight="76.4"
android:layout_width="match_parent"
android:layout_height="0px" />
<FrameLayout
android:id="#+id/bottomPanel"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="23.6">
</FrameLayout>
</LinearLayout>
sample_content_fragment framelayout takes the below code
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<View
android:id="#+id/grey"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#58595B" />
<View
android:id="#+id/orange"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F26724"
android:visibility="invisible" />
<View
android:id="#+id/yellow"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FDB518"
android:visibility="invisible" />
<View
android:id="#+id/blue"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#1893D2"
android:visibility="invisible" />
<TextView
android:layout_width="260dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/welcome"
android:id="#+id/welcomeTextView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="51dp"
android:textColor="#ffffff"
android:textSize="20dp"
android:textStyle="bold"
android:visibility="invisible"
/>
<ImageView
android:layout_height="60dp"
android:layout_width="60dp"
android:id="#+id/clinicloud_logo"
android:clickable="true"
android:src="#drawable/clinicloud_icon"
android:layout_marginRight="50dp"
android:layout_alignParentStart="true"
android:layout_marginStart="35dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="40dp" />
<View
android:id="#+id/orangeErrorView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F26724"
android:visibility="invisible" />
<EditText
android:layout_width="match_parent"
android:layout_height="90dp"
android:inputType="textEmailAddress"
android:ems="10"
android:id="#+id/emailEditText"
android:text="#string/enter_email"
android:layout_gravity="center"
android:background="#f4f4f4"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:visibility="invisible"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/errorMessage"
android:layout_alignTop="#+id/welcomeTextView"
android:layout_centerHorizontal="true"
android:textColor="#ffffff"
android:visibility="invisible"/>
<!--UI components for welcome back page-->
<View
android:id="#+id/welcomeBackView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#134F9F"
android:visibility="invisible" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/welcome_back"
android:id="#+id/welcomeBackTxtView"
android:textColor="#ffffff"
android:layout_alignTop="#+id/welcomeTextView"
android:layout_alignStart="#+id/clinicloud_logo"
android:textSize="20dp"
android:textStyle="bold"
android:visibility="invisible"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="90dp"
android:ems="10"
android:id="#+id/password"
android:layout_alignTop="#+id/emailEditText"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:text="Password"
android:visibility="invisible"
android:background="#d2d1d1"
/>
<!--UI components for Let's get started page-->
<View
android:id="#+id/letsGetStartedView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#5C3A91"
android:visibility="invisible" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Let&apos;s get started"
android:id="#+id/letsGetStartedTxtView"
android:textColor="#ffffff"
android:layout_alignTop="#+id/welcomeTextView"
android:layout_alignStart="#+id/clinicloud_logo"
android:textSize="20dp"
android:textStyle="bold"
android:visibility="invisible"/>
<!--Linear layout to show the create and confirm password-->
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="180dp"
android:gravity="bottom"
android:layout_alignBottom="#+id/emailEditText"
android:layout_alignParentStart="true"
android:weightSum="1"
android:id="#+id/passwordMasterView"
android:visibility="invisible">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:id="#+id/createPwd"
android:layout_weight="0.50"
android:background="#F4F4F4"
android:text="#string/create_password"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:id="#+id/confirmPwd"
android:layout_weight="0.50"
android:background="#E3E3E3"
android:text="#string/confirm_password" />
</LinearLayout>
<!-- To show password errors-->
<LinearLayout
android:id="#+id/orangePasswordErrorView"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F26724"
android:visibility="invisible" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/errorPasswordMessage"
android:layout_alignTop="#+id/welcomeTextView"
android:layout_centerHorizontal="true"
android:textColor="#ffffff"
android:visibility="invisible"
/>
<!-- To show password errors-->
</RelativeLayout>
The java logic is in https://stackoverflow.com/questions/30523599/sliding-up-and-down-android
Please direct for any incorrect codings
I kept the view invisible and when required I made it visible and wrote the code to slide up.
I think you must do it in DialogFragment or custom AlertDialog that will be shown above your layout, if dont want to do this try envelop your RelativeLayout in FrameLayout and adding in it LinearLayout for example but visout your code difficult to say in what problem it is
You should consider using a library for the sliding panel, one that I've had excellent experiences with (and supports the functionality that your after) is umano's AndroidSlidingUpPanel. https://github.com/umano/AndroidSlidingUpPanel

Why does button click event not work when it is in a LinearLayout?

I have an oddity I can't figure out. With the layout below, the button click event that I've assigned does not work. Nothing happens when I tap the button.
(the layout is a bit more complex than this, but I've removed the irrelevant stuff):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutOverview"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/lblOverviewText"
android:textStyle="bold"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:id="#+id/layoutSingleBlock"
android:layout_below="#+id/lblOverviewText"
android:layout_above="#+id/linearLayoutSetUnsetButtons"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<!-- irrelevant stuff removed -->
<Button
android:id="#+id/btnMyButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MY BUTTON" />
</LinearLayout>
</RelativeLayout>
However, if I move the button outside the context of the LinearLayout, e.g. below, it works.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutOverview"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/lblOverviewText"
android:textStyle="bold"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btnMyButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MY BUTTON" />
<LinearLayout
android:id="#+id/layoutSingleBlock"
android:layout_below="#+id/lblOverviewText"
android:layout_above="#+id/linearLayoutSetUnsetButtons"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<!-- irrelevant stuff removed -->
<!-- button moved outside the LinearLayout -->
</LinearLayout>
</RelativeLayout>
What is it about this LinearLayout that is preventing button click events?
Edit
as requested this is how I assign the click listener in OnCreate: (C#/Mono for Android)
FindViewById<Button>(Resource.Id.btnMyButton).Click += (s, e) => AlertAlarmEvents();
Just to note, this exact code works outside the context of the LinearLayout, but not inside, as is outlined in the question.
You forgot add android:clickable="true" to linearLayout, i think you mistook id's

Dynamically resize relative layout

I'm trying to create something that looks like this.
I can sort of get the header working, and the word cloud look-alike stuff on the right.
I'm calling it from a Fragment.
But I'm struggling to get the two column thing working.
I tried to get the width of the parent like this:
parentLayout = (RelativeLayout) view.findViewById(R.id.ParentLayout);
parentLayout.getMeasuredWidth()
this returns 0, while the parentLayout has layout_width="match_parent"
I can't seem to find tutorials/example on this type of "view", or maybe the keyword I'm using for the search is wrong.
Any input is appreciated!!
Thanks in advance!!
p.s. I tried to use onMeasure() as well, but got error "must override or implement a supertype method"
Linear Layouts really can handle this type of layout just fine. I would use an arrangement like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:background="#color/black"
android:orientation="vertical"
android:layout_width="fill_parent">
<!-- your header stuff here
-->
<LinearLayout android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal"
>
<LinearLayout android:id="left picture pane"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ImageView android:layout_width="100dp"
android:background="#drawable/logo"
android:layout_height="50dp"
android:layout_margin="10dp"
/>
<ImageView android:layout_width="100dp"
android:background="#drawable/logo"
android:layout_height="50dp"
android:layout_margin="10dp"
/>
<ImageView android:layout_width="100dp"
android:background="#drawable/logo"
android:layout_height="50dp"
android:layout_margin="10dp"
/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some Text"
android:textColor="#FFFFFF"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some Text"
android:textColor="#80ffff"
android:layout_marginTop="2dp"
android:layout_marginLeft="15dp"
/>
<!-- your fancy lettering in here
or you could put them in a relative layout
instead of this linear one -->
</LinearLayout>
</LinearLayout>
</LinearLayout>

adding elements to scrollview using java when there is more then 1 child

I am trying to add elements to my scrollview,
this scroll view has a vertical linear layout inside of it.
(i added another linear layout - horizontl, inside that will manage 2 textviews and an image according to the attributes i assigned them),
i am trying to add elements of this inner layout to my scroll view (id=insideLineLayout)
(each element added to scrollview will contain these 2 texts and an image)
because of the fact scrollview can only have 1 child i am unable to do this.
would appreciate some help, nothing seems to be working here.
Thanks.
my current xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/searchScreenOuterLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="#+id/catagoryselectedText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:text="selected catagory"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="12dp"
android:orientation="horizontal" >
<EditText
android:id="#+id/searchText"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp" />
<ImageButton
android:id="#+id/searchButton"
android:layout_width="50dp"
android:layout_height="40dp"
android:background="#drawable/search_icon" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="10dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/radiusText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:paddingTop="5dp"
android:text="Search Radius"
android:textAppearance="?android:attr/textAppearanceMedium" />
<SeekBar
android:id="#+id/radiusBar"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp" />
</LinearLayout>
<ScrollView
android:id="#+id/searchScrollView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/searchScrollViewLinear"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingTop="10dp" >
<LinearLayout
android:id="#+id/insideLineLayout"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:orientation="horizontal"
android:weightSum="100"
>
<TextView
android:id="#+id/linesPrice"
android:layout_weight="20"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="tmp1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/linesInfo"
android:layout_weight="60"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:text="tmp2"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="#+id/imageInfo"
android:layout_width="40dp"
android:layout_height="match_parent"
android:background="#drawable/tv_icon" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
Consider how long your list can get as ListView has been optimized in many ways which a regular scroll view is not. If you have too many elements you might start get out of memory exceptions.
In order to dynamically add views to a ScrollView you first need to get the container view inside the ScrollView (most likely a LinearLayout), then you simply do following for each view you add:
View view = inflater.inflate(R.layout.view_to_add, container, false); // this is used if you create from xml, if you make it in code you'd use the regular constructor.
// update the view content (view.findViewById(); etc.)
container.addView(view);

MergeAdapter layout issue in a fragment

I'm trying to make a layout in Android with Fragments. I started to use Commonsware's MergeAdapter but I'm having a weird bit of trouble. The layout worked fine before, but now I get this:
http://img856.imageshack.us/img856/2796/layoutbug.png
There are a couple problems: The white strip should have text going all the way across its length. I set the TextView with a white background to make sure the width was being set correctly. The checkbox right below it should also say "Issue?" but it's trying to wrap the text for some reason.
Here is the layout code that is being added:
<?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">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Instructions" />
<TextView android:id="#+id/tvInstructions" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Instructions go here" android:textSize="32dp" android:background="#FFFFFF"></TextView>
<LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="horizontal" android:paddingTop="24dp">
<CheckBox android:id="#+id/cbIssues" android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="0dp" android:text="Issues?" />
<TextView android:id="#+id/tvStation" android:layout_height="wrap_content" android:layout_width="0dp" android:layout_weight=".5" android:text="Station:" />
<Spinner android:id="#+id/spStation" android:layout_height="wrap_content" android:layout_width="0dp" android:layout_weight=".5"/>
</LinearLayout>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Pending data" />
</LinearLayout>
and here's how I'm inflating it inside the Fragment's onActivityCreated:
LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
MergeAdapter _adapter = new MergeAdapter();
View observationHeaderView = inflater.inflate(R.layout.observationheader, getListView(), false);
_adapter.addView(observationHeaderView);
I get a feeling it has something to do with how I'm inflating the layout. Any help would be appreciated. Thanks.
Don't use match_parent for android:layout_height for something going into an AdapterView, such as your root LinearLayout.
You might also consider temporarily putting your header layout outside the ListView (e.g., in a vertical LinearLayout also holding the ListView) and get it debugged before adding the complication of having it in the ListView.
Beyond that, fire up Hierarchy View (Eclipse perspective or standalone edition) and try to figure out where your layout rules are going wrong.
Also, I am uncertain how well your Spinners will work inside of a ListView on Honeycomb.
It appears my problem was with layout_width being match_parent in certain places in my header, when it should have been wrap_content. The following worked:
<?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">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Instructions" />
<TextView android:id="#+id/tvInstructions" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Instructions go here" android:textSize="32dp" android:background="#FFFFFF"></TextView>
<LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="horizontal" android:paddingTop="24dp">
<CheckBox android:id="#+id/cbIssues" android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="wrap_content" android:text="Issues?" />
<TextView android:id="#+id/tvStation" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_weight=".5" android:text="Station:" />
<Spinner android:id="#+id/spStation" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_weight=".5"/>
</LinearLayout>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Pending data" />
</LinearLayout>

Categories

Resources