Is there a way to specify android:layout_height dynamically during onCreate? - android

I would like to set
android:layout_height="wrap_content"
to some pixels(int) during my application start. This is a textView and the reason behind this is that i want the height of the textView dynamic based on some inputs from my end which will be computed when the onCreate method is called.
Is this possible? if yes any example would be great.
Forgot to add my textView xml looks like this
<TextView
android:id="#+id/sometext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fadingEdge="vertical"
android:background="#drawable/dropshadow"
android:scrollbars="vertical"
android:text="Getting data on your slow n/w..."
android:textColor="#ffffff"
/>

Just edit the layout params of the view.
TextView v = (TextView) findViewById(R.id.sometext);
LayoutParams lp = v.getLayoutParams();
lp.height = 50;
v.setLayoutParams(lp);
The view will be 50px high in this example.
Note that it's generally not recommended to use pixel dimensions for layouts due to the many device specs out there. Rather use dp (density independent pixels) instead. You can calculate pixel dimensions from dp values in the following way (50dp to px here):
float dp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50,
getResources().getDisplayMetrics());

Related

How to give layout width as double value in android?

I am trying to set layout width as float value programmatically. But it accept only int value.
LinearLayout linearLayout = findViewById(R.id.parent_layout);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
layoutParams.width = 200.75; // Here I am getting error.
But if I set by xml I am able to give float value.
<LinearLayout
android:id="#+id/parent_layout"
android:orientation="vertical"
android:layout_width="200.75dp"
android:layout_height="200dp"/>
But not able to set programmatically. Please let me some idea to resolve this.
DP (DIPs) are not pixels and "layoutParams.width=" requires Pixels.
200.75dp is not equal to 200.75px because it's Display Density dependant.
200.75dp could be more than 600px on some display.
You can convert PX in DP and vice-versa using simple methods (please search online).
However an internal conversion and Math.round() is done when "layout_width="200.75dp"" is executed in Runtime, and you can simulate it using appropriated conversion methods.

How to explain this weird behaviour when programmatically setting margins to a view inside RelativeLayout?

I have a RelativeLayout inside a fragment that one of three tabs in a TabView. See this Screenshot - big pink square is my RelativeLayout:
screenshot
The view that is inside it is the little blue square in the bottom right corner. Since the RelativeLayout is 300x300dp and the little square is 8x8dp, if I set its top and left margin to 292dp it ends up in that corner.
Now I want to change its position programmatically, but when I do it, my values keep getting divided by two. So if I change the margins from 292 to 292 it ends up in the center of the RelativeLayout, but if I set each to 292*2, it ends up back in the corner.
Maybe there is someone who knows what is happening here?
This is my layout file:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="40dp"
android:height="40dp"
android:background="#color/colorAccent"
android:text="Test!" />
<RelativeLayout
android:layout_width="300dp"
android:layout_height="300dp"
android:background="#color/colorAccent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<View
android:id="#+id/position_dot"
android:layout_width="8dp"
android:layout_height="8dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#color/colorPrimary"
android:layout_marginLeft="292dp"
android:layout_marginTop="292dp"/>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
</layout>
and this is the method I use to update the margins:
fun moveDotToCurrentPosition() {
var params = positionDot.layoutParams as RelativeLayout.LayoutParams
params.topMargin = 292
params.leftMargin = 292
positionDot.layoutParams = params
}
I tried to keep the code short and limited to what's relevant, if something important is missing please let me know and I put it in.
I'm writing this one in Kotlin, but Java answers will be helpful too.
Thank you.
Almost all Java-code dimension values that are just raw int or float use px units, not dp. It is very likely that you are executing your code on an hdpi device. If so, your screen density means that 1dp == 2px, and this would explain why everything seems to be "divided by 2".
You can find your current screen density with code like this:
float density = [context].getResources().getDisplayMetrics().density;
And you can then multiply any dp values by this density to get the px values.
Alternatively, if you're working with dimensions that you've stored as <dimen> resources, you can just call:
int dimen = [context].getResources().getDimensionPixelSize(R.dimen.my_dimen);
The values you are using to update your margins are in px size
params.topMargin = 292
params.leftMargin = 292
If you want to change them to 292dp you should load a dimen resource or multiply the value for displayMetric density
You can use the anko dip function
fun Context.dip(value: Int): Int = (value * resources.displayMetrics.density).toInt()
Your code will look like this:
params.topMargin = dip(292)
params.leftMargin = dip(292)
Maybe this can fix your issue.

How to set a textView width dynamically larger than the width of the screen after animation?

I have a text view with a height which wraps content and a width that matches parent. So that the textView width == the screen Width.
But at one point I want the text to rotate 90 degrees.
Now I want to be able to change the views width so that it is the devices height instead of width.
This would cause that the width would expand.
(Basically like when one does orientation changes, but I can´t just have an orientation change for only one textview so I have to rotate it.)
My problem: I can´t set the textViews width larger than the device width.
Even when I am already done with the animation.
So is it possible to make the textView width larger than the device width?
and if not can anyone please suggest how I could solve my problem because I really need to change the orientation of the textView...
EDIT----EDIT
Is there a way to create a landscape text view and put it in a portait activity? That would completly solve my problems of the last week...
EDIT
My fragment:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/f"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/t"
/>
</RelativeLayout>
I tried different things like
fragment.setRotation(90);
LayoutParams params = fragment.getLayoutParams();
params.height = text.getHeight();
params.width = deviceHeight;
fragment.setLayoutParams(params);
LayoutParams params1 = text.getLayoutParams();
params1.height = text.getHeight();
params1.width = deviceHeight;
text.setLayoutParams(params1);
EDIT________EDIT
Or has anyone ever written something like a verticalTExtView Class?
You can set the attributes of TextView as android:singleLine="true" and android:layout_width="wrap_content" and android:layout_height="wrap_content" // incase if you do not want the height to be increashed so singleLine is making the text to go out of screen width then you will need ScrollView for such behavior.
Hope this helps.

Android textView size is bigger than dynamically created one

I got some issue with dynamically created TextViews. To be more specific:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some text"
android:textColor="#color/black"
android:textSize="30px" />
appears much larger than:
TextView prName = new TextView(this);
prName.setTextColor(getResources().getColor(R.color.black));
prName.setText("Some text");
prName.setTextSize(TypedValue.COMPLEX_UNIT_PX, 30);
How to made them equal? Thanks beforehand
For text you should use scale points (SP) instead of pixel.
For xml:
android:textSize="30sp"
For code:
prName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
Set height and width as wrap_content for your textview.
LinearLayout.LayoutParams Params1 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
prName.setLayoutParams(Params1);
Make sure when you setTextSize for any type of view, you should set it in scalable points (sp) and not in pixels (px) like this:
In xml:
android:textSize="18sp"
In code:
prName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
Using scalable points will let your TextView text size be equal on all devices, while using pixels will let your TextView text size be unequal on devices with different resolutions.
use setTextSize(int unit, float size)
TypedValue.COMPLEX_UNIT_PX //Pixels
TypedValue.COMPLEX_UNIT_SP //Scaled Pixels
TypedValue.COMPLEX_UNIT_DIP //Device Independent Pixels
In here just set
prName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
Remove this line:
prName.setTextSize(TypedValue.COMPLEX_UNIT_PX, 30);
... or use this instead:
prName.setTextSize(30);

My button doesn't shows up with its background image's size When i using WRAP_CONTENT

Here is my Button
convertView = mInflater.inflate(R.layout.guide_view_image_item, null);
viewHolder = new ViewHolder();
viewHolder.myButton = (Button) convertView.findViewById(R.id.btn_goto_loginview);
Here is guide_view_image_item.xml with btn_goto_loginview inside
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/imgView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/ic_delete" >
</ImageView>
<Button
android:id="#+id/btn_goto_loginview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/skep_button_in_guide_page"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"/>
</RelativeLayout>
skep_button_in_guide_page is a 182*182 PNG(32-bit color)6.10Kb Image
my program works fine, but the myButton just shows up too big!(my ScreenWidth is 480 and this button's width coverd half of my ScreenWidth)
Then i use
RelativeLayout.LayoutParams myButtonLayout = (RelativeLayout.LayoutParams) viewHolder.myButton.getLayoutParams();
and i find that both myButtonLayout.width & myButtonLayout.height equals to -2, I think maybe it means WRAP_CONTENT. Then i use
BitmapFactory.Options buttonOptions = new BitmapFactory.Options();
imageOptions.inJustDecodeBounds = true;
BitmapFactory.decodeResource(EngageApp.getIns().getResources(), R.drawable.skep_button_normal, buttonOptions);
Then i find that buttonOptions.outWidth & buttonOptions.outHeight both equals to 273
so i think it's the actual(real) width which is shown on the screen.
It's strange, where does 273 comes from? it should be 182..
Finally, I have to use this:
RelativeLayout.LayoutParams myButtonLayout = (RelativeLayout.LayoutParams) viewHolder.myButton.getLayoutParams();
myButtonLayout.width = 182;
myButtonLayout.height = 182;
viewHolder.myButton.setLayoutParams(myButtonLayout);
enforce set the width&heigth to 182
Then, Everything is ok, myButton got smaller which is my exact purpose.
But I dont think It's the right way to solve this problem.
Anyone can help me?? Thanks in advance.
Maybe my description is confusing, Let me make it more clear.
First, My Image is 182 width and i use WRAP_CONTENT, why my button doesn't shows up with 182 width on a 480 width screen directly.
Second, why i got 273 when i using 'decodeResource'? 273 is not the width of the image(182 is,i don't even know where this number comes from), but it is the width that the button is actually showing. Help me explain this.
Enforcing it to 182 is wrong but there is nothing wrong with setting the layout params.
it's 182 for the 480 width what if the width changes to 320 or say 720 you should try
private void getTheDisplay() {
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
width = dm.widthPixels;
hieght = dm.heightPixels;
}
RelativeLayout.LayoutParams myButtonLayout = (RelativeLayout.LayoutParams) viewHolder.myButton.getLayoutParams();
myButtonLayout.width = ((182*width)/480); //---got the ratio 182/480 and got the exact width for other screen size as well
myButtonLayout.height = ((182*height)/854);
viewHolder.myButton.setLayoutParams(myButtonLayout);
it will scale it up automatically for every screen size
To Answer your question in comment
the 273 width come from the pixel to dp conversion this is how it works
Size you see=(actual Size of image * Your screen pixel Density) /160
which in your case is (182*240)/160=273

Categories

Resources