Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
<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="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:
<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"
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" >

<include layout="@layout/first_activity">
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:

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
package="com.example.codereuse"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk>
android:minSdkVersion="8"
android:targetSdkVersion="17" />

<application>
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity>
android:name="com.example.codereuse.SecondActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN">

<category android:name="android.intent.category.LAUNCHER">



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







Readers Reply very soon...
Posted
Comments
Ganesh KP 7-Sep-13 2:08am    
Hi Ashish, You want to make an activity as a common useer control that can be used in many places. Am I right?

Can u post your log cat? Please use pastie.org to post your log cat or any sort of code. and just go through the links
http://stackoverflow.com/questions/8821240/android-how-to-create-my-own-activity-and-extend-it

and also check this

http://stackoverflow.com/questions/8080212/android-cannot-start-android-class-that-extends-activity-from-another-activity

if any doubt please post your query here...

1 solution

hello Ganeshcse..
thanx for reply..
i am sorry to say but the link that you have provided is not working for me..
can you give me more precise solution..
what i want is i have a common menu with 5 buttons so i want to make a separate activity code so that i can use that code in all other activity so that i dont have to repeat that common code again and again...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900