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);
}
}
Related
I am pretty new in Android.
I have some problems to create a dynamic LinearLayout programmatically.
I actually have some information in ArrayLists that I need to display. The problem is that I don't know how many items are in the ArrayList, so I need to create the same layout (LinearLayoutChild) for every item in the ArrayList.
For example, I have created this (in this case, let's say the ArrayLists have 2 items each)
ArrayList<String> alistA = new ArrayList<String>();
ArrayList<String> alistB = new ArrayList<String>();
ArrayList<String> alistC = new ArrayList<String>();
ArrayList<String> alistD = new ArrayList<String>();
ArrayList<String> alistE = new ArrayList<String>();
alistA.add(0, "TextA1");
alistA.add(1, "TextA2");
alistB.add(0, "TextB1");
alistB.add(1, "TextB2");
alistC.add(0, "TextC1");
alistC.add(1, "TextC2");
alistD.add(0, "TextD1");
alistD.add(1, "TextD2");
alistE.add(0, "TextE1");
alistE.add(1, "TextE2");
int NumberArray = 2;
for(int i = 0; i<NumberArray;i++){
// How can i do this?
}
I want to display it like :
alistA[0] -> Tv1
alistB[0] -> Tv2
and so on...
alistA[[1]] -> Tv1 (newly created)
alistB[[1]] -> Tv2 (newly created)
...
My XML file :
<?xml version="1.0" encoding="utf-8"?><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="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"
tools:context="com.example.murat.testlol.MainActivity"
android:id="#+id/LinearLayout1"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/Et1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/Btn1"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="12"
android:orientation="horizontal"
android:id="#+id/LinearLayoutChild">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="4"
android:orientation="vertical"
android:id="#+id/Ll1">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/Tv1"
android:gravity="center"
android:text="TextView1"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView2"
android:id="#+id/Tv2"
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="4"
android:orientation="vertical"
android:id="#+id/Ll2">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/Tv3"
android:gravity="center"
android:text="TextView3"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="#+id/Tv4"
android:text="TextView4"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="4"
android:orientation="vertical"
android:id="#+id/Ll3">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/Tv5"
android:gravity="center"
android:text="TextView5"/>
</LinearLayout>
</LinearLayout>
The EditText and button need to stay at the same place.
Thank you.
Sounds like a good use case for Android's ListView. From Google's documentation:
ListView is a view group that displays a list of scrollable items. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database query and converts each item result into a view that's placed into the list.
To go this route, update main.xml to include a ListView element -- and move the LinearLayoutChild into its own XML file.
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<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="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"
tools:context="com.example.murat.testlol.MainActivity"
android:id="#+id/LinearLayout1"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/Et1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/Btn1"/>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/list"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
generic_linear_layout_child.xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="4"
android:orientation="vertical"
android:id="#+id/linear_layout_child">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/Tv1"
android:gravity="center"
android:text="TextView1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView2"
android:id="#+id/Tv2"
android:gravity="center"/>
</LinearLayout>
Then, create an adapter to bind the data in your array to the appropriate UI elements. Here's a SO post on how to create a custom adapter: Custom Adapter for List View
You need to create the linearlayouts and textviews dynamically, using routines like this:
public TextView makeTextView (String text)
{
TextView tv = new TextView (context)
tv.setText (text) ;
// customise the layout of the text here, eg...
tv.setTextColor(Color.RED) ;
return tv;
}
public LinearLayout makeHorizLayout ()
{
LinearLayout ll = new LinearLayout (context);
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
return ll;
}
After you have loaded the schema, you find the view that you want to insert the linearLayouts into like this:
View mainView = findViewById (R.id.LinearLayout1);
You then add a linearlayout to a view like this:
LinearLayout ll = makeHorizlayout ();
mainView.add(ll);
Then you add textviews to the LinearLayout like this:
TextView tv = makeTextView ("whatever text");
ll.addView (tv);
Do the following. Get your data and fill a linearLayout container during runtime after you have retrieved / created your data. Here is an example for creating the views.
private LinearLayout container;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
container = new LinearLayout(this);
container.setOrientation(LinearLayout.VERTICAL)
// + other layout stuff / better inflate this from xml
// Get data and call createLayout with the data
}
createLayout(List<List<String>> lists) {
for (List<String> list : lists) {
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
// + other layout stuff / better inflate this from xml
for (String string : list) {
TextView textView = new AppCompatTextView(this);
textView.setText(string);
linearLayout.addView(textView);
}
container.addView(linearLayout);
}
}
I have a slight problem getting my layout_weight to work. I am creating a custom bottomBar. But i cant make it to stretch as i want it to.
Bottom Bar View
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/bottom_bar_root"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/bottom_bar_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="4"
android:orientation="horizontal"/>
</RelativeLayout>
This is the big container i am adding my buttons (items) to.
Bottom Bar Item
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<android.support.v7.widget.AppCompatImageView
android:id="#+id/bottom_bar_item_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/arrow_up_float"/>
<TextView
android:id="#+id/bottom_bar_item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEXT"/>
</LinearLayout>
This is my items that i add dynamically to the view. But somehow the view is not stretched properly.
But when i hardcode it in. It works. So could it be that layout weight does not work dynamically?
How i add the views (items)
private void updateItems(final List<BottomBarTab> bottomBarTabs) {
if (bottomBarTabs.size() < TABS_COUNT) {
throw new RuntimeException("Not enough buttons for bottomBar");
}
for (int i = 0; i < TABS_COUNT; i++) {
bottomBarTabs.get(i).setOnClickListener(this);
bottomBarTabs.get(i).prepareLayout();
container.addView(bottomBarTabs.get(i));
}
}
Screenshot
LayoutWeight is given to inner components of layout only not on parent Linearlayout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:weightSum="2">
<android.support.v7.widget.AppCompatImageView
android:id="#+id/bottom_bar_item_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#android:drawable/arrow_up_float"/>
<TextView
android:id="#+id/bottom_bar_item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TEXT"/>
</LinearLayout>
public void prepareLayout() {
View view = inflate(getContext(), R.layout.bottom_bar_item,this);
LinearLayout.LayoutParams params =
new LayoutParams(0,LayoutParams.MATCH_PARENT,LAYOUT_WEIGHT);
view.setLayoutParams(params);
if(backgroundColor != null) {
view.setBackgroundColor(backgroundColor);
}
TextView titleText = (TextView) view.findViewById(R.id.bottom_bar_item_text);
titleText.setText(title);
AppCompatImageView imageView = (AppCompatImageView) view.findViewById(R.id.bottom_bar_item_icon);
imageView.setImageResource(iconResId);
}
I changed in my prepareLayout function and put new layoutParams. Because somehow after inflation. The view ignores the weight that was set to it. So i had to force it by code. Maybe this is a android bug?
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.
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 want to create a game list wherein the game details like opponent name,score,etc will be displayed.I am having problem fetching the data into xml and then displaying it.please help.
I have created two xml.I have to set data into the 2nd xml and then include that 2nd xml into 1st xml.
my 1st xml is
<FrameLayout
android:id="#+id/gameListFrame"
android:layout_width="fill_parent"
android:layout_height="380dp"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dip" >
<LinearLayout
android:id="#+id/GameListHolder"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/scrollBackG"
android:orientation="horizontal" >
</LinearLayout>
</ScrollView>
</FrameLayout>
2nd xml is
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gamelist_items"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50px"
android:gravity="left"
android:orientation="horizontal"
android:background="#FFF" >
<ImageView
android:id="#+id/gl_avatar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<LinearLayout android:orientation="horizontal"
android:layout_width="225px"
android:layout_height="fill_parent"
android:gravity="left"
android:background="#000">
<LinearLayout android:orientation="vertical"
android:layout_width="175px"
android:layout_height="wrap_content"
android:gravity="top">
<TextView android:id="#+id/oppName"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top" >
<TextView android:id="#+id/lastWord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView android:id="#+id/diffTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
My code is
gameListItem=(LinearLayout) findViewById(R.id.gamelist_items);
for (i=0;i<gamelistVector.size();i++)
{
rowView=View.inflate(context, R.layout.gamelist_details,null );
TextView oppName=(TextView) rowView.findViewById(R.id.oppName);
TextView lastWord=(TextView) rowView.findViewById(R.id.lastWord);
TextView diffTime=(TextView) rowView.findViewById(R.id.diffTime);
oppName.setText(gList.getOppName());
lastWord.setText(gList.getOppLastWord());
diffTime.setText(gList.getDiffTime()+"hr");
gameListItem.addView(rowView); //show the total games}
You need to extend ListAdapter and override the getView() method that sets the data for each row.
See this example
You need inflater reference to inflate views.
To GetInflater reference use:
LayoutInflater inflater = (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
gameListItem=(LinearLayout) findViewById(R.id.gamelist_items);
for (i=0;i<gamelistVector.size();i++)
{
rowView=inflater.inflate(R.layout.gamelist_details, null);
TextView oppName=(TextView) rowView.findViewById(R.id.oppName);
TextView lastWord=(TextView) rowView.findViewById(R.id.lastWord);
TextView diffTime=(TextView) rowView.findViewById(R.id.diffTime);
oppName.setText(gList.getOppName());
lastWord.setText(gList.getOppLastWord());
diffTime.setText(gList.getDiffTime()+"hr");
gameListItem.addView(rowView); //show the total games
}
thanks guys 4 answering to my query...i saw ur suggestions and figured a way out.this is wat i did nd its working...
private void doDisplay(){
this.runOnUiThread(new Thread() {
#Override
public void run() {
TextView gameCount=(TextView) findViewById(R.id.game_count);
gameCount.setText(" found "+maxGames+" games ");
for (i=0;i<gamelistVector.size();i++)
{
rowView=View.inflate(context, R.layout.gamelist_details, null);
TextView oppName=(TextView) rowView.findViewById(R.id.oppName);
TextView lastWord=(TextView) rowView.findViewById(R.id.lastWord);
TextView diffTime=(TextView) rowView.findViewById(R.id.diffTime);
TextView score=(TextView) rowView.findViewById(R.id.opplastMoveScore);
oppName.setText(" "+gamelistVector.get(i).getOppName());
lastWord.setText(" "+gamelistVector.get(i).getOppLastWord());
diffTime.setText(gamelistVector.get(i).getDiffTime()+"");
score.setText("("+gamelistVector.get(i).getLastMoveScore()+")");
ImageView oppImage = (ImageView) rowView.findViewById(R.id.gl_avatar);
oppImage.setImageBitmap(gamelistVector.get(i).getOppImage());
ImageView status = (ImageView) rowView.findViewById(R.id.gl_status);
status.setImageResource(gamelistVector.get(i).getTurnUserID());
gameListItem.addView(rowView);
}
}
});
}