I have a hashmap full of values which will populate the listview. the image sets fine but the text does not. The hashmap is not null, just some odd reason why it would not display. Any help would be appreciated
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null){
vi = inflater.inflate(R.layout.list_row, null);
}
TextView description = (TextView)vi.findViewById(R.id.description);
TextView username = (TextView)vi.findViewById(R.id.usernamae);
TextView likes = (TextView)vi.findViewById(R.id.likes);
ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
HashMap<String, String> song = new HashMap<String, String>();
song = data.get(position);
description.setText(song.get(MenuActivity.KEY_description));
username.setText(song.get(MenuActivity.KEY_username));
likes.setText(song.get(MenuActivity.KEY_likes));
imageLoader.DisplayImage(song.get(MenuActivity.KEY_THUMB_URL), thumb_image);
return vi;
}
<?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="wrap_content"
android:background="#drawable/list_selector"
android:orientation="horizontal"
android:padding="5dip" >
<LinearLayout android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:layout_alignParentLeft="true"
android:background="#drawable/image_bg"
android:layout_marginRight="5dip">
<ImageView
android:id="#+id/list_image"
android:layout_width="50dip"
android:layout_height="50dip"
/>
</LinearLayout>
<TextView
android:id="#+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/thumbnail"
android:layout_toRightOf="#+id/thumbnail"
android:textColor="#040404"
android:typeface="sans"
android:textSize="15dip"
android:textStyle="bold"/>
<TextView
android:id="#+id/username"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/description"
android:textColor="#343434"
android:textSize="10dip"
android:layout_marginTop="1dip"
android:layout_toRightOf="#+id/thumbnail" />
<TextView
android:id="#+id/likes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#id/username"
android:gravity="right"
android:layout_marginRight="5dip"
android:textSize="10dip"
android:textColor="#10bcc9"
android:textStyle="bold"/>
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
I was able to show text using the layout you provided above, but only for "description" and "username", "likes" is hidden behind the last ImageView, try using android:layout_toLeftOf="#id/id_of_image"for likes. I only did a quick test with square images, depending on your actual images, you might have more hidden views, but this confirms the issue lies with the xml. Try rearranging your xml so your views don't overlap each other.
Happy coding !
Try use description.setText(String.valueOf(song.get(MenuActivity.KEY_description)));
Also try rebuild your project.
EDIT:
I watch your xml, you also add extra ", look:
android:id="#+id/"description"
need write like this:
android:id="#+id/description"
Have you tried if text shows in the TextViews, if you hard-code a string in the layout XML for the TextViews? Maybe the views are just not visible, try re-arranging your layout in that case.
description.setText(song.get(MenuActivity.KEY_description).toString());
username.setText(song.get(MenuActivity.KEY_username).toString());
likes.setText(song.get(MenuActivity.KEY_likes).toString());
Related
Android version 11.0.4
I am trying to use a ListView to display a series of elements with different types of information to display in the same list using a custom ListAdapter.
To make the same ListAdapter able to display different types of information I'm creating a custom XML template with all possible elements that I want to display and hiding the ones I don't want to show programmatically.
However, when I hide the extra elements I don't want to show the size of the view displaying them shrinks.
When the extra elements are shown
When the extra elements are gone
The code that hides the elements:
TextView catagory = (TextView) convertView.findViewById(R.id.title);
TextView time1 = (TextView) convertView.findViewById(R.id.time1);
TextView time2 = (TextView) convertView.findViewById(R.id.time2);
TextView location = (TextView) convertView.findViewById(R.id.location);
TextView description = (TextView) convertView.findViewById(R.id.description);
View separator1 = (View) convertView.findViewById(R.id.separator_vertical1);
View separator2 = (View) convertView.findViewById(R.id.separator_horizontal1);
View separator3 = (View) convertView.findViewById(R.id.separator_vertical2);
View separator4 = (View) convertView.findViewById(R.id.separator_horizontal2);
catagory.setText(item.name);
Class type = item.getType();
if (EventItem.class.equals(type)) {
time1.setText(String.valueOf(((EventItem)item).timeStart));
time2.setText(String.valueOf(((EventItem)item).timeEnd));
location.setText(((EventItem)item).location);
description.setText(((EventItem)item).description);
} else if (TaskItem.class.equals(type)) {
time1.setText(String.valueOf(((TaskItem)item).timeDue));
description.setText(((TaskItem)item).description);
//hide unused elements
separator2.setVisibility(View.GONE);
location.setVisibility(View.GONE);
separator3.setVisibility(View.GONE);
time2.setVisibility(View.GONE);
} else if (ReminderItem.class.equals(type)) {
time1.setText(String.valueOf(((ReminderItem)item).time));
//hide unused elements
separator2.setVisibility(View.GONE);
location.setVisibility(View.GONE);
separator3.setVisibility(View.GONE);
time2.setVisibility(View.GONE);
separator4.setVisibility(View.GONE);
description.setVisibility(View.GONE);
} else if (SubjectItem.class.equals(type)) {
description.setText(((SubjectItem)item).description);
} else if (NoteItem.class.equals(type)) {
description.setText(((TaskItem)item).description);
//hide unused elements
separator1.setVisibility(View.GONE);
time1.setVisibility(View.GONE);
separator2.setVisibility(View.GONE);
location.setVisibility(View.GONE);
separator3.setVisibility(View.GONE);
time2.setVisibility(View.GONE);
} else if (ContactItem.class.equals(type)) {
//hide unused elements
separator1.setVisibility(View.GONE);
time1.setVisibility(View.GONE);
separator2.setVisibility(View.GONE);
location.setVisibility(View.GONE);
separator3.setVisibility(View.GONE);
time2.setVisibility(View.GONE);
}
The XML template:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/rectangle">
<TextView
android:id="#+id/title"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:textAlignment="textStart"
android:textSize="16sp"
android:textColor="#ffffffff"/>
<View
android:id="#+id/separator_vertical1"
android:layout_width="1dp"
android:layout_height="23dp"
android:layout_toStartOf="#id/time1"
android:background="#android:color/white"/>
<TextView
android:id="#+id/time1"
android:layout_width="94dp"
android:layout_height="wrap_content"
android:layout_toStartOf="#id/title"
android:layout_alignParentEnd="true"
android:textAlignment="center"
android:textSize="16sp"
android:textColor="#ffffffff"/>
<View
android:id="#+id/separator_horizontal1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#id/title"
android:background="#android:color/white"/>
<TextView
android:id="#+id/location"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#id/separator_horizontal1"
android:textAlignment="textStart"
android:textSize="16sp"
android:textColor="#ffffffff"/>
<View
android:id="#+id/separator_vertical2"
android:layout_width="1dp"
android:layout_height="23dp"
android:layout_toStartOf="#id/location"
android:layout_below="#id/separator_horizontal1"
android:background="#android:color/white"/>
<TextView
android:id="#+id/time2"
android:layout_width="94dp"
android:layout_height="wrap_content"
android:layout_toStartOf="#id/location"
android:layout_below="#id/separator_horizontal1"
android:layout_alignParentEnd="true"
android:textAlignment="center"
android:textSize="16sp"
android:textColor="#ffffffff"/>
<View
android:id="#+id/separator_horizontal2"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#id/location"
android:background="#android:color/white"/>
<TextView
android:id="#+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/separator_horizontal2"
android:textAlignment="textStart"
android:textSize="20sp"
android:textColor="#ffffffff"/>
The ListView the elements are displayed in:
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
android:id="#+id/text_overview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
android:textColor="#ffffffff"
android:divider="#android:color/transparent"
android:dividerHeight="8dp"
android:headerDividersEnabled="true"
android:footerDividersEnabled="true"
android:scrollbars="none"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
The answer to your question is to use View.INVISIBLE
However, when I hide the extra elements I don't want to show the size of the view displaying them shrinks.
View.GONE: This view is invisible, and it doesn't take any space for layout purposes.
View.INVISIBLE: This view is invisible, but it still takes up space for layout purposes.
Here's the link of Documentation.
Use View.INVISIBLE which takes up space and invisible the view.
while you are using View.GONE which doesn't takes up space for the view and invisible the view.
For more description, you can go with:https://developer.android.com/reference/android/transition/Visibility
I eventually got it working, not sure how but setting each element as visible before going into the if statements stoped the shrinking of the view window. It also solved the issue of some list items missing elements that shouldn't have been hidden. I can only assume some resources were being reused somewhere and arriving pre-hidden.
I've created what I want in xml but I need to do it programatically and I can't get it to work. It's a table layout with one row. In that row there is two more table layouts. The first of which has one row containing a picture and the second table layout has two rows containing two textviews and a picture. Is there some kind of xml to java converter? I've tried to write the code now for a couple of hours and can't get the layouts to work. I'd post a picture but I need 10 rep points first. The whole xml file is in a linear layout if it helps.
<TableLayout
android:id="#+id/tablemain"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:gravity="center_horizontal" >
<TableRow
android:id="#+id/TableRow1"
android:background="#drawable/border"
android:weightSum="100" >
<TableLayout
android:id="#+id/Tableout2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0"
android:background="#drawable/border"
android:gravity="center_horizontal" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView1"
android:adjustViewBounds="true"
android:maxHeight="100dp"
android:maxWidth="100dp"
android:padding="3dp"
android:src="#drawable/unknown" />
</TableRow>
</TableLayout>
<TableLayout
android:id="#+id/Tableout3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="100"
android:gravity="center_horizontal"
android:weightSum="100" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="50"
android:background="#drawable/border" >
<TextView
android:id="#+id/Judgename"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="7dp"
android:text="Name"
android:textSize="20sp"
android:textStyle="bold" />
</TableRow>
<TableRow
android:id="#+id/tablerowctroom"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="50"
android:background="#drawable/border"
android:weightSum="100" >
<TextView
android:id="#+id/Courtroom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="70"
android:padding="7dp"
android:text="Room"
android:textSize="15sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:layout_marginRight="10dp"
android:layout_weight="30"
android:src="#drawable/umid" />
</TableRow>
</TableLayout>
</TableRow>
</TableLayout>
This is the new code I'm trying to use based on Nathan Walter's advice. However, It'll set the first set text but the rest are just the default value. Although the layout looks right. How can I set the textfields for each individual view when the method is called.
public void Display(String name, String ctroom) {
ViewGroup parent = (ViewGroup) findViewById(R.id.container);
view = LayoutInflater.from(this).inflate(R.layout.tablebutton, null);
parent.addView(view);
TextView jn = (TextView) findViewById(R.id.JudgenameCourts);
jn.setText(name);
TextView ct = (TextView) findViewById(R.id.Courtroomnew);
ct.setText(ctroom);
view = LayoutInflater.from(this).inflate(R.layout.tablebutton, null);
parent.addView(view);
jn.setText("Name1");
ct.setText("Courtroom1");
}
I think the code you want looks something like this. Let me know if it makes sense.
// Create an array of things to create views from.
// You will probably be getting your array from somewhere else
YourObjectHere[] things = new YourObjectHere[2];
things[0] = new YourObjectHere();
things[1] = new YourObjectHere();
// Get a reference to a LinearLayout to hold your views
LinearLayout list = (LinearLayout) findViewById(R.id.list);
// Loop through your list of objects
for(int i = 0; i < things.length; i++) {
// Inflate an instance of your layout
View listItem = getLayoutInflater().inflate(R.layout.child, null);
// Set the TextViews to the appropriate things
((TextView) listItem.findViewById(R.id.name)).setText(things[i].getName());
((TextView) listItem.findViewById(R.id.type)).setText(things[i].getType());
// Add the inflated view to the list
item.addView(child);
}
I'm trying to set the color of one specific view with the parent of the expandable list, however when I do I'm finding that colors in the child views are being affected as well which is strange.
Changing the view to original view from white to black seems to solve the issue and it only seems to arise on some phones, its really starting to drive me mad, but I can't find anybody thats run into the same issue.
Heres my custom class that extends BaseExpandableListAdapter.
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(SUM_HOST, parent, false);
}
View statView = (View)v.findViewById(R.id.statViewABCDE);
int caseTest = hostParents.get(groupPosition).getState();
switch (caseTest) {
case Constants.STATE_OK:
statView.setBackgroundColor(ctx.getResources().getColor(R.color.host_up_state));
break;
case Constants.STATE_DOWN:
statView.setBackgroundColor(ctx.getResources().getColor(R.color.host_down_state));
break;
case Constants.STATE_UNKNOWN:
statView.setBackgroundColor(ctx.getResources().getColor(R.color.host_unreachable_state));
break;
}
return v;
}
Here is my XML file for the parent:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="0dp" >
<LinearLayout
android:id="#+id/sumHostLay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/defViewMargin"
android:layout_marginRight="#dimen/defViewMargin"
android:layout_marginTop="#dimen/defViewMargin"
android:background="#drawable/rounded_corners_background"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:padding="#dimen/defViewPadding" >
<View
android:id="#+id/statViewABCDE"
android:layout_width="3dip"
android:layout_height="fill_parent"
android:background="#android:color/black" />
<TextView
android:id="#+id/sumHostTitleAndOutput"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:singleLine="true"
android:text="#string/hostrow_hostname"
android:textSize="13sp" />
</LinearLayout>
Here is my XML for the child.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="0dp" >
<LinearLayout
android:id="#+id/asdasd"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="#dimen/defViewMargin"
android:orientation="vertical" >
<View
android:id="#+id/line1"
android:layout_width="1dip"
android:layout_height="fill_parent"
android:layout_margin="3dp"
android:background="#android:color/white" />
</LinearLayout>
<LinearLayout
android:id="#+id/line2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" >
<View
android:id="#+id/line4"
android:layout_width="20dp"
android:layout_height="1dp"
android:background="#android:color/white" />
</LinearLayout>
In theory I am just trying to set the view in the parent XML i.e statViewABCDE however I am finding that the background colors are changing in the child view aswell. Specifically "line1" and "line4" are changing.
EDIT:
Setting the lines to be different colors (i.e Black for one set and white for the others, seems to have worked, although I'm not sure why.)
Found this issue while testing on a phone running Android 4.1.2.
I can't find any conclusive evidence of whats going on here.
To get around this issue I set the color of the background in java of each view, this seems to have resolved the issue.
I have read many post similar to this, but none of them resolved my problem. Another funny thing is, on the emulator the place for the edittext is held (meaning other widgets are shifted down), but is not shown, on the other hand the same implementation on the real phone doesn't even show a place for the widget. I have the following LinearLayout inside which I need to dynamically add an EditText widget when the TextView Add a Team Member is clicked.
<LinearLayout
android:id="#+id/layout_add_task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/separator"
android:layout_margin="5dp"
android:background="#color/yellow_background"
android:orientation="vertical"
android:padding="5dp">
<LinearLayout
android:id="#+id/layout_task_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/iv_check_research_tasks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon_check_empty" />
<EditText
android:id="#+id/et_task_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:selectAllOnFocus="true"
android:text="Task Name"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tv_add_team_member"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="5dp"
android:text="Add a team member"
android:textColor="#color/text_green"
android:textSize="15dp"
android:textStyle="bold" />
<LinearLayout
android:id="#+id/ll_add_task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tv_add_team_member"
android:orientation="horizontal"
android:weightSum="100" >
<ImageView
android:id="#+id/iv_create_assignment_attach"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="10"
android:src="#drawable/icon_flag_alt" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp"
android:layout_weight="15"
android:text="Due"
android:textSize="15dp" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="65"
android:hint="None"
android:selectAllOnFocus="true" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="10"
android:src="#drawable/icon_calendar_empty" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="#+id/btn_create_assignment_trash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="30dp"
android:layout_marginTop="5dp"
android:text="Trash" />
<Button
android:id="#+id/btn_create_assignment_done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="5dp"
android:text="Done" />
</LinearLayout>
</LinearLayout>
Here is the Java code, the onClickListener implementation of the TextView.
tvAddTeamMember.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout ll = (LinearLayout) getActivity().findViewById(R.id.layout_task_name);
EditText et = new EditText(getActivity());
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
et.setLayoutParams(p);
et.setText("Text");
ll.addView(et);
}
You may proceed this way ! This is not what you're looking for. But your problem will get resolved.
1) By default add the EditText view in your XML and make it invisible.
2) On Clicking the TextView, make it visible.
enjoy !
Hi #Anas Azeem: your code is correct.through your code we can add editetxt to linear layout dynamically. problem is in the layout file only.in the layout mentioned the orientation for added layout as "horizontal" ,instead of that one use "vertical" like below
<LinearLayout
android:id="#+id/layout_task_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/iv_check_research_tasks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<EditText
android:id="#+id/et_task_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:selectAllOnFocus="true"
android:text="Task Name"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
Try to change the line :
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
With this:
android.widget.LinearLayout.LayoutParams p= new android.widget.LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//Then do your work
If you know you might be adding a layout, you could always declare ViewStub. After that you simply find the viewStub and call inflate() on it:
ViewStub stub = (ViewStub) findViewById(R.id.stub);
View inflated = stub.inflate();
This is the promoted way to do that in Android SDK. Thanks to that you can define the infalte layout in an xml file.
Try with this code.
replace ur line ll.addView(et);
by::
ll.addView(et,p);
and delete ur line et.setLayoutParams(p);
A simple way to do this.
Button add = (Button) findViewById(R.id.add_checkpoint);
final LinearLayout layout = (LinearLayout) findViewById(R.id.addLayout);
// layout has assigned weight = "1.0" in xml & it's a child of ScrollView
add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater oLayoutInflater = getLayoutInflater();
// oView is declared globally & Layout "row_checkpoint" contains a EditText
oView = oLayoutInflater.inflate(R.layout.row_checkpoint, null);
final EditText speciesEditText = (EditText) oView
.findViewById(R.id.checkpoint);
speciesEditText.setId(layout.getChildCount());
// Perform whatever required over EditText, same way you can add more elements.
layout.addView(oView);
}
});
Hope it will help.
I have a list of articles in a listView with text on the left, and an image on the right. If the article doesn't have an image, I'd like the text to go full-width (as opposed to having a marginRight of 60dp).
My noob-java thought process is: I'm already looping through and checking if there's an image... (if there is one, I change which image shows up) within that loop, can I somehow use java to alter the XML of my view to add or remove the imageView and add or remove the margin?
The code I'm using to repeat through and alter the image:
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.displayImage("", viewHolder.thumbView); //clears previous one
if(article.filepath != null && article.filepath.length() != 0) {
imageLoader.displayImage(
"http://img.mysite.com/processes/resize.php?image=" + article.filepath + "&size=100&quality=70",
viewHolder.thumbView
);
}
My "article_entry_list_adapter.xml":
<?xml version="1.0" encoding="utf-8"?>
<!-- Layout for individual news entries in a list -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp" >
<!-- Title of the news entry -->
<TextView
android:id="#+id/article_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="60dp"
android:textSize="13sp"
android:textStyle="bold"
android:textColor="#drawable/list_title_selector" android:typeface="serif"/>
<!-- Subtitle contains author and date -->
<TextView
android:id="#+id/article_subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="60dp"
android:layout_alignLeft="#id/article_title"
android:layout_below="#id/article_title"
android:textSize="11sp"
android:textColor="#drawable/list_subtitle_selector" />
<com.sltrib.views.WebImageView
android:id="#+id/article_thumbnail"
android:layout_width="50dp"
android:layout_height="50dp"
android:adjustViewBounds="true"
android:layout_alignParentRight="true"
android:background="#color/super_light_gray"
android:padding="1dp"
android:scaleType="centerCrop" android:cropToPadding="true"/>
</RelativeLayout>
I would suggest changing your xml layout to something like this:
<?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:padding="10dp" >
<!-- Title of the news entry -->
<com.sltrib.views.WebImageView
android:id="#+id/article_thumbnail"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:adjustViewBounds="true"
android:background="#color/super_light_gray"
android:cropToPadding="true"
android:padding="1dp"
android:scaleType="centerCrop"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="5dip"
android:layout_toLeftOf="#id/article_thumbnail"
android:orientation="vertical" >
<TextView
android:id="#+id/article_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Titol"
android:textColor="#drawable/list_title_selector"
android:textSize="13sp"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:id="#+id/article_subtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Subtitol"
android:textColor="#drawable/list_subtitle_selector"
android:textSize="11sp" />
</LinearLayout>
Then, when there is no image, just set the WebImageView visibility to GONE and the text will take the parent's width.
With your example:
viewHolder.thumbView.setVisibility(View.VISIBLE); //to show the image
viewHolder.thumbView.setVisibility(View.GONE); //to hide the image
You can either alter your viewGroup (setting visibility of your imageView to GONE and adding a margin programmatically) or choose to inflate another xml. As you want to do =)
Just while looping setVisiblity(VIEW.Gone) of article_thumbnail.
TextView textView = (TextView) findViewById(R.id.textView1);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
llp.setMargins(50, 0, 0, 0); // llp.setMargins(left, top, right, bottom);
textView.setLayoutParams(llp);