1

Please can anyone help me, since i am new to android i am not able to get solution for - the button OnClick event is not working if i use the inflated layout. Here is my usemerge.xml code,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#2996ff" >
<LinearLayout
    android:id="@+id/i1" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
   >
    </LinearLayout>
  </RelativeLayout>

toplayout.xml

   <LinearLayout 
   xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linear1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >

     <Button
        android:id="@+id/butt1"
        android:layout_width="130dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="24dp"
        android:layout_marginStart="20dp"
        android:layout_marginTop="24dp"
        android:background="@drawable/mybutton" 
        android:text="@string/schedule" />
 </LinearLayout>

MainActivity.java

 private  LinearLayout lin1; 
  private Button bt1;
   protected void onCreate(Bundle savedInstanceState) {
      // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.usemerge);

     final LayoutInflater  inflater =       
    (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      lin1 = (LinearLayout)findViewById(R.id.i1);
        View vi=inflater.inflate(R.layout.toplayout,lin1,false);

        lin1.addView(vi);
        bt1=(Button)vi.findViewById(R.id.butt1);

        bt1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View vo) {
                // TODO Auto-generated method stub

                Intent ain=new Intent(getBaseContext(), ScheduleActivity.class);
                startActivity(ain); 
            }
        });

6 Answers6

0

Define activity in AndroidManifest.xml :

<activity android:name=".ScheduleActivity"/> 

Set click listener before add Button to LinearLayout :

public class MainActivity extends FragmentActivity {

    private LinearLayout lin1;
    private Button bt1;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.usemerge);

        lin1 = (LinearLayout) findViewById(R.id.i1);
        View vi = LayoutInflater.from(this).inflate(R.layout.toplayout, lin1, false);
        bt1 = (Button) vi.findViewById(R.id.butt1);
        bt1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View vo) {
                Intent ain = new Intent(MainActivity.this, ScheduleActivity.class);
                startActivity(ain);
            }
        });
        lin1.addView(vi);
    }

}
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
0

There are two problems ...

The first is that you have commented out the find for the Button.

Button bt1=(Button)vi.findViewById(R.id.butt1);

The second is that you have the onclick function calling the startActivity of the activity in an anonymous interface. There are two solutions you can do.

First is to implement the interface onClickListener in the MainActivity class

class MainActivity extends Activity implements Button.onClickListener {
    //Your code Goes here...
      bt1.setOnClickListener(this);
    //Rest of your code

    @Override
    public void onClick(View view){
       Intent ain = new Intent(this, ScheduleActivity.class);
       startActivity(ain);
    }
}

Otherwise you can also do the following in your onclick:

Intent ain = new Intent(MainActivity.this, ScheduleActivity.class);
MainActivity.this.startActivity(ain);
MiltoxBeyond
  • 2,683
  • 1
  • 13
  • 12
  • Can you post the error messages? It may be something else. Additionally you should try looking into the xml merge tags and include tags that basically does the same thing you try to do with the layout inflater. – MiltoxBeyond Nov 13 '14 at 19:45
0

Notice that you do setContentView(R.layout.usemerge); at the beginning, and after that you do your inflate and bind the button to the inflated view, this mean that you don't see the inflated layout anyway (the button that u see on the screen isn't bt1 from inflated view).

You should do setContentView(vi); after inflating the view.

Ferrmolina
  • 2,737
  • 2
  • 30
  • 46
Liran Peretz
  • 580
  • 8
  • 11
0

Define Your Activity in AndroidManifest.xml

<activity android:name=".ScheduleActivity">

Instead of getBaseContext() use this line

Intent ain=new Intent(MainActivity.this, ScheduleActivity.class);

Implement the interface OnClickListener like this

public class MainActivity extends Activity implements OnClickListener {
    private  LinearLayout lin1; 
      private Button bt1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
          // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         final LayoutInflater  inflater =       
        (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          lin1 = (LinearLayout)findViewById(R.id.i1);
            View vi=inflater.inflate(R.layout.activity_sub,lin1,false);

            lin1.addView(vi);
            bt1=(Button)vi.findViewById(R.id.butt1);
            bt1.setOnClickListener(this);

    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
         Intent ain=new Intent(MainActivity.this, SubActivity.class);
         startActivity(ain); 
    }
}
M S Gadag
  • 2,057
  • 1
  • 12
  • 15
karthickraja
  • 214
  • 1
  • 7
0

Add setContentView(vi); after inflating , and remove setContentView(R.layout.usemerge);

Ferrmolina
  • 2,737
  • 2
  • 30
  • 46
Liran Peretz
  • 580
  • 8
  • 11
-1

Your MainActivity.java should look like this:

LinearLayout lin1;
Button bt1;

 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.usemerge);

 LayoutInflater inflater = getLayoutInflater().inflate(R.layout.toplayout, null, true);
 bt1 = inflater.findViewById(R.id.butt1);

 lin1 = (LinearLayout)findViewById(R.id.i1);
 View vi=inflater.inflate(R.layout.toplayout,lin1,false);

    lin1.addView(vi);

    bt1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View vo) {
            // TODO Auto-generated method stub

            Intent ain=new Intent(getBaseContext(), ScheduleActivity.class);
            startActivity(ain); 
        }
    });
gegobyte
  • 4,945
  • 10
  • 46
  • 76