Add xml dynamically to layout - java

I have a (complex) RelativeLayout with a few TextViews, ImageViews and a Button. The amount of how often this layout is created depends on data from the database. I get this data from a list and want to show it dynamically, what have I to do?
xml to add dynamically:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/zusatz_content" >
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:layout_marginRight="15dp"
android:layout_marginLeft="15dp"
android:text="Lorem Ipsum 1" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/text2"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="#string/button_desc" />
<ImageView
android:id="#+id/image"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_below="#+id/text1"
android:layout_centerHorizontal="true"
android:contentDescription="#string/ima_zusatz_desc"
android:src="#drawable/nix" />
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/text1"
android:layout_below="#+id/image"
android:text="Lorem Ipsum 2" />
Notice: The texts and image should be changed dynamically by the content from the database.
thanks, tomtom

Besides your XML, you will need an Adaptor and a ListView of some kind. In your Adapter you will have to implement
public View getView(int position, View convertView, ViewGroup parent)
In this method, return your new view (Probably created with LayoutInflator and your xml) and update that data displayed on each button/text view.
Make sure to research how convertView works. If your lists are long, the performance boost from using convertView properly is huge. If you get this correct, Android's listview and baseadpater will do the hard work for you.

Related

Widget ListView loading view

I'm trying to display a dynamic list on a widget and so far i've been able to display my 3 headers (which are simply textview inside a linearlayout) but for my items (2 textview in one linearlayout inside another linear layout) the view displays Loading... instead of the content (a task label).
I've already bumped getViewTypeCount to 2 in my RemoteViewsFactory to match the number of views returned by the getViewAt.
I can't figure out what is wrong inside this layout that would cause a problem:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="3dp"
android:clickable="true"
android:focusable="true"
style="#style/SelectableItemBackground"
android:id="#+id/widget_item"
>
<LinearLayout
android:paddingLeft="22dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="•"
android:paddingRight="6dp"
android:textSize="17dp"
android:textColor="#color/colorText" />
<TextView
android:id="#+id/widget_item_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="17dp"
android:textColor="#color/colorText" />
</LinearLayout>
Thank you for reading :)
To anyone reading this, i have been able to solve it by suppressing the style attribute

emptyView always showing even if list is not empty

I wish someone could take a look on that and let me know what is wrong.this is my emptyView container in the activity_main
<ListView
android:id="#+id/display_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RelativeLayout
android:id="#+id/empty_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<TextView
android:id="#+id/empty_shelter_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:fontFamily="sans-serif-medium"
android:paddingTop="16dp"
android:text="#string/empty_view_title_text"
android:textAppearance="?android:textAppearanceMedium" />
<TextView
android:id="#+id/empty_subtitle_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/empty_shelter_title"
android:layout_centerHorizontal="true"
android:fontFamily="sans-serif"
android:paddingTop="8dp"
android:text="#string/empty_view_sub_text"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#A2AAB0" />
</RelativeLayout>
I have set emptyView in each fragment, and even though when items are showing in the list it is still showing the empty view fields.
//set empty activity
ListView listView = rootView.findViewById(R.id.display_view);
View emptyView = rootView.findViewById(R.id.empty_view);
listView.setEmptyView(emptyView);
//create a new cursor adapter
mCursorAdapter = new PlatformCursorAdapter(getActivity(),null);
listView.setAdapter(mCursorAdapter);
I will highly appreciate it if you can tell me what i did wrong.
Try hiding the empty_view in xml.
<RelativeLayout
android:id="#+id/empty_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_centerInParent="true">
my friends you have a send a null in adapter. send a list
mCursorAdapter = new PlatformCursorAdapter(getActivity(),null);
and other way are used to check a listview is null hide the
display_view view and show the empty_view .

How to keep views from disappearing in wrap_content

I have a problem where a view of mine is disappearing in my RecyclerView when the layout of that view is wrap_content. The RecyclerView is match_parent both directions, and uses a LinearLayout that inflates the below xml file. Here is what each piece of the RecyclerView is supposed to look like:
And here is the XML for that:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<View
android:id="#+id/scheduleBlockColor"
android:layout_width="15dp"
android:layout_height="match_parent"
android:layout_alignBottom="#+id/scheduleBlockText"
android:background="#color/colorCougarRed" />
<RelativeLayout
android:id="#+id/scheduleBlockText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toEndOf="#id/scheduleBlockColor"
android:layout_toRightOf="#id/scheduleBlockColor"
android:paddingEnd="0dp"
android:paddingLeft="5dp"
android:paddingRight="0dp"
android:paddingStart="5dp">
<TextView
android:id="#+id/scheduleBlockTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp"
android:text="#string/schedule_block_time_default"
android:textSize="16sp" />
<TextView
android:id="#+id/scheduleBlockClassName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/scheduleBlockTime"
android:ellipsize="end"
android:padding="2dp"
android:singleLine="true"
android:text="#string/schedule_block_class_default"
android:textColor="#000000"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/scheduleBlockRoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/scheduleBlockClassName"
android:padding="2dp"
android:text="#string/schedule_block_room_default"
android:textSize="16sp" />
</RelativeLayout>
</RelativeLayout>
My question is, how do I keep that colored piece from disappearing when the RecyclerView loads? If I have the parent RelativeLayout above set to match_parent, it works fine. I've tried this answer, but haven't had the same success
It is because of the height set on the <View>. It gets messed up (even with match_parent) by the fact that it doesn't have content. Since the intention is to have a vertical stripe, you could anchor it to the top and bottom of the parent. You already are doing it for the bottom part (kind of, aligning it to the bottom of the text view container), so you only need to take care of the top anchoring:
<View
android:id="#+id/scheduleBlockColor"
...
android:layout_alignParentTop="true"
... />
Instead of padding using layout_margin inside your RelativeLayout.

diffculty choosing an item from a list view

Hello I've built a list view that in its XML file have 2 layouts with background and 1 text view on each layout,I'm getting from the strings array text, And changing the text view,this is my code for the row I'm getting for for the list view:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:weightSum="2"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#drawable/purple"
android:layout_weight="1"
android:id="#+id/rl1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textColor="#ffffff" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#drawable/purple"
android:layout_weight="1"
android:id="#+id/rl2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textColor="#ffffff" />
</RelativeLayout>
</LinearLayout>
Now my problem is when I'm trying to choose only one of the boxes on list view on item selected and doing a switch on the positions I'm getting the 2 boxes on the same position so it don't matter what what box I'm choosing, Iv'e try to give the linear layouts id and then use set on click listener on them with no successes,I'll appreciate and suggestions.
I'm not sure I understand.
Do you use this layout to create ListView items ?
If that is the case, the problem may be that when the ListView is refreshed, it inflates instances of this layout (in its default configuration, the components are in their default position). In order to preserve the changes, you will need to save each item's state and modify its layout every time the list is refreshed.

RelativeLayout Params addRule without ID

I want to add the following rule to a TextView within my RelativeLayout
TextView anyView = new TextView(anyContext);
RelativeLayout.LayoutParams layout;
..
layout.addRule(RelativeLayout.LEFT_OF, anyView.getID());
This only works if I add the ID of the element. The problem is that I do not have and do not want to have any IDs specified, because my view can be used multiple times within the same ViewGroup later.
So either I have to assign that rule by reference or I need to create new IDs dynamically so that I don't create multiple Views with the same ID.
Any suggestions what to do in this situation?
I tried to copy from this xml:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentRight="true"
android:id="#+id/gridLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A lot of text for some width"/>
</GridLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="123"
android:layout_centerVertical="true"
android:gravity="center_horizontal"
android:layout_toLeftOf="#+id/gridLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
try this
RelativeLayout.LayoutParams pAC = new RelativeLayout.LayoutParams(
your width,
your height);
pAC.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
anyView .setLayoutParams(pAC);

Categories