2nd layout Button is not working - java

This is my First Question & I'm a new android learner,
Suddenly in my ellipse 2nd layout button is not working;
after pressing button of my first layout it working & going to second layout.
But in 2nd layout my Button is not working, I tested it very simple code. Please check
public class MainActivity extends Activity {
Button b1,b2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
b1.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
//setContentView(R.layout.activity_main_activity2); Need to remove
startActivity(new Intent(MainActivity.this,MainActivity2.class));
}
});
}
#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;
}
}
This is my 2nd activity code
public class MainActivity2 extends Activity {
EditText ed;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
ed = (EditText) findViewById(R.id.editText1);
button = (Button) findViewById(R.id.button21);
button.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
Toast.makeText(getBaseContext(), "Working", Toast.LENGTH_SHORT).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_activity2, menu);
return true;
}
}

*You are simply changing the layout on first activity using setContentView(). You are not navigating from one activity to another.
Either you can register the button of layout 2 by onClicklistener after setting it on activity.
b1.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
setContentView(R.layout.activity_main_activity2);
button = (Button) findViewById(R.id.button21);
button.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
Toast.makeText(getBaseContext(),"Working",Toast.LENGTH_SHORT).show();
}
});
}
});
or you can goto your other activity
b1.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// setContentView(R.layout.activity_main_activity2); remove
startActivity(new Intent(MainActivity.this,MainActivity2.class));
}
});

Related

Toolbar Icon unable to click and execute to next page Android Studio Java

I want my Home Icon in my Toolbar in my MainActivity to execute to go next page, but apparently, it is not working. Despite clicking the Home Icon in the Toolbar, it did not execute.
What is the correct way to correct the codes below
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setSupportActionBar(toolbar);
buttonTiaoLiWan = (ImageButton) findViewById(R.id.BtnTLW);
buttonTiaoLiWan.setOnClickListener(new View.OnClickListener() {
public void onClick (View v){
openTLWpage();
}
});
buttonZhenZhuWan = (ImageButton) findViewById(R.id.BtnZZW);
buttonZhenZhuWan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openZZWpage();
}
});
buttonBuYaoJing = (ImageButton) findViewById(R.id.BtnBYJ);
buttonBuYaoJing.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) { openBYJpage();}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_item,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
switch(item.getItemId()){
case R.id.action_home:
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
return true;
default:
return super.onOptionsItemSelected(item);
}
}

eclipse android application syntax error

i just started to use eclipse, and im trying to do my first android application. But apparently something is wrong with my syntax error , and i dont know how can i fix it or where did i do wrongly. so i hope someone here can help me. Thanks :) pardon me for my bad english.
Those underlined in red are errors but i dont know how can i fix them.
starts below here
Here is my .java
public class Category extends Activity { <---(**this sign is underlined in red**)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_category);
Button switchButton = (Button) findViewById(R.id.button1);
switchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Category.this, Cake.class);
startActivity(intent);
Button switchButton = (Button) findViewById(R.id.button2);
switchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Category.this, Cookie.class);
startActivity(intent);
};
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.category, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
} ^
(**this sign is underlined in red**)
};
Unfortunately you're trying to initialize your button listener in another button listener. That's wrong.
Corrected:
Button switchButton = (Button) findViewById(R.id.button1);
switchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Category.this, Cake.class);
startActivity(intent);
};
});
Button switchButton2 = (Button) findViewById(R.id.button2);
switchButton2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Category.this, Cookie.class);
startActivity(intent);
};
});

Can't start another activity in Android

I'm learning android developpement, and I wrote a short and easy code, but it doesn't work.
I can't start another activity,despite many try ! Here is the code of the main activity :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pageaccueil);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.pageaccueil, menu);
return true;
}
public void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pageaccueil);
final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Pageaccueil.this, Devise.class);
startActivity(intent);
}
});
}
}
And the button part of the XML Layout from the first/main activity :
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="38dp"
android:text="convertisseur de devises"
/>
The second activity is "devise", and here is its code :
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class Devise extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_devise);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.devise, menu);
return true;
}
}
Does anyone know How can I make the second activity to launch ? I tried many times without any success .
Thank you in advance !!
use Following in your onCreate() instead of onCreate1()
final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Pageaccueil.this, Devise.class);
startActivity(intent);
}
});
}
also study the Life Cycle of Activity http://developer.android.com/training/basics/activity-lifecycle/index.html

Transfer data from EditText to TextView of another activity using setText and onClickListener?

I am a newbie.. I am trying to get text on another activity which I have created, from an activity created apriori. I could do this using onClickListener, but I am unable to get it done using onClick and setText(). The code runs well until the second activity, but when I type and send button, the code crashes. Help me!
Here's the XML file:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout="#+id/tv1"
android:text="#string/hello_world" />
Here's the Java file:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt = (Button) findViewById(R.id.bt1);
bt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,
DisplayMessageActivity.class);
EditText et = (EditText) findViewById(R.id.et1);
String message = et.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
});
}
#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;
}
}
This code can help you
MainActivity.java
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText et=(EditText) findViewById(R.id.editText2);
Button bt=(Button) findViewById(R.id.button2);
bt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent=new Intent(Main.this,DisplayMessageActivity.class);
intent.putExtra("theText", et.getText().toString());
startActivity(intent);
}
});
}
DisplayMessageActivity.java
public class DisplayMessageActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.example.theme.R.layout.DisplayActivity);
TextView tv=(TextView) findViewById(com.example.theme.R.id.textView2);
tv.setText(getIntent().getExtras().getString("theText"));
}
Replace
android:layout="#+id/tv1"
with
android:id="#+id/tv1"

error in java android programming

I'm trying to make a property animation on ImageButton, a button will start the animation when clicked on, but in the private static class ImageButtonAnimatorHelper method, it's showing an error.
public class MainActivity extends Activity {
// ImageButton jackfruit = (ImageButton) findViewById(R.id.btnjackfruit);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button but = (Button) findViewById(R.id.btn);
but.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ball();
}
public void ball() {
ImageButton ball = (ImageButton) findViewById(R.id.btnball);
ball.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
// ImageButton b = (ImageButton) findViewById(R.id.btnball);
ImageView banan = (ImageView) findViewById(R.id.banana);
banan.setVisibility(View.VISIBLE);
} // onClick
}); // setOnClickListener
ObjectAnimator horizontalAnimator = ObjectAnimator.ofInt(
new ImageButtonAnimatorHelper(ball), "marginLeft", 0, 600);
horizontalAnimator.setDuration(2000);
horizontalAnimator.setRepeatCount(ValueAnimator.INFINITE);
horizontalAnimator.setRepeatMode(ValueAnimator.REVERSE);
horizontalAnimator.setInterpolator(new LinearInterpolator());
horizontalAnimator.start();
} // onCreate
private static class ImageButtonAnimatorHelper {
ImageButton ballButton;
public ImageButtonAnimatorHelper(ImageButton imagebutton) {
ballButton = imagebutton;
}
public void setMarginLeft(int margin) {
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) ballButton
.getLayoutParams();
params.leftMargin = margin;
ballButton.setLayoutParams(params);
} // setMarginLeft
} // ImageButtonAnimatorHelper
});
}
#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;
}
}
I'm confused with when to use private/public/abstract class..

Categories

Resources