This question seems to have been asked a dozen times already bu I haven't been able to deduce the solution from them.
I have a layout called tile.xml that looks like this:
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="10dp"
android:text="May Fong Robinson"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="10dp"
android:text="Beautiful star-shaped spillway, Kechut Reservoir, Jermuk, Armenia"
android:textAppearance="?android:attr/textAppearanceMedium" />
I need to add this one of my views called feed.xml which I've declared as a ScrollView because I want to have multiple objects in it so I can vertically scroll though them. Here's my feed.xml file:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:background="#F1F1F1"
android:layout_height="fill_parent" >
</ScrollView>
I'd like to dynamically add my "tile" from tile.xml to my "feed" feed.xml view while changing the text of the TextViews on the fly.
How can I do this? Thanks
The new feed.xml:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#F1F1F1" >
<LinearLayout
android:id="#+id/tiles"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</LinearLayout>
</ScrollView>
...and the new tile.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="#FFFFFF"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="10dp"
android:text="May Fong Robinson"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="10dp"
android:text="Beautiful star-shaped spillway, Kechut Reservoir, Jermuk, Armenia"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
First of all, add the 2 TextViews inside a layout.
then, in your code, where you want to dynamically add the TextViews you do the following :
ScrollView scroll = (ScrollView) findViewById(R.id.scrollView1);
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.tile, null);
scroll.addView(view);
To access the TextViews you have to do the following :
TextView textView1 = (TextView) view.findViewById(R.id.textView1);
textView1.setText("");
Also make sure to give your TextViews different IDs in the XML file. Currently they are both textView1
ScrollView can only have 1 child, meaning the current structure of your "tile" layout can't be added to the ScrollView.
What you need is to add another Layout, let's say LinearLayout to your ScrollView so that it would be the ScrollView's only direct child. To this LinearLayout you can add as many childs as you want.
This can be done by using the LayoutInflater which you've probably encountered in numerous examples.
**TRY THIS**
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#F1F1F1" >
<LinearLayout
android:id="#+id/tiles"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<include layout="#layout/tile" />
</LinearLayout>
Related
I have this code xml layout that I am trying to implement in a round android wear watch. I want the TextView above the ExpandableListView to scroll with it rather than staying on the top and taking up space.
I tried using ScrollView with it rather than RelativeLayout (ScrollView in RelativeLayout and vice versa as well) but what that does is that the LinearLayout requires a set height for the ExpandableListView to show the list which is not possible as the list dynamically changes (so it can have a random number of group members) and if I use "wrap_content" it only shows one group at a time. Each group has 4 child members as well.
Can someone tell me how to make this layout suitable for me so that the TextView and the ExpandableListAdapter scroll together without having a set height for the ExpandableListAdapter?
round_activity_card_view_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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"
tools:context="com.jawad.wifihotspotfinder.CardViewLayout"
tools:deviceIds="wear_round"
android:background="#FF3C00"
android:smoothScrollbar="true">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="20dp"
android:paddingLeft="15dp"
android:paddingRight="10dp"
android:paddingBottom="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cardViewHeader"
android:textSize="17sp"
android:textColor="#color/white"
android:fontFamily="sans-serif"
android:paddingLeft="20dp"
android:paddingRight="5dp"/>
<ExpandableListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/round_expandableListView"
android:background="#FF3C00"
android:choiceMode="multipleChoice"
android:groupIndicator="#null"
android:longClickable="true"/>
</LinearLayout>
</ScrollView>
Look into addHeaderView(View v). Create your TextView programmatically or inflate from XML and then add it to your ExpandableListView using that method.
The TextView should then scroll with your ExpandableListView.
Thanks for showing me what to do, Alex. I had to come up with some way to do that which took ages but finally got it right. Thanks for the instructions which really helped me a lot! The code is used is given below:
LinearLayout mLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.expandable_header, expListView, false);
mLayout.setLayoutParams(new AbsListView.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
expListView.addHeaderView(mLayout);
Also the changed xml files:
expandable_header.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#FF3C00">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/expCardViewHeader"
android:textSize="17sp"
android:textColor="#color/white"
android:fontFamily="sans-serif"
android:paddingLeft="20dp"
android:paddingRight="5dp"
android:paddingBottom="2dp"
android:background="#FF3C00"
android:text="#string/no_results"/>
</LinearLayout>
round_activity_card_view_layout.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.jawad.wifihotspotfinder.CardViewLayout"
tools:deviceIds="wear_round"
android:background="#FF3C00"
android:smoothScrollbar="true">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#FF3C00"
android:paddingTop="20dp"
android:paddingLeft="15dp"
android:paddingRight="10dp"
android:paddingBottom="10dp">
<ExpandableListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/round_expandableListView"
android:background="#FF3C00"
android:choiceMode="multipleChoice"
android:groupIndicator="#null"
android:longClickable="true"
android:smoothScrollbar="true" />
</LinearLayout>
</RelativeLayout>
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.
Alright so I quickly created an XML file with the following contents:
<?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" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Text View 1"
android:textSize="30dp" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</ListView>
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Text View 2"
android:textSize="30dp" />
<ListView
android:id="#+id/listView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</ListView>
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Text View 3"
android:textSize="30dp" />
<ListView
android:id="#+id/listView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</ListView>
</LinearLayout>
</ScrollView>
</LinearLayout>
And the question is, is that why does it look so much different when I click on the "Graphical Layout" tab? What I imagined the graphical layout to look like would basically be 3 labels right under each other (this is because I set everything to wrap content, and because there are no items inside either of the ListViews, so I'd think that it wouldn't even be visible). But anyway, the graphical layout shows it to be:
I'm not sure if that's correct or not, and if I run it will it look like I imagine it to look? I basically want (everything inside 1 ScrollView) 3 TextView's, and 1 ListView immediately following each TextView. So the layout will look like this:
ScrollView
TextView
ListView
TextView
ListView
TextView
ListView
End of ScrollView
Something exactly like the layout shown above. Could someone please tell me what is wrong within my XML file for all the other labels not be showing on it?
Note: When I click on the components on the side, it seems that a few things are shifting. (When I tried clicking on the TextView2 (to try to search for the blue bounds box) it seemed like the TextView1 label got pushed down a bit, and the second TextView was still not visible. If anyone could please help me achieve the layout that I want, it would be greatly appreciated.
EDIT: take a look at this approach.
<?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" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Text View 1"
android:textSize="30dp" />
</ScrollView>
<ScrollView
android:id="#+id/scrollView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/scrollView1">
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Text View 2"
android:textSize="30dp" />
</ScrollView>
<ScrollView
android:id="#+id/scrollView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/scrollView2" >
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Text View 3"
android:textSize="30dp" />
</ScrollView>
</RelativeLayout>
As for the ListView you should not put a ListView inside a ScrollView. Here is an explanation of why you should not put a ListView inside a ScrollView: Listview inside ScrollView
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.
I am trying to create a UI using relative layout where in the UI should look as such:
In the top of the screen i have a TextView
followed by another TextView
followed by scroll view where in i have to display the text in the
scrollable format
in the end i have a imageview.
How can i achieve this.
I have tried using the combination of Relative and Linear Layout but the elements gets overlapped. Anyone have any suggestions on how to proceed?
<?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="#drawable/background_main" android:id="#+id/g_description">
<TextView android:id="#+id/titleBar" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:background="#drawable/header"
android:layout_alignParentTop="true" android:gravity="center"
android:text="#string/aboutus" android:textAppearance="? android:attr/textAppearanceLarge"
android:textStyle="bold" />
<TextView android:id="#+id/wd_name"
android:layout_width="fill_parent" android:layout_below="#id/titleBar"
android:layout_height="wrap_content" android:textStyle="bold"
android:textColor="#FFFFFF" android:textSize="18dip"
android:background="#drawable/background_main" android:gravity="center" />
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrolltext" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_below="#id/wd_name"
android:padding="5dip">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/wd_description"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:textSize="14dip" android:textColor="#FFFFFF">
</TextView>
</LinearLayout>
</ScrollView>
<ImageView android:id="#+id/bottomBar" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:src="#drawable/bottom_bar"
android:layout_alignParentBottom="true" />
you can achieve it like this:
Take relative layout and put textview in it.
Take another textview and add this to it android:layout_below="id of the 1st textview".
Take scrollview and add this to it android:layout_below="id of the 2nd textview".
Take linear layout inside scrollview.
Put textview inside linear layout.
Take imageview and add this to it android:layout_below="id of the scrollview".
Now your layout is ready and if you get any error then let me know with your code....
If you have a series of elements that you want to flow down the screen, then it sounds like you just need a LinearLayout.
It goes as below. Placing a TextView inside scrollview will make it scrollable. Reset is selfexplanary.
<?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">
<TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="Below TOP TextView" android:id="#+id/TextView01" android:layout_alignParentTop="true"></TextView>
<TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="TOP TextView" android:id="#+id/TextView02" android:layout_below="#id/TextView01"></TextView>
<ScrollView android:layout_height="wrap_content" android:layout_width="fill_parent" android:fillViewport="true" android:layout_below="#id/TextView02">
<TextView android:id="#+id/textView1" android:layout_height="wrap_content" android:text="Scrollable textViewafkasjf;laksjflaskjf;lasjf;alsfjal;sfj;aslfj;aslfja;slfj;aslkfj;asldfj;aslkfjasl;dfj;a asfj aslfj asldfjk as;lfjk asfjka;sldf jl;asj df;asdfjasfj aslfj asljf asjfl;asfasj fasj f;asj flas jf;lasjf;asjkf;las f;asj df;as jf;lasjf ;asjf;asjf;asjf;asjf;asjf;jasf;asfja s;dfa;sf asf jf asf a; sldfja;slfj;asldfjk" android:layout_width="fill_parent"></TextView>
</ScrollView>
<ImageView android:src="#drawable/icon" android:layout_height="wrap_content" android:id="#+id/imageView1" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_width="fill_parent"></ImageView>
</RelativeLayout>
It works fine with setting android:orientation="vertical" for each text view.