How to implement horizontal line with custom textview in android? - android

I want to implement two horizontal line side by side and a custom textview in between those lines (exactly like the pic shown below)
I wrote the following code to implement the horizontal line
<View
android:id="#+id/separator"
android:layout_width="fill_parent"
android:layout_height="3dip"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:background="#color/green" />
Plz help me to create that custom text showing "OR" in between those lines. what should be the design approach? shall i use an imageview ?

I would suggest creating it as an image resource, and using an imageview instead of a textview. It would make things much easier for you.
I would suggest creating a medium density and high density version of the image though and placing them in the appropriate folders in your res folder.
/res/drawable-mdpi (put medium density image here)
/res/drawable-hdpi (put high density image here)

Simply use following with TextView
drawable_or_bg.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="0dp"
android:shape="ring"
android:thicknessRatio="2"
android:useLevel="false" >
<solid android:color="#YOUR_COLOR" />
</shape>
set it as background to TextView
Don't use image approche As image required for every density it will increase size of APK by atleast some bytes

Related

Image doesn't resized even if size is defined in XML (Android Studio)

I want to make button with image like this:
This button has image above its text and image is resized from 512x512.
When I searched google, there were some methods to achive this. But I thought putting Text and Image inside LinearLayout and register onClick doesn't look nice so I decided to use drawableTop instead.
<Button
android:id="#+id/btn_gps"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="8dp"
android:drawableTop="#drawable/main_icon_gps"
android:padding="20dp"
android:text="#string/main_btn_gps"
android:visibility="visible"
app:layout_constraintBottom_toTopOf="#+id/guideline_h60"
app:layout_constraintEnd_toEndOf="#+id/guideline_v50"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/guideline_h40" />
Original image is 512x512 so I made another XML file(main_icon_gps) inside drawable directory.
<!-- #drawable/main_icon_gps -->
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="#drawable/drawable_gps"
android:width="64dp"
android:height="64dp"
/>
</layer-list >
It looked nice when inspecting in Android Studio preview but when I launched the app in AVD, image resize doesn't work properly.
To sum up, I tried to set image size with android:width and android:height inside separate XML drawable file and it worked nicely in Android Studio preview. But Android doesn't respect width and height that I defined in XML drawable file. How to make Android respect the width and height that I've defined at XML?
I have to use same image as different size so resizing actual image or have multiple image don't seem to be best idea.
Nesting things inside LinearLayout isn't good idea to me but if I have to achive what I want to make, then I will use that method. Before that, I want to fix the problem that image doesn't get resized.
You can use MaterialButton and iconSize attribute.
or use VectorDrawable instead of png image, in this way you can set width and height in your xml file, like this:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="40dp"
android:height="40dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#fff"
android:pathData="M10,20v-6h4v6h5v-8h3L12,3 2,12h3v8z" />
</vector>

Drawable resolution in custom widgets

Good day, I am a little confused and sorry if it is dublicated, but I cant find correct answer. My task is to create widget (RadioButton, RatingBar) with custom drawables + make them high quality and not blur.
Here comes my problems and what I have tried. So, for example, I have custom drawable for radio buttons:
radioButtonArray[i].setButtonDrawable(R.drawable.radio_button_selector);
radio_button_selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/rb_unchecked" android:state_checked="false"/>
<item android:drawable="#drawable/rb_checked" android:state_checked="true"/>
</selector>
Now the problem is in quality of drawables. If I will add rb_unchecked.png and rb_checked.png in different folders (hdpi,mdpi,xhdpi...) with different px size, then my radio buttons will be enourmous big on some phone screens. Ok, next thing that I tried was to set programmatically width and height of my radio buttons, but found that it is hard and unefficient (for example, I have the same situation with RatingBar and I cant find how to set custom width and height of items in it). Another solution that I tried is to add rb_unchecked.png and rb_checked.png only to drawable folder with size of 18px*18px and, on the one hand it solved the problem, size is correct, but the radio buttons now are low resolution and kind of blur.
My quastion is what I am doing wrong? I expected that on devices with different dp android will take specific image in hdpi/xhdpi folder and scale it to specific size, but instead of this images from specific folders just wrap_content | crop in my custom widget?
I found the reason why my radiobutton drawable was enourmous big, I used following layout:
<RadioButton
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="30dp"
android:layout_height="30dp"
android:button="#drawable/radio_button_selector"/>
The right aproach:
<RadioButton
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#drawable/radio_button_selector"
android:button="#android:color/transparent"/>

Android Overlaying images in layer-list

I am trying to create a custom drawable which includes a 9-patch background image (named custom_bg below) and a regular png image (accordion_up). The latter should not be scaled.
I tried several variations of something along the lines of:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/custom_bg"/>
<item
android:layout_width="10dp"
android:layout_height="10dp"
android:drawable="#drawable/accordion_up"
android:gravity="right" />
</layer-list>
Tried different values for layout_width/layout_height and also width/height but all I get is the accordion image is stretched to fill the entire view. I would like it to float at the top right of the view instead, scaled only to match the dpi of the device, but not expanded to fit the view. How can I do that?

Replacing existing drwable in imageview is wrong scaled on some devices

The Situation:
I have a imageview with a layer list of two drwables.
The frame drwable is 800x400px and the cover drawable is 800x380px.
Both images reside in the folder drawable
<ImageView
android:id="#+id/preview_img_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.9"
android:adjustViewBounds="true"
android:src="#drawable/result_preview"/>
The Drwable layer list in drawable/result_preview
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/cover">
<bitmap
android:gravity="left|top"
android:src="#drawable/cover" />
</item>
<item
android:id="#+id/frame">
<bitmap
android:gravity="left|top"
android:src="#drawable/frame" />
</item>
</layer-list>
This setup works as expected the cover is displayed framed on all devices. Now the user can replace the sample cover with another cover of the same size.
Replacing the cover in the layer list and leave the frame as is.
LayerDrawable layer = (LayerDrawable) getResources().getDrawable(R.drawable.result_preview);
InputStream coverIs = getContentResolver().openInputStream(Uri.parse(coverUri));
this.drwCover = (BitmapDrawable) BitmapDrawable.createFromStream(coverIs, coverUri);
drwCover.setGravity(Gravity.LEFT | Gravity.TOP);
//drwCover.getBitmap().setDensity(160);
layer.setDrawableByLayerId(R.id.cover, drwCover);
imageView.setImageDrawable(layer);
The Problem:
Replacing the current cover with a new one (same dimensions as old) produces different results depending on the Device used.
On a device with a 3.2 screen and a 480x320 resolution the cover replaced fits in the frame. On a device with a 3.7 and a 800x480 resolution the replaced cover is displayed smaller then the old one.
What I found out is that the drwable in the imageview on the small device has a intrinsic height of 800x400 same as the dimensions of the drawable.
On the bigger screen the intrinsic height of the drawable is 30% bigger.
I know the the intrinsic values may differ from screen to screen.
What I was expecting is that the drawable that replaces the old one should will be scaled up the same way the old one was to +30%. But this did not happen.
Question:
Is there a option to tell the imageview or the layer list to Adpt itself? I think there should be a way to do so because the system did it already at the beginning.
First, I'd suggest doing the cover/frame differently:
make your frame a nine-patch drawable, so you can define in the nine-patch the padding that will remain visible when the cover is drawn on top of it.
put the frame drawable as a background
set the cover as the src of the image, and not the layer list
don't forget to set a scaleType for your ImageView, play with the different options, so suit your needs.
<ImageView
android:id="#+id/preview_img_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.9"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:background="#drawable/frame"
android:src="#drawable/cover"/>
For other having the same problem. 9patch png is a solution to a problem I don't have.
So far the only thing that came close to solve the problem was to put my resources into the folder drawable-nodpi. Here is the reference to the docs

Android ignores scrollbarsize

I'm trying to modify a ListView scrollbar's width without success
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:choiceMode="singleChoice"
android:scrollbars="vertical"
android:scrollbarTrackVertical="#drawable/scrollbar_vertical_track"
android:scrollbarThumbVertical="#drawable/scrollbar_vertical_thumb"
android:scrollbarSize="4px"
android:clickable="true"/>
First I tried using a drawable image 4px wide, but the .png was resized. Then I tried using a shape extracted from SamplesApi, without success.
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:width="40px">
<gradient android:startColor="#505050" android:endColor="#C0C0C0"
android:angle="0"/>
<corners android:radius="0dp" />
I've tried with and without the android:width attribute.
There's a question on the same topic (Width of a scroll bar in android), but it doesn't try anything different that what I'm already trying. As far as I know, creating my own theme shouldn't change the output.
There's an example in SamplesApi (Views/ScrollBars). I tried modifying the scrollbarSize attribute without result.
I know about ninepatch images, but there's an attribute which should do what I want.
Any hint? Thanks in advance.
By my experience, Android ignores scrollbarSize, and computes size of both scrollbars from height of horizontal scroll track drawable (PNG image) which is set by android:scrollbarTrackHorizontal.
Did you tried
android:scrollbarSize="wrap_content"
and then just putting the size thru shape? I had kind of the same problem like this and I solved it in that way

Categories

Resources