Home > Net >  Why does text is not coming exactly to center even thought tried changing with Height & Width in And
Why does text is not coming exactly to center even thought tried changing with Height & Width in And

Time:11-23

Please see this expected image below, in screenshot it is looking big but I want to make it very small, for that you can see my textview dimensions..image

This is my output after spending whole day on this, but the problem is is not coming into center: enter image description here

Now this is my XML:

<?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">

    <data>

    </data>

    <androidx.cardview.widget.CardView
        style="@style/match_match"
        android:layout_margin="@dimen/dimen_16"
        android:background="@drawable/product_list_cardview_border"
        app:cardCornerRadius="@dimen/dimen_2">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="@dimen/dimen_16">
...
...
  <com.test.consumer.utils.CustomTextViewV2
                android:id="@ id/tv_item_plus"
                android:layout_width="12dp"
                android:layout_height="12dp"
                android:layout_marginEnd="@dimen/dimen_10"
                android:background="@drawable/shape_circle_style_item_default"
                android:text=" "
                android:textStyle="bold"
                android:gravity="center"
                android:textColor="@color/color_black"
                android:textSize="9sp"
                app:customFont="@string/font_raleway_semibold"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintEnd_toStartOf="@ id/tv_final_mrp" />

</androidx.constraintlayout.widget.ConstraintLayout>

    </androidx.cardview.widget.CardView>

Please note: I don't want to take any other nested view, the ideal solution fix should be: Textview should be inside parent constraint layout.

Background drawable:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <stroke
        android:width="@dimen/dimen_1"
        android:color="@color/color_moderate_gray_shade_one" />
    <solid android:color="@android:color/transparent" />
</shape>

Also, I tried with imageview as well!

CodePudding user response:

//Take relative layout as parent and inside that layout put image view.(Copy and 
paste this) 

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/circle">

<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:src="@drawable/ic_baseline_add_circle_outline_24"/>
   </RelativeLayout>
</LinearLayout>

CodePudding user response:

Using ShapeableImageView, we can give strokeWidth & strokeColor.

<com.google.android.material.imageview.ShapeableImageView
    android:id="@ id/siv_plus"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_add"
    android:padding="2dp"
    app:shapeAppearanceOverlay="@style/RoundedImageViewStyle"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:strokeColor="@color/black"
    app:strokeWidth="1dp" />

Add following lines in your themes.xml/style.xml

 <style name="RoundedImageViewStyle" parent="Theme.MyApplication">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">12dp</item>
</style>

Replace 'Theme.MyApplication' with your parent theme name.

See Output (screenshot.jpg)

  • Related