How can I get the top blank when I add textview programmatically?

I am making an application like whatsup. My question is about the interface. I can get the desc message and analyze it. After that, I can add the layout programmatically, but it writes the same coordinate all the time. I tried tv.layout(l, t, r, b) and tv.setTop but did not work.

Where is my problem?

  bubbleLayout=(RelativeLayout)findViewById(R.id.layoutbubble); for(int i=0;i<msgid.length;i++){ RelativeLayout relativeLayout = new RelativeLayout(this); RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT); // Creating a new TextView TextView tv = new TextView(this); tv.setText(msgdesc[i]); tv.setTop(20); // Defining the layout parameters of the TextView RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT); // Setting the parameters on the TextView tv.setLayoutParams(lp); // Adding the TextView to the RelativeLayout as a child bubbleLayout.addView(tv); 

My xml file:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="fill_parent" android:orientation="vertical" > <RelativeLayout android:layout_width="1300dip" android:layout_height="80dip" android:background="#084B8A" > <TextView android:id="@+id/messagetitle" android:layout_width="wrap_content" android:layout_height="90dip" android:layout_centerInParent="true" android:layout_marginTop="30dip" android:textColor="#ffffff" android:textSize="12pt" /> </RelativeLayout> <RelativeLayout android:id="@+id/layoutbubble" android:layout_width="1300dip" android:layout_height="500dip" android:layout_marginTop="10dip" > </RelativeLayout> <RelativeLayout android:layout_width="1300dip" android:layout_height="80dip" android:layout_marginTop="10dip" > <EditText android:id="@+id/replytext" android:layout_width="1000dip" android:layout_height="90dip" android:layout_marginLeft="10dip" /> <Button android:id="@+id/sendbutton" android:layout_width="200dip" android:layout_height="90dip" android:layout_marginLeft="1050dp" android:background="#084B8A" android:text="Send" android:textColor="#ffffff" android:textSize="12pt" /> </RelativeLayout> </LinearLayout> 

Screen shot

+4
source share
2 answers

I solved my problem. Added RelativeLayout to LinearLayout in xml

 <LinearLayout android:id="@+id/layoutbubble" android:layout_width="1300dip" android:layout_height="500dip" android:layout_marginTop="10dip" android:orientation="vertical"> </LinearLayout> 

And change my code as follows:

 bubbleLayout=(LinearLayout)findViewById(R.id.layoutbubble); for(int i=0;i<msgid.length;i++){ // Creating a new TextView TextView tv = new TextView(this); tv.setText(msgdesc[i]); tv.setPadding(30, 10, 0, 0); bubbleLayout.addView(tv); } 

That's all.

+3
source

I think you are using RelativeLayout or regular LinearLayout?

Try using LinearLayout with android:orientation="vertical"

+2
source

All Articles