Generate a Dynamic UI where i can create widgets on fly and use then around the application and the widgets should have a dynamic drag and drop facility...
1
Expert's answer
2015-05-20T04:20:21-0400
Answer:
File of a class of anapplication window. package com.samples.ui.dynamicui; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.ViewCroup.LayoutParams; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; public class DynamicUIActivity extends Activity implements View.OnClickListener { private static final int ID_BUTTON1 = 101; private static final int ID_BUTTON2 = 102; private static final int ID_TEXT = 103; private TextView text; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
//comment method, we do not need //setContentView(R.layout.main);
//create the root layout LinearLayout layout = new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); layout.setLayoutParams(new Linearlayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); //create the first button Button button1 = new Button(this); button1.setText("Button1"); button1.setId(ID_BUTTON1); button1.setLayoutParams(new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); button1.setOnClickListener(this); //create the second button Button button2 = new Button(this); button2.setText("Button2"); button2.setId(ID_BUTTON2); button2.setLayoutParams(new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); button2.setOnClickListener(this); //Create a text field text = new TextView(this); text.setId(ID_TEXT); text.setText("Event:"); text.setLayoutParams(new LenaerLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); layout.addView(button1); layout.addView(button2); layout.addView(text); //add the created content on Activity this.addContentView(layout, new LinearLayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); } //button event handler @Override public void onClick(View v) { switch (v.getId()) { case ID_BUTTON1: text.setText("Click on First Button"); breack; case ID_BUTTON2: text.setText("Click on Second Button"); breack; } } }
Comments
Leave a comment