Please help me.
I have eight activities.
Suppose that A1 is first activity.
A1 has seven button.
i want to call other activities by clicking buttons From A1.
button2 = (Button) findViewById(R.id.Home_Page);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(activity1.this, activity2.class);
startActivity(i);
finish();
}
});
you have to insert intent in every button and then give the class path like I did..
Related
I have three fragments in two activities;fragmentA in activityA and fragmentB,fragmentC in activityB.when i click a button in fragmentA i want to go specifically to fragmentB (not fragment c) in activityB. please help me in this regard.
add this in OnCreateView of Fragment A:
Button button = v.findViewById(R.id.mybutton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), ActivityB.class);
startActivity(intent);
}
});
I am having multiple activity in my program the flow of it is like MainActivity-LoginActivity-NextLoginActivity etc.,I am applying transparency theme on each activity except main activity so I want when I click any activity it should show MainActivity in background rather than previous activity.
Maybe you want result below?
You open app, app into these activity in order: MainActivity -> LoginActivity -> OtherActivity
And you want to click a button that in OtherActivity, you don't want to return the LoginActivity but MainActivity
If it is, you can do this:
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
});
In this line: intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Activity will use the Launch Mode by singleTask.
If activity stack has MainActivity, it will finish other activity that on top of MainActivity, then return MainActivity.
You can try to give an id to the parent layout in each activity and handle clicks on it.
Something like:
LinearLayout LoginActivity = (LinearLayout) findViewById(R.id.login_activity);
LoginActivity.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(this, MainActivity));
//If you want to close this one
//this.finish();
}
});
i created one tab menu.when i click the tab it opens one activity under the tab that activity contains two buttons.when clicking the buttons it open the new activity it does not open a activity under that tab itself.how should i open a another activity under the same menu.please help friends.
i used this code to open a another activity when button was clicked
button1.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Intent intent = new Intent(SongsActivity.this,FiveActivity.class);
startActivity(intent);
finish();
} });
button2.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent obj=new Intent("com.layout.Contactregistration");
startActivity(obj);
}
});
If i understood your question then You can use simple logic for your button click.
First you can change your layout on button click that you are trying to open in a new activity.for this you can use
setContentView(R.layout.SecondLayout);
again on button click and initialize your FiveActivity's resources and use them.
this saves your memory and comfusion of activities.
Am relatively new to android, and this is the issue am having, i have a main layout which i would call layout A and another which i will call B for the purpose of this question, i included layout B in layout A with a include tag, layout B serves as a header in layout A, but layout B comes with a button, this button is not clickable in layout A, Please how can i make this button from layout B clickable.on the click of the button it is suppose to start another activity. thank you for your response. i will wait here for a while for any other questions you might have.
This is what you need to do to jump to second activity from your current activity.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button1);// id of your button in layout.xml
button.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent myIntent = new Intent(CurrentActivity.this, SecondActivity.class);
CurrentActivity.this.startActivity(myIntent);
}
});
}
Im making a simple Androidgame , where the user selects an answer to a question (ind Activity1) by clicking a radiobutton. When the correct radiobutton is clicked, a button in the "Credits" (Activity2) will get VISIBLE and be available for the user.
How can I make this happen? i canĀ“t get the two activities to work together?
The code from Activity 1 (the Question) where the user clicks the radiobutton:
final Button s1 = (Button) findViewById(R.id.radio0);
final Button s2 = (Button) findViewById(R.id.radio1);
final Button s3 = (Button) findViewById(R.id.radio2);
s1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
btnEliminar.setVisibility(View.VISIBLE);
btnKort.setVisibility(View.VISIBLE);
s1.setVisibility(View.GONE);
s2.setVisibility(View.GONE);
s3.setVisibility(View.GONE);
AlertDialog.Builder builder = new AlertDialog.Builder(Activity1.this);
builder.setMessage("...");
builder.setCancelable(true);
builder.setPositiveButton("...", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
The code from Activity2 where the button should get visible:
public class Activity2 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
Button credit1 = (Button) findViewById(R.id.buttoncredit1);
credit1.setVisibility(View.INVISIBLE);
....
credit1.setVisibility(View.VISIBLE);
Hope someone is able to help me
Thank you
This can be done via Intent's extras. Should look something like this:
//Somewhere in Activity1
Intent intent = new Intent();
intent.setClass(getApplicationContext(), Activity2.class);
intent.putExtra("makeButtonVisible", true); // Or false
startActivity(intent);
//Somewhere in Activity2
boolean isButtonVisible = getIntent().getBooleanExtra("makeButtonVisible");
// Change button's visibility accordingly
To understand the best answer to this question you must understand that each Activity in Android is essentially isolated from any other activities. This means you cannot change things such as widget visibility from one activity to another.
That said, the best solution is likely to pass extras using an Intent when you start up your 2nd activity. That can be done quite easily by following these steps.
I hope this helps!
In Activity1
s1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent(this,Activity2.class);
if(s1.isChecked)
intent.putExtra("state",0);
else
intent.putExtra("state",-1);
startActivity(intent);
});
In Activity 2
Intent intent = getIntent();
int state = Integer.parseInt(intent.getExtras().get("state").toString());
credit1.setVisibility(state);
Hope this help
You can do it by different ways:
1) Implement static method in Activity2, and call it from Activity1. Don't change visibility directly - just change some static field of Activity2 and then handle it in onStart()
2) When starting Activity2, put values into Intent and then handle them in Activity2. But keep in mind, that user can use "Back" button, so you need to handle it correctly (for example, you can .finish() activity and then start new one).
First one may sounds easier, but try to avoid such static methods. Second way is better for your app design.
you should use Intent for change a activity to an other activity:
Intent intent = new Intent(thisClss.this, AnOtherClass.class);
startActivity(intent);