lisovaccaro
lisovaccaro

Reputation: 33946

Add TextView on top of Button?

I'm building a contacts app which displays big contact photos. I need to add a label with the contacts name of each contact on top of the button (near the bottom) however I don't know how to get two views on top of each other. I cannot simply use settext since I need to add a semi-transparent background to the label.


EDIT: I managed to get it on top but I cannot figure out how to get it on the bottom of the button.

RelativeLayout icon = new RelativeLayout(context);
// Create button
Button button = new Button(context);
button.setLayoutParams(new    LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
layout.addView(button);
// Create label
TextView label = new TextView(context);
label.setText(name);
label.setBackgroundColor(Color.argb(120, 0, 0, 0));
label.setLayoutParams(new      LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
icon.addView(button);
icon.addView(label);

However the text appears on the top of the image and I want it to be on the bottom like this:

enter image description here

With xml this would be something like: android:layout_alignBottom="@+id/myButton" but I'm doing this programatically and I haven't found a way to do it. How can I place my label near the button?

Upvotes: 1

Views: 8598

Answers (2)

Prasad
Prasad

Reputation: 240

Try this

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center" >

        <Button
            android:id="@+id/button1"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:text="John"
            android:background="@drawable/button_action_active" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="test textView" />

    </FrameLayout>

</LinearLayout>

Sample screen shot for above code

Dynamically you can do by

 TextView lable = new TextView(this);  
        lable.setLayoutParams(new LayoutParams(android.widget.LinearLayout.LayoutParams.WRAP_CONTENT, android.widget.LinearLayout.LayoutParams.WRAP_CONTENT));  
        lable.setTextSize(25);  
        lable.setGravity(Gravity.CENTER);  
        lable.setText("John");  


        Button button = new Button(this);  
        button.setBackgroundResource(R.drawable.button_action_active);  
        button.setLayoutParams(new LayoutParams(android.widget.LinearLayout.LayoutParams.MATCH_PARENT, android.widget.LinearLayout.LayoutParams.WRAP_CONTENT));  

        FrameLayout fl = new FrameLayout(this);  
        fl.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));  
        fl.addView(button);  
        fl.addView(lable);  

        setContentView(fl); 

Upvotes: 2

jcw
jcw

Reputation: 5162

To do this you can use a frame layout. The documentation for frame layout can be found here - http://developer.android.com/reference/android/widget/FrameLayout.html

However, frame layout is depreciated (if I remember correctly). So instead I would recommend using a relative layout. In your relative layout you can set the position of the button and then give the textview the attributes android:layout_alignLeft=@id/somethingand android:layout_alignRight="@id/Something"

Upvotes: -1

Related Questions