Android Button click go to another xml page - android

So what I've done in Eclipse, in layouts I have: activity_main.xml and activity_main2.xml. What I tried is to create a button in activity_main.xml and on click to go on screen of activity_main2.xml
so in com.example.myfirstapp I have
MainActivity.Java:
package com.example.myfirstapp;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void click1(View v) {
Log.i("clicks","You Clicked B1");
Intent i=new Intent(
MainActivity.this,
MainActivity2.class);
startActivity(i);
}
}
MainActivity2.java
package com.example.myfirstapp;
import android.os.Bundle;
import android.view.Menu;
import android.app.Activity;
public class MainActivity2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}

Write below code in your MainActivity.java file instead of your code.
public class MainActivity extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mBtn1 = (Button) findViewById(R.id.mBtn1);
mBtn1.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public void onClick(View v) {
Log.i("clicks","You Clicked B1");
Intent i=new Intent(MainActivity.this, MainActivity2.class);
startActivity(i);
}
}
And Declare MainActivity2 into your Androidmanifest.xml file using below code.
<activity
android:name=".MainActivity2"
android:label="#string/title_activity_main">
</activity>

Change your FirstyActivity to:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn_go=(Button)findViewById(R.id.YOUR_BUTTON_ID);
btn_go.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("clicks","You Clicked B1");
Intent i=new Intent(
MainActivity.this,
MainActivity2.class);
startActivity(i);
}
}
});
}
Hope it will help you.

There is more than one way to do this.
Here is a good resource straight from Google:
http://developer.android.com/training/basics/firstapp/starting-activity.html
At developer.android.com, they have numerous tutorials explaining just about everything you need to know about android. They even provide detailed API for each class.
If that doesn't help, there are NUMEROUS different resources that can help you with this question and other android questions.

Related

Create a button than send the user to the main page

I am new with Android Studio world. I made a two activity pages. The first button in the first page send the user to the second page which it works fine
public void change(View v){
setContentView(R.layout.activity_main_activity2);
but I made the same button in the second page to send the user the main page but no luck. Please see the code and let me know why I got this error.
public void HomePage(View v){
setContentView(R.layout.activity_main_activity);
This is the first activity page
package com.example.lenovo;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends ActionBarActivity {
private Menu menu;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
this.menu = menu;
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void change(View v){
setContentView(R.layout.activity_main_activity2);
}
}
and this is the second activity
package com.example.lenovo;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import static com.chapter2.example.lenovo.chapter2.R.layout;
public class MainActivity2 extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(layout.activity_main_activity2);
Button mButton = (Button) findViewById(R.id.button2);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent mIntent = new Intent(MainActivity2.this,
MainActivity.class);
startActivity(mIntent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main_activity2, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void HomePage (View v){
setContentView(R.layout.activity_main);
}
}
Currently you are switching layouts of Activity instead of changing Activity. this not right way to switch between Activities. use startActivity method with intent to start next Activity on Button click:
Start MainActivity2 from MainActivity:
public void change(View v){
Intent intent=new Intent(v.getContext(),MainActivity2.class);
startActivity(intent);
}
Start MainActivity2 from MainActivity:
public void HomePage(View v){
Intent intent=new Intent(v.getContext(),MainActivity.class);
startActivity(intent);
}
and make sure to have added both Activities in AndroidManifest.xml
If you want use inbuilt device back button then simply put
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
}
and if you want to go back by button click put this line
bbsubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
onBackPressed();
}
});
}
In the first activity you are not launching the mainactivity2.class when you click the button, instead you are just changing the content view.
and the R.id.button2 is not registered in the first activity.
So it is not working
You have to send user first page to second page using Intent
You need to add onClick tag in that button in activity_main layout like this
android:onClick="change"
in activity_main_activity2 add onClick tag in that button like this
android:onClick="HomePage"
In MainActivity2 in setContentView change like this
setContentView(R.layout.activity_main_activity2);
In first activity do like this
public void change(View v){
Intent mIntent = new Intent(MainActivity.this,MainActivity2.class);
startActivity(mIntent);
}
In second activity do like this
public void HomePage(View v){
Intent mIntent = new Intent(MainActivity2.this,MainActivity.class);
startActivity(mIntent);
}
Don't forget to declare those Activities in manifest

How to on click create a new window in an android project on eclipse?

I was trying to learn how to program with eclipse and I can't figure out how to on click of a button make a new window or just get rid of my current buttons and show others.
main activity.java
`import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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;
}
}
main activity.xml
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button" />
If you want to create a new activity on a button click you could use the below code.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mybutton = (Button)findviewbyId(R.id.button1);
mybutton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(i);
}
});
}
#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;
}

How to add & link a second activity in actionbar compat in android 2.3.3

I am using actionbar compat in my application first time. I want to add another activity to the application. When i press a SETTINGS from actionbar, i like to start the second activity.
How is possible ?
codes for my application as follows
MainActivity.java
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch(item.getItemId()){
case R.id.action_settings:
Toast.makeText(getBaseContext(), "You selected Settings", Toast.LENGTH_SHORT).show();
startActivity( new Intent().setClass(this, SecondActivity.class));
break;
}
return true;
}
}
mainactivity image
Secondactivity.java
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
public class SecondActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondactivity);
// TODO Auto-generated method stub
}
}
secondactivity image
thanks in advance
Just use a
Intent intent = new Intent(this, YourActivityToStart.class);
startActivity(intent);
on the position where you pop up a Toast.
Using 'app' instead of 'android' namespace prefix for 'actionLayout' attribute fixed it for me.
So, basically, use
app:actionLayout="#layout/yourLayout"
instead of
android:actionLayout="#layout/yourLayout"
I am assuming you are using app_compat library to get the theming engine.

onClickListener issues cannot find solution

I can't find a solution for my problem! I keep getting the error:
onClickListener cannot be resolved to a
type
Please Help!! Here is my code
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity implements onClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button send = (Button) findViewById(R.id.button1);
send.setOnClickListener(this);
}
#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;
}
Try adding this method according to your aim:
#Override
public void onClick(View view)
{
//....
//....
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
finish();
}
onClickListener should be like this OnClickListener
public class MainActivity extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button send = (Button) findViewById(R.id.button1);
send.setOnClickListener(this);
}
#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;
}
#Override
public void onClick(View v) {
}
}
Create a method like addOnClickListener();
and work on it like:
public void addOnActionListener(){
Button send=(Button)findViewById(R.id.button1);
send.setOnClickListener(new OnClickListener(){
public void onClick(View v){
//...
//...

How do you change background in activity_main from MainActivity in android sdk?

package mine.app;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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;
}
public void changeBackgroundColor(View view) {
setContentView(R.layout.color_list);
}
public void setBackgroundRed(View view) {
findViewById(R.layout.activity_main).setBackgroundColor(Color.RED);
setContentView(R.layout.activity_main);
}
}
When the app runs it creates a button that runs changeBackgroundColor. It then shows a button that runs setBackgroundRed which crashes the app.
Create a method in your MainActivity something like....
public void setActivityBackgroundColor(int color) {
View view = this.getWindow().getDecorView();
view.setBackgroundColor(color);
}
then call this function from your OnClickListener passing in colour whatever you want.
Change Your onCreate as Following
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setBackgroundDrawableResource(R.color.blue);// specify background drawable resource or background color resource here
}

Categories

Resources