Get calculated width of dynamically added UI element - android

In my android app, I try to publish some data on ButtonClick.
I've created separate layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TableLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3" >
<TableRow>
<TextView
android:id="#+id/TV1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/strTV1" />
</TableRow>
</TableLayout>
<ImageView
android:id="#+id/overallPortraitIV"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:contentDescription="#string/strPortrait"/>
</LinearLayout>
My onClick event:
View view = getLayoutInflater().inflate(R.layout.overall_rfid_layout, null);
overallResultTab.addView(view);
ImageView portraitImage=(ImageView)view.findViewById(R.id.overallRfidPortraitIV);
decodedByte = scaleToFitWidth(decodedByte, portraitImage.getMeasuredWidth());
portraitImage.setImageBitmap(decodedByte);
As you see, my ImageView width is set to 0, but it has weight, so, factical width should not be 0. So how to get amount of pixels, which factically are occupied by the ImageView?
As shown in code, I've tried to getMeasuredWidth, also, getWidth, but they return 0;

Since you are measuring ImageView's width before setting any image to the view thats why getMeasuredWidth returning 0. In your XML, you set the layout_width to "0dip" so before setting any image to this ImageView it's width will remain "0dip".

Related

How to change the size of background 9patch image according to the size of child textview

I am making a chat bubble. I made the bubble using 9patch image. But I want to resize the bubble according to the size of textview.. How to do it ?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:background="#drawable/senderbubble2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="10dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true">
<TextView
android:id="#+id/txt_msg"
android:text="hello world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:textColor="#000000" />
</LinearLayout>
</RelativeLayout>
The above is my xml code of chat bubble...
You can get the height & width of textView, by calling below methods on textview reference, below method returns int value, you can use it to resize image.
textView.getMeasuredHeight();
textView.getMeasuredWidth();
If you want the height/width of text in textView you can try
Rect bounds = new Rect();
textView.getPaint().getTextBounds(textView.getText().toString(), 0, textView.getText().length(), bounds)
see documentation for more details
https://developer.android.com/reference/android/view/View.html

Android inflate view not honoring dynamically set width

I'm trying to generate a bitmap to show in the notification. The layout is a bit complex, however, I need to set the width according to the screen size. In the XML, I have a LinearLayout in the root and I inflate the view with
LinearLayout v = (LinearLayout) i inflater.inflate(R.layout.notification_expanded_detail, null, false);
This layout has quite a bunch of elements which I need to fill. And their width depends on the width of the layout itself and the width of the layout varies with phone. I set the width for the inflated layout with
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int) available_width, LinearLayout.LayoutParams.WRAP_CONTENT);
v.setLayoutParams(params);
However, when I try to generate a bitmap from this, it appears that the size set with the setLayoutParams is not affecting the child views of the inflated layout. There are many questions around i in SO, however, all of them deals with inflating a layout that already has a rootView and nothing worked for me. A couple of answers suggested that I call the measure() method on the view. However, after calling the measure method, the getMeasuredWidth returns a width lesser that what I had set with the setLayoutParams call.
Update
A little more context about the problem. I was trying to show a notification that had a placeholder ImageView which I would later fill in with a bitmap generated from a view inflated from an XML. The remote view layout is
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/sliderViewBottom"
android:layout_width="match_parent"
android:layout_height="256dp"
android:background="#color/detail_page_blue"
android:clickable="true"
android:focusable="false"
android:orientation="vertical"
android:gravity="center"
android:padding="10dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#color/white"
>
<ImageView
android:id="#+id/progressImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#D05757"
android:gravity="center"
android:paddingBottom="8dp"
android:paddingTop="8dp"
>
<TextView
android:id="#+id/overAllDelay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delayed by "
android:textColor="#color/white"
/>
<TextView
android:id="#+id/overAllDelayVal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/overAllDelay"
android:text="-"
android:textColor="#color/white"
/>
</RelativeLayout>
</LinearLayout>
The layout that I was trying to inflate and generate a bitmap from to set in the progressImage was
<RelativeLayout
android:id="#+id/swipe_page_track_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingLeft="#dimen/swipe_layout_info_card_track_margin"
android:paddingRight="#dimen/swipe_layout_info_card_track_margin"
>
<ImageView
android:id="#+id/fromGreenCircle"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_alignParentLeft="true"
android:layout_marginTop="1dp"
android:src="#drawable/from_green_circle"/>
<ImageView
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_alignParentRight="true"
android:layout_marginTop="1dp"
android:src="#drawable/station_end"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="3dp"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="4.5dp"
android:background="#drawable/swipe_page_empty_track"
/>
<ImageView
android:id="#+id/fromGreenTrack"
android:layout_width="105dp"
android:layout_height="3dp"
android:layout_alignParentLeft="true"
android:layout_marginTop="4.5dp"
android:src="#drawable/from_green_track"
/>
<ImageView
android:id="#+id/trackNavigationIcon"
android:layout_width="12dp"
android:layout_height="12dp"
android:layout_marginLeft="100dp"
android:src="#drawable/ic_track_detail_card_current_station_pointer"
/>
<TextView
android:id="#+id/currentStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/trackNavigationIcon"
android:layout_marginTop="10dp"
android:textColor="#color/black"
/>
</RelativeLayout>
I was trying to inflate this second layout with
LinearLayout v = (LinearLayout) i inflater.inflate(R.layout.notification_expanded_detail, null, false);
And then was using
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int) available_width, LinearLayout.LayoutParams.WRAP_CONTENT);
v.setLayoutParams(params);
This available_width was computed dynamically. However, when I later tried to generate a Bitmap from this inflated view by using v.layout(), it kep t generating an image which was not honouring the width that I set.
Later, I came across this SO thread where I noticed that while inflating, instead of specifying null as the parentView, new LinearLayoutParams(contex) was specified. I tried that and now, the dynamically set width is being honoured.
I came across this SO thread where I noticed that while inflating, instead of specifying null as the parentView, new LinearLayoutParams(contex) was specified. I tried that and now, the dynamically set width is being honoured

How to set font size of a textview equal to its height

The code is a simple xml file containing a linear layout.Inside the linear layout , I have two text Views and a lnother linear layout, containing a button.
I want to set the font size equal to height of the textview. We get the height using weight and weight sum method.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/orange"
android:weightSum="100"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="45"
android:text="Coming Soon"
android:gravity="center_vertical"
android:background="#color/mainscreen" />
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="10"
android:text="TextView" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="45"
android:background="#color/mainscreen">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
</LinearLayout>
You can achieve this programatically.
TextView defaultTextView = new TextView(context);
float sourceTextSize = defaultTextView.getTextSize();
TextView.setTextSize(sourceTextSize/getResources().getDisplayMetrics().density);
The defaultTextView.getTextSize() returns size in pixels and setTextSize(size) takes input as sp, So your text becomes bigger.
Here, instead of directly setting up the text size, first we are converting them according to density.
Draw a shape drawable for textview .
<solid android:color="#color/your_background_coler"/>
and put it in textview background attribute.
We can not set text size like wrap_content,fill_parent or match_parent
as like textview height so, if you need to set text size equals to
Textview height than you need to go for fix textview height and text
size like....
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="#string/hello_world"
android:textSize="50dp"/>

adjust the first image view height according to second imageview

<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight=".40"
android:orientation="vertical" >
<ImageView
android:id="#+id/First"
android:layout_width="match_parent"
android:layout_height="160px"
android:layout_centerInParent="true"
android:padding="5dp" />
<ImageView
android:id="#+id/Second"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#drawable/play_small" />
</RelativeLayout>
I want to set the height of first imageview with id --> First according to the second image view id -- > Second. I am setting the second image view src from xml and i am downloading the first image from some weblink.
You can use LayoutParams for this task.
Firstly you have to find your second ImageView height, and set it to the LayoutParams of your first ImageView.
LayoutParams params = firstImage.getLayoutParams();
params.height = secondImage.getWidth();
firstImage.setLayoutParams(params);
Hope this will help you.

ImageViews and Buttons Android Apps

I'm trying to build an android application and I've created a screen with a table layout and added to the first table row an image view and two large texts, to the second row I've added one button.
When I change the text of the button it automatically changes the size of the image view above to match the size of the button.
Here is the code:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/TableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".ChildInfoActivity" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:contentDescription="#string/child_info_image_descreption" />
<TextView
android:id="#+id/childInfoNameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/name_text_view"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_gravity="center_vertical" />
<TextView
android:id="#+id/childInfoHasArrivedTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/has_arrived_text_view"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_gravity="center_vertical" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/more_info_button"
android:layout_weight="1" />
</TableRow>
why is that?
The reason is because when using TableLayout, each TableRow will have same size for each same column. Since the ImageView and Button are both on column 0, everything that affects Button's width will also affect ImageView's width.
If you permit the Button to span over many columns, add android:layout_span="2" to the Button. Else, use different layout, such as LinearLayout or RelativeLayout.
Because your row item, which holds imageView and textViews, has "wrap_content" value as height. So probably when your texts' heights getting bigger than your imageviews it increases its parent height as well.
You may try to put imageView and textViews inside a relativeLayout and wrap textViews with imageViews height and then put that relativeLayout into row item. It will be nested, but i think still it is better than given exact size because that way it wouldn't be seen so good on some devices...
Your buttons have
android:layout_width="wrap_content". If you set
android:layout_width="100dp"
then your width won't be dynamic.
remove android:layout_gravity="center_vertical" and as u have given added android:layout_weight="1" to button add this line to other components also

Categories

Resources