linear layout inside linear layout is adding but is not visible - android

I'm having one empty Linear Layout in xml. And I'm adding some text view to it dynamically.
once these textviews exceeds 5 then i'm creating one more linear layout inside it and adding text views to newly created Layout. And finally adding this new layout to main layout.
I'm able to add, i.e in emulator it occupy that space but, will not display the info in the textviews.
my xml is as follows:
<?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" >
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/dyn_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dip" >
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/dyn_button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add TextViews" />
</LinearLayout>
and my java file is as follows:
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class AddTextViewsDynamically extends Activity implements
OnClickListener {
Button button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_textview_dynamically);
button = (Button) findViewById(R.id.dyn_button1);
button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.dyn_button1:
LinearLayout layout = (LinearLayout) findViewById(R.id.dyn_layout);
LinearLayout layout2 = null;
LinearLayout layout3 = null;
for (int i = 0; i < 10; i++) {
if (i > 4) {
if (i == 5) {
layout2 = new LinearLayout(this);
layout2.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
layout2.setOrientation(LinearLayout.VERTICAL);
layout2.setPadding(10, 60, 10, 10);
layout3 = new LinearLayout(this);
layout3.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
layout3.setOrientation(LinearLayout.HORIZONTAL);
layout3.setPadding(10, 10, 10, 10);
}
System.out.println("**** Adding text view " + i);
TextView text = new TextView(this);
text.setText("The Value of i is :" + i);
text.setTextSize(12);
text.setGravity(Gravity.LEFT);
text.setLayoutParams(new LayoutParams(155,
LayoutParams.WRAP_CONTENT));
layout3.addView(text);
if (i == 9) {
System.out
.println("Added second linear layout to first");
layout2.setVisibility(View.VISIBLE);
layout2.addView(layout3);
layout.addView(layout2);
}
} else {
System.out.println("###### Adding text view " + i);
TextView text = new TextView(this);
text.setText("The Value of i is :" + i);
text.setTextSize(12);
text.setGravity(Gravity.LEFT);
text.setLayoutParams(new LayoutParams(155,
LayoutParams.WRAP_CONTENT));
layout.addView(text);
}
}
}
}
}
Where am I going wrong?

Your primary LinearLayout is set to Horizontal so the first 5 text view and the layout2 are shown on the same line. Adding Layout3 to Layout2 makes the Layout3 to be shown from the right of the last text view from primary Linear Layout. On a 10 inch tablet i see only the first 2 elements of your LinearLayout. Perhaps on a smaller screen you don't see them. Try using
text.setLayoutParams(new LayoutParams(50, LayoutParams.WRAP_CONTENT));
instead of
text.setLayoutParams(new LayoutParams(155, LaoutParams.WRAP_CONTENT));
and you should see all your text views.
EDIT :
In your case this should do the trick;
xml :
<?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" >
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/dyn_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dip" >
</LinearLayout>
<LinearLayout
android:id="#+id/dyn_layout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:visibility="gone"
android:padding="10dip" >
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/dyn_button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add TextViews" />
</LinearLayout>
and code :
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.dyn_button1:
LinearLayout layout = (LinearLayout) findViewById(R.id.dyn_layout);
LinearLayout layout2 = (LinearLayout) findViewById(R.id.dyn_layout2);
LinearLayout layout3 = null;
for (int i = 0; i < 10; i++) {
if (i > 4) {
if (i == 5) {
layout2.setPadding(10, 60, 10, 10);
layout3 = new LinearLayout(this);
layout3.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
layout3.setOrientation(LinearLayout.HORIZONTAL);
layout3.setPadding(10, 10, 10, 10);
}
System.out.println("**** Adding text view " + i);
TextView text = new TextView(this);
text.setText("The Value of i is :" + i);
text.setTextSize(12);
text.setGravity(Gravity.LEFT);
text.setLayoutParams(new LayoutParams(155,
LayoutParams.WRAP_CONTENT));
layout3.addView(text);
if (i == 9) {
System.out
.println("Added second linear layout to first");
layout2.setVisibility(View.VISIBLE);
layout2.addView(layout3);
}
} else {
System.out.println("###### Adding text view " + i);
TextView text = new TextView(this);
text.setText("The Value of i is :" + i);
text.setTextSize(12);
text.setGravity(Gravity.LEFT);
text.setLayoutParams(new LayoutParams(155,
LayoutParams.WRAP_CONTENT));
layout.addView(text);
}
}
}
}

Actually you are doing things right, I just removed padding and made orientation of all Linear Layouts to vertical. Modify this on xml file too and it will work :))) Its just you are adding a padding of 10 or 60 which is going of the view taken by the parent.
Modified Java Code:
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.dyn_button1:
LinearLayout layout = (LinearLayout) findViewById(R.id.dyn_layout);
LinearLayout layout2 = null;
LinearLayout layout3 = null;
for (int i = 0; i < 10; i++) {
if (i > 4) {
if (i == 5) {
layout2 = new LinearLayout(this);
layout2.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
layout2.setOrientation(LinearLayout.VERTICAL);
// layout2.setPadding(10, 10, 10, 10);
layout3 = new LinearLayout(this);
layout3.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
layout3.setOrientation(LinearLayout.VERTICAL);
// layout3.setPadding(10, 10, 10, 10);
}
System.out.println("**** Adding text view " + i);
TextView text = new TextView(this);
text.setText("The Value of i is :" + i);
text.setTextSize(12);
text.setGravity(Gravity.LEFT);
text.setLayoutParams(new LayoutParams(155,
LayoutParams.WRAP_CONTENT));
layout3.addView(text);
if (i == 9) {
System.out
.println("Added second linear layout to first");
layout2.setVisibility(View.VISIBLE);
layout2.addView(layout3);
layout.addView(layout2);
}
} else {
System.out.println("###### Adding text view " + i);
TextView text = new TextView(this);
text.setText("The Value of i is :" + i);
text.setTextSize(12);
text.setGravity(Gravity.LEFT);
text.setLayoutParams(new LayoutParams(155,
LayoutParams.WRAP_CONTENT));
layout.addView(text);
}
}
}
}

Try this one, then you will see that your code is OK.
<?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"
android:weightSum="100" >
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="80" >
<LinearLayout
android:id="#+id/dyn_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dip" >
</LinearLayout>
</HorizontalScrollView>
<Button
android:id="#+id/dyn_button1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="20"
android:text="Add TextViews" />
</LinearLayout>

Related

Create buttons programmatically goes new line

I created a layout which programmatically created buttons (see code). With this layout I have a column of buttons, but I would create something like the picture below. I tried something with linear layout but the buttons didn't go to new line automatically and didn't show, so I change it to table layout. How can I create something like the picture programmatically?
Thank you.
protected TableLayout layout;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layout = (TableLayout) findViewById(R.id.workflows_activity_layout);
private void createButton() {
loading.setVisibility(View.VISIBLE);
layout.removeAllViews();
if (items.length == 0) {
TableRow row = new TableRow(this);
TextView no_item = new TextView(this);
no_questions.setText("there are no items");
no_questions.setTextSize(35);
row.addView(no_item);
layout.addView(row);
} else {
for (int i = 0; i < items.length; i++) {
TableRow row = new TableRow(this);
final Button btn = new Button(this);
TextView tw = new TextView(this);
tw.setText(items[i].getName());
tw.setTextSize(25);
btn.setBackgroundResource(R.drawable.button_image);
btn.setId(i);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int btn_selected = btn.getId();
Intent openItempage = new Intent(MainActivity.this, ItemsActivity.class);
startActivity(openItempage);
}
});
row.addView(btn);
row.addView(tw);
layout.addView(row, params);
items_buttons.add(btn);
items_nameText.add(tw);
}
}
loading.setVisibility(View.GONE);
}
There's an easy way to achieve what you want. Look at this code:
<?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:layout_gravity="center_horizontal"
android:orientation="vertical">
<TableLayout
android:id="#+id/tab_lay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:stretchColumns="*"
android:gravity="center_vertical" />
TabLayout tab_lay = (TableLayout)view.findViewById(R.id.tab_lay);
for(int i = 1; i <= 4; ){
TableRow a = new TableRow(getActivity());
TableRow.LayoutParams param = new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f);
a.setLayoutParams(param);
a.setGravity(Gravity.CENTER_VERTICAL);
for(int y = 0; (y < 2) && (i <= 4); y++) {
Button x = new Button(getActivity());
x.setText("Text " + i);
x.setGravity(Gravity.CENTER);
TableRow.LayoutParams par = new TableRow.LayoutParams(y);
x.setLayoutParams(par);
int ids = i;
x.setId(ids);
x.setOnClickListener(this);
a.addView(x);
i++;
}
tab_lay.addView(a);
}
If you want more buttons, then just change 4 which is compare to i to your value. Hope I help.
You can implement this using labraries, for instance
FlowLayout
Usage:
<com.wefika.flowlayout.FlowLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="start|top">
<View
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lorem ipsum" />
</com.wefika.flowlayout.FlowLayout>
Another solution is:
AndroidFlowLayout
Usage:
<com.liangfeizc.flowlayout.FlowLayout
android:id="#+id/flow_layout"
flowlayout:vertical_spacing="10dp"
flowlayout:horizontal_spacing="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

Layout Problems with Buttons

I'm trying to add table rows programatically (see code following)
Each row contains some textfields and terminates with sometimes a Button
But the button is not aligned like textfields..(see image of output)
I would like to have the same height for textfield and Button
How can i do that ?
Many thanks for your support
Jcarlier
Brussels
public class MainActivity extends Activity {
............
TableLayout tl = (TableLayout) findViewById(R.id.table);
// heading
TableRow tr_head = new TableRow(this);
TextView label_club= new TextView(this);
label_club.setText("REMARQUE");
label_club.setGravity(Gravity.CENTER);
label_club.setTextColor(getResources().getColor(R.color.OrangeRed));
label_club.setBackground(getResources().getDrawable(R.drawable.border));
tr_head.addView(label_club);
// etc..
// iterate sqlite and show in table TableLayout
Integer icount=0;
Integer j =c.getColumnCount()-1;
if (c != null && c.getCount() > 0){
if (c.moveToFirst()) {
do {
TableRow tr = new TableRow(this);
tr.setBaselineAligned(false);
//tr.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
for( int i=0;i<j+1;i++)
{
if ( (i == j) & ( c.getString(i).length() > 4) ) {
//map.put(icount, c.getString(i).toString());
Button btn=new Button(this);
btn.setId(icount);
//btn.setMaxWidth(10);
btn.setVisibility(View.VISIBLE);
btn.setText("Tableaux");
btn.setClickable(true);
//btn.setHeight(20);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// blabla
}
}
});
//btn.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
//btn.setLayoutParams (new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
//btn.setGravity(Gravity.CENTER_HORIZONTAL);
// btn.setBackground(getResources().getDrawable(R.drawable.border));
tr.addView(btn); }
else {
TextView text=new TextView(this);
switch(c.getType(i))
{
case Cursor.FIELD_TYPE_INTEGER:
text.setText(String.valueOf(c.getInt(i)));
break;
case Cursor.FIELD_TYPE_STRING:
text.setText(c.getString(i));
break;
}
text.setPadding(0,0,10,0);
//text.setHeight(20);
//text.setBackground(getResources().getDrawable(R.drawable.border));
tr.addView(text);
}
}
// add row to table
tl.addView(tr);
icount++;
} while (c.moveToNext());
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:id="#+id/layout"
android:scrollbars="horizontal|vertical"
android:layout_alignParentBottom="true"
android:layout_height="fill_parent"
>
<HorizontalScrollView
android:id="#+id/horizontalScrollView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<TableLayout
android:id="#+id/table"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="2dp"
android:stretchColumns="*">
</TableLayout>
</HorizontalScrollView>
</ScrollView>

I need help to implement xml

I found online the working code of a menu slider.
In this code, however, the layout is generated in the code, could someone help me to configure it in a layout file in *. Xml?
Sorry for my bad english.
code:`
private boolean Menu_Displayed=false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Display display = getWindowManager().getDefaultDisplay();
final int width = display.getWidth();
// menu:
LinearLayout li_menu = new LinearLayout(this);
li_menu.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
li_menu.setOrientation(1);//1 is vertical
li_menu.setBackgroundColor(Color.GREEN);
Button btn1 = new Button(this);
btn1.setText("button 1");
btn1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
li_menu.addView(btn1);
//body:
final HorizontalScrollView hsv = new HorizontalScrollView(this){
#Override
// do not let hsv consume the click itself. Then the view under the hsv will also consume the click
//so that the menu will be clicked
//when menu is not showed up, let hsv be the only view to consume the click.
//so that the menu will not be clicked
public boolean onTouchEvent(MotionEvent ev) {
if(Menu_Displayed){
return false;
}
else{
return true;
}
}
};
hsv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
hsv.setBackgroundColor(Color.TRANSPARENT);
hsv.setHorizontalFadingEdgeEnabled(false);
hsv.setVerticalFadingEdgeEnabled(false);
final LinearLayout li_body = new LinearLayout(this);
li_body.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
li_body.setOrientation(0);//0 is horizantal
li_body.setBackgroundColor(Color.TRANSPARENT);
hsv.addView(li_body);
//body: place holder transparent
TextView placeholder = new TextView(this);
placeholder.setTextColor(Color.TRANSPARENT);
placeholder.setLayoutParams(new LayoutParams(width-100, LayoutParams.FILL_PARENT));
placeholder.setVisibility(View.INVISIBLE);
li_body.addView(placeholder);
//body: real content
LinearLayout li_content = new LinearLayout(this);
li_content.setLayoutParams(new LayoutParams(width, LayoutParams.FILL_PARENT));
li_content.setOrientation(1);//1 is vertical
li_content.setBackgroundColor(Color.CYAN);
TextView tv1 = new TextView(this);
tv1.setText("txt 1");
tv1.setTextSize(40);
tv1.setTextColor(Color.BLACK);
TextView tv2 = new TextView(this);
tv2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tv2.setTextSize(50);
tv2.setText("txt 2");
tv2.setTextColor(Color.WHITE);
//use this button to scroll
Button btn_showMenu = new Button(this);
btn_showMenu.setText("Menu");
btn_showMenu.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
btn_showMenu.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
hsv.post(new Runnable() {
#Override
public void run() {
if(Menu_Displayed){
hsv.smoothScrollTo(width-100, 0);
}
else{
hsv.smoothScrollTo(0, 0);
}
Menu_Displayed = !Menu_Displayed;
}
});
}
});
li_content.addView(tv1);
li_content.addView(tv2);
li_content.addView(btn_showMenu);
li_body.addView(li_content);
//add menu and body in to frame
FrameLayout mainFrame = new FrameLayout(this);
mainFrame.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
mainFrame.addView(li_menu);
mainFrame.addView(hsv);
//scroll to the body real content to block the menu
hsv.post(new Runnable() {
#Override
public void run() {
hsv.scrollBy(width-100, 0);
}
});
setContentView(mainFrame);
}
`
This is not a good implementation of a SlidingMenu, you can try this open sources project:SideMenu
To implements the menu swap on you example, is calculating the screen width-100, width cannot be converted to xml, the base view will be like this file.
<?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:background="#ff669900"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1" />
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:fadingEdge="none" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#android:color/transparent"
android:orientation="horizontal" >
<TextView
android:id="#+id/placeholder"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingRight="100dp" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>

How to manage linear sub layout programmatically?

Issue about layout aliment.
i want to add view dynamically.
Logical code :
linear1 = (LinearLayout) findViewById(R.id.parent);
linear2 = (LinearLayout) findViewById(R.id.chiled);
int len = 4;
for (int i = 1; i <= len; i++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params1.gravity = Gravity.RIGHT;
final int id_txt;
ImageView iv = new ImageView(this);
iv.setId(i);
id_txt = iv.getId();
iv.setBackgroundResource(R.drawable.ic_launcher);
linear1.addView(iv, params);
iv = ((ImageView) findViewById(id_txt));
for (int j = 1; j < 2; j++) {
final int id_;
Button btn = new Button(this);
btn.setId(i);
id_ = btn.getId();
btn.setText("button " + id_);
linear2.addView(btn, params1);
btn = ((Button) findViewById(id_));
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(v.getContext(),
"Button clicked index = " + id_,
Toast.LENGTH_SHORT).show();
}
});
}
// btn.setBackgroundColor(Color.rgb(70, 80, 90));
// linear1.addView(txt, params);
// params.addRule(RelativeLayout.RIGHT_OF, txt.getId());
iv.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast.makeText(view.getContext(),
"text clicked index = " + id_txt,
Toast.LENGTH_SHORT).show();
}
});
}
}
Xml Code :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/chiled"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
</LinearLayout>
I want to add image in parent view and two button in child view dynamical.
I inspires to make this type view form
Android heterogeneous gridview like pinterest?
It should be like as
current output as
I don't know where is the problem.
One strange issues i am facing now in my editor if i see layout in code then its shows me android:orientation="vertical" and if i see in outline that shows android:orientation="horizontal" for each layout. how is it possible ?
Help me to solve out.Thanks
EDIT:
First off, start by adding your buttons to the xml. Along with the image view.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/extra_root"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/chiled"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Like" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dislike" />
</LinearLayout>
</LinearLayout>
You can then remove any concept of adding views, as they already exist.
package com.example.yul;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class extra extends Activity {
Button like, dislike;
LinearLayout root, sub;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.extra);
like = (Button) findViewById(R.id.button1);
dislike = (Button) findViewById(R.id.button2);
// add button listeners here.
ImageView iv = (ImageView) findViewById(R.id.image);
iv.setBackgroundResource(R.drawable.ic_launcher);
}
}
}
This will only display the one image though, with buttons.
What you need to do is apply this xml, and code to a gridView or similar. As you will have id clashes otherwise.
Since the horizontal LinearLayout chiled is the first child of the vertical LinearLayout parent it is aligned as the first item. So adding Views to it will place them on top.
Try moving the chiled Layout to the root LinearLayout root.

android - add TableRow (containing LinearLayout) to Table

I have a complicated table, so I would like to make one row a little more flexible for adding a button or text view, so I have the following code for adding TableRow to Table. I am trying to set LinearLayout to TableRow programmatically, but the row does not show up when I run it. I'd appreciated if somebody can point what the problem is.
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
final CheckBox checkbox = new CheckBox(this);
checkbox.setPadding(10, 5, 0, 0);
checkbox.setTextSize(TypedValue.COMPLEX_UNIT_PX, 15);
checkbox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String item_clicked_on = (String) ((TextView) view).getText();
Toast.makeText(getApplicationContext(), item_clicked_on, Toast.LENGTH_SHORT).show();
}
});
TextView tv = new TextView(this);
tv.setText(" " + count + ". " + baby.getName());
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, 14);
tv.setPadding(10, 10, 0, 0);
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String item_clicked_on = (String) ((TextView) view).getText();
Toast.makeText(getApplicationContext(), item_clicked_on, Toast.LENGTH_SHORT).show();
}
});
checkbox.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
tv.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
LinearLayout linearLayout = new LinearLayout(tr.getContext());
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.addView(checkbox, new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
linearLayout.addView(tv, new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
tableRow.addView(linearLayout);
table.addView(tr, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
I am trying to make it look like this xml.
<TableRow android:id="#+id/tableRow3" android:layout_width="wrap_content" android:layout_height="wrap_content">
<LinearLayout android:id="#+id/linearLayout2" android:layout_width="wrap_content" android:layout_height="wrap_content">
<CheckBox android:id="#+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox>
<TextView android:id="#+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Choice 1" android:textColor="#FFFFFF"></TextView>
</LinearLayout>
</TableRow>
You are adding linear layout to tableRow .
tableRow.addView(linearLayout);
But you are adding table row tr to Table table
table.addView(tr, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
I think you have to add tableRow to table. Once check it.

Categories

Resources