Android: create TextView at the bottom of a LinearLayout dynamically - android

I created an app where clicking a button I create a TextView inside a LinearLayout(hosted inside a ScrollView).
When I click the button the TextViews are displayed starting from the top like this example:
First Image
http://imgur.com/8fhtYZs (Sorry I don't have reputation to display it in the post so I inserted the link)
But my task is to create the textviews at the bottom of my LinearLayout and make the TextViews already created scroll up the new one. (Sorry for my bad english) I show an example to be more comprehensible.
Second Image
http://imgur.com/RLHooaH
This is my code:
public class Messaggi2 extends ActionBarActivity implements OnClickListener {
LinearLayout mLayout;
ScrollView scroll;
Button invia;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.messaggi2);
mLayout = (LinearLayout) findViewById(R.id.LinearScrollLayout);
scroll = (ScrollView) findViewById(R.id.scrollView2);
invia = (Button) findViewById(R.id.Invia);
invia.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
mLayout.addView(createNewTextView("Message"));
}
private TextView createNewTextView(String text) {
final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final TextView textView = new TextView(this);
textView.setLayoutParams(lparams);
textView.setText(text);
textView.setBackgroundResource(R.drawable.balloon_broadcast_incoming_pressed);
return textView;
}
}
And this is my Layout.XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ScrollView
android:id="#+id/scrollView2"
android:layout_width="fill_parent"
android:layout_height="640dp"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin" >
<LinearLayout
android:id="#+id/LinearScrollLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/Messaggio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/balloon_broadcast_incoming_pressed"
android:text="#string/messaggi" />
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginTop="70dp"
android:background="#drawable/linear_layout_bg" >
<EditText
android:id="#+id/Scrivi"
android:layout_width="440dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="#string/scriviMessaggio" />
<Button
android:id="#+id/Invia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/send_btn" />
</LinearLayout>
</LinearLayout>

There is no such thing as LinearLayout.BOTTOM, nor can we add rule for LinearLayout.
But the good news is to insert into LinearLayout, one could decide the sequence.. Just use the addView function with 3 arguments as below
mLayout.addView(view, index, param);
The index determined the order.

You need to set your linear layout's height to the height of your ScrollView so instead of using wrap_content cause that is wrapping your linearlayout change it to match_parent
example:
<LinearLayout
android:id="#+id/LinearScrollLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
When you want to add the button at the bottom of the linearlayout just add a rule to your layout params.
example:
private TextView createNewTextView(String text) {
LinearLayout.LayoutParams layoutParams =
(RelativeLayout.LayoutParams) txt1.getLayoutParams();
layoutParams.addRule(LinearLayout.BOTTOM, 1);
final TextView textView = new TextView(this);
textView.setLayoutParams(lparams);
textView.setText(text);
textView.setBackgroundResource(R.drawable.balloon_broadcast_incoming_pressed);
return textView;
}

Related

how to set inflate layout center which added dynamically in linear layout Android

I am adding inflate layout dynamically in linear layout,i need to set layout center based on number of layouts.suppose if layouts are two , then it start showing from center.
following is my xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<HorizontalScrollView
android:id="#+id/hsv_category_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:scrollbars="none">
<LinearLayout
android:id="#+id/ll_placeHolder"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
and following are my class
public class ScrollTest extends Activity {
HorizontalScrollView hsv_category_list;
LinearLayout ll_placeHolder;
View layoutView[];
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scroll_test);
hsv_category_list = (HorizontalScrollView)
findViewById(R.id.hsv_category_list);
ll_placeHolder = (LinearLayout) findViewById(R.id.ll_placeHolder);
layoutView = new View[2];
for (int i = 0; i < 2; i++) {
layoutView[i] =
LayoutInflater.from(this).inflate(R.layout.category_list_item, null);
layoutView[i].setId(i);
View parent = layoutView[i].findViewById(i);
TextView tvTime = (TextView)
parent.findViewById(R.id.tv_arrival_time);
tvTime.setText("" + i);
ll_placeHolder.setGravity(Gravity.CENTER);
ll_placeHolder.addView(layoutView[i]);
}
}
}
i need to set the inflate layout start from center.
Please help me to solve this.Thank you.
Set layout gravity to center_horizontal in xml file like this
<LinearLayout
android:id="#+id/ll_placeHolder"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:gravity="center_horizontal"
android:layout_gravity="center"
android:orientation="horizontal">
</LinearLayout>

LinearLayout does not expand when adding edittext programmatically

So I am trying to add EditText to LinearLayout programmatically. I have added a '+' ImageButton on whose click, I will add an EditText until there are five. Below is a snippet of my XML file.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/margin_16"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
</LinearLayout>
<ImageButton
android:id="#+id/add_field"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="#dimen/margin_16"
android:background="#drawable/cyan_top_rounded"
android:src="#drawable/add" />
</LinearLayout>
The LinearLayout with id "Container" will have EditText as child. Below is my code for adding EditText.
mAddField.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (count < 5) {
mAddField.setEnabled(true);
mContainer.addView(getTextField());
count++;
} else {
mAddField.setEnabled(false);
}
}
});
private EditText getTextField() {
EditText et = new EditText(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
et.setLayoutParams(params);
et.setText("abcd");
return et;
}
private void initUI() {
mAddField = (ImageButton) findViewById(R.id.add_field);
mContainer = (LinearLayout) findViewById(R.id.container);
EditText et = new EditText(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
et.setLayoutParams(params);
et.setText("abcd");
mContainer.addView(et);
}
However, when I click on the ImageButton, nothing happens. If the EditText is added at all, the LinearLayout also does not expand. Please help.
Try this:
...
<LinearLayout
android:id="#+id/container"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
</LinearLayout>
...
Change ur layout to this it will work fine:(Make the necessary changes according to ur own). Problem is with the height and width of the outer as well as the inner LinearLayout.
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
</LinearLayout>
<ImageButton
android:id="#+id/add_field"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="#drawable/image" />
</LinearLayout>
The view in which you what to add your Edittext dynamically,you have to set it width and height as "wrap_content". So just change your internal layout to :
<LinearLayout
android:id="#+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
I think this will help you out.

Android place view dynamically relative view

Similar to a to do app. I have a top bar that is set by the xml file and at run time I would like to create some textviews which go directly under it. Also there will be a button that adds new text views when clicked.
When I create a new textview from the button how do I make sure that the new Textview stays under the static textviews and/or layouts that I have created. Right now it's not and it's just overlapping.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
Button btn = (Button) findViewById(R.id.Add_new);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AddNewTask(v);
}
});
}
I tried both options to add a new textview and both of them just overalpp and I can't find any documentation on a method such as under/below, most I can find are just alight top and align bottom which wouldn't be useful in this case
public void AddNewTask(View v) {
RelativeLayout rl = (RelativeLayout) findViewById(R.id.overallview);
TextView tv= new TextView(this);
tv.setText("TEST");
rl.addView(tv);
}
My XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/overallview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.robertli.hustle.MainActivity"
tools:showIn="#layout/activity_main"
android:background="#00095e">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:id="#+id/topbar"
>
<TextView
android:id="#+id/app_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="2"
android:text="Hustle"
android:textSize="25dp" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:layout_below="#id/topbar"
android:id="#+id/Add_new"
android:background="#00095e"
android:textColor="#ffffff"
android:clickable="true"
android:textSize="50dp" />
Suggestion : You should take a look at ListView to do this !!! ListView help you dynamically add the textview as well.
if you still want to adding directly to Relativelayout
refer this example:
RelativeLayout relativeLayout = new RelativeLayout(context);
View view = new View(context);
relativeLayout.addView(view);
code to dynamically add alignment in RelativeLayout
RelativeLayout.LayoutParams viewLayoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
viewLayoutParams.addRule(RelativeLayout.BELOW, R.id.your_view);
take a look at documentation for more
https://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html

Adding items to LinearLayout (and resizing)

I have the following layout
<?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="horizontal"
android:background="#f00"
android:id="#+id/main_layout">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#0f0"
android:id="#+id/bed_list"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00f"
android:text="Howdy cowboy?"
android:textSize="50dip"
android:textColor="#fff"
android:id="#+id/textview"/>
</LinearLayout>
I want to add some textviews to R.id.bed_list (as if they were vertical tabs)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
LinearLayout bedList = (LinearLayout) findViewById(R.id.bed_list);
for (int i = 0; i < 12; i++) {
System.out.println("In the loop");
TextView textView = new TextView(this);
textView.setText("HJM-" + i);
textView.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
textView.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
bedList.addView(textView,i,
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
}
}
So far, the code is run, the System.out.println messages in the loop appear at the console, but I only see the textview (R.id.textview). I wanted the list to appear at the left of the screen with the options calculated, but it does not work.
Further on, if I define a first textview in the label (so that there is some items at the very beginning and a space is reserved), I get to see that textview but the others (added from the program) do not appear either.
Your textview (R.id.textview) has a width of match_parent, effectively pushing your other views (the linearlayout) off screen. Instead you can give it a width of wrap_content and possibly a layout_weight of 1.

Add different TextViews to ScrollView

I Have activity that get some data from the internet, and shows it to the screen.
I'm using scroll view cause it's long text, I also want different text style for a different data,so I use few textViews with a different style and to show it on the Activity screen,
my problem is that scroll view can handle only one view, so how can I use scrolling to show different style of Text view, I tried to add LinearLayout to the scrollView and add all the textViews dynamically in code to this LinearLayout ,but I'm getting exception - scroll view can host only one direct child.
The code below:
/** this is the function, which called from the onClick method.
wanted data object contains 2 strings title message and the message itself.
When debug the code i can see that there's two String values in each loop.
but i cant add the linearLayout to my scrollView - exception ScrollView can host only one direct child */
private void showResult(ArrayList<WantedData> result) {
// TODO Auto-generated method stub
TextView title;
TextView data;
scrollLayout = (LinearLayout) findViewById(R.id.LlScrollView);
for (WantedData curr : result) {
if (curr.getTitle() == null) {
break;
}
title = new TextView(this);
title.setText(curr.getTitle());
scrollLayout.addView(title, LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
data = new TextView(this);
data.setText(curr.getData());
scrollLayout.addView(data, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
}
scroll.addView(scrollLayout, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
//at the onCreate method - scroll = (ScrollView) findViewById(R.id.SvShowTextFromServer);
}
the xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<include
android:id="#+id/layout_reffernce"
layout="#layout/explore" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter City" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="#+id/EtCity"
android:layout_width="210dp"
android:layout_height="wrap_content"
android:layout_weight="0.14"
android:orientation="vertical" >
<requestFocus />
</EditText>
<Button
android:id="#+id/bSearchCity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search" />
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter State" />
<EditText
android:id="#+id/EtState"
android:layout_width="253dp"
android:layout_height="wrap_content"
android:orientation="vertical" />
</LinearLayout>
<ScrollView
android:id="#+id/SvShowTextFromServer"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/LlScrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/backround"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</LinearLayout>
The problem is double creating of container in ScrollView. You should not create it in activity, but take already defined from xml:
LinearLayout scrollContainer = (LinearLayout)findViewById(R.id.LlScrollView);
for (...) {
//create here some text
scrollLayout.addView(text);
}
If you have defined a LinearLayout in XML you don't have to create a new LinearLayout in your code but you have to retrieve the existing one in this way
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.LlScrollView);
Otherwise you have to remove the LinearLayout in your XML and add all by code.

Categories

Resources