1

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?

lisovaccaro
  • 32,502
  • 98
  • 258
  • 410

2 Answers2

2

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); 
Prasad
  • 240
  • 3
  • 7
  • I just tried it, anyway I know how to do it in XML. However I'm creating both things dynamically and don't know how to set the TextView to the bottom of the container. (What would be `android:layout_alignBottom="@+id/myButton"` in xml) – lisovaccaro Jan 30 '13 at 22:18
  • I need to put the label at the bottom of the layout. That's my only problem right now. – lisovaccaro Jan 30 '13 at 22:52
-1

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"

jcw
  • 5,132
  • 7
  • 45
  • 54
  • I know how to do that on xml, I just do `android:layout_alignBottom="@+id/myImageView"` however I'm creating both things dynamically – lisovaccaro Jan 30 '13 at 22:17
  • I hope Framelayout is not deprecated – Prasad Jan 30 '13 at 22:20
  • @Liso22 I have not created a layout using layout params before, but if you can set the layout_gravity of the view, then use the frame layout and give your text view a gravity within the layout – jcw Jan 30 '13 at 22:22
  • @Prasad - maybe it is not fully depreciated, but I believe that developers were encouraged at some point to use relative layouts instead – jcw Jan 30 '13 at 22:24
  • How do I change the alignBottom programatically? – lisovaccaro Jan 31 '13 at 06:59
  • @Liso22 Look here, at the addRule method. It should help(but you will need to play around with the attributes a little bit) - http://stackoverflow.com/questions/2305395/laying-out-views-in-relativelayout-programmatically – jcw Jan 31 '13 at 07:08
  • `lp.addRule(RelativeLayout.BELOW, button.getId());` did the trick – lisovaccaro Jan 31 '13 at 07:36