0

I have 2 layouts in named first_activity and second_activity... the first_activity has a single button button1 on which i have applied onclick listener to make a simple toast and i have used this first_activity in the FirstActivity... the second_activity has a single button along with that i have included the first_activity in the second_activity so the second_activity has 2 buttons... now have coded and run the application using FirstActivity has the main activity and the i am getting the toast onclick of 1st button... Now the SecondActivity extends FirstActivity along its own button 2 click event... now i have changed the start activity as SecondActivity and after running i am getting the click event of only 2nd button not of 1st button.. so where i am making mistake.. my intention is to make code reuse for example i have an app in which there are 30 layout and all the layout have common menu which is included in each layout so i just want to code for that menu once and reuse that code in all the other layout...

here are the code of my app.... first_activity:

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="200dip"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".FirstActivity" >

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1st Button" />

second_activity:

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".FirstActivity" >


android:id="@+id/layout"/>

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/layout"
android:text="2nd Button" />

FirstActivity:

package com.example.codereuse;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class FirstActivity extends Activity {

Button b1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_activity);

b1 = (Button) findViewById(R.id.button1);

b1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Button 1 Clicked", Toast.LENGTH_LONG).show();
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


}

SecondActivity:

package com.example.codereuse;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class SecondActivity extends FirstActivity {

Button b2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);

b2 = (Button) findViewById(R.id.button2);

b2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Button 2 Clicked", Toast.LENGTH_LONG).show();
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

AndroidMenifest:

package="com.example.codereuse"
android:versionCode="1"
android:versionName="1.0" >


android:minSdkVersion="8"
android:targetSdkVersion="17" />


android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >

android:name="com.example.codereuse.SecondActivity"
android:label="@string/app_name" >

android:name="com.example.codereuse.FirstActivity"
android:label="@string/app_name" >

Readers Reply very soon...

1 Answers1

0

If you have 30 layouts which have the same content, then you can use one xml file for all those.

If you are using an id from one activity, there is no problem using the same id for the element in different activity.

AshMv
  • 392
  • 2
  • 12