Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating dynamic buttons using two layouts in android
    text
    copied!<p>I would like to create dynamic buttons in my main activity. The buttons are in a separate activity with an own layout file. I can show you my test project. I guess it could be better, but I am still a beginner in this field. ;-)</p> <p>This is my main activity. Nothing special:</p> <pre><code>package de.example.testprojekt; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.MenuItem; import android.support.v4.app.NavUtils; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } } </code></pre> <p>I have another Class with following code:</p> <pre><code>package de.example.testprojekt; import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.LinearLayout; public class TestClass extends LinearLayout { LayoutInflater inflator; public TestClass (Context context) { super (context); initialize(); } public TestClass (Context context, AttributeSet attrs) { super (context, attrs); initialize(); } public TestClass (Context context, AttributeSet attrs, int defStyle) { super (context, attrs, defStyle); initialize(); } void initialize() { inflator = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); inflator.inflate(R.layout.secondary_activity, this); } @Override protected void onFinishInflate() { super.onFinishInflate(); setupLayout(); } Button button1; String TAG = "tag"; private void setupLayout () { button1 = (Button) findViewById(R.id.button1); button1.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { Log.e(TAG,"test"); } }); } } </code></pre> <p>And here are my two XML Files:</p> <pre><code>&lt;RelativeLayout 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" &gt; &lt;LinearLayout android:id="@+id/linearLayout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" &gt; &lt;de.example.testprojekt.TestClass android:id="@+id/mainCtrl1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="30dp" /&gt; &lt;de.example.testprojekt.TestClass android:id="@+id/mainCtrl2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="30dp" /&gt; &lt;/LinearLayout&gt; &lt;TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:padding="@dimen/padding_medium" android:text="@string/hello_world" tools:context=".MainActivity" /&gt; </code></pre> <p></p> <p>And the second one:</p> <pre><code>&lt;RelativeLayout 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" &gt; &lt;Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="Button" /&gt; </code></pre> <p></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload