Setting LayoutParams via post method in List View adapter - android

I am trying to set a margin in a text view and padding in a linear layout in a list view adapter. Basically, if the text view has 2 or less lines of text I am creating a margin/padding for that list item.
Here is the code:
public class StockCountListAdapter extends ArrayAdapter<StockCountItem> {
private TextView txtProduct;
private LinearLayout llStockCountItem;
public StockCountListAdapter(Context context, int textViewResourceId, List<StockCountItem> objects) {
super(context, textViewResourceId, objects);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.stock_count_list_item, parent, false);
txtProduct = (TextView) rowView.findViewById(R.id.stock_count_item_product);
llStockCountItem = (LinearLayout) rowView.findViewById(R.id.ll_stock_count_item);
StockCountItem item = getItem(position);
txtProduct.setText(item.Product);
llStockCountItem.post(new Runnable() {
#Override
public void run() {
if (txtProduct.getLineCount() <= 2) {
llStockCountItem.setPadding(0, 0, 0, 10);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 7);
params.setMargins(1, 1, 1, 10);
txtProduct.setLayoutParams(params);
}
}
});
return rowView;
}
The margin/padding is applied when the list is scrolled, but not when the list is first displayed or on orientation change.
How can I get it to apply the margin/padding on activity load or orientation change?

Handling Runtime Changes
When Screen orientation such a change occurs, Android restarts the running Activity (onDestroy() is called, followed by onCreate()). The restart behavior is designed to help your application adapt to new configurations by automatically reloading your application with alternative resources that match the new device configuration.
To properly handle a restart, it is important that your activity restores its previous state through the normal Activity lifecycle, in which Android calls onSaveInstanceState() before it destroys your activity so that you can save data about the application state. You can then restore the state during onCreate() or onRestoreInstanceState().
http://developer.android.com/guide/topics/resources/runtime-changes.html

GRAVITY
in code
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,
LinearLayout.LayoutParams.WRAP_CONTENT, 7);
params.setMargins(1, 1, 1, 10);
params.gravity = Gravity.FILL; // this is where i come in
txtProduct.setLayoutParams(params);
txtProduct.invalidate(); // edits
txtProduct.requestLayout();//edits
the type of Gravity is up to you now with rotation you need to override onOnrientationChanged() (something like that) and call that same code there
Edit
base on your post runnable code i am thinking of a workaround, i do not think it will work but worth it.. since txtProduct.getLineCount() returns 0 after layout pass, i am improvising
if(txtProduct.getLineCount()==0){ //if false you run your previous code without post
int textSize = txtProduct.getTextSize()/2,mw,lines;
// for the textsize i am thinking of a rectangle
if(txtProduct.getMeasuredWidth() > 0) //if this line checks out 0 the code is trash
//assume none where zero
int fillW = textSize * txtProduct.getText().toString().length();
if(fillW > mw){ //my logic here is we have more than one line
if(fillW / mw > -1){ //
lines = fillW/mv;
if(fillW%mv !=0){// it is not a factor which means there is another line trending
lines++;
// now add your normal code here without post to check if lines is what you want
}
}
}
}else{ //add your code without post
see if this works,

Related

addView not displaying after removeAllViews on custom view

I'm building a custom view, sort of like a custom bar chart. I'm extending the LinearLayout for this one. The custom view then populates the views from data. Problem is, whenever I want the views to be 'refreshed', I am calling removeAllViews() and similar methods, so the custom view layout is in clean slate, then to repopulate the data, I call addView(), but child views don't show up. Reason why I need to call removeAllViews is for the child views to not duplicate in the custom views.
These are some of the snippets from my custom view, I also implemented onLayout() so whenever I display the custom views I get proper heights for layouting purposes. BarChartData is just a model class for the data that should be displayed in this custom view:
public void setChartData(BarChartData data) {
this.chartData = data;
addBarDataToUi();
}
void addBarDataToUi() {
Log.d(TAG, "Add bar data to UI called");
if (chartData != null) {
//this.removeAllViews(); -> first one I tried, no luck, not displaying views after `addView`
//this.removeAllViewsInLayout(); -> tried this too but no luck
this.removeViewsInLayout(0, this.getChildCount()); // again, to no avail :(
for (int i = 0, count = chartData.getItemCount(); i < count; i++) {
addBarItemDataUi(chartData.getItemByPos(i));
}
Log.d(TAG, "Child count: " + this.getChildCount());
}
}
void addBarItemDataUi(BarItemData data) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.bar_chart_item, this, false);
FrameLayout mainLayout = (FrameLayout) layout.findViewById(R.id.bar_chart_item_main_layout);
//TextView topText = new TextView(getContext());
TextView topText = (TextView) layout.findViewById(R.id.bar_chart_item_top_text);
TextView bottomText = (TextView) layout.findViewById(R.id.bar_chart_item_bottom_text);
topText.setText(String.valueOf(data.percentage));
bottomText.setText(data.title);
mainLayout.setBackgroundColor(data.backgroundColor);
Log.d(TAG, "Height: " + this.getMeasuredHeight() + ", Top text height: " + topText.getMeasuredHeight());
int heightRel = (int) (data.getPercentageFractal() * (double) this.getMeasuredHeight());
mainLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, heightRel));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f);
params.gravity = Gravity.BOTTOM;
layout.setLayoutParams(params);
this.addView(layout);
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
Log.d(TAG, "On layout..");
if (chartData != null) {
addBarDataToUi();
}
}
Well, I have searched this problem, there are very few that came up, almost the same scenario and problem, but I think they have not resolved their problems about addView after removeAllViews.
I'm guessing that by calling the removeAllViews() function inside the addBarDataToUi() which is inside onLayout() when the function gets called by setChartData(BarChartData data) it adds the child views which triggers the onLayout() function which calls addBarDataToUi() and removes view and such in some kind of constant loop. The android documentation says to
avoid using removeAllViews() inside onDraw() or any related function
http://developer.android.com/reference/android/view/ViewGroup.html#removeAllViews()
Which I'm assuming might also include the onLayout() function as well.
My best suggestion is moving your removeAllViews() function call to inside your setChartData(BarChartData data) function just before you call addBarDataToUi()
Hope it helps

How do I fit an exact number of ListView items on screen?

I would like the rows in a ListView to be sized so that exactly six of them can fit on the screen. For that, I would need to know how much vertical space is available to the ListView (not the whole screen). However, no measuring can be done in onCreate() since no views have been rendered yet.
If I make measurements after rendering, the ListView might be drawn and then resized, which may be distracting. What is the smartest way to establish the necessary row height before rendering the ListView?
in onCreate you can get the height of your screen and divide by 6.
Now in your getView you get the reference of the top layout for each item, suppost you have named it's id to root and i.e it's a LinearLayout.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if(view == null){ some inflate }
LinearLayout root = (LinearLayout) view.findViewById(R.id.root);
LayoutParams lp = root.getLayoutParams();
lp.height = screenHeight/6;
root.setLayoutParams(lp);
....
return view;
}
Yes, this assumes the ListView is in fullscreen.
If you have other layouts, you will have to get those height into account.
Then your height will be: int heightForEachItem = (screenHeight - otherlayoutsHeightTogether) / 6;
Turns out that the earliest you can measure a ListView is in the onGlobalLayout() callback.
Here is my example.
params = new AbsListView.LayoutParams(-1,-1);
listview.getViewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
#Override
public void onGlobalLayout(){ //this is called just before rendering
params.height = listview.getHeight()/6; // this is what I was looking for
listview.getViewTreeObserver.removeOnGlobalLayoutListener(this); // this is called very often
}
adapter = new ArrayAdapter<...>(int position, ...){
#Override
public View getView(...){
LinearLayout item = new LinearLayout(context);
item.setLayoutParams(params);
// add text, images etc with getItem(position) and item.addView(View)
return item;
}
}
listview.setAdapter(adapter);

Prevent GridView reset scroll to top when element is tapped?

I have a GridView that is populated by all apps installed on the device. The user can select certain apps here. I want the selected apps to be opaque and non-selected to be partially transparent. I did this with the following:
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout linearLayout = new LinearLayout(mContext);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setGravity(Gravity.CENTER_HORIZONTAL);
LinearLayout.LayoutParams layoutParamsText = new LinearLayout.LayoutParams(150, 90);
ImageView imageView = new ImageView(mContext);
TextView appLabel = new TextView(mContext);
final OurAppInfo info = (OurAppInfo) getItem(position);
if(!installedApplications.contains(info)){
AlphaAnimation alpha = new AlphaAnimation(0.4F, 0.4F);
alpha.setDuration(0);
alpha.setFillAfter(true);
linearLayout.startAnimation(alpha);
}
String appName = info.label;
if (appName.length() > 25) {
appName = appName.substring(0, 25);
appName = appName + "...";
}
appLabel.setText(appName);
appLabel.setTextColor(Color.BLACK);
appLabel.setGravity(Gravity.CENTER_HORIZONTAL);
appLabel.setTypeface(null, Typeface.BOLD);
imageView.setImageDrawable(info.drawableAppIcon);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(110, 110));
appLabel.setTextSize(15);
linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (installedApplications.contains(info)){
installedApplications.remove(info);
receiveUpdate(installedApplications, false, false);
} else {
installedApplications.add(info);
Collections.sort(installedApplications);
receiveUpdate(installedApplications, false, false);
}
}
});
appLabel.setLayoutParams(layoutParamsText);
linearLayout.addView(imageView);
linearLayout.addView(appLabel);
return linearLayout;
}
This is part of the GridAdapter extends BaseAdapter. The code works as expected, when I tap on an app it is either removed from or added to the list and according to transparency is set. However, whenever I tap on an element in the GridView, the view is reset and I am brought to the top of the scrollable GridView. Obviously, this isn't a problem for a small number of apps, but if you're selecting apps near the XYZ letters, every time you select one you are brought back to ABC. How can I prevent this from happening?
It looks like you're refreshing the adapter whenever you make changes that makes the grid go back to initial position. You could try saving and restoring the position before making any changes to the adapter.
//Before refreshing the adapter you get both X and Y position
int xPos = grid.getScrollX();
int yPos = grid.getScrollY();
Then you update your adapter.
After the adapter is recreated you restore the grid position:
grid.scrollTo(xPos, yPos);
You could also use (everytime possible) the method notifyDataSetChanged() instead of creating a new adapter.
Hope it helps.
Check all child views for automatic height or width.
I guess gridview calculates size of this views whenever you change data.
This was solution in my case.
In my case changed this:
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
To this:
<ImageView
android:id="#+id/image"
android:layout_width="100dp"
android:layout_height="100dp" />

When Extending LinearLayout Child Views are not visible

Alright. So I start my activity in Main, grab the FragmentManager and instantiate a Fragment which needs to return a View. OK. So I extended a LinearLayout in order to have something to return. My Activity and Fragment are happy but I am not.
Three LinearLayouts which I create in the parent ViewGroup are there (code below). I have verified this by counting children and by setting the background colors to contrast one another. The parent also changes size depending on how tall I make the children (when I don't declare any LayoutParams on the parent).
public class Mainmenu extends LinearLayout {
private ArrayList<LinearLayout> panes = new ArrayList<LinearLayout>();
private Context context;
private final int
LEFT = 0, CENTER = 1, RIGHT = 2;
public Mainmenu(Context c) {
super(c);
context = c;
setBackgroundColor(Color.WHITE);
setOrientation(HORIZONTAL);
setLayoutParams(
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT));
for(int i=0;i<=RIGHT;i++){ //Create the (3) Panes
LinearLayout ll = new LinearLayout(context);
ll.setLayoutParams(
new LinearLayout.LayoutParams(300,
LinearLayout.LayoutParams.FILL_PARENT));
switch(i){
case LEFT | RIGHT:
ll.setBackgroundColor(Color.DKGRAY);
default:
ll.setBackgroundColor(Color.BLACK);
}
ll.setOrientation(LinearLayout.VERTICAL);
ll.setVisibility(VISIBLE);
ll.setWillNotDraw(false);
panes.add(i, ll);
addView(ll);
}
LinearLayout.LayoutParams buttons =
new LinearLayout.LayoutParams(100, 50);
buttons.setMargins(15, 5, 5, 0);
TextView tv1 = new TextView(context);
tv1.setText("hello");
tv1.setTextColor(Color.RED);
panes.get(LEFT).addView(tv1, buttons);
Button button = new Button(context);
button.setText("Launch Editor");
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
}
});
panes.get(CENTER).addView(button);
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub
}
}
Nothing is null, all my elements (3 ViewGroups and 2 Views) are present in the tree but not visible. I've tried bringing children to the front through the parent and the children, creating them in different super.methods and invalidating the view in a similarly shotgunned fashion. What's going on? Is it as simple as not having any idea what I'm doing?
The problem is simply because you are overriding onLayout and doing nothing with it. You only need to override this if you want to layout the children yourself (ie, you were designing some unique custom layout). In this case just remove that method, or call super.onLayout.

When I scroll a listview with a custom adapter too fast up and down, getView() starts behaving oddly. Why?

I have a listview with a custom arrayadapter that handles about 15 strings. The style of each row alternates (between labels and values for those labels--for example row 1 could be "email address" and row 2 would be the actual email address). I'm changing the style of each row to alternate like this in the arrayadapter's getView() method. So if the item at the current position is a label, I'll change the styling from the default row style (which is what the values have applied to them). When the listview first loads, the styling is perfect and just how I want it to be. If I scroll the list slowly up or down, it stays that way. However, if I scroll the list fast up and down, the styling of the value rows starts changing to that of the label ones until all of the rows have the styling of a label row. Does anyone know why this would be happening? I've used custom adapters on other listviews in the app with no problems like this.
Edit: Found out that it also changes all of the rows to the label styling on portrait->landscape orientation changes. Doesn't do this on landscape->portrait changes. Below is the adapter I'm using. Am I missing something?
public class DetailsAdapter extends ArrayAdapter<String> {
private TextView text = null;
private String item = null;
public DetailsAdapter(Context context, int resource, int textViewResourceId, String[] objects) {
super(context, resource, textViewResourceId, objects);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
text = (TextView) super.getView(position, convertView, parent);
item = getItem(position);
if (item.equals("Name") || item.equals("Mobile") || item.equals("Home") || item.equals("Email") || item.equals("Address")) {
text.setBackgroundColor(0xFF575757);
text.setTextSize(15);
text.setTypeface(null, Typeface.BOLD);
text.setPadding(8, 5, 0, 5);
} else {
text.setPadding(15, 15, 0, 15);
}
return text;
}
#Override
public boolean isEnabled(int position) {
item = getItem(position);
if (item.equals("Name") || item.equals("Mobile") || item.equals("Home") || item.equals("Email") || item.equals("Address")) {
return false;
} else {
return true;
}
}
}
Android reuses views fairly aggressively, and it is quite possible that a view that was used as an email address row gets reused on a row that's supposed to display a label, and vice-versa.
As a result, you cannot rely on "default" values. Set your padding, typeface, text size and background color in all cases:
if (item.equals("Name") || item.equals("Mobile") || item.equals("Home") || item.equals("Email") || item.equals("Address")) {
text.setBackgroundColor(0xFF575757);
text.setTextSize(15);
text.setTypeface(null, Typeface.BOLD);
text.setPadding(8, 5, 0, 5);
} else {
text.setBackgroundColor(DEFAULT_BACKGROUND);
text.setTextSize(DEFAULT_TEXT_SIZE);
text.setTypeface(null, DEFAULT_TYPEFACE);
text.setPadding(15, 15, 0, 15);
}
Don't need to do anything. I too faced the same problem and solved it like this:
Just inside the getView method add a first line
convertView=null;
It wont redraw the view immediately destroyed but instead would create new ones each time based on your logic (even odd or whatever)

Categories

Resources