I think the solution will be quite obvious, but as long as I'm rookie in android forgive me :)
I want to place some picture on the right upper side of the view, and a textview, which will fit the rest of the view. Here's what I've done:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/logo_in_listing"
android:layout_width="100dip"
android:layout_height="100dip"
android:layout_alignParentRight="true"
android:layout_marginRight="11dip"
android:layout_marginTop="11dip"
android:background="#drawable/border_for_imageview"
android:src="#drawable/facebook_logo" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/someString"
android:id="#+id/textview1"
android:layout_toLeftOf="#id/logo_in_listing"
android:layout_margin="3dip"/>
</RelativeLayout>
Here's what I get:
My questions:
1) How to make the textview fit also the lower part of imageview? I don't want that space to be left unused.
2) As you see, I've used dips for placing ImageView. What would you recommend in this case? I mean, how to write in a way, that it will be same for all screen sizes? Or maybe dip is OK?
You have to use Spanned interface. Here's a good example
1) How to make the textview fit also the lower part of imageview? I
don't want that space to be left unused.
Flow around text isn't possible with currently available layouts. However you can display content as HTML in a WebView or even spanned-HTML in a TextView.
2) As you see, I've used dips for placing ImageView. What would you
recommend in this case? I mean, how to write in a way, that it will be
same for all screen sizes? Or maybe dip is OK?
Dip is for device independent pixels. On different screen sizes, 1 Dip will take different number of pixels, but visually, 1 dip will appear almost same to eye on all screens.
If You donĀ“t want to left space, You could use spannables. For example:
int TEXT_START = 0;
int TEXT_END = yourTextView.length();
Spannable span = (Spannable) yourtextview.getText();
span.setSpan(new AlignmentSpan.Standard(Alignment.ALIGN_CENTER), TEXT_START, TEXT_END,
0);
For Your second Question: use dp instead of dip.
Related
Is it possible to create a scaled TextView like this where the text itself is scaled in one direction? In the picture, the top half shows a basic TextView outlined in blue. The bottom half shows the same TextView after the scaling I'm trying to do. The height is the same but the width of the view has been cut in half.
I don't think that this is possible with a TextView using the default font. To accomplish the effect you are looking for you would probably have better luck creating and resizing an image or dynamically using a different font that has half the width per character.
I personally use the library autofittextview from grantland in my projects.
The usage is simple.
<me.grantland.widget.AutofitTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:maxLines="2"
android:textSize="40sp"
autofit:minTextSize="16sp"
/>
for example. The result is as follows.
I have an ImageView (human body) and buttons over this image.
Is it possible somehow easily create this in RelativeLayout for the most different screen sizes? Or should I create different layouts for different screen sizes?
How would you proceed? What do you recommend?
Have a look at the percent library.
It would be perfect for something like this, and would work for every device. You'd have to work out the percentage width, height, and margin for each PercentRelativeLayout relative to your person image.
Here's an example:
<android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
app:layout_widthPercent="25%"
android:layout_height="100dp"
app:layout_marginLeftPercent="5%"
android:background="#ff0000" />
</android.support.percent.PercentRelativeLayout>
and remember to
compile 'com.android.support:percent:23.0.0'
This is an example for an imageview with clickeable areas.
https://blahti.wordpress.com/2012/06/26/images-with-clickable-areas/
I hope it will helps to you.
Yes it is possible with Relative layout but its difficult.
for this you have to focus on
different images with perfect cutting.
put images in all drawable.
Create Relative layout and starting with image-view at one side to end side as reference wise.
Margin add in different dimen.xml
I have a TextView and an ImageView in a LinearLayout:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="72sp"
android:orientation="horizontal"
android:keepScreenOn="true" >
<TextView
android:id="#+id/instructionView"
android:layout_width="245sp"
android:layout_height="70sp"
android:textSize="18sp" />
<ImageView
android:contentDescription=""
android:id="#+id/ma_landmarkView"
android:layout_width="70sp"
android:layout_height="70sp"
android:layout_gravity="right" />
</LinearLayout>
In my Code, I set different Images to the ImageView (All 68x68 pixels size) with
myView.setImageDrawable(this.getResources().getDrawable(
R.drawable.mypicture));
The Problem is now, the Image is no more seen if the TextView gets other Text by .setText(...) and redraws itself. The Image also dissappears if other Views outside this LinearLayout change their size and get redrawn (e.g. an MapView that has been zoomed in/out).
If I set all .setText(...) from this TextView in comments //, the picture from the ImageView stays visible and doesn't disappear anymore.
But I don't want to go without that TextView...
I already hardcoded the Views heights and widths but that does not help.
Any Ideas ?
EDIT:
I just saw at testing, that by setting different Text to the TextView it can get a bigger width (even bigger than I stated in the *xml) if this TextView gets too width, the Image disappears. Maybe a work around solution works, that prevents the TextView from getting too width. Some1 knows how to?
Try .setBackgroundDrawable instead
You should change the different view's units to dp instead of sp. sp is usually used for the actual text size. So basically change the layout_width and layout_height to use dp units, and keep the textSize in sp units.
Please change these and re-run your application, and let me know if this changed the results. If it doesn't work, add some of your java code so I can better tell where the issue is.
I wanted to know if it was possible to create a custom TextView that centers text perfectly, no matter what the font is. That's my major issue right now, but I'm also wondering if it's possible to set the specific height using pixels so that the height would also be consistent.
This picture shows how different fonts are sized and centered. The longest black line in the picture is the middle of the white space. Letters in the picture are the same in every way except for the fonts. The text size is the same (text.setTextSize(TypedValue.COMPLEX_UNIT_PX, 450);), and they're all centered. I hope someone knows the answer to these questions! If you need any code from my app, just ask. Thanks!
EDIT: I am aware of android:gravity="veritcal_center", but that only works to an extent. What you see above is that implemented in the textview, but each font has a different center of gravity, so android:gravity="veritcal_center" wouldn't really make all of these center perfectly along the screen. I'm looking for a way to create a custom textView that somehow measures the height of text and centers it with those parameters. A suggestion by #vmironov in the comments works, but only if the textview has one character. I have not been able to mess around with his code, but I will when I get a chance and I'll post here if I find anything. Thanks!
A simple way to achieve what you want is to use following code:
<LinearLayout
android:layout_width="100dp"
android:layout_height="20dp"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="#string/your_text" />
</LinearLayout>
set the height and width values of LinearLayout fixed if you want to set the text to be alligned in center within constant height and width
Set the gravity of the view to "center_vertical". Something like :
android:gravity="center_vertical"
You should be able to center your text by applying a gravity attribute to the containing TextView.
In XML you would assign an attribute to your TextView, which would look like this:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="18sp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/some_text" />
If you do want to specify a particular size for your text, it is strongly suggested you use SP (Scaled Pixels) as your unit of measurement, and not Pixels. You could also set an Appearance Attribute for your TextView to control the size, which is also shown in the code example.
I have a RelativeLayout which currently has a fixed size. Widths, heights, margins, font heights of child views are specified so everything looks just right.
I now want to scale the layout (to fit screen size). The layout should scale as if it was a flat image, so everything gets smaller in proportion (fonts, margins etc.)
I made a simplified example, below. Scaled to 0.5, this would display the text "ONE QUARTER" with margin left 200dip and margin top 120dip.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/RelativeLayout01"
android:layout_width="1600dip"
android:layout_height="960dip"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView
android:text="ONE QUARTER"
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="400dip"
android:layout_marginTop="240dip"
></TextView>
</RelativeLayout>
Of course, I'm not asking anyone to help me hand code an algorithm to scale all these values: just wondering if there's some simple way to achieve this...
Thanks!
If you just want your app to look ok on another device then specifying things in dip and sp should do the trick.
If you actually want to shrink or expand the scale of your app on the same device then you would have to do it manually, perhaps using themes or styles.