Here is the result of my layout
My problem is that I don't want the distance between "new asset" ( witch shows when clicking on the button new asset) and "old asset" to be too long , "old asset" has to be just below the button and "new asset" should be placed just on top of old asset when clicking on the button .
Here is my xml code :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
<LinearLayout
android:id="#+id/contain"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.01"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:id="#+id/a"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Build a list of significant assets for the organization"
android:textSize="14sp"
android:textStyle="bold" >
</TextView>
<Button
android:id="#+id/addasset"
style="#style/btnStyleBlue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text=" + add new asset" />
<LinearLayout
android:id="#+id/newa"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="0.01"
android:orientation="vertical"
android:padding="10dp" >
</LinearLayout>
<LinearLayout
android:id="#+id/old"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="0.01"
android:layout_gravity="top"
android:orientation="vertical"
android:padding="10dp" >
</LinearLayout>
</LinearLayout>
</ScrollView>
and my java code :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.assets, container, false);
final LinearLayout newa = (LinearLayout) rootView
.findViewById(R.id.newa);
final LinearLayout old = (LinearLayout) rootView
.findViewById(R.id.old);
Button add_asset = (Button) rootView.findViewById(R.id.addasset);
add_asset.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TextView tv1 = new TextView(v.getContext());
tv1.setText("New asset ");
tv1.setTextSize(14);
tv1.setTypeface(null, Typeface.BOLD);
tv1.setPadding(0, 15, 0, 10);
newa.addView(tv1);
}
});
TextView tv = new TextView(rootView.getContext());
tv.setText("old asset ");
tv.setTextSize(14);
tv.setTypeface(null, Typeface.BOLD);
tv.setPadding(0, 15, 0, 10);
old.addView(tv);
return rootView;
}
First of all, I think you are misunderstanding weights. The parent layout should have a weightSum then give child Views weights to equal that some to distribute the amount of space they take up according to what you need.
Second, you have android:padding="10dp" in each LinearLayout so there will effectively be 20dp padding between each. Remove those if you don't want that space.
You have android:layout_height="match_parent" in what appears to be an empty LinearLayout. This looks like it could be removed.
I think the weight is probably what is giving you the most trouble. Remove that
However, I think I would use a RelativeLayout, you could have the two LinearLayouts still if you want, and specify android:layout_below="#+id/contain" to put the second LinearLayout below the first
Also note that fill_parent is deprecated and you should use match_parent instead
Change xml code of linear layouts to this
<LinearLayout
android:id="#+id/newa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp" >
</LinearLayout>
<LinearLayout
android:id="#+id/old"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:orientation="vertical"
android:padding="10dp" >
</LinearLayout>
Related
I have two layouts, i inflate one layout into another and want to display this inflated layout including textview as many times as condition is true . I set the textview value as value of i and also set the layout top margin. but it displaying value of i in one row only and rest are blank,also its setMargin() also not working.Below is what i have done yet.
public void displayRows(){
int n= 6;
for(int i=0;i< n;i++){
LinearLayout container = (LinearLayout)findViewById(R.id.layoutContainer);
View hiddenInfo = getLayoutInflater().inflate(R.layout.dynamic, container, false);
container.addView(hiddenInfo);
LinearLayout hiddenLayout = (LinearLayout)findViewById(R.id.dynamic);
TextView textView = (TextView) findViewById(R.id.text);
textView.setText("Text"+i);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 30, 0, 0);
hiddenLayout.setLayoutParams(layoutParams);
hiddenLayout.addView(textView);
}
container.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="#+id/layoutContainer"
android:animateLayoutChanges="true"
android:layout_gravity="center|top"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/li1"
android:layout_gravity="center"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:background="#color/colorAccent"
android:onClick="displayRows"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:textColor="#color/colorPrimary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:layout_gravity="center"
android:text="Rows"/>
</LinearLayout>
</LinearLayout>
dynamic.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/dynamic"
android:background="#color/colorAccent"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="wrap_content">
<TextView
android:id="#+id/text"
android:text=""
android:textColor="#color/colorPrimary"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
I also want to know how to remove all the generated dynamic rows on click remove button.
Replace
LinearLayout hiddenLayout = (LinearLayout)findViewById(R.id.dynamic);
TextView textView = (TextView) findViewById(R.id.text);
with
LinearLayout hiddenLayout = (LinearLayout) hiddenInfo.findViewById(R.id.dynamic);
TextView textView = (TextView) hiddenInfo.findViewById(R.id.text);
And remove this line
hiddenLayout.addView(textView);
I am trying to add multiple LinearLayouts into one declared in xml. Everyone has 3 textView which will be edited in code. My problem is, when i am inflating xml layout to View object in code, all margins are ignored. My second question:
How can i dynamically set ids to textViews and then edit text in it?
LinearLayout xml which is inflating:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/pointsAwaiting"
android:layout_marginTop="40dp"
android:background="#drawable/background_blue"
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:textColor="#FFFFFF"
android:textSize="20dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:textColor="#FFFFFF"
android:textSize="15dp" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="2"
android:padding="10dp"
android:textColor="#FFFFFF"
android:textSize="20dp" />
</LinearLayout>
He is inflating into this piece of code:
<
ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
tools:context="pl.com.qiteq.zielonomocni_rework.HistoryActivity">
<LinearLayout
android:id="#+id/mainView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<LinearLayout
android:id="#+id/historyView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
</ScrollView>
And finnaly java code: (loop counter is for example)
LinearLayout mainView = (LinearLayout) findViewById(R.id.historyView);
for (int i=0; i<=2; i++){
View layout = View.inflate(this, R.layout.history_bar, null);
mainView.addView(layout);
}
The reason all your margins are being ignored is that you are passing null into your inflater here:
View layout = View.inflate(this, R.layout.history_bar, null);
When you do this all the LayoutParams of your view are thrown away. You should replace it with:
View layout = inflater.inflate(this, R.layout.history_bar, mainView, false);
To set text in the TextViews you don't need to set a different id for each one, just give each one an id. Then in your loop you can do something like:
List<TextView> textViews = new ArrayList<>();
LayoutInflater inflater = LayoutInflater.from(this);
for (int i=0; i<=2; i++){
View layout = inflater.inflate(this, R.layout.history_bar, mainView, false);
mainView.addView(layout);
TextView textView = (TextView) layout.findViewById(R.id.text1);
textViews.add(textView);
}
You would then have a reference to each of your TextViews in the List
private void showText(){
textView.setText(Html.fromHtml(text));
Log.d("MyLog","tags.size="+tags.size());
for (int i=0;i<tags.size();i++){
Button button = new Button(context);
button.setText(tags.get(i));
ll.addView(button, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
tagButtons.add(button);
}
}
This should be simple but this buttons doesn't show. I checked with logs- tags.size=5 so the problem is not in this. ll is a LinearLayout
Here's layout file :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:layout_margin="5dp"
android:id="#+id/scrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<LinearLayout
android:orientation="horizontal"
android:id="#+id/ll2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="TextView" />
<LinearLayout
android:id="#+id/ll"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
So, it's a very simple code and I don't understand why are theese views are not being added to a layout with id "ll"
You should call invalidate() on the parent layout which forces children to be drawn on the screen
Remove the linearlayout reference which you have not posted in the question and use like as i have done, Try this
Call showText() in OnCreate- method If you are using Activities
private void showText(){
textView.setText(Html.fromHtml(text));
for (int i=0;i<tags.size();i++){
Button button = new Button(context);
button.setText(tags.get(i));
LinearLayout ll=(LinearLayout) findViewById(R.id.ll);
ll.addView(button);
}
}
I need to add views to a vertical Linearlayout at bottom programmatically. (Yes we know that, it's in the title).
Ok : i can add views to the linearlayout, it's not a problem. But it's for a chat, so i need to add views at the bottom for each message which is displayed. I don't find any attributes to the LinearLayout to do that.
Adding messages :
mHandler.post(new Runnable() {
#Override
public void run() {
TextView message = new TextView(ChatActivity.this);
String[] splitMessage = text.split(":");
message.setText(splitMessage[0]+"\n"+splitMessage[1]);
message.setGravity(Gravity.LEFT);
message.setMaxWidth(200);
message.setBackgroundColor(getResources().getColor(android.R.color.tertiary_text_light));
message.setTextColor(getResources().getColor(android.R.color.black));
message.setSingleLine(false);
mLayout.addView(message);
}
});
chat_layout.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/live_obs_mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/empty"
android:orientation="vertical" >
<ScrollView
android:id="#+id/chat_layout"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="10" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:isScrollContainer="tue"
>
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="bottom"
android:orientation="horizontal" >
<EditText
android:id="#+id/edit_chat"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_gravity="left|center_vertical"
android:layout_weight="5" />
<Button
android:id="#+id/chat_submit"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_gravity="right|center_vertical"
android:onClick="onSubmit"
android:text="OK" />
</LinearLayout>
</LinearLayout>
You can do it pragmatically like this, this is just an example. You may want to change your code based on that.
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.mylayout);
TextView txt1 = new TextView(MyClass.this);
LinearLayout.LayoutParams layoutParams =
(RelativeLayout.LayoutParams) txt1.getLayoutParams();
layoutParams.addRule(LinearLayout.BOTTOM, 1);
txt1.setLayoutParams(layoutParams);
linearLayout.addView(txt1);
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.