The code is:
View v = convertView;
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
v = inflater.inflate(R.layout.newsfeed_custom_listview_facebook, parent, false);
TextView username = (TextView) v.findViewById(R.id.username);
TextView prayerTitle = (TextView) v.findViewById(R.id.prayerTitle);
TextView createdOn = (TextView) v.findViewById(R.id.createdOn);
TextView prayerMessage = (TextView) v.findViewById(R.id.prayerMessage);
ScrollView scrollView = (ScrollView) v.findViewById(R.id.scrollView);
MixedObj menuItemsBean = menuItems.get(position);
RoundedImageView img = (RoundedImageView) v.findViewById(R.id.image);
username.setText(menuItemsBean.name);
prayerTitle.setText(menuItemsBean.title);
createdOn.setText(menuItemsBean.created_on);
TextView textView = new TextView(context);
//prayerMessage.setText(menuItemsBean.message);
RelativeLayout.LayoutParams p = new
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.BELOW, R.id.image);
textView.setLayoutParams(p);
textView.setPadding(5, 5, 5, 5);
textView.setTextColor(Color.parseColor("#ffffff"));
//textView.setText(menuItemsBean.message);
String message = "please God forgive me for all my mistakes i have ever done in my life please God forgive me for all my mistakes i have ever done in my life please God forgive me for all my mistakes i have ever done in my life please God forgive me for all my mistakes i have ever done in my life please God forgive me for all my mistakes i have ever done in my life please God forgive me for all my mistakes i have ever done in my life please God forgive me for all my mistakes i have ever done in my life please God forgive me for all my mistakes i have ever done in my life please God forgive me for all my mistakes i have ever done in my life end";
message = menuItemsBean.message;
textView.setText(message);
ViewGroup viewGroup = (ViewGroup) v;
viewGroup.addView(textView);
RelativeLayout.LayoutParams param = (RelativeLayout.LayoutParams)
textView.getLayoutParams();
AbsListView.LayoutParams layoutParams = (AbsListView.LayoutParams)
viewGroup.getLayoutParams();
//layoutParams.height += ();
return v;
I want to Change Height of a Custom view Inflated for a listView.
I have tried this but its not working I want a different height on every cell depended upon the message TextView Size.
Anyone please Help..
The problem of your realization it's basics of ListView.
From base object, ListView invokes just single size for all views, and invalidate every action. For your task you need to use your own list. For ex.
1)
Using several layouts:
<ScrollView>
<LinearLayout>
//Add in loop all your views in code above!
</LinearLayout>
</ScrollView>
2)
Using LinkedListView:
This view like ListView, but contains link to every views, which you added, and doesn't invalidate them. So you can use this, and more than simple ListView - you may changes any views in runtime without artifacts. Check this list on Github - LinkedListView
UPDATE:
Using LinkedListView, just import to your project. Than create adapter
public class YourAdapter extends LinkedListView.Adapter {
private Context mainContext;
private LinkedList<View> mainViewList;
public CirclePagerAdapter (Context mainContext) {
this.mainContext = mainContext;
mainViewList = new LinkedList<>();
}
//CALL THIS TO ADD YOUR OWN VIEW
public void addSimpleView (View v) {
mainViewList.add(v)
notifyDatasetChanged();
}
//CALL THIS TO DELETE VIEW
public void deleteView(int index) {
mainViewList.remove(index);
notifyDatasetChanged();
}
#Override
public View getObjectView(int position) {
if (position < mainViewList.size())
return mainViewList.get(position);
return null;
}
#Override
public int getObjectCount() {
return mainViewList.size();
}
After creating adapter call in your Activity!
LinkedListView listView = new LinkedListView (getContext());
LinkedListView.Adapter adapter = new MyBaseAdapter ();
listview.setViewPager(adapter);
Work pretty well!
Related
I'm trying to create a game. I need a kitten object with a image displayed.
I need to use this object within multiple activities so I decided it's probably better using an outside class.
I used something like this. But the function findViewById(int) is not aviable from outside an activity. How to do this?
public class Kitten {
Context parent;
View parentView;
ImageView imgBody, imgHead;
Kitten(Context context, View view) {
parent = context;
parentView = view;
//Create body & head
imgBody = new ImageView(parent);
imgBody.setImageResource(R.mipmap.img_kitty_body_white);
addImage(imgBody);
imgHead = new ImageView(parent);
imgHead.setImageResource(R.mipmap.img_kitty_head_white);
addImage(imgHead);
}
private void addImage(ImageView img) {
RelativeLayout rl = (RelativeLayout) findViewById(R.id.adoptView);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.BELOW, R.id.adoptView);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
rl.addView(img, lp);
}
}
The main problem I have, is within the addImage method...
Have you tried using the method from your specific view? Try parentView.findViewById()
You should store the image ID in the "Kitten" object and retrieve it where you need with a getter method, or you can pass the parent View if you want to use the findViewById from there.
But, you don't usually store the Views itself.
I have ListView which contains items containing a View and a TextView:
chat_item.xml:
<?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="90dp"
android:id="#+id/chat_message_wrapper"
xmlns:pixlui="http://schemas.android.com/apk/com.neopixl.pixlui">
<View
android:id="#+id/message_indicator"
android:layout_width="10dp"
android:layout_height="90dp"/>
<com.neopixl.pixlui.components.textview.TextView
android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/midnight_blue"
android:padding="14dp"
android:layout_centerVertical="true"/>
</RelativeLayout>
And this is getView() in my Adapter class:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.chat_item, null);
}
ChatMessageItem item = getItem(position);
if(item != null) {
TextView messageTextView = (TextView) view.findViewById(R.id.message);
ViewGroup messageWrapper = (ViewGroup) view.findViewById(R.id.chat_message_wrapper);
View messageIndicatorView = view.findViewById(R.id.message_indicator);
if(messageTextView != null) {
messageTextView.setText(String.valueOf(item.getMessage()));
RelativeLayout.LayoutParams textParams = (RelativeLayout.LayoutParams)messageTextView.getLayoutParams();
RelativeLayout.LayoutParams indicatorParams = new RelativeLayout.LayoutParams(10, ViewGroup.LayoutParams.MATCH_PARENT);
RelativeLayout.LayoutParams messageWrapperParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 90);
if(item.getSender() == ChatMessageItem.Sender.ME) {
textParams.addRule(RelativeLayout.LEFT_OF, R.id.message_indicator);
indicatorParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
messageIndicatorView.setBackgroundColor(getContext().getResources().getColor(R.color.light_blue));
}
else if(item.getSender() == ChatMessageItem.Sender.OTHER) {
textParams.addRule(RelativeLayout.RIGHT_OF, R.id.message);
indicatorParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
messageIndicatorView.setBackgroundColor(getContext().getResources().getColor(R.color.silver));
}
messageTextView.setLayoutParams(textParams);
messageIndicatorView.setLayoutParams(indicatorParams);
messageWrapper.setLayoutParams(messageWrapperParams);
}
}
return view;
}
I added the following line to at least keep the height of the items constant, which used to also change (which will probably give me problems later, as the content is dynamic, but ok..)
RelativeLayout.LayoutParams messageWrapperParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 90);
The problem is when I scroll up and down more and more text disappears. messageIndicatorView does not disappear though, only the text disappears. If I keep scrolling enough, all text will disappear. What am I doing wrong and how can I fix it? Thanks. (I know I must use a ViewHolder for better performance, but I will do that when this problem is fixed)
The problem is that as the ListView recycles and reuses the views, conflicting rules are added to the RelativeLayout.LayoutParams instances for the #id/message TextView. In particular this happens whenever a view for a "ME" message is reused for an "OTHER" message, or vice-versa.
RelativeLayout.LayoutParams keeps a list of rules (actually an array by verb, so that you cannot add, say, two LEFT_OF rules -- but any other combination is possible, including problematic ones).
The easiest solution is to use a new RelativeLayout.LayoutParams object each time, by changing this line:
RelativeLayout.LayoutParams textParams =
(RelativeLayout.LayoutParams)messageTextView.getLayoutParams();
into:
RelativeLayout.LayoutParams textParams =
new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
There are other solutions, such as having different actual layouts for each kind of ListView item (via getItemViewType()) but it's probably overkill in this case. However if the differences between the two kinds of views were greater, it might be worth considering.
I am calling the function setLayoutData() for many places i want to clear the view whenever it is called.What is happening now the layout get appends .But i don't want to append how can we clear the view .Please help me this is my function
void setLayoutData() {
resultTrips = (LinearLayout) findViewById(R.id.trip_list);
LinearLayout.LayoutParams mainTripDetailsLayout = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
LayoutInflater inflater = (LayoutInflater) getBaseContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0;i<4;i++) {
final LinearLayout tripdetailsdata = (LinearLayout) inflater
.inflate(R.layout.mytripinner, null);
tripdetailsdata.setId(i);
resultTrips.addView(tripdetailsdata);
i++;
}
TextView dummy = new TextView(this);
dummy.setLayoutParams(mainTripDetailsLayout);
resultTrips.addView(dummy);
}
If I understand you right, you want to clear your LinearLayout?
so put in your second line after findView... resultTrips.removeAllViews();
The better aproach would be only adding one TextView to your Linearlayout and edit this one.
You can use resultTips.removeAllViews(); to remove the resultTips layout or resultTips.removeView(dummy); to remove the TextView. Hope it helps.
I want to build some sort of twitter application. In DashboardActivity i need to add a status box everytime i click the "Post" button.My dashboard xml looks like this:
<RelativeLayout>
<LinearLayout></LinearLayout> -->header layout
<LinearLayout></LinearLayout> -->some layout with some titles
<LinearLayout></LinearLayout> --> post status layout with the post button
<LinearLayout></LinearLayout> --> layout with a horizontal rule
<LinearLayout></LinearLayout> --> this is the layout with id "rootStatusBox" where i want to add the status box
</RelativeLayout>
Now, i want to be able to add a new LinearLayout after the horizontal rule layout everytime i click the "Post" button.
I tried something like this in my DashboardActivity:
postStatus.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
addUserStatusBox(firstname,lastname,status);
}});
And addUserStatusBox() looks like this:
public void addUserStatusBox(String firstname, String lastname,String status) {
LinearLayout rootStatusBox = (LinearLayout)findViewById(R.id.rootStatusBox);
LinearLayout userStatusBox = new LinearLayout(this);
userStatusBox.setOrientation(LinearLayout.HORIZONTAL);
userStatusBox.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
LinearLayout.LayoutParams layout = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layout.setMargins(0, 300, 0, 0); // llp.setMargins(left, top, right, bottom);
userStatusBox.setLayoutParams(layout);
TextView friendName = new TextView(this);
TextView friendStatus = new TextView(this);
TextView dataCrearePost = new TextView(this);
friendName.setText(firstname+ " " + lastname);
friendName.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
friendName.setTextSize(10);
friendName.setTypeface(null, Typeface.BOLD);
friendStatus.setText(status);
friendStatus.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
llp.setMargins(-70, 20, 0, 0); // llp.setMargins(left, top, right, bottom);
friendStatus.setLayoutParams(llp);
friendStatus.setTextSize(10);
userStatusBox.addView(friendName);
userStatusBox.addView(friendStatus);
rootStatusBox.addView(userStatusBox);
}
This is working only for the first time when i add a status.I don't know how to add more posts after the horizontal rule layout and to be able to see the old posts below my new one.I would appreciate a little bit of help.Thank you
I would use a customized list view for this purpose.
You need to create the following:
Layout for ListItem: This represents single row in the list. You can customize it by creating separate layout for this. Say you create: listitem_post.xml
Adapter: Write an adapter by extending BaseAdapter class (say: PostsAdapter.java). Fill in all the overridden methods. Most importantly, in the getView() method, inflate the post_listitem. Assign that to convertView object (which is passed in as an argument).
public View getView(int index, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
convertView = inflater.inflate(R.layout.listitem_post, parent, false);
}
//Code other parts
return convertView;
}
Activity: In your xml code of activity, insert a ListView say listview_posts. In the java file for the activity, set adapter created in step 2 for listview_posts inside onCreate() method.
PostsAdapter postsListAdapter = new PostsAdapter();
ListView postsListView = (ListView) this.findViewById(R.id.listview_posts);
postsListView.setAdapter(postsListAdapter);
That is how you specify that each list element is listitem_post.
Follow this tutorial
I actually trying something quite straightforward, however android and java are making my life not easy. The idea is to scroll to a specified child in the table layout.
I am using a Layoutinflater to add the entries to the table layout like the following :
TableLayout tl = (TableLayout)findViewById(R.id.myTableLayout3);
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.element_news, null);
TextView firstname = (TextView) itemView.findViewById(R.id.tvname);
firstname.setText(FN + " " + iLN);
TextView date = (TextView) itemView.findViewById(R.id.tvdate);
date.setText(newPD);
TextView post = (TextView) itemView.findViewById(R.id.tvpost);
post.setText(c.getString(iP));
post.setFocusable(true);
tl.addView(itemView, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
I tried to use the getChildAt() for the tablelayout and I do get the child, however when I use the child it returns for everything "0".
Example Code:
LinearLayout row = (LinearLayout) tl.getChildAt(1);
TextView tv = (TextView) row.getChildAt(1);
tv.requestFocus();
Log.w("news", Integer.toString(tv.getHeight()));
For the TextView Height it returns "0" eventhough it contains multiple lines and the requestfocus() does not work either.
So how can I scroll to the child in the table layout?
Thanks in advance for any help.
That code sample doesn't work because you probably use it in the onCreate method and at that time the UI isn't drawn yet so the views don't have any dimensions. If this is the case you could simply post a Runnable on one of your views to delay a bit the dimensions gathering until after the onCreate method like this:
// post the Runnable
tl.post(new Runnable() {
#Override
public void run() {
LinearLayout row = (LinearLayout) tl.getChildAt(1);
// get the top position relative to the parent
int top = tr.getTop();
// scroll to that top position the wrapping ScrollView
sv.scrollTo(0, top);
}
});
There is something strange about your code(from my point of view), if the code above doesn't work you should post more details about your layout files and use scenario.