I have TextView created programmatically, like that:
TextView mTextView = new TextView(getApplicationContext());
final LayoutParams params = new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
((MarginLayoutParams) params).setMargins(8, 8, 0, 0);
mTextView.setText(mText);
mTextView.setLayoutParams(params);
mTextView.setPadding(2, 2, 2, 2);
mTextView.setBackground(getResources().getDrawable(R.drawable.note_corps));
tagMap.addView(mTextView);
textViewsWidth = mTextView.getWidth();
But mTextView.getWidth() always returns 0
And if I try:
mTextView.getLayoutParams().width
It returns the LayoutParams corresponding value in the LayoutParams class (-1 or -2)
How can I get the view's width ?
EDIT I need to do this here:
#Override
public void onPostExecute(Hashtable<String, Integer> hash){
final ScrollView tagMapScroll = (ScrollView) findViewById(R.id.tagMapScroll);
final LinearLayout tagMap = (LinearLayout) findViewById(R.id.tagMap);
final ArrayList<Integer> arr = new ArrayList<Integer>(hash.values());
final Enumeration<String> e = hash.keys();
int index = 0;
int textViewsWidth = 0;
while(e.hasMoreElements()){
final TextView tV = new TextView(getApplicationContext());
tV.setTextColor(Color.parseColor("#666666"));
tV.setText(Html.fromHtml(randomColor() + e.nextElement()));
tV.setTextSize(arr.get(index));
final LayoutParams params = new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
((MarginLayoutParams) params).setMargins(8, 8, 0, 0);
tV.setLayoutParams(params);
tV.setPadding(2, 2, 2, 2);
tV.setBackground(getResources().getDrawable(R.drawable.note_corps));
tagMap.addView(tV);
textViewsWidth += tV.getWidth();
index++;
}
tagMapScroll.setVisibility(ScrollView.VISIBLE);
}
EDIT SOLUTION I used this from #androiduser's answer:
mTextView.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
int width = mTextView.getMeasuredWidth();
int height = mTextView.getMeasuredHeight();
Problem solved !
I used this solution:
yourView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
// Ensure you call it only once
yourView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
// Here you can get the size :)
}
});
get this way:
tV.post(new Runnable() {
#Override
public void run() {
int width=tV.getMeasuredWidth();
}
});
those values are constant value for FILL_PARENT and WRAP_CONTENT. If you want to check the view size you can try this way:
tagMap.addView(mTextView);
tagMap.post(new Runnable() {
#Override
public void run() {
mTextView. getWidth();
}
});
you have to wait until android draws the TextView. This way you are posting a Runnable in the tagMap queue, that is executed, hopefully after the textview is draw (so it should be weight and height)
In this method you can get the width height of the view..
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
int width = mTextView.getWidth();
int height = mTextView.getHeight();
}
and define TextView mTextView as global
You have to use ViewTreeObserver class which is used to register listeners that can be notified of global changes in the view tree. Such global events include, but are not limited to, layout of the whole tree, beginning of the drawing pass, touch mode change.
In the Activity's onCreate mehotd put this:
ViewTreeObserver vto = mTextView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
mTextView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
int viewWidth = mTextView.getMeasuredWidth();
}
});
Related
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.
Because I would like to make the same, like Whatsapp, it puts message and hour in the same line
or in another line.
Sometimes there is space and Whatsapp puts the hour in same line. However sometimes there is not space and Whatsapp puts the hour in other line.
Inside or outside...
Any idea?
public static final String TAG = "MainActivity";
private TextView mText;
private RelativeLayout relativeLayout;
private Boolean mFirstTime = true;
private static final int WIDH_HOUR = 382;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final int width = getScreensWidh();
mText = (TextView) findViewById(R.id.activity_main_text);
relativeLayout = (RelativeLayout) findViewById(R.id.activity_main_relative);
mText.setText("aaaaa dfsafsa afdsfa fdsafas adfas fdasf adfsa dsa aaaa dfsafsa afdsfa fdsafas adfas fdasf adfsa");
ViewTreeObserver vto = mText.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if (mFirstTime) {
Layout layout = mText.getLayout();
int lines = layout.getLineCount();
int offset = layout.layout.getLineWidth(lines - 1);
int freeSpace = width - offset;
TextView hour = new TextView(MainActivity.this);
hour.setText("12:20");
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
if (freeSpace > WIDH_HOUR) {
params.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.activity_main_text);
} else {
params.addRule(RelativeLayout.BELOW, R.id.activity_main_text);
}
hour.setLayoutParams(params);
relativeLayout.addView(hour);
Log.d(TAG, String.valueOf(freeSpace));
mFirstTime = false;
}
}
});
}
public int getScreensWidh() {
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
return size.x;
}
Two Public Methods
public abstract int getLineCount ()
Return the number of lines of text in this layout.
public int getLineWidth(int line)
Gets the unsigned horizontal extent of the specified line, including leading margin indent and trailing whitespace.
I think you can give textView.getLayout().getLineWidth(lineNum) a try (lineNum is obviously the last line).
Then compare it to the parent layout width, and if the difference is bigger than timestamp.getWidth() plus some extra padding, then you display it on the same line.
I am new to Android Development. I have been working with using a GridLayout to display Dynamically inserted ImageViews.
My issue is located in "onFocusWindowChanged" but I pasted my onCreate where I do my assignments of the images.
private List<Behavior> behaviors = null;
private static int NUM_OF_COLUMNS = 2;
private List<ImageView> images;
private GridLayout grid;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_behaviors);
XMLPullParserHandler parser = new XMLPullParserHandler();
try {
behaviors = parser.parse(getAssets().open("catagories.xml"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
grid = (GridLayout) findViewById(R.id.behaviorGrid);
images = new ArrayList<ImageView>();
grid.setColumnCount(NUM_OF_COLUMNS);
grid.setRowCount(behaviors.size() / NUM_OF_COLUMNS);
for (Behavior behavior : behaviors)
images.add(this.getImageViewFromName(behavior.getName()));
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
View view = (View) findViewById(R.id.scrollView);
int width = (int) (view.getWidth() * .45);
Log.i("ViewWidth", Integer.toString(width));
GridLayout.LayoutParams lp = new GridLayout.LayoutParams();
lp.height = width;
lp.width = width;
int childCount = images.size();
ImageView image;
for (int i = 0; i < childCount-1; i++) {
image = images.get(i);
image.setLayoutParams(lp);
grid.addView(image);
}
}
In my (short) previous experience, using
grid.add(View);
worked fine, but now I am only seeing the last child display only. Looking through the debugger I can see that the gridview is being populated with more than just the last element, as well as the last imageview.
Thank you for your help
you should create a GridLayout.LayoutParams for each ImageView:
for (int i = 0; i < childCount-1; i++) {
GridLayout.LayoutParams lp = new GridLayout.LayoutParams();
lp.height = width;
lp.width = width;
......
}
GridLayout.LayoutParams contains location information, e.g [column:2, row:3]. In your code, all ImageViews are set the same GridLayout.LayoutParams, so they are located in the same cell(overlapping each other).
When use LinearLayout.LayoutParams instead, there is no location information in it. GridLayout will create a new GridLayout.LayoutParams for each child view, so all ImageViews use their own different GridLayout.LayoutParams and location.
Wish this help. You can read the GridLayout.java and ViewGroup.java for more details.
So I solved my issue, although I'm not sure how-
GridLayout.LayoutParams lp = new GridLayout.LayoutParams();
changed to...
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(x,y);
made it work just as I wanted it. But I'm not sure why- If anyone could explain, please do :)
I use below code for new height to scroll view
But not work
viewFindPos = (ScrollView) findViewById(R.id.Roll);
viewFindPos.getLayoutParams().height = 5000;
// OR : viewFindPos.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 5000));
After you get the Layout parameters of the ScrollView, you need to set the LayoutParameters back to the View. Later request the layout, so it's redrawn. Check out this code below, which will help you to understand.
LayoutParams params = viewFindPos.getLayoutParams();
params.height = 5000;
viewFindPos.setLayoutParams(params);
viewFindPos.requestLayout();
This works:
#Override
public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
ScrollView viewFindPos = (ScrollView) findViewById(R.id.scrollView);
viewFindPos.getLayoutParams().height = 500;
}
OR
#Override
protected void onResume()
{
super.onResume(hasFocus);
ScrollView viewFindPos = (ScrollView) findViewById(R.id.scrollView);
viewFindPos.getLayoutParams().height = 100;
}
I'm adding some textview in a RelativeLayout called "number_container", this is my code but i don't see nothing, the textviews are not added...
private void labelAnimation(){
TextView number = new TextView(getApplicationContext());
number.setGravity(Gravity.CENTER);
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/Gang of Three.ttf");
number.setTypeface(font);
Random random = new Random();
int random_size = random.nextInt(170 - 30) + 30;
number.setTextSize(random_size);
number.setTextColor(000000);
int random_text = random.nextInt(10-1)+1;
number.setText(Integer.toString(random_text));
int random_width = random.nextInt(width_screen - 50) + 50;
int random_height = random.nextInt(height_screen - 50) + 50;
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(random_width, random_height, 0, 0);
number.setLayoutParams(lp);
number_container.addView(number);
int random_delay = random.nextInt(800-100)+100;
final Handler animation_sun = new Handler();
animation_sun.postDelayed(new Runnable() {
#Override
public void run() {
labelAnimation();
}
}, random_delay);
}
I tried also to remove the custom font but no good results.
What can I do?
My problem was the color of the textView...
number.setTextColor(color.WHITE);