add LinearLayout via addContentView(). Sliding drawer now beneath the last added Content - android

Friends,
i have a xml file including a Sliding drawer.
I setContentView in the start of the application.
I create dynamically a LinearLayout in Java with a few TextViews and Edittext and add it to the application via addContentView().
The SlidingDrawer still works but LinearLayout i added via addContentView() is displayed above the SlidingDrawer.
Is there a way to make the SlidingDrawer work correct?
Heres part of my code
setContentView(R.layout.real_time_report_layout);
LinearLayout linearLayoutRealTimeReportActivity = new LinearLayout(ctx);
TextView tv_questionType1 = new TextView(ctx);
linearLayoutRealTimeReportActivity.addView(tv_questionType1);
addContentView(linearLayoutRealTimeReportActivity, layoutParamsRealTimeReportActivity);
I m glad for any support also work around!!!
heres my xml layout
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/et_real_time_report_description"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:orientation="horizontal"
android:weightSum="100" >
<Button
android:id="#+id/b_real_time_report_send"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="50"
android:text="#string/b_real_time_report_send" >
</Button>
<Button
android:id="#+id/b_real_time_report_cancel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="50"
android:text="#string/b_real_time_report_cancel" >
</Button>
</LinearLayout>
</LinearLayout>
<SlidingDrawer
android:id="#+id/sd_real_time_report_layout_SlidingDrawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:content="#+id/content"
android:handle="#+id/sd_real_time_report_layout_handle" >
<Button
android:id="#+id/sd_real_time_report_layout_handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Handle" >
</Button>
<RelativeLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.google.android.maps.MapView
android:id="#+id/mv_real_time_report"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="50dp"
android:apiKey="0IfKjDM6XzpM6rGFR0H6X03Y1aWVBOnJ1C8b6wQ"
android:clickable="true"
android:enabled="true" />
<TextView
android:id="#+id/tv_real_time_report_userLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:text="test"
android:textSize="20dp" >
</TextView>
<Button
android:id="#+id/b_real_time_report_deleteUserLocationPin"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="#string/b_real_time_report_deleteUserLocationPin" />
<ToggleButton
android:id="#+id/tb_real_time_report_chooseLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:textOff="#string/tb_real_time_report_ChooseLocationOFF"
android:textOn="#string/tb_real_time_report_ChooseLocationON" />
</RelativeLayout>
</SlidingDrawer>

You need slight changes.
Give an id to your parent LinearLayout in layout xml (let's say, android:id="#+id/parent_container").
You will get your LinearLayout in java file.
something like this:
LinearLayout container=(LinearLayout)this.findViewById(R.id.parent_container);
Now when adding new View to container we will use index to put view at specific index.
something like this:
container.addView(linearLayoutRealTimeReportActivity, container.getChildCount()-2);
We substracted -2 because we want to put View at second last index.
So SlidingDrawer is still at the last index.

Android draws Views in order of adding them to the container, so your view will be drawn under Drawer.
The easiest soluition would probably be to add your cusom view to FrameLayout (or any other layout) defined in xml.

Related

How to make a Relative Layout Duplicate Itself Inside a Linear Layout On Button Click

I have an alert dialogue set up inside a RelativeLayout. A small section is a ListView with a RelativeLayout inside. My xml file is as follows:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
...
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/bar_main"
android:id="#+id/dialoguelistview"
android:padding="10dp">
<RelativeLayout>
<EditText
android:id="#+id/Item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:hint="#string/Item"
/>
<EditText
android:id="#+id/Quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_alignParentLeft="true"
android:hint="#string/Quantity"
android:layout_below="#id/Item"/>
<EditText
android:id="#+id/Cost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_toRightOf="#id/Quantity"
android:hint="#string/Cost"
android:layout_below="#id/Item"/>
<Button
android:id="#+id/Button01"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentRight="true"
android:layout_marginTop="20dp"
android:layout_marginRight="10dp"
android:layout_below="#id/Item"
android:background="#drawable/round_button"
android:text="#string/another_item"
android:textColor="#fff"
android:onClick = "addAnotherItem"></Button>
</RelativeLayout>
</ListView>
...
</RelativeLayout>
</ScrollView>
When the button is clicked, I need the inner relative layout to duplicate itself inside the list view. How can I do this? Thanks in advance!
Create a separate layout xml file for RealtiveLayout that you want to inflate.
Example
The xml of the parent page is:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="em.example.InfoActivity">
<LinearLayout
android:id="#+id/infolayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:layout_marginTop="50dp">
</LinearLayout>
</RelativeLayout>
Child Page
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#55934d"
android:padding="5dp">
<TextView
android:id="#+id/TextAddress"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TEXT ADDRESS:"
android:textSize="10sp"
android:textColor="#fff"
/>
</RelativeLayout>
And this is your activity.java code
public void addChildLayout(){
//Inflater service
LayoutInflater layoutInfralte=(LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//parent layout xml refrence
LinearLayout linearLayout=(LinearLayout)findViewById(R.id.infolayout);
//Child layout xml refrence
View view=layoutInfralte.inflate(R.layout.infoitem, null);
//add child to parent
linearLayout.addView(view);
}
Using the above pattern you can dynamically add new views.
Another way of doing it would be to keep two copies of the relativelayout in the parent linearlayout . Keep the visibility of one of them as 'gone' and mark it visible when you want it to appear.
Benefit of this approach would be to minimize code.

ListView hidden behind button

The contents of my list view get hidden behind the button as follows:
The xml file is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="top"
>
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
/>
<RelativeLayout
android:id="#+id/InnerRelativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
>
<Button
android:text="Send"
android:id="#+id/Button"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="addItems"
>
</Button>
<EditText
android:id="#+id/message"
android:layout_width="fill_parent"
android:layout_toLeftOf="#id/Button"
android:layout_height="wrap_content"
>
</EditText>
</RelativeLayout>
</RelativeLayout>
The TextView for each row is as follows:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content
android:layout_gravity="right"
android:gravity="right"
android:textSize="20sp"
/>
What should i do to align it properly (as in above the button)?
Also when the listView is with just one or two entries, and when the keyboard is opened to type, the whole view shifts? How do I fix that as well? Thanks in advance
Cleaned up and corected the code a bit. Perhaps this is what you were looking for. Let me know if it works. Cheers !
<?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:orientation="vertical" >
<RelativeLayout
android:id="#+id/InnerRelativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<Button
android:id="#+id/Button"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:onClick="addItems"
android:text="Send" />
<EditText
android:id="#+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:layout_toLeftOf="#id/Button" />
</RelativeLayout>
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/InnerRelativeLayout" />
</RelativeLayout>
adding android:layout_above="#+id/InnerRelativeLayout" to your ListView.
RelativeLayout has child views specify their position relative to the parent view or to each other (specified by ID). So you can align two elements by right border, or make one below another, centered in the screen, centered left, and so on. By default, all child views are drawn at the top-left of the layout, so you must define the position of each view using the various layout properties available from RelativeLayout.LayoutParams.
For put your InnerRelativeLayout layout at Top add android:layout_alignParentTop="true" to your InnerRelativeLayout and remove android:layout_alignParentBottom="true"
Update: set up your Layout like below:
<?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:background="#color/Beige "
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/InnerRelativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
<Button
android:id="#+id/Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:onClick="addItems"
android:text="Send" >
</Button>
<EditText
android:id="#+id/message"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/Button"
android:hint="Enter text" >
</EditText>
</RelativeLayout>
<ListView
android:id="#+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/InnerRelativeLayout"
android:drawSelectorOnTop="false" />
</RelativeLayout>
Output:
use android:layout_above="#+id/InnerRelativeLayout" in your listview
Make the outer RelativeLayout into a LinearLayout with orientation vertical. Then, for the ListView set layout_height as 0dp and layout_weight as 1. The ListView will fill up all the remaining space in the LinearLayout without overlapping other views.
Also, stop using fill_parent, it's been deprecated and you should use match_parent instead.

Changing Layout Dynamically in Android

I have an activity which loads a few layouts in a layout.xml file. The First LinearLayout host the all the views together. The second linear layout hosts two labels together. The Relative layout host a few labels and a ListView that populates items when loaded. I want to improve navigation by achieving a "TRANSITION" of the RelativeLayout to another Layout without affecting other layouts in the activity. In other words, when clicking on one of the rows populated by the ListView (OnItemClickListener), this event listener replaces the current RelativeLayout with a new Layout.
Any idea of achieving this?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:background="#FFDECE"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Date"
android:layout_weight="0.5"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gang Code"
android:layout_weight="1.0"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp">
<TextView
android:id="#+id/tvEmpyName"
....
</RelativeLayout>
<ListView
android:id="#+id/lvATRDisplay"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
You can use ViewFlipper for that. And use setDisplayedChild(int) to display particular layout i.e. child
e.g.
setDisplayChild(0);

Relative Layout is causing issues with formatting

I have been working on an app for my school recently and wanted to clean it up a bit before possibly publishing it. On the scheduling portion of the app, I have 5 buttons that perform actions on a ListView that is also on the screen at the same time. However, I have the issue when I have around 6 or more events on the screen as once the list view takes over the screen and pushes the buttons off the screen, making it so that I cannot delete the events, make new ones, and so on.
I tried setting the list view to a static size (400px) which worked for normal screen orientation, but if the phone is set to landscape view you cannot see the buttons either. With my current code it would appear to work in the XML viewer but in practice is not the case.
This is the code without the static size setting:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#551A8B"
android:textColor="#FFD700"
>
<Button android:text="#string/New"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/button3">
</Button>
<Button android:text="#string/Edit"
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/button3"
android:layout_alignTop="#id/button3">
</Button>
<Button android:text="#string/delete"
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/button4"
android:layout_alignTop="#id/button4">
</Button>
<Button android:layout_height="wrap_content"
android:id="#+id/button7"
android:layout_width="wrap_content"
android:text="#string/Previousweek"
android:layout_below="#id/button3">
</Button>
<Button android:layout_height="wrap_content"
android:id="#+id/button6"
android:layout_width="wrap_content"
android:text="#string/Next"
android:layout_below = "#id/button3"
android:layout_toRightOf = "#id/button7">
</Button>
<ListView android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#FFD700"
android:layout_below="#id/button7"
android:textSize="10sp" >
</ListView>
</RelativeLayout>
The XML viewer for this code is:
Which would lead me to believe it would work fine. I tested it on my emulator and got the following result after entering a bunch of silly events however:
This result is consistent with multiple versions of the emulator.
How I can fix this problem without using static size constraints that cause landscape orientation issues?
Separate the buttons into a separate RelativeLayout and enclose this and the ListView in a vertical LinearLayout.
Then:
<LinearLayout android:id="#+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout [...]
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<!-- Your buttons -->
</RelativeLayout>
<ListView android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" />
</LinearLayout
The key point here is the height and weight on the ListView. This means that it fills the remaining space in the LinearLayout after space has been correctly allocated for the buttons.
Add a android:weigth in your listView tag and set the android:weigth value to 1. This will work when your list view height and width is set to fill_parent and your list view is covering entire layout. So try it, it will work.
One simple solution would be to separate the buttons in their own relative layout and put the whole thing in a linear layout, eg:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#551A8B"
android:textColor="#FFD700">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#551A8B"
android:textColor="#FFD700">
<!-- your buttons -->
</RelativeLayout>
<ListView
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#FFD700"
android:layout_below="#id/button7"
android:textSize="10sp">
</ListView>
</LinearLayout>
Use a vertical LinearLayout with two rows of Buttons (each row as a LinearLayout), then give the ListView a layout_weight value of "1". In fact, use layout_weight to clean up the size of your buttons too.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button android:text="#string/New"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"
android:id="#+id/button3" />
<Button android:text="#string/Edit"
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button android:text="#string/Delete"
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button android:layout_height="wrap_content"
android:id="#+id/button7"
android:layout_width="wrap_content"
android:layout_weight="1"
android:text="#string/Previousweek" />
<Button android:layout_height="wrap_content"
android:id="#+id/button6"
android:layout_width="wrap_content"
android:layout_weight="1"
android:text="#string/Next" />
</LinearLayout>
<ListView android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:textColor="#FFD700"
android:textSize="10sp" >
</ListView>

Balloon chat application issues

I am doing a simple chat application and I want to show balloons similar to the iphone's sms app.
So I am doing an Activity with a ListView with a certain layout.
This are my layouts:
/* Activity Layout */
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView android:id="#+id/chat_log"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stackFromBottom="true"
android:layout_marginTop="50dp"
android:transcriptMode="alwaysScroll"
android:layout_weight="1"
android:cacheColorHint="#00000000"
android:clickable="false"
/>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<EditText android:id="#+id/chat_input_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="bottom"
/>
<Button android:id="#+id/chat_send_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/chat_send_button"
/>
</LinearLayout>
</LinearLayout>
Other:
/* Row Layout */
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/userprofile_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/chat_ballon_left" >
<TextView
android:id="#+id/chat_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="2dp"
android:text="haasdasdasdasdasdasdasdoo" />
</FrameLayout>
Here's the result.
My issues:
The gray line which I would like to remove.
Text is not using the whole space.
Somehow even thought I've added android:clickable="false" the balloons are clickable.
The FrameLayout is unnecessary, so use the TextView as a root element (with the chat_ballon_left background of course). Set the width to match_parent so that the text takes the whole space.
BTW nice baloons, don't forget to have hdpi versions too :)

Categories

Resources