RelativeLayout addRule at BaseAdapter doesn't work very well - java

After one of rule is added to RelativeLayout, I get a don't very correct result. ID does not apply to element/is applied incorrectly. This affects at rules that must be bound to widget.
As a container, I use ListView and add objects to it through BaseAdapter. This obviously does not give desired result, but after reusing same widget that is returned back to the adapter and reused (element is guaranteed not changing again), rules starting working correctly.
#Override
protected void onCreate(Bundle state) {
super.onCreate(state);
ListView view = new ListView(this);
view.setBackgroundColor(Color.BLACK);
view.setId("files_list".hashCode());
view.setAdapter(new TestAdapter(this));
...
private class TestAdapter extends BaseAdapter {
...
#Override
public View getView(int position, View convertView, View parent) {
if(convertView == null) convertView = inflateView();
// only changes text & pictures, doesn't affecting display
manipulateItem(position, convertView);
return convertView;
...
private View inflateView() {
RelativeLayout layout = new RelativeLayout(context);
layout.setLayoutParams(new ViewGroup.LayoutParams(-1, -2));
ImageView icon = new ImageView(context);
icon.setBackgroundColor(Color.BLACK);
icon.setPadding(20, 20, 20, 20);
icon.setId("file_icon".hashCode());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(110, -1);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params.rightMargin = 30;
layout.addView(icon, params);
...
LinearLayout additional = new LinearLayout(context);
additional.setOrientation(LinearLayout.VERTICAL);
additional.setGravity(Gravity.RIGHT);
additional.setBackgroundColor(Color.RED);
additional.setPadding(30, 0, 30, 0);
additional.setId(java.lang.String("additional_info").hashCode());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(-2, -2);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
layout.addView(additional, params);
...
LinearLayout uniqal = new LinearLayout(context);
uniqal.setOrientation(LinearLayout.VERTICAL);
uniqal.setBackgroundColor(Color.BLUE);
uniqal.setId("uniqal_info".hashCode());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(-2, -2);
// problem occurs here
params.addRule(RelativeLayout.LEFT_OF, additional.getId());
params.addRule(RelativeLayout.RIGHT_OF, icon.getId());
params.addRule(RelativeLayout.CENTER_IN_PARENT);
layout.addView(uniqal, params);
...
return layout;
}
This is what widget will looks like after first scrolling: elements after first drawing screen
And so, after scrolling and reusing same widget: working screen
After firstly 5 uses (so much got into my test screen), everything becomes fine. For the first time, views don't want to be attached by ID to another widget in any way. Is there a way around this?
Options for inflating layout from application .xml don't suit me

Manual update of layout helped me, however this doesn't work right away and my question remains relevant. Here is modified inflateView code:
private View inflateView() {
...
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(-2, -2);
params.addRule(RelativeLayout.LEFT_OF, additional.getId());
params.addRule(RelativeLayout.RIGHT_OF, icon.getId());
params.addRule(RelativeLayout.CENTER_IN_PARENT);
uniqal.post(new Runnable() {
public void run() {
uniqal.requestLayout();
}
});
layout.addView(uniqal, params);
...
return layout;
}

Related

How to use Layout Params in RecyclerView Item

I have app about surveys. I list all active surveys in RecyclerView.
In the bottom of RecyclerView custom item, I add profile pictures of users who attendant to that survey (like in facebook - seen by bla bla)
I decided to implement imageview to that layout horizontally with params, somehow i couldn't and images are not displaying on item.
Here is my OnBindViewHolder(Other parts work well)
#Override
public void onBindViewHolder(ViewHolder holder, final int position) {
Picasso.with(context)
.load(surveyList.get(position).getItem_image())
.into(holder.item_survey_image);
String imageStringList = surveyList.get(position).getItem_survey_users_image();
List<String> splitList = Arrays.asList(imageStringList.split(","));
for(int i=0; i<splitList.size(); i++){
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, 20);
Glide.with(context)
.load(splitList.get(i))
.into(holder.item_survey_users_image);
if (holder.item_survey_users_image.getParent() != null)
((ViewGroup) holder.item_survey_users_image.getParent()).removeView(holder.item_survey_users_image);
holder.item_layout_users.addView(holder.item_survey_users_image, layoutParams);
}
holder.item_survey_head.setText(surveyList.get(position).getItem_survey_head());
holder.item_survey_desc.setText(surveyList.get(position).getItem_survey_desc());
holder.item_survey_amount.setText(surveyList.get(position).getItem_survey_amount());
holder.item_survey_time.setText(surveyList.get(position).getItem_survey_time());
holder.item_survey_users.setText(surveyList.get(position).getItem_survey_users());
holder.item_cardview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(context, ""+position, Toast.LENGTH_SHORT).show();
}
});
}
how you can tell the picture absence is caused by layoutparams ? however here is how you implement a layoutParams in your OnBindViewHolder, say you have RelativeLayout
RelativeLayout.LayoutParams params
=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
params.setMargins(0, 0, 0, 0); //if you need margins
holder.item_survey_image.setLayoutParams(params);

RecyclerView scroll to position of child view's child

I want to scroll to the position in the picture:
Here is my code but there is NullPointerException
public class ViewMenu extends LinearLayout {
protected Handler mHandler;
#BindView(R.id.recycler)
protected RecyclerView mRecycler;
//...
#Override
protected void onFinishInflate() {
super.onFinishInflate();
ButterKnife.bind(this);
mHandler = new Handler();
mRecycler.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
//...
Message msg=new Message();
msg.what=1;
Bundle bundle=new Bundle();
bundle.putInt("targetPosition",3);//assume this is a valid position
bundle.putInt("targetChild",2);//assume this is a valid child
msg.setData(bundle);
handler.sendMessage(msg);
}
class Handler extends android.os.Handler {
#Override
public void handleMessage(Message msg) {
int targetPosition=msg.getData().getInt("targetPosition");
int targetChild=msg.getData().getInt("targetChild");
LinearLayoutManager layoutManager = (LinearLayoutManager) mRecycler.getLayoutManager();
ViewGroup targetViewGroup = (ViewGroup) layoutManager.findViewByPosition(targetPosition);//targetViewGroup becomes null
View targetView = targetViewGroup.getChildAt(targetChild);//NullPointerException
layoutManager.scrollToPositionWithOffset(targetPosition, targetView.getLeft());
}
}
}
I think the problem is that when the targetViewGroup is invisible findViewByPosition returns null. Can anyone find a better way to do that?
Finally I solved it myself. The cause while I guess, is when you call layoutManager.scrollToPositionWithOffset() method, a scroll event is sent to the handler. The recycler view will not do a real scroll until the looper got the message. So the proper way is use handler.post() after calling scrollToPositionWithOffset().
LinearLayoutManager layoutManager = (LinearLayoutManager) mRecycler.getLayoutManager();
//scroll to make the target page visible
if (page < layoutManager.findFirstVisibleItemPosition())
//if the target page is in left and invisible, post: scroll to show one pixel in the screen left
layoutManager.scrollToPositionWithOffset(page + 1, 1);
else if (page > layoutManager.findLastVisibleItemPosition())
//if the target page is in right and invisible, post: scroll to show one pixel in the screen right
layoutManager.scrollToPositionWithOffset(page, mRecycler.getWidth() - mRecycler.getPaddingLeft() - mRecycler.getPaddingRight() - 1);
//post: scroll to candidate
getHandler().post(() -> {
ViewGroup pageView = (ViewGroup) mRecycler.findViewHolderForAdapterPosition(page).itemView;
View candView = pageView.getChildAt(cand);
layoutManager.scrollToPositionWithOffset(page, left ? candView.getLeft() : candView.getRight());
});

How to add an ImageView over a Button

I want to add an ImageView within the onClick() of the Button. But the ImageView should be aligned to the centre of the button.
How can I do this pragmatically?
I'm creating a new instance of the ImageView to be added on top of the View that called it and it's position is equivalent of the View that called it.
How can I get the X and Y coordinates?
EDIT:
#Override
public void onClick(final View v) {
if(v.getWidth() > v.getHeight()) size = v.getHeight();
if(v.getWidth() < v.getHeight()) size = v.getWidth();
RelativeLayout.LayoutParams button = new RelativeLayout.LayoutParams(size,size);
buttonc = new ImageView(getApplicationContext());
buttonc.setBackgroundResource(R.drawable.round);
layout.addView(buttonc,button);
I'm using the above code to attempt to pragmatically set the size of the ImageView to that of the button, and it's working.
As for the coordinates, I still haven't figured it out.
To add clarity: I'm attempting to create a rounded ripple effect over the view that calls it.
I suggest a better solution create an image view before hand place it in the xml as you see fit,at first set its visibility to false .
On click switch its visibilty to true, and the image will appear
This solution is easier than what you are searching.
Feel free to ask questions
to add Imageview
#Override
public void onClick(final View v) {
//LinearLayout
LinearLayout linearLayout= new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
//ImageView
ImageView imageView = new ImageView(this);
//image resource
imageView.setImageResource(R.drawable.specs);
//image position
imageView.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
//adding view to layout
linearLayout.addView(imageView);
//make visible to program
setContentView(linearLayout);
}

Having trouble removing and then adding views programatically to FrameLayout

I am having trouble removing a LinearLayout from a FrameLayout, to then add a new LinearLayout that is populated with EditText generated programatically.
The first time that the view is loaded, I catch a reference to the FrameLayout using findViewById(), and then I make a new LinearLayout programmatically and add a number of EditText to it.
It looks like this
formPlace = (FrameLayout) findViewById(R.id.form_place);
LinearLayout linearView = new LinearLayout(this);
linearView = makeEditText(arrayItems,category);
linearView.setBackgroundColor(Color.GRAY);
linearView.setOrientation(LinearLayout.VERTICAL);
formPlace.addView(linearView);
Next is the function that makes the EditTexts:
private LinearLayout makeEditText(ArrayList<String[]> arrayString, int position){
LinearLayout view = new LinearLayout(this);
arrayInputs = new ArrayList<>();
String[] arrayHints = arrayString.get(position);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
for (int i=0;i<arrayHints.length;i++){
EditText editText = new EditText(this);
Log.i("myApp","tantas veces" + i);
editText.setId(i);
editText.setTextColor(Color.BLACK);
editText.setHint(arrayHints[i]);
editText.setHintTextColor(Color.GRAY);
editText.setLayoutParams(params);
view.addView(editText);
arrayInputs.add(editText);
}
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
buttonParams.gravity = Gravity.RIGHT;
android.widget.Button save = new android.widget.Button(this);
save.setText("Guardar");
save.setLayoutParams(buttonParams);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("myApp", "guardar");
saveClicked(category);
}
});
view.addView(save);
android.widget.Button cancel = new android.widget.Button(this);
cancel.setText("cancelar");
cancel.setLayoutParams(buttonParams);
cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("myApp", "cancelar");
cancelClicked();
}
});
view.addView(cancel);
return view;
}
Then, when a spinner event is called, I catch the reference of the FrameLayout and pass .removeAllViews(). This works as intended, and my pevious view is deleted. The problem comes when I try to add new views to the FrameLayout in the makeEditText line:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, final int position, long id) {
if(firstTime==false) {
FormActivity.this.formPlace.removeAllViews();
LinearLayout auxLinearView = new LinearLayout(FormActivity.this);
auxLinearView = makeEditText(arrayItems, position);
FormActivity.this.formPlace.addView(auxLinearView);
}
firstTime =false;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
What happens is that the old view is deleted but the new view is not set properly. It only shows 1 element of the 4 is intended to show, and the buttons that I create programmatically don't show at all. And my FrameLayout occupies all the remaining space in my screen, even though is set to wrap content. What it implies is that the view is not behaving as it should.
EDIT:
Here are some Screenshots
Here is how it looks before removeAllViews():
https://dl.pushbulletusercontent.com/tExAXJexe4BfG5sGuKvb1RFtGOFd51nE/Screenshot_2015-05-04-17-54-18.png
Here is how it looks after adding the views to the FrameLayout:
https://dl.pushbulletusercontent.com/JM72D3ZrLwTh0YC9g7PODREyOfCj667P/Screenshot_2015-05-04-17-54-25.png

Image caching not in List View android

Hi guys i am stuck with image caching. i need to populate Linear Layout with images from web.
I was searching examples how to cache images in android and found these examples:
http://developer.android.com/resources/samples/XmlAdapters/src/com/example/android/xmladapters/ImageDownloader.html
Lazy load of images in ListView
I tried it implement for my code.
String picPath = "https://www.google.lt/logos/classicplus.png";
try {
/*
View v = null;//new View(this);
ImageLoader loader = new ImageLoader(this);
ImageView im = (ImageView)this.findViewById(R.id.image);
loader.DisplayImage(picPath, im);
addImage( im);*/
LayoutInflater inflater =(LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View convertView = inflater.inflate(R.layout.item, null);
ImageView image = (ImageView)convertView.findViewById(R.id.image);
ImageDownloader down = new ImageDownloader();
down.download(picPath, image);
addImage(image);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
public void addImage( View view){
LinearLayout pubLayout = (LinearLayout)findViewById( R.id.scrollerLinearlayout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//setting layout_margin
params.setMargins(15, 30, 15, 30);
pubLayout.addView(view,params);
}
However this didn't worked. I believe because i add view to linear layout and when image is downloaded it can't repaint it ?
There are examples how to do image caching using ListView. But what to do if i am not using List View.
I would write my own caching but how to make callback to image downloaded as i want first add image to layout and on callback update it ?
Thanks.
you have to call invalidate on parent view
pubLayout.getParent().invalidate();

Categories

Resources