Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting a textview in the middle of a imageview android

I need to put a text view in the middle of a imageview so i have this code

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/row_background"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<TextView
    android:id="@+id/menu_label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:lines="1" />

<ImageView
    android:id="@+id/menu_icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerInParent="true"
    android:layout_marginBottom="@dimen/menu_icon_margin_top_bottom"
    android:layout_marginRight="@dimen/menu_icon_margin_right"
    android:layout_marginTop="@dimen/menu_icon_margin_top_bottom" />

<TextView
    android:id="@+id/notif_number"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@id/menu_icon"
    android:layout_alignLeft="@id/menu_icon"
    android:layout_alignRight="@id/menu_icon"
    android:layout_alignTop="@id/menu_icon"
    android:lines="1"
    android:text="69"
    android:textColor="@android:color/white" />

and i need the last textview to be always in center of the menu_icon ImageView but my methdot doesnt work it just puts it in the top left corner.

like image 445
user924941 Avatar asked Feb 02 '13 20:02

user924941


People also ask

How to set imageview in edittext while entering text?

This example demonstrate about How to set ImageView in edittext while entering text. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml.

How to add icons for textview Android?

Adding Icons for TextView Android also allows adding drawable with the text views. There are three positions to add the icons for the TextView. They are a start, end, top, and bottom. Refer to the following code and its output, to know how to add the drawable icons to the Text View.

How to create custom listview with image and text in Android?

Create listView with image inside at left side with textview implement in each row. Android custom listview with image and text is most commonly used list view for all android apps.

How to add an image file to an imageview in Android?

Whenever ImageView is added to an activity, it means there is a requirement for an image resource. Thus it is oblivious to provide an Image file to that ImageView class. It can be done by adding an image file that is present in the Android Studio itself or we can add our own image file.


3 Answers

As your ImageView not fills all RelativeLayout you cant achieve desireable effect. I suggest you to place ImageView and TextView in FrameLayout.

<FrameLayout
    android:layout_width = "wrap_content"
    android:layout_height = "wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerInParent="true">

 <TextView
    android:id="@+id/menu_label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:lines="1" />

 <ImageView
    android:id="@+id/menu_icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginBottom="@dimen/menu_icon_margin_top_bottom"
    android:layout_marginRight="@dimen/menu_icon_margin_right"
    android:layout_marginTop="@dimen/menu_icon_margin_top_bottom" />
</FrameLayout>
like image 175
Leonidos Avatar answered Sep 23 '22 16:09

Leonidos


You just need to add:

android:layout_centerInParent="true"

to your last TextView

like image 32
Damian Avatar answered Sep 24 '22 16:09

Damian


FrameLayout is the best way to go. You can use a RelativeLayout also, btw.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="40dp"
    android:layout_height="40dp">

    <ImageView
        android:id="@+id/marker_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        tools:src="@drawable/ic_marker_selected" />

    <TextView
        android:id="@+id/marker_title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:textColor="#fff"
        android:textSize="14sp"
        tools:text="A" />

</FrameLayout>
like image 28
div Avatar answered Sep 23 '22 16:09

div