I have edit view that I animated from the middle of the screen to the top using object animator:
animators.add(ObjectAnimator.ofFloat(ev, "y", 0));
On animationEnd I try to add another view below it:
EditText ev2= new EditText(getActivity());
ev2.setText("Test");
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.BELOW, ev.getId()); // first view id (ev)
params2.leftMargin = 107;
groupView.addView(ev2, params2);
This will be added below to the original position of the first edit view. How can I add other view exactly below the view after animation.
Just use LinearLayout with vertical orientation.
Related
In my recyclerview onCreateViewholder method I create a dynamic which would have unspecified number of text views inside a linear layout
if (sessionManager.getBookScrolOrientation() == AppConstents.HORIZONTAL) {
LinearLayout linearLayout = new LinearLayout(context);
ViewGroup.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT , ViewGroup.LayoutParams.MATCH_PARENT);
linearLayout.setLayoutParams(layoutParams);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
Spannable spannable = getVerseSpanableHorizontal(pidreference.get(position) , cidreference.get(position));
Pagination pagination = new Pagination(spannable,
pageWidth,
pageHight,
textPaint,
lineSpacingMultiplier,
lineSpacingExtra,
isTextPaddingIncluded);
List<CharSequence> list = pagination.getPages();
for (CharSequence charSequence : list){
TextView tv = new TextView(linearLayout.getContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(Tools.getScreenWidth() , ViewGroup.LayoutParams.MATCH_PARENT);
tv.setLayoutParams(params);
tv.setText(charSequence);
linearLayout.addView(tv);
}
MainViewHolder viewHolder = new MainViewHolder(linearLayout);
return viewHolder;
}
When i use SnapHelper with this recyclerview it scrolls to the center of linear layout not the next textview. How can i make it scroll to the next textview inside the linearlayout?
If I understand correctly, the problem is that SnapHelper snaps the center of the current view to the center of the recycler. As the docs say:
The implementation will snap the center of the target child view to the center of the attached RecyclerView.
If you want the the current view to snap to the start of the recycler use a libraray such as this one. Import it with gradle and activate it like this:
GravitySnapHelper(Gravity.START).attachToRecyclerView(recyclerview);
I'm adding multiple Views by code into Layout. I need each new View to be above previous one(top of the parent layout).
EDIT: To be more accurate I'll describe what the app module should does. User start with clean screen and one button at the bottom of the screen. The button adds a View at the top of the screen. Next clicks should add next views above previous ones to make the newest View be on the top of a container. The app saves state and on restart user see views in the same order.
Call the following method from Button's onClick Event.
private final int LAYOUT_TOP_INDEX = 0;
private void addViewOnTop(View view){
if(layout != null && view !=null)
layout.addView(view, LAYOUT_TOP_INDEX);
}
where 'layout' is your Layout (e.g., LinearLayout) to which the View is to be added.
Would really need more information from you to give a more accurate answer, but if you're saying what i think you are then you can just add these views to a LinearLayout with orientation set to vertical.
And assuming you're iterating through a list to dynamically add views, instead of incrementing from 0, increment down from the size of the list.
for(int i = size; i >= 0; i--){
linearLayout.add(new TextView(Context));
}
View positions inside ViewGroups are defined by the LayoutParams
How does this happen? Views pass their LayoutParams to their parent ViewGroups
//100% programatic approach with simple LayoutParams
LinearLayout myLinearLayout = new LinearLayout(this);
//if the **parent** of the new linear layout is a FrameLayout
FrameLayout.LayoutParams layoutParams =
new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
//or if you have the XML file you don't have to worry about this
//myLinearLayout = (LinearLayout)findViewById(R.id.my_simple_linear_layout);
//you could have a LinkedList<TextView>
LinkedList<TextView> textViewList = new LinkedList<>();
//assuming the order is the correct order to be displayed
Iterator<TextView> descendingIterator = textViewList.descendingIterator();
while(descendingIterator.hasNext())
{
//just add each TextView programatically to the ViewGroup
TextView tView = descendingIterator.next();
myLinearLayout.addView(tView);
}
Just like we defined LayoutParams for the LinearLayout we could also define LayoutParams for the TextView
IMPORTANT: when setting LayoutParams you need to be sure they fit the VIEWGROUP, that is the parent of the View being added
private TextView textViewFactory(String myText) {
TextView tView = new TextView(getBaseContext());
//controling the position relatively to the PARENT
//because you are adding the textview to a LINEAR LAYOUT
LinearLayout.LayoutParams paramsExample =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
tView.setLayoutParams(paramsExample);
//configuring the insides of the textview
//you can also do all kinds of stuff programatically
tView.setGravity(Gravity.CENTER_HORIZONTAL);
tView.setPadding(10, 10, 10, 10);
tView.setTypeface(Typeface.DEFAULT_BOLD);// (null, Typeface.BOLD_ITALIC);
tView.setTypeface(Typeface.SANS_SERIF);
tView.setTypeface(null, Typeface.ITALIC);
tView.setTypeface(Typeface.defaultFromStyle(R.style.AppTheme));
tView.setId(R.id.aux_info);
tView.setText(myText);
//.........all kinds of stuff really
return tView;
}
If you mean adding a view programmatically so that the new one is added above the previous one, instead of below it, then I suggest this:
Maintain an ArrayList with the items you want to turn into views
Put them into a ListView
When you want to add a new view that must appear at the top of the list, insert it as the first element of your ArrayList and recreate the ListView from it.
I am getting stucked on the following problem:
For a questions app I want to implement a nice output for a survey showing the possible answers and the percentage of people answered the specific question. Therefore, I want to add a "bar" to my app that displays the percentage. I try to solve this by using views and weighting them in a linear layout.
I want to add the different answers programmatically, this is the code I've got so far.
My problems are, that I am not getting closer to weighting the views and resizing them.
/* Add all questions */
RelativeLayout my_root = (RelativeLayout) findViewById(R.id.ownerRL);
/* Add a new Linearlayout as a container for the question */
LinearLayout A = new LinearLayout(this);
A.setOrientation(LinearLayout.HORIZONTAL);
my_root.addView(A);
/* Create a new View in this container, for the status bar */
View new_view = new View(getBaseContext());
new_view.setBackgroundColor(Color.YELLOW);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(50, 20, 3);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) A.getLayoutParams();
params.addRule(RelativeLayout.BELOW, R.id.question1);
A.addView(new_view);
View new_view2 = new View(getBaseContext());
new_view.setBackgroundColor(Color.GREEN);
ViewGroup.LayoutParams lp2 = new ViewGroup.LayoutParams(50, 20);
RelativeLayout.LayoutParams params2 = (RelativeLayout.LayoutParams) A.getLayoutParams();
params2.addRule(RelativeLayout.BELOW, R.id.question1);
params2.addRule(RelativeLayout.LEFT_OF, new_view.getId());
A.addView(new_view2);
The Green view should actually be right of the yellow one (which is not visible).
How do get it managed such that it creates a red/white bar that has a height of 4px and the red and weight parts are weighted?
Thanks for your help!
check this link, Set Linear Layout weight it like this
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT, 1.0f);
The last parameter is weight.Hope this help.
I am currently having a programatically created framelayout with a imageview and a text view. By original layout is relative layout.
RelativeLayout layout = (RelativeLayout) findViewById(R.id.rel);
FrameLayout fr = new FrameLayout(this);
fr.setBackgroundResource(R.drawable.question_bar_icon);
ImageView b1 = new ImageView(this);
TextView b2 = new TextView(this);
b1.setBackgroundResource(R.drawable.mcq);
fr.addView(b1);
fr.addView(b2);
layout.addView(fr);
Now i am unable to resize the imageview and the button, also m unable to position them, in the sence textview in the center and imageview in the verticle center and left positioned.
I tried layout params, but not working, any help would be greatly appreciated!
Try using the overloaded version of addView that takes a LayoutParameter as well in every case you are programatically adding a view, otherwise it is inserted to the ViewGroup with default layout parameters. (P.S. you can also control the order of insertion).
For example for the ImageView you wanted to be in the center of the frame layout:
int width = FrameLayout.LayoutParams.WRAP_CONTENT; //or whatever you want
int height= FrameLayout.LayoutParams.WRAP_CONTENT; //or whatever you want
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(width, height, Gravity.CENTER);
fr.addView(b1,lp);
Do the same for the other view, and most importantly, do the same, using RelativeLayout.LayoutParams for inserting the frame element itself into your RelativeLayout.
I have added a button and an edittext onto a framelayout. The problem is I appear to lose the functionality of being able to click the button. When I click the button, the keyboard appears for the edittext. How can I add button/editext objects into a layout so that they appear on a new row and do not affect each other. I was using the padding option to move them about, but this still causes the button to be non-clickable.
Here is my code: -
FrameLayout reportLayout = (FrameLayout) findViewById(R.id.reportDetailLayout);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
Button executeButton = new Button(this);
executeButton.setClickable(true);
executeButton.setOnClickListener(handleOnClick(executeButton));
EditText text1 = new EditText(this);
executeButton.setText("Execute");
executeButton.setMinimumHeight(10);
executeButton.setMinimumWidth(150);
text1.setId(1);
text1.setHint("Enter Value");
executeButton.setPadding(0, 0, 0, 0);
text1.setPadding(12, 70, 0, 0);
executeButton.setLayoutParams(params);
text1.setLayoutParams(params);
reportLayout.addView(executeButton);
reportLayout.addView(text1);
FrameLayout will cause Views to overlap. Try LinearLayout with setOrientation().
And you don't use padding for placement. Padding is not same thing as a margin.