Presentation is loading. Please wait.

Presentation is loading. Please wait.

Adding Components to Activity

Similar presentations


Presentation on theme: "Adding Components to Activity"— Presentation transcript:

1 Adding Components to Activity
We have two options: 1- Creating a xml layout file with button in it. 2- Adding button dynamically to your Activity.

2 1- Creating a xml layout file with button in it.
<LinearLayout xmlns:android=" xmlns:tools=" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TableLayout" /> </LinearLayout> and use this layout in your Activity: public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button myTableButton=(Button) findViewById(R.id.button); myTableButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Log.d("Hi", "hi"); } });

3 2- Adding button dynamically to your Activity.
<LinearLayout xmlns:android=" xmlns:tools=" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TableLayout" /> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"></LinearLayout> </LinearLayout> *******************************Add this to your main activity. LinearLayout layout = (LinearLayout) findViewById(R.id.layout1); layout.setOrientation(LinearLayout.HORIZONTAL); Button myNew; myNew = new Button(this); myNew.setText("Hi"); layout.addView(myNew); myNew.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d("First","Log"); } }); Button btn = new Button(this); btn.setText("OK1"); layout.addView(btn); ...


Download ppt "Adding Components to Activity"

Similar presentations


Ads by Google