Clear View in Android - android

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.

Related

How to make a View's Height Dynamic

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!

how to insert a new LinearLayout after a specific one everytime i click a button?

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

Duplicate Views on Android during Run-time

I have created a Layout file for an activity. In this layout I have created a LinearLayout with a textview and an edittext.
Now I want to create additional LinearLayouts that will look and contain the exact same views that my original LinearLayout, but with different texts. I also want to do it programmatically during run because the amount of these LinearLayout will vary between runs.
I've read some about inflaters but I don't understand them enough to use them.
I'm thinking something like this, obviously the code is wrong, but hopefully you understand what I want to do:
LinearLayout llMain = (LinearLayout)findViewById(R.id.mainLayout);
LinearLayout llToCopy = (LinearLayout)findViewById(R.id.linearLayoutToCopy);
for(int player = 0; player < size; player++)
{
LinearLayout llCopy = llToCopy.clone();
TextView tv = (TextView)llCopy.getChildAt(0);
tv.setText(players.get(player).getName());
llMain.addView(llCopy);
}
There are several ways to accomplish this.
A quick and easy approach is to inflate a new layout in every iteration of the loop:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout parent = (LinearLayout) inflater.inflate(R.layout.main, null);
for (int i = 0; i < 10; i++) {
View child = inflater.inflate(R.layout.child, null);
TextView tv = (TextView) child.findViewById(R.id.text);
tv.setText("Child No. " + i);
parent.addView(child);
}
setContentView(parent);
Another (more elegant) solution is to create a separate class extending LinearLayout:
public class ChildView extends LinearLayout {
private TextView tv;
public ChildView(Context context) {
super(context);
View.inflate(context, R.layout.child, this);
tv = (TextView) findViewById(R.id.text);
}
public void setText(String text) {
tv.setText(text);
}
}
Now you can create a ChildView in every iteration of your loop and set the text via the setText(String text) method:
for (int i = 0; i < 10; i++) {
ChildView child = new ChildView( this );
child.setText("Child No. " + i);
parent.addView(child);
}
You can achieve that by using layout inflater
get the layout inflatter by using this
LayoutInflater inflater = (LayoutInflater) context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout newlayout = inflater.inflate(R.layout.yourlayout, null);
// newlayout is the copy of your layout and you can use it and to get
// the textview and edittext do it like this
TextView text = (TextView) newlayout.findView(R.id.yourtextviewid);
text.setText("new text");
EditText et = (EditText) newlayout.findView(R.id.yourtextviewid);

Android: Adding multiple Views to a Linear Layout dynamically

I checked the solution here:
Adding multiple views of the same type
Its given that, create a new View everytime you add it instead of changing only 1 view.
But i am doing this:
for (int i = 0; i < 10; i++) {
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(
CommentsActivity.LAYOUT_INFLATER_SERVICE);
View cv = vi.inflate(R.layout.item, null);
TextView textView1 = (TextView) cv.findViewById(R.id.tv1);
textView1.setText("-" + i);
TextView textView2 = (TextView) cv.findViewById(R.id.tv2);
textView2.setText("--" + i);
LinearLayout insertPoint = (LinearLayout) findViewById(R.id.layout);
insertPoint.addView(cv, 0, new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
}
so its like creating a new inflater and view for every i. But i am only getting the last item.
ie.., only 1 inflatedView with tv1 as -9 and tv2 as --9
seems like everytime i go into the for loop, the old view is being replaced by the new view. How to add all the 10 views??
ThankYou
usually I use this
private void renewDetail(){
llDetail.removeAllViews();
for (int i = 0; i < 10; i++) {
llDetail.addView(new ChildDetailNotePieDiagram(context, "Name", 1000, 10));
}
}
the logic is first I clear all view from the parent layout and then add view to the parent layout.
where llDetail is a linear layout and I create a linear layout class ChildDetailNotePieDiagram and add it to the linear layout so basically it's a different solution from what you use now
but I think you can try my solution if you want :)
feel free to ask any question about this in the comment

Android: Add a view to a specific Layout from code

I want to dynamically add some views to a LinearLayout that is already defined in XML.
I'm able to add views to the screen but they are not being placed 'inside' the right LinearLayout.
How can I get a reference to this specific Layout from code, similar to getting a View by using findViewById()?
As Marcarse pointed out you can do
ViewGroup layout = (ViewGroup) findViewById(R.id.your_layout_id);
TextView tv = new TextView(this);
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tv.setText("Added tv");
layout.addView(tv);
The LinearLayout class extends ViewGroup which itself extends the View class. This makes acquiring a reference to a layout as easy as getting a reference to another View.
This should work:
LinearLayout layout = (LinearLayout) findViewById(R.id.your_layout_id);
TextView tv = new TextView(this);
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tv.setText("Added tv");
layout.addView(tv);
Better Version:
To add another layout in activity/Fragment
LayoutInflater mInflater = LayoutInflater.from(getActivity());
View mProgressBarFooter = mInflater.inflate(R.layout.spinner, null, false);
textLoader = (TextView) mProgressBarFooter.findViewById(R.id.footer_label);
Happy Coding(-:

Categories

Resources