public class Check extends Activity implements OnClickListener {
//private check2 check2;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button usemirror = (Button)findViewById(R.id.widget28);
usemirror.setOnClickListener(this);
}
public void onClick(View view){
Intent mi = new Intent(this , check2.class);
startActivity(mi);
}
}
package com.exaple;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
public class check2 extends Activity {
public void onCreate(Bundle savedInstanceState) {
// super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
Toast.makeText(
this,
"Welcome to second page", Toast.LENGTH_LONG).show();
finish();
}
}
ANDROID manifest.xml
<activity android:name=".check2" >
I am doing this but doesn't show me the message written in my other activities
#Uttam Didn't you got super not called exception?
I suppose you must call super.onCreate() in your check2.Not calling this in your check2 is causing Force Close
public class check2 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); //first call super.onCreate()
Toast.makeText(
this,
"Welcome to second page", Toast.LENGTH_LONG).show();
finish();
}
I suppose you still need to add:
#Override
public void onPause(){
super.onPause();
}
to both first and second activities.
Please change your first activity code as below
public class Check extends Activity implements OnClickListener {
//private check2 check2;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button usemirror = (Button)findViewById(R.id.widget28);
usemirror.setOnClickListener(this);
}
public void onClick(View view){
Intent mi = new Intent(Check.this , check2.class);
startActivity(mi);
}
}
& second activity code as below
public class check2 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); //first call super.onCreate()
Toast.makeText(
this,
"Welcome to second page", Toast.LENGTH_LONG).show();
finish();
}
Related
ANSWERED
I am trying to send and int to another activity.There is a button in first act. and it opens second act. and takes int from here to other page.But i cant start two of them at one time.
Here is my first activity :
public class ana_ekran extends AppCompatActivity {
public TextView ana_ekran_kule;
public TextView ana_ekran_can;
public int ana_ekran_can_int=30;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ana_ekran);
ana_ekran_kule=(TextView) findViewById(R.id.textView);
ana_ekran_can=(TextView) findViewById(R.id.textView2);
ana_ekran_can.setText(ana_ekran_can_int+" CAN");
}
public void devam (View v){
Intent i = new Intent(getApplicationContext(),fight_1.class);
i.putExtra("deger", ana_ekran_can_int);
startActivity(i);
startActivity(new Intent(this,fight_1.class));
}
}
and this is second:
public class fight_1 extends AppCompatActivity {
public TextView fight_1_can;
public int fight_1_can_int;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fight_1);
fight_1_can=(TextView) findViewById(R.id.textView3);
int i = getIntent().getIntExtra("deger",-1);
fight_1_can_int=i;
fight_1_can.setText(fight_1_can_int+"");
}
}
This startActivity(i); will do the same thing as next line but it will carry the data as well whereas startActivity(new Intent(this,fight_1.class)); will only start the other instance of fight_1 activity so
public void devam (View v){
Intent i = new Intent(getApplicationContext(),fight_1.class);
i.putExtra("deger", ana_ekran_can_int);
startActivity(i);
// start fight_1 again without any data so not required
//startActivity(new Intent(this,fight_1.class));
}
I want to finish an activity by an image, and here is what I did:
there's an ImageView in the layout [activity_login.xml] .
/**
* the activity that I want to destroy
*/
public class LoginActivity extends Activity {
public static LoginActivity activityInstance;
private ImageView imgBtnBack;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// for closing this activity by an another class
activityInstance = this;
// add event listener
imgBtnBack = (ImageView) findViewById(R.id.img_btn_back);
imgBtnBack.setOnClickListener(new LoginActivityListener());
}
#Override
protected void onDestroy() {
super.onDestroy();
}
}
/**
* the event handler of LoginActivity
*/
public class LoginActivityListener implements View.OnClickListener{
private Context context;
#Override
public void onClick(View view) {
context = view.getContext();
int id = view.getId();
switch (id) {
case R.id.img_btn_back: // close the activity by an image
LoginActivity.activityInstance.finish();
default:
break;
}
}
}
And I don't know if it was good by the way I did.
can anyone find and tell me a better way to make this work.
Add below code in your onCreate method
imgBtnBack = (ImageView) findViewById(R.id.img_btn_back);
imgBtnBack .setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
I have created an activity called ButtonActivity that has a lot of buttons and listeners. I want to create another activity TwoButtonsActivity to extend ButtonActivity so that the listeners I created can be resused.
TwoButtonsActivity is similar to ButtonActivity but with small changes.
Is this possible?
When I execute the code, I find that the extended activity do not respond to button click.
Here is the base activity:
public class ButtonActivity extends Activity {
int count = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
button.setText("Got Pressed:" + ++count);
}
});
}
}
Below is the extends Activity:
public class TwoButtonsActivity extends ButtonActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
you can have a activity with listeners same below
public class ButtonActivity extends Activity {
int count = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
}
public void mylistener1(View v)
{
switch(v.getid()){
case R.id.button:{
//do somthings
}break;
}}
public class TwoButtonsActivity extends ButtonActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
and in layout of TwoButtonsActivity(main) you set in tag of your button android:onclick="mylistener1"
and you should set to any button or view that you want it use this listener
I'm trying to set a background from ActivityB to AcitivtyA using getInstance but it only show me the Toast message, whenever turn back to ActivityA, there's no change.
I have this in my ActivityA:
private static MainActivity activityA;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activityA = this;
}
public static MainActivity getInstance(){
return activityA;
}
public void setLeavesBackground() {
FrameLayout mainFrameLyt = (FrameLayout) findViewById(R.id.mainFrameLayout);
mainFrameLyt.setBackgroundColor(Color.parseColor("#00FF00"));
Toast.makeText(getBaseContext(), "New style applied", Toast.LENGTH_SHORT).show();
}
And this in my ActivityB:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_change_style);
ImageButton leavesBtn = (ImageButton) findViewById(R.id.leavesBtn);
leavesBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity.getInstance().setLeavesBackground(); //call myFunction using activityA
}
});
}
What should I do?
You approach to get an Instance of another Activity and modify it from within another activity is wrong:
public static MainActivity getInstance(){
return activityA;
}
You have to obey the Activity lifecycle
A way how you could set another activities background is by creating a shared preferences value for the background color and use it in the onCreate method of your Activity.ñ
When creating a titlebar with a button, which is common in all activities e.g. title bar created in tabactivities. how is it possible to reach the button in all of the sub activities??
public class tabActivity extends TabActivity implements OnClickListener{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
c = this;
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.tabactivity);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("Settings",
res.getDrawable(R.drawable.preferences)).setContent(
new Intent(this, Settings.class)));
mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("About",
res.getDrawable(R.drawable.newspaper)).setContent(
new Intent(this, About.class)));
This is here where i initialize my tabs, and the custom title with buttons..
And in this class i would like to reach the buttons in the custom title.:
public class About extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
ImageView imag = (ImageView) findViewById(R.id.Position);
imag.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println("heeey");
}
});
}
The listener doesnt work??
Hooow is this possible??
public class tabActivity extends TabActivity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
c = this;
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.tabactivity);
ImageView imag = (ImageView) findViewById(R.id.Position);
imag.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
tabActivity.listener.onClick(v);
}
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("Settings",
res.getDrawable(R.drawable.preferences)).setContent(
new Intent(this, Settings.class)));
mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("About",
res.getDrawable(R.drawable.newspaper)).setContent(
new Intent(this, About.class)));
}
public static void setListner(OnClickListener listener)
{
tabActivity.listner = listener;
}
main activity does not implements eventListener
public class About extends Activity implements OnClickListener
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
});
public void onResume()
{
tabActivity.setListener(this);
}
}
code goes like this. It's hard to explain
What is the purpose of that? The event controll is available in main activity.