Get Element Positions after adding to layout in Android - android

I'm trying to get the postions of an ImageView after i added it to a relative Layout.
I'm adding ImageViews randomly within the relative layout by clicking on a button. So far this works very good. But why does getLeft, getRight, getTop, getBottom return 0? The Thing is, that the values are all available on the next buttonclick. So when ImageView 1 has everything 0 on the creation, it has the information if i click the button again.
So to make it clear:
Buttonclick-1 -> ImageView-1 added (outputs 0 on getLeft,Top,Right,Bottom)
Buttonclick-2 -> ImageView-2 added (ImageView-1 outputs coordinates, but ImageView-2 outputs 0)
I'm sure this has to do with the drawing of the ImageView. Surely it is not ready drawn on the view when i iterate over the children and ask for the actual positions.
Here is my code so far:
final ConstraintLayout cl = (ConstraintLayout) findViewById(R.id.constraintLayout);
final ScrollView sc=(ScrollView) findViewById(R.id.scrollView);
final Button btnButton = (Button) findViewById(R.id.buttontest);
final LinearLayout ll = (LinearLayout) findViewById(R.id.linearLayout);
final RelativeLayout rl = (RelativeLayout) findViewById(R.id.rLayout);
btnButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ImageView im= new ImageView(UserAreaActivity.this);
im.setImageResource(R.mipmap.ic_launcher);
im.setScaleType(ImageView.ScaleType.FIT_XY);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(150, 150);
Random r = new Random();
Random r2 = new Random();
int x = r.nextInt(rl.getWidth());
int y = r2.nextInt(rl.getHeight());
if(x+(layoutParams.width) >= rl.getWidth()){
x=x-layoutParams.width;
}
if(y+(layoutParams.height) >= rl.getHeight()){
y=y-layoutParams.height;
}
layoutParams.leftMargin=x;
layoutParams.topMargin=y;
im.setLayoutParams(layoutParams);
TextView tv = new TextView(UserAreaActivity.this);
tv.setText("my text");
rl.addView(im);
ll.addView(tv);
System.out.println("ID "+im.getId());
System.out.println("Left "+im.getLeft());
System.out.println("Right "+im.getRight());
System.out.println("Top "+im.getTop());
System.out.println("Bottom "+im.getBottom());
for (int i = 0; i < rl.getChildCount(); i++) {
View subView = rl.getChildAt(i);
if (subView instanceof ImageView) {
ImageView imageView = (ImageView) subView;
System.out.println("ID "+i);
System.out.println("Left "+imageView.getLeft());
System.out.println("Right "+imageView.getRight());
System.out.println("Top "+imageView.getTop());
System.out.println("Bottom "+imageView.getBottom());
}
}
}
});

When you call rl.addView(im); you are kicking off a process that will eventually involve a "layout pass" for your RelativeLayout and your ImageView. It is only after this layout pass completes that you will be able to get valid coordinates for your ImageView.
Unfortunately, there's no one-liner for "give me the coordinates after the layout pass". All the best options are asynchronous (i.e. you register some code to run once the ImageView has been laid out). I recommend ViewTreeObserver.OnGlobalLayoutListener.
Something like this:
im.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
System.out.println("ID "+im.getId());
System.out.println("Left "+im.getLeft());
System.out.println("Right "+im.getRight());
System.out.println("Top "+im.getTop());
System.out.println("Bottom "+im.getBottom());
im.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});

Related

Create buttons in sequential order programmatically

I want to parse text, and create for each word - button, but i don't know how to arrange them one after the other
String s = "Alice was beginning to get very tired of sitting";
String[] q = s.split(" ");
for (int i = 0; i < q.length; i++) {
Button myButton = new Button(this);
myButton.setText(q[i]);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout1);
layout.addView(myButton, params);
}
See this custom library: FlowLayout
While you're adding views inside FlowLayout, it automatically wraps when there is no space for the next item.
There's not much wrong about your approach, it's only that relative layout as name suggests requires child views to have some parameters to align the views relative to them e.g. above, below etc. As a result you are getting views overlapping each other and hence only the last added view is visible being on top.
Use FlowLayout instead and you'll be fine.
You need to define RelativeLayout parameters as in example below
Heres an example to get you started, fill in the rest as applicable:
TextView tv = new TextView(mContext);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
params.leftMargin = 107
...
mRelativeLayout.addView(tv, params);
The docs for RelativeLayout.LayoutParams and the constructors are
here
From: How to add a view programmattically to RelativeLayout?
Check the link below to get more useful informations.
Hope it will help
In the following code, you should change the upper limits of the for, to a variable.
public class MainActivity
extends Activity
implements View.OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TableLayout layout = new TableLayout (this);
layout.setLayoutParams( new TableLayout.LayoutParams(4,5) );
layout.setPadding(1,1,1,1);
for (int f=0; f<=13; f++) {
TableRow tr = new TableRow(this);
for (int c=0; c<=9; c++) {
Button b = new Button (this);
b.setText(""+f+c);
b.setTextSize(10.0f);
b.setTextColor(Color.rgb( 100, 200, 200));
b.setOnClickListener(this);
tr.addView(b, 30,30);
} // for
layout.addView(tr);
} // for
super.setContentView(layout);
} // ()
public void onClick(View view) {
((Button) view).setText("*");
((Button) view).setEnabled(false);
}
} // class

HorizontalScrollView - Adding Item at specific position

I want that the user can scroll throw my HorizontalScrollView and if he press my Button, a TextView will be shown on the current position of my HorizontalScrollView.
So far I already know how to present a TextView, but not on a specific position...
TextView textView;
textView = new TextView(MainActivity.this);
textView.setText(editText.getText().toString());
linearLayout.addView(textView);
Any help is welcomed!
Do some thing like this ,
final HorizontalScrollView scrollView = (HorizontalScrollView) findViewById(R.id.scroller);
final RelativeLayout container = (RelativeLayout) findViewById(R.id.container);
findViewById(R.id.addButton).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TextView iv = new TextView(Act2.this);
iv.setText(new Date().toString());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(300, 400);
params.leftMargin = scrollView.getScrollX();
params.topMargin = scrollView.getScrollY();
container.addView(iv, params);
}
});
And the relative layout should be inside the HorizontalScrollView. This will add a textview exactly in the current position , but you should also write code for avoiding the overlapping the TextViews.

Android Dyanmic buttons layout

When creating dynamic buttons I would like them to stack one under the other vertically. I am not sure how to create this effect.
for(int i = 0; i <notificationArrayList.size(); i++)
{
if(i == 0)
{lp.addRule(RelativeLayout.BELOW, R.id.searchButton);}
else
{} //maybe tell the code here to stack under the lastID?
Notification oNote = notificationArrayList.get(i);
Button btn = new Button(this);
btn.setId(i);
final int id_ = btn.getId();
btn.setText(oNote.NotificationText);
btn.setBackgroundColor(Color.rgb(70, 80, 90));
rl.setLayoutParams(lp);
rl.addView(btn, lp);
}
Maybe in the else statement have it get the last id and add RelativeLayout that way?
The easiest way would be to put all the buttons in a LinearLayout and just add the LinearLayout beneath the search button. This produces easier code, but slightly worse drawing performance. Pseudocode would be like:
LinearLayout ll = new LinearLayout(context);
for(i=0; i<numButtons; i++) {
ll.addView(new Button(context));
}
RelativeLayout.LayoutParam lp = new RelativeLayout.LayoutParam();
lp.addRule(RelativeLayout.BELOW, R.id.searchButton);
relativeLayout.addView(ll,lp);
This example should give you an idea:
public class MyActivity extends Activity {
private RelativeLayout rel;
private EditText editText;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mine);
rel = (RelativeLayout) findViewById(R.id.main_rel);
editText = (EditText) findViewById(R.id.pref_edit_text);
Button button = new Button(this);
button.setText("Delete");
// create the layout params that will be used to define how your
// button will be displayed
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// add the rule that places your button below your object (here a editText)
params.addRule(RelativeLayout.BELOW, editText.getId());
// set the layoutParams on the button
button.setLayoutParams(params);
// add button to your RelativeLayout
rel.addView(button);
}
}

How to check if TextView is within the Visible Screen Android

I wanted to know if my textview is within the visible screen region or not..
I have looked at links as:
Android - Get the visible area of a view?
Android: how to check if a View inside of ScrollView is visible?
But nothing seems to work for me.
Code that i used to check is
Rect rect = new Rect();
//textview intialized in onCreate as text1
text1.getHitRect(rect);
text1.measure(0, 0);
//layout is the parent layout (linear) in which i am adding the text view
Rect acctualView = new Rect(Math.round(layout.getX()), Math.round(layout.getY()),
Math.round(layout.getX() + layout.getWidth()), Math.round(layout.getY() +layout.getHeight()));
if(Rect.intersects(acctualView, rect))
{
Toast.makeText(context, "visible", Toast.LENGTH_LONG).show();
}else {
Toast.makeText(context, "Not visible ", Toast.LENGTH_LONG).show();
}
Every width and height of textview and layout is giving me 0 "zero",
Please let me know how to fix this and know whether a view is inside the visible region or not
Here is my complete activity code
public class MainActivity extends Activity {
private TextView text1, text2, text3, text4, text5, text6;
private Context context;
private LinearLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getLayoutInflater().getContext();
layout = (LinearLayout) findViewById(R.id.parentLayout);
setTextViews();
checkViewAndUpdate();
}
private void checkViewAndUpdate() {
Rect rect = new Rect();
layout.getHitRect(rect);
if (text1.getLocalVisibleRect(rect)) {
Toast.makeText(context, "visible", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "Not visible", Toast.LENGTH_LONG).show();
}
}
private void setTextViews() {
text1 = new TextView(context);
text2 = new TextView(context);
text3 = new TextView(context);
text4 = new TextView(context);
text5 = new TextView(context);
text6 = new TextView(context);
text1.setText("text1");
text2.setText("text2");
text3.setText("text3");
text4.setText("text4");
text5.setText("text5");
text6.setText("text6");
text1.setTextSize(30);
text2.setTextSize(30);
text3.setTextSize(30);
text4.setTextSize(30);
text5.setTextSize(30);
text6.setTextSize(30);
//layout.removeAllViews();
layout.addView(text1);
layout.addView(text2);
layout.addView(text3);
layout.addView(text4);
layout.addView(text5);
layout.addView(text6);
}
}
and my layout only contains LinearLayout (empty)
as i wrote on comment, checking visible rect at onCreate won't work.
Try this code at end of onCreate (instead of call checkAndUpdate() directly).
layout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
checkViewAndUpdate();
}
});
View layout does not finish though if you added view at onCreate. yes, this time user cannot see the actual activity and view's actual size are not set.
onGlobalLayout will call when view layout is ended (and activity is visible).
at that timing, we can now get size and position of them!
FYI, you can remove this listener by calling layout.getViewTreeObserver().removeOnGlobalLayoutListener(this) in onGlobalLayout()
try with this:
if (yourView.getParent().getVisibility() == View.VISIBLE) {
// visible
} else{
// hiden
}
Or this:
Rect rect = new Rect();
parentView.getHitRect(rect);
if (imageView.getLocalVisibleRect(rect)) {
// imageView is within the visible window
} else {
// imageView is not within the visible window
}
Why don't you use the for method?
for(int i = 0; i < 6; i++){
TextView tv = new TextView(context);
tv.setTextSize(30);
layout.addView(tv);
//Maybe set some listeners...
}

Making all Buttons in a vertical LinearLayout the same width

I am trying to make all the buttons in one horizontal LinearLayout the same width, based on one button which contains the longest text.
What i tried is to go through all the buttons in the layout and setting there size like this.
LinearLayout layout = (LinearLayout) findViewById(R.id.buttonLayout);
for(int i = 0;i < layout.getChildCount();i++){
View v = layout.getChildAt(i);
if(v instanceof Button){
if(!(v.getId() == R.id.widestButton)){
((Button) v).setWidth(findViewById(R.id.widestButton).getWidth());
}
}
}
This does set all the buttons size, however, the size being set is not the size of the widestButton, its about 40% of it.
How do i make this work?
I found the answer myself, could have probably found it by googling.
Problem is, i was calling this code on the Activitys onCreate.
At this moment, the sizes of the Buttons are not calculated yet.
So i found the ViewTreeObserver, which is able to add a listener to be called when the Layout is done loading. The code i am using now is:
final LinearLayout layout = (LinearLayout) findViewById(R.id.buttonLayout);
ViewTreeObserver vto = layout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
layout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
for(int i = 0;i < layout.getChildCount();i++){
View v = layout.getChildAt(i);
if(v instanceof Button){
if(!(v.getId() == R.id.widestButton)){
((Button) v).setWidth(findViewById(R.id.widestButton).getWidth());
}
}
}
}
});
Which works perfectly fine.

Categories

Resources