Android: position of RelativeLayout.LayoutParams does not work? - android

I've create new application with Android Studio and altered onCreate method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.activity_main, new PlaceholderFragment())
.commit();
}
int width = 400;
int height = 20;
RelativeLayout.LayoutParams params;
params = new RelativeLayout.LayoutParams(
width,
height
);
params.leftMargin = 20;
params.topMargin = 200;
TextView textView = new TextView(this);
textView.setText("Text Added dinamically");
textView.setLayoutParams(params);
FrameLayout activityMain = (FrameLayout) findViewById(R.id.activity_main);
activityMain.addView(textView);
}
I expecteted to see "Text Added dinamically" at 200 distance from top. Indeed it appears snappet to top. How can I fix this?

Related

onScrollChange() is not being called for every pixel scrolled on ScrollView

I want to add new child views after every 50 pixels of scrolling but the statement Log.d("scroll",scrollY+":"+oldScrollY); is missing some values i.e. scrollY = 149 and scrollY = 151 are there in Log but scrollY = 150 is missing. So the code doesn't run for scrollY = 150 and new child views is not added.
Although it works as it should for most of the values of scrollY but for few values of scrollY, the event is not being triggered.
How can i resolve this issue? Or is there any other way to achieve this.
public class MainActivity extends AppCompatActivity implements ScrollViewListener {
RelativeLayout parent;
TextView height;
ObservableScrollView scrollView;
int position=1;
View view1,view2;
#RequiresApi(api = Build.VERSION_CODES.M)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
parent = (RelativeLayout) findViewById(R.id.parent);
scrollView = (ObservableScrollView) findViewById(R.id.scroll_view);
LayoutInflater inflater = LayoutInflater.from(this);
view1 = inflater.inflate(R.layout.education,null, false);
view1.setId(R.id.parent+position);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.LEFT_OF,R.id.divider);
params.topMargin = 100;
params.rightMargin = 3;
parent.addView(view1, params);
position++;
final Animation leftAnimation = new TranslateAnimation(view1.getX()-50,view1.getX(),view1.getY()+100,view1.getY());
leftAnimation.setDuration(1000);
view1.setAnimation(leftAnimation);
scrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
#Override
public void onScrollChange(View view, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
Log.d("scroll",scrollY+":"+oldScrollY);
if((scrollY-oldScrollY)>0 && scrollY%50 == 0)
{
Log.d("scroll","abcghfg");
if(position%2==0)
{
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.RIGHT_OF,R.id.divider);
// params.topMargin = (position-1)*(view1.getLayoutParams().height)+position*100;
params.topMargin = 30;
params.leftMargin = 3;
params.addRule(RelativeLayout.BELOW,parent.getChildAt(position-1).getId());
Log.d("scroll","abc");
view2 = LayoutInflater.from(MainActivity.this).inflate(R.layout.education,null,false);
view2.setId(R.id.parent+position);
Animation rightAimation = new TranslateAnimation(view2.getX()+50,view2.getX(),view2.getY()+100,view2.getY());
rightAimation.setDuration(1000);
view2.startAnimation(rightAimation);
parent.addView(view2, params);
}
else
{
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.LEFT_OF,R.id.divider);
//params.topMargin = (position-1)*(view1.getLayoutParams().height)+position*100;
params.topMargin = 30;
params.rightMargin = 3;
params.addRule(RelativeLayout.BELOW,parent.getChildAt(position-1).getId());
Log.d("scroll","def");
view2 = LayoutInflater.from(MainActivity.this).inflate(R.layout.education,null,false);
view2.setId(R.id.parent+position);
Animation animation = new TranslateAnimation(view2.getX()-50,view2.getX(),view2.getY()+100,view2.getY());
animation.setDuration(1000);
view2.startAnimation(animation);
parent.addView(view2, params);
}
position++;
}
}
});
}
}
Thanks in advance.
Android doesn't render every pixel in a scroll, because it would be bad for performance. If you scroll really slow, you will see that your callback will be called for every pixel, but if you scroll fast, it will be called just when the render triggers.
In your case, you need to check if it rendered 50 or more pixels since last time you rendered the last view.

Get layout width and set new width value in onGlobalLayoutListener

I create programmatically few layouts and I need get width one of them, recalculate this value and change. All this action I do in onGlobalLayoutListener. But when I set a new width value, he doesnt change.
private RelativeLayout batteryContainer;
private LinearLayout batteryLeftSide;
private LinearLayout batteryRightSide;
public void drawBattery(){
handler.post(new Runnable() {
#Override
public void run() {
//Контейнер всієї батарейки
batteryContainer = new RelativeLayout(activity);
RelativeLayout.LayoutParams batteryContainerParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
batteryContainerParams.setMargins((int) convertDpToPixel(containerMarginLeft), (int) convertDpToPixel(containerMarginTop), (int) convertDpToPixel(containerMarginRight), (int) convertDpToPixel(containerMarginBottom));
if(layoutBelow != null){
batteryContainerParams.addRule(RelativeLayout.BELOW, layoutBelow.getId());
}
if(layoutAbove != null){
batteryContainerParams.addRule(RelativeLayout.ABOVE, layoutAbove.getId());
}
batteryContainer.setLayoutParams(batteryContainerParams);
batteryContainer.setBackgroundColor(Color.parseColor("#505050"));
batteryContainer.setId(containerId);
int leftWidth = 0;
int rightWidth = 0;
//Ліва частина батарейки
batteryLeftSide = new LinearLayout(activity);
batteryLeftSide.setOrientation(LinearLayout.HORIZONTAL);
RelativeLayout.LayoutParams batteryLeftSideParams = new RelativeLayout.LayoutParams((int)convertDpToPixel(leftWidth), (int)convertDpToPixel(sideHeight));
batteryLeftSideParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
batteryLeftSide.setLayoutParams(batteryLeftSideParams);
batteryLeftSide.setBackgroundColor(Color.parseColor("#900000"));
batteryLeftSide.setId(leftSideId);
//Права частина батарейки
batteryRightSide = new LinearLayout(activity);
batteryRightSide.setOrientation(LinearLayout.HORIZONTAL);
RelativeLayout.LayoutParams batteryRightSideParams = new RelativeLayout.LayoutParams((int)convertDpToPixel(rightWidth), (int)convertDpToPixel(sideHeight));
batteryRightSideParams.addRule(RelativeLayout.RIGHT_OF, batteryLeftSide.getId());
batteryRightSide.setLayoutParams(batteryRightSideParams);
batteryRightSide.setBackgroundColor(Color.parseColor("#009900"));
batteryRightSide.setId(rightSideId);
//Додамо праві та ліві сторони в контейнер
batteryContainer.addView(batteryLeftSide);
batteryContainer.addView(batteryRightSide);
//Додамо контейнер в кореневий Layout на активності
rootLayout.addView(batteryContainer);
//Обчислення яка сторона батарейки займе 50%
ViewTreeObserver observer = rootLayout.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
rootLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
int batteryContainerWidth = batteryContainer.getMeasuredWidth();
int halfPlace = batteryContainerWidth * 50 / 100;
trustFromMe = halfPlace;
if(trustToMe > trustFromMe){
batteryLeftSide.getLayoutParams().width = halfPlace;
}
if(trustToMe < trustFromMe){
batteryRightSide.getLayoutParams().width = halfPlace;
}
if(trustToMe == trustFromMe){
batteryLeftSide.getLayoutParams().width = halfPlace;
batteryRightSide.getLayoutParams().width = halfPlace;
}
}
});
//but width not change
}
});
}
Try adding this to the end of the GlobalLayoutListener:
rootLayout.requestLayout();
This should force the layout to redraw it's size. If this doesn't work you may want to try creating and setting new LayoutParams as well.

setMargins(left, top, right, bottom); Android doesn't work

I have this code, but the textview are all without Margin right and i don't know why.
I add some lines of onCreate for declaration.
And the functions about the creation of the textview.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLayout = (LinearLayout) findViewById(R.id.linearlayout);
mEditText = (EditText) findViewById(R.id.input_materia);
mButton = (Button) findViewById(R.id.add_materia);
mButton.setOnClickListener(onClick());
TextView textView = new TextView(this);
textView.setText("New text");
/*
* add textview from editext
*/
private OnClickListener onClick() {
return new OnClickListener() {
#Override
public void onClick(View v) {
mLayout.addView(createNewTextView(mEditText.getText().toString()));
}
};
}
private TextView createNewTextView(String text) {
final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lparams.setMargins(0, 0, 30, 0);
final TextView textView = new TextView(this);
textView.setLayoutParams(lparams);
textView.setText(text);
textView.setGravity(Gravity.CENTER);
return textView;
}
some helps?
Have you tried referencing Relative Layout Params or Linear Layout Params depending on what is set in your XML?
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);

How to add a whole layout withing main layout in android

I've tried following code to add two layouts within main layout
but this gives error while setting layout to frame and while adding it into view
public class MainActivity extends Activity {
FrameLayout rootLayout;
FrameLayout frame1,frame2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rootLayout=new FrameLayout(getApplicationContext());
frame1=(FrameLayout)findViewById(R.layout.frame1);
frame2=(FrameLayout)findViewById(R.layout.frame2);
LayoutParams param1=new FrameLayout.LayoutParams(180,300);
LayoutParams param2=new FrameLayout.LayoutParams(180,300);
param1.gravity=Gravity.LEFT;
param2.gravity=Gravity.RIGHT;
param1.setMargins(0, 0, 30, 30);
param2.setMargins(30, 0, 0, 30);
frame1.setLayoutParams(param1);
frame2.setLayoutParams(param2);
rootLayout.addView(frame1);
rootLayout.addView(frame2);
}
}
You have to remove both frame1 and frame2 Views from their parent View...
and this may help you...
FrameLayout rootLayout;
FrameLayout frame1, frame2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
rootLayout = new FrameLayout(this);
frame1 = (FrameLayout) getLayoutInflater().inflate(R.layout.frame1,null);
frame2 = (FrameLayout) getLayoutInflater().inflate(R.layout.frame2, null);
FrameLayout.LayoutParams param1 = new FrameLayout.LayoutParams(180, 300);
FrameLayout.LayoutParams param2 = new FrameLayout.LayoutParams(180, 300);
param1.gravity = Gravity.LEFT;
param2.gravity = Gravity.RIGHT;
param1.setMargins(0, 0, 30, 30);
param2.setMargins(30, 0, 0, 30);
frame1.setLayoutParams(param1);
frame2.setLayoutParams(param2);
rootLayout.addView(frame1);
rootLayout.addView(frame2);
setContentView(rootLayout);
}

Pop up android app

I have a pop up that displays an image upon clicking a button. I got to display the pop up but the image is so small and I want it to occupy the 90% of the screen and add a close button to it.
Here is my code:
PopupWindow popUp;
LinearLayout layout;
TextView tv;
LayoutParams params;
ImageView imageView;
boolean click = true;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bmi);
popUp = new PopupWindow(this);
layout = new LinearLayout(this);
tv = new TextView(this);
imageView = new ImageView(this);
imageView.setImageResource(R.drawable.animalbite);
WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int fullScreenWidth = display.getWidth();
int dialogWidth = (int) (fullScreenWidth * 0.9);
WindowManager.LayoutParams params = getWindow().getAttributes();
params.height = LayoutParams.WRAP_CONTENT;
params.width = dialogWidth;
getWindow().setAttributes(params);
imageView.setLayoutParams(params);
//tv.setText("Hi this is a sample text for popup window");
layout.addView(imageView, params);
popUp.setContentView(layout);
Button ViewBmi = (Button) findViewById(R.id.ViewBmi);
ViewBmi.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (click) {
popUp.showAtLocation(layout, Gravity.CENTER, 10 ,10);
click = false;
} else {
popUp.dismiss();
click = true;
}
}
});
}
Here is the display:
ANy ideas?
Try this...
PopupWindow popUp;
LinearLayout layout;
TextView tv;
LayoutParams params,params2;
ImageView imageView;
boolean click = true;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Display display = getWindowManager().getDefaultDisplay();
final int height = (display.getHeight()*90)/100;
final int width = (display.getWidth()*90)/100;
popUp = new PopupWindow(this);
layout = new LinearLayout(this);
tv = new TextView(this);
imageView = new ImageView(this);
imageView.setImageResource(R.drawable.ic_launcher);
params = new LayoutParams(width,height-50);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(imageView, params);
Button button = new Button(this);
button.setText("Close");
params2 = new LayoutParams(100,50);
layout.addView(button,params2);
popUp.setContentView(layout);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
popUp.dismiss();
click = true;
}
});
Button ViewBmi = (Button) findViewById(R.id.button1);
ViewBmi.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (click) {
popUp.showAtLocation(layout, Gravity.CENTER, 10, 10);
popUp.update(10, 20, width,height);
click = false;
} else {
popUp.dismiss();
click = true;
}
}
});
}
Try This....
Button button = new Button(this);
button.setText("Close");
params2 = new LayoutParams(100,50);
--->params2.setMargins(100, 0, 0, 0);
layout.addView(button,params2);
I'd do it like this:
height = (int)(0.9*getWindow().getWindowManager().getDefaultDisplay().getHeight());
params.height = height;
Try:
params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
...
layout.addView(imageView, params);
This way the ImageView will be as wide, as possible.
If the underlying dialog is not wide enough, then you can set it's width also full screen:
WindowManager.LayoutParams params = getWindow().getAttributes();
params.height = LayoutParams.WRAP_CONTENT;
params.width = LayoutParams.MATCH_PARENT;
getWindow().setAttributes(params);
Or if you want it to be for example 90% of the screen, then:
// get the width of the whole screen
Display display = getWindow().getDefaultDisplay();
int fullScreenWidth = display.getWidth();
//then set 90% of it to the width of the Dialog:
int dialogWidth = (int) fullScreenWidth * 0.9);
WindowManager.LayoutParams params = getWindow().getAttributes();
params.height = LayoutParams.WRAP_CONTENT;
params.width = dialogWidth;
getWindow().setAttributes(params);

Categories

Resources