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);
}
});
}
Related
I have a main activity where the user has 3 list views and 3 buttons displayed. When the user clicks on 2 of the three buttons I want the user to be sent to another screen (another layout). I already have the two other layouts created and the two other classes created. I am not sure why this is not working. My application reports no errors, no warnings, and I have found that I cannot go through my code step by step because my debugger is not working.
I have an OnClickListner(I am currently just testing one button at a time to get the issue fixed) that is linked to the button in the Main Activity layout.xml android:OnClick. I have an Intent put in place within the Main Activity that (according to multiple sources and YouTube tutorials) is set up properly and the new activity that the user should be redirected to is linked to the appropriate layout. When I run the application the button click registers (I know this because I have a sound effect in place when the button is pressed) but nothing else happens. Also all of my activities are present in the AndroidManifest.xml file. And all of the activity files are at the same folder level, all layouts are under res/layout
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// instantiate button
Button btnClicked = (Button)findViewById(R.id.goal);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.click);
btnClicked.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,
ChangeGoal.class));
mp.start();
}
});
// method called when set goal button is clicked
public void setGoal(View view)
{
// intent to send user from main activity to the change goal
activity
Intent setGoal = new Intent(this, ChangeGoal.class);
startActivity(setGoal);
}
}
public class ChangeGoal extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_change_goal); // connect
class to the other layout
}
}
//button from activity_main.xml
<Button
android:id="#+id/goal"
android:layout_width="200dp"
android:layout_height="200dp"
android:text="#string/Set"
android:onClick="setGoal"
android:textSize="20sp"
android:textColor="#000000"/>`
Again, I am expecting the button click from the main activity to send the user to a new activity where they need to make a selection and then return to the main activity. Thus far the button click registers (I can hear it since I included some audio already upon the button click) but the application does nothing, I am left on the same screen and the button can be pressed again and again.
In your Activity layout, you have declared this on your Button:
android:onClick="setGoal"
Then, in code, you have attached an onClickListener to the same Button. This is wrong. Do either the one, or the other, but not both.
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.
On button click event I'm using setContentView(R.layout.activity_main); it works correctly.
When if I try to start new activity with Intent and startactivity commands it loads layout twice it looks like layout loading correctly then 1 second same layout loaded again.
Before start activity its loaded single time.
show.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
String selected = spinner0.getSelectedItem().toString();
if(selected.equals("Item 2"))
{
Intent intent = new Intent(second_layout.this,MainActivity.class);
setContentView(R.layout.activity_main);
startActivity(intent);
}
}
});
I'm guessing this line is the problem.-
setContentView(R.layout.activity_main);
setContentView will just change the layout for the current activity, so you're changing the current layout to activity_main, and then you open the Intent for MainActivity class.
Just remove that line.
When you are starting a new activity, there is no need of setContentView while starting the intent.
The intent which is getting started will have the code for loading the layout. So please remove this line.
I hope, in your MainActivity.class, you will already be writing setContentView(R.layout.activity_main) and this is enough for showing the required layout. So remove this extra line which you have included while starting the intent.
You can not set your second activity layout in your first activity before starting your second activity. It will set automatically in your second activity's onCreate() method. So you should write setContentView(R.layout.activity_main); in your MainActivity's onCreate() method. Just remove it from the onClick listener.
So write in your onClick as below:
show.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
String selected = spinner0.getSelectedItem().toString();
if(selected.equals("Item 2"))
{
Intent intent = new Intent(second_layout.this,MainActivity.class);
startActivity(intent);
}
}
});
And in your MainActivity you have to set your layout as below:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
setContentView(R.layout.activity_chart);
}
});
Hi i have the above code wherein upon clicking a button i am trying to display them activity activity_chart. In that activity i want to display a graph. Here i am calling a method createIntent(). But my problem is that the graph is not getting plotted. Please help i am new to android.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
createIntent();
}
public Intent createIntent()
{
...
}
Am i calling the method right.
A new Activity is called with:
startActivity(new Intent(currentActivity.this, nextActivity.class));
Then in your new Activities onCreate(Bundle savedInstance) method you can call setContentView(Layout layout); to set the new Layout.
So if you want to change the Activity when clicking on a Button you have to do the following:
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
startActivity(new Intent(currentActivity.this, nextActivity.class));
}
});
You are currently only changing the layout of the current Activity when clicking the button and not changing to another Activity.
I hope I understood you correctly. If not then provide me with some more code so I can try to understand what you want to do.
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);