• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Creating LinearLayout by the click on Button

walsash

Lurker
So, I want to add(create) LinearLayout in working application by the click on button. And I almost do this, something add(free space), but I don't see anything.

Code:
public class MainActivity extends Activity implements OnClickListener {
	LinearLayout layout_for_sides;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		layout_for_sides = (LinearLayout) findViewById(R.id.layout_for_sides);

		btn_add_side = (Button) findViewById(R.id.btn_add_side);

		btn_add_side.setOnClickListener(this);
	}
	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.btn_add_side:
			LinearLayout newlayout = new LinearLayout(this);
			newlayout.setOrientation(LinearLayout.VERTICAL);

			newlayout.setLayoutParams(new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT, 1)); 

			newlayout.setBackgroundColor(Color.BLACK); //IT DOESN'T WORK, THERE IS NOTHING WITH BLACK BACKGROUND
												
			layout_for_sides.addView(newlayout);
			
			LayoutParams lpView = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			
			TextView tv = new TextView(this);
	                tv.setText("TextView");
	                tv.setId(5);
	                tv.setTextColor(Color.BLACK);
	                this.newlayout.addView(tv, lpView); //IT WORKS, I SEE FREE SPACE, BUT NEITHER TEXT
	        
			break;

		default:
			break;
		}

	}
}
 
Solved =| Problem was in layout_for_sides that has another LinearLayout. But, I don't understand how this another layer prevents to show my newlayout.

Update: Problem in wrong parameters of parent LinearLayout - layout_for_sides.

It has
Code:
android:orientation="horizontal"
, but needs
Code:
android:orientation="vertical"
. Because of that I haven't seen anything.
 
Back
Top Bottom