How to set margin for index 0 of listView - android

I have a proper list view which has an adapter that extends BaseAdapter.
Everything works fine, but I need a margin of 30 dp for index 0. How to achieve this?
I tried the below 2 code but it crashes.
View subView = createButtonView((MenuButton) getItem(position));
if(position==0){
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)subView.getLayoutParams();
layoutParams.setMargins(30, 0, 0, 0);
subView.setLayoutParams(layoutParams);
}
return subView;
public View getSubView(int position){
View subView = createButtonView((MenuButton) getItem(position));
if(position==0){
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
subView.setLayoutParams(layoutParams);
}
return subView;
}
Also I cannot use a headerView because the child view might not always be a same view, its will be dynamic.

simple alternative is use
SpaceView with 30dp width in xml( space view is very light view in android)
and adjust the visiblity for positions
if (position == 0)
yourxmlSpaceView.setVisibility(View.VISIBLE);
else
yourxmlSpaceView.setVisibility(View.GONE);
for 30dp you can use this method to convert int to pixels
public int dpToPx(int dp) {
DisplayMetrics displayMetrics = context.getResources()
.getDisplayMetrics();
int px = Math.round(dp
* (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
return px;
}

In the adapter class check for position zero and specify your layout parameters inside that if clause and add it to the convertView
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) mcontext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.adapter_main, convertView, false);
}
if(position == 0){
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)subView.getLayoutParams();
layoutParams.setMargins(30, 0, 0, 0);
convertView.setLayoutParams(layoutParams);
}
return view;
}

your listview can add a view to fake space with height is 30dp.
View:
<View
android:layout_width="match_parent"
android:layout_height="30dp"
android:id="#+id/fake_view"
/>
adapter:
public View getView(int position, View v, ViewGroup parent){
....
View fakeView = v.findViewById(R.id.fake_view);
fakeView.setVisibility(position == 1? View.VISIBLE : View.GONE)
....
}

Related

espresso checking text in a recycler view header added as item decoration

so I have a recycler view, and I needed to add items to it as sections or headers. I'm achieving this using recycler views ItemDecorations onDrawOver method, so writing it to a canvas and inserting it like this
private void drawHeader(Canvas c, View child, View headerView) {
c.save();
if (sticky) {
c.translate(0, Math.max(0, child.getTop() - headerView.getHeight()));
} else {
c.translate(0, child.getTop() - headerView.getHeight());
}
headerView.draw(c);
c.restore();
}
#Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDrawOver(c, parent, state);
if (headerView == null) {
headerView = inflateHeaderView(parent);
header = (TextView) headerView.findViewById(R.id.title);
fixLayoutSize(headerView, parent);
}
CharSequence previousHeader = "";
for (int i = 0; i < parent.getChildCount(); i++) {
View child = parent.getChildAt(i);
final int position = parent.getChildAdapterPosition(child);
String title = sectionCallback.getSectionHeader(position);
header.setText(title);
if (!previousHeader.equals(title) || sectionCallback.isSection(position)) {
drawHeader(c, child, headerView);
previousHeader = title;
}
}
}
private View inflateHeaderView(RecyclerView parent) {
return LayoutInflater.from(parent.getContext())
.inflate(R.layout.request_sticky_header, parent, false);
}
I'm now writing tests for this but it crashes with 'No views in hierarchy found matching: with text:' I can't see the text in the heirarchy and I'm wondering if its because the layout is drawn to the canvas, can anyone help me with a way to check if the view has been added?
btw the view is there i can see it i just want a test thats proves it, if its not possible ill rely on manual checking and screenshots many thanks

Can i set padding to ExpandableListView and mantain a textview on each child?

EDIT: can I do something like this...
public View getChildView(int i, int i2, boolean b, View view, ViewGroup viewGroup) {
if(view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.schedule_layout, null);
}
if(i2 == 3) {
view.setPadding(0, 0, 0, 20);
} else {
view.setPadding(0, 0, 0, 0);
}
TextView tv = new TextView(context);
tv.setText(child.get(i).get(i2));
tv.setPadding(30, 10, 10, 10);
tv.setTextSize(25);
return view;
}
...and still get the text to display && keeping the padding.
In order to do this, you can return many view from your adapter. As well, you could define a specific view for every line.
Please see my article: http://raverat.github.io/android-listview-multi-views/
Even if this is for a simple ListView, I guess it's the same for an ExpandableListView! ;)

hook LayoutInflater and change values when inflating views

I want to implement a custom LayoutInflater that scales dimension values, for example,
use:
int scale = 2;
MyInflater.inflate(R.id.testview, null, parent, scale);
will inflate a xml with all dimension values doubled.
that is, a xml like this:
<View android:layout_width="10dp" android:layout_height="10dp" />
will inflate to a view that width and height are 20dp.
LayoutInflater.Factory cannot solve my problem.
Is there a way that I can achieve this?
Perhaps you could use this code to loop all the children of your inflated layout and multiply the width and height by setting the LayoutParams:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
MyInflater inflater = new MyInflater(LayoutInflater.from(this), this);
ViewGroup viewGroup = (ViewGroup) findViewById(R.id.my_layout);
inflater.inflate(R.layout.test_view, viewGroup, true, 2);
}
private class MyInflater extends LayoutInflater {
private int mScale;
protected MyInflater(LayoutInflater original, Context newContext) {
super(original, newContext);
}
#Override
public LayoutInflater cloneInContext(Context newContext) {
return null;
}
public View inflate(int resource, ViewGroup root, boolean attachToRoot, int scale) {
mScale = scale;
// This will return the parent of the inflated resource (if it has one)
View viewRoot = super.inflate(resource, root, attachToRoot);
if (viewRoot instanceof ViewGroup) {
loopViewGroup((ViewGroup) viewRoot, viewRoot == root);
} else {
doubleDimensions(viewRoot);
}
return viewRoot;
}
private void doubleDimensions(View view) {
ViewGroup.LayoutParams params = view.getLayoutParams();
Log.d(TAG, "Before => "+params.width+" : "+params.height);
params.width = params.width * mScale;
params.height = params.height * mScale;
Log.d(TAG, "After => "+params.width+" : "+params.height);
view.setLayoutParams(params);
}
private void loopViewGroup(ViewGroup group, boolean isRoot) {
// If viewRoot == root, skip setting ViewGroup params
if (!isRoot) doubleDimensions(group);
// Loop the ViewGroup children
for (int i=0; i<group.getChildCount(); i++) {
View child = group.getChildAt(i);
// If a child is another ViewGroup, loop it too
if (child instanceof ViewGroup) {
loopViewGroup((ViewGroup) child, false);
} else {
doubleDimensions(child);
}
}
}
}

get image height from drawable at ArrayAdapter class

i try to make my image view position dynamic base on image height. but i stuck to get the image height from drawable.. help me to solve it thanks..
this is my code :
#Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_wall, null, true);
TextView txtProfileUser = (TextView) rowView.findViewById(R.id.txtWallProfile);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txtWallContentTitle);
TextView txtContent = (TextView) rowView.findViewById(R.id.txtWallContent);
ImageView imgWallProfile = (ImageView) rowView.findViewById(R.id.ivWallProfile);
ImageView imgWallContent = (ImageView) rowView.findViewById(R.id.ivWallContent);
txtTitle.setText(web2[position]);
txtContent.setText(web3[position]);
txtProfileUser.setText(web[position]);
imgWallProfile.setImageResource(imageId[position]);
imgWallContent.setImageResource(imageId2[position]);
GetSize gz = new GetSize();
Integer h = gz.getImageSize(imageId[position]);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(20,h, 0, 0);
imgWallProfile.setLayoutParams(lp);
return rowView;
}
and this my getImageSize method :
public Integer getImageSize(Integer d){
Drawable bd = getResources().getDrawable(d);
int height=bd.getIntrinsicHeight();
return height;}
If I understand correctly you want to get the width and height of your image view instead of the bitmaps width and height. Layout parameters allows you to access the imageView and view and modify it's parameters.
public int getImageSize(View myView) {
RelativeLayout.LayoutParams myParams = (RelativeLayout.LayoutParams) myView.getLayoutParams();
return myParams.height;
}

setImageResource in GridView makes holes

Hello everyone I have a problem.
I want to do a GridView with a Custom Loyout so i used the layoutInflater and i did this:
private ImageView prima;
private ImageView seconda;
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if (convertView == null) {
LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.icon, null);
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
prima = (ImageView) v.findViewById(R.id.imageView1);
prima.getLayoutParams().height = width / 3;
prima.getLayoutParams().width = width / 3;
seconda = (ImageView) v.findViewById(R.id.imageView2);
seconda.getLayoutParams().height = width / 3;
seconda.getLayoutParams().width = width / 3;
v.setLayoutParams(new GridView.LayoutParams(width / 3, width / 3));
v.setPadding(0, 0, 0, 0);
} else {
v = convertView;
}
prima.setImageResource(mThumbIds[position]); //mThumbIds[] is an array with R.drawable.vip_0_mini, R.drawable.a_1, R.drawable.b_2, R.drawable.c_3 .....
return v;
};
When i run my application the image are arranged random and there are many black space with no images.
What i did wrong ?
Your View and convertView are not linked.
Try with ViewHolder concept like this:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder v;
if (convertView == null) {
LayoutInflater li = getLayoutInflater();
convertView = li.inflate(R.layout.icon, null);
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
v = new ViewHolder();
v.prima = (ImageView) convertView .findViewById(R.id.imageView1);
v.prima.getLayoutParams().height = width / 3;
v.prima.getLayoutParams().width = width / 3;
v.seconda = (ImageView) convertView .findViewById(R.id.imageView2);
v.seconda.getLayoutParams().height = width / 3;
v.seconda.getLayoutParams().width = width / 3;
convertView .setLayoutParams(new GridView.LayoutParams(width / 3, width / 3));
convertView .setPadding(0, 0, 0, 0);
convertView.setTag(v);
} else {
v = (ViewHolder)convertView.getTag();
}
v.prima.setImageResource(mThumbIds[position]); //mThumbIds[] is an array with R.drawable.vip_0_mini, R.drawable.a_1, R.drawable.b_2, R.drawable.c_3 .....
//v.seconda
return convertView;
};
class ViewHolder{
ImageView prima;
ImageView seconda;
}
You forgot to apply images to
seconda = (ImageView) v.findViewById(R.id.imageView2);
prima.setImageResource(....);

Categories

Resources