As far as I can tell, everything needed for a button to respond is in place. Im baffled so would appreciate a pair of extra eyes on this.
Simple homepage activity, login or create a profile buttons displayed. Click the login button and the next activity should display. Application runs on device but buttons do not respond.
Code
public class HomeActivity extends Activity implements OnClickListener {
Button login;
Button createProfile;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
login = (Button) findViewById(R.id.loginButton);
login.setOnClickListener(this);
createProfile = (Button) findViewById(R.id.createProfileButton);
createProfile.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.home, menu);
return true;
}
#Override
public void onClick(View view) {
Intent intent;
switch(view.getId()){
case R.id.loginButton: /** Start a new Activity LoginActivity.java */
intent = new Intent(this, LoginActivity.class);
this.startActivity(intent);
break;
case R.id.createProfileButton: /** Start a new Activity About.java */
//intent = new Intent(this, AboutActivity.class);
//this.startActivity(intent);
break;
}
}
}
HomeScreen layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".HomeActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:id="#+id/loginButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="160dp"
android:layout_marginRight="51dp"
android:text="Login" />
<Button
android:id="#+id/createProfileButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/loginButton"
android:layout_alignBottom="#+id/loginButton"
android:layout_toLeftOf="#+id/loginButton"
android:text="createProfile" />
Login Layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".HomeActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is the login page!" />
Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.nutrtionintuition"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.nutrtionintuition.HomeActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".LoginActivity"
android:label="#string/app_name" >
</activity>
</application>
LoginACtivity
public class LoginActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
}
In both classes (HomeActivity and LoginActivity), you set the content view like this:
setContentView(R.layout.activity_home);
Your code is working, but the new activity is loading the same layout.
Change
setContentView(R.layout.activity_home);
to
setContentView(R.layout.activity_login);
(or whatever your login layout is named) in your LoginActivity class.
Your code is OK and it must work. It seems that your activites are not included in your AndroidManifest.xml.
If so, maybe you are actually launching your another activity, but you may have missed to put the right layout id in the setContentView function.
Related
I have to make a list menu item in android that contains three menu items in a list view i want to show some content on click of each menu.Here is a screen of which i have done so far..
This is the menu i have created .i want that i will click on Menu1 and the Menu1 will show in a page..i am posting my code..
public class MainActivity extends ListActivity {
String[] menus={"Menu1","Menu2","Menu3"};
#Override
protected void onListItemClick(ListView l,View v,int position,long id){
super.onListItemClick(l,v,position,id);
String name=menus[position];
try{
Class main=Class.forName("com.betacoading.createlistmenu.app"+name);
Intent intent=new Intent(MainActivity.this,main);
startActivity(intent);
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
setListAdapter(new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_expandable_list_item_1,menus));
}
#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) {
// 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);
}
}
public class Menu_1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_1);
Button back=(Button)findViewById(R.id.button);
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent in=new Intent(getApplicationContext(),MainActivity.class);
startActivity(in);
}
});
}
}
public class Menu_2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_2);
Button back=(Button)findViewById(R.id.button);
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent in=new Intent(getApplicationContext(),MainActivity.class);
startActivity(in);
}
});
}
}
public class Menu_3 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_3);
Button back=(Button)findViewById(R.id.button);
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent in=new Intent(getApplicationContext(),MainActivity.class);
startActivity(in);
}
});
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.betacoading.createlistmenu.app" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.betacoading.createlistmenu.app.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Menu_1"
android:label="#string/app_name" >
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Menu_2"
android:label="#string/app_name" >
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Menu_3"
android:label="#string/app_name" >
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Menu_1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Menu 1"
android:textSize="24dp"
android:layout_gravity="center_horizontal"
/>
<Button
android:id="#+id/button"
android:layout_width="200dp"
android:layout_height="400dp"
android:text="Back"
android:textSize="16dp"
/>
</LinearLayout>
Menu2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Menu 2"
android:textSize="24dp"
android:layout_gravity="center_horizontal"
/>
<Button
android:id="#+id/button"
android:layout_width="200dp"
android:layout_height="400dp"
android:text="Back"
android:textSize="16dp"
/>
</LinearLayout>
menu3.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Menu 3"
android:textSize="24dp"
android:layout_gravity="center_horizontal"
/>
<Button
android:id="#+id/button"
android:layout_width="200dp"
android:layout_height="400dp"
android:text="Back"
android:textSize="16dp"
/>
This is my code i have tried so far..please help me to make it dynamic
Try this link
http://www.androidviews.net/2013/04/sliding-layer/
This will help you to make sliding menu from bottom, left, right, and top with lots of animation.
You can put anything inside it and easily work on it in you java class.
Hope it will help.
I just wanted to click on a button in the MainActivity. After I clicked on this button a new activity appears. In this activity is also a button and when I click on this button a text appears. My Problem now is, that the text doesn't appear, when I click on the button. My code for clicking on a button so that a text appears is actually working. But not if I use it in a new Activity/Layout/ page. So there must be s.th. wrong with the Connection between the first and the second activity?!
MainActivity.java:
package com.example.xxx;
import android.os.Bundle;
import android.app.Activity;
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 page2 (View view){
setContentView(R.layout.pagetwo);
}}
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="#+id/BtnKlick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="178dp"
android:text="Button"
android:onClick="page2"/>
</RelativeLayout>
Pagetwo.java:
package com.example.xxx;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Pagetwo extends Activity implements OnClickListener {
public Button btn1;
public TextView tw1;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.pagetwo);
btn1 = (Button) findViewById(R.id.BtnKlick);
tw1 = (TextView) findViewById(R.id.Text);
btn1.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
tw1.setText("Hallo");
}}
pagetwo.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/BtnKlick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Klick" />
<TextView
android:id="#+id/Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="87dp" />
</RelativeLayout>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.xxx"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.xxx.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Update
All you seem to be doing is swapping your layouts within PageOne activity.
Change
public void page2 (View view){
setContentView(R.layout.pagetwo);
}
to
public void page2 (View view){
Intent i = new Intent(this, Pagetwo.class);
startActivity(i);
}
This will actually launch your pagetwo activity rather than just swapping layouts within pageone activity
Make that change and all should be good. You will most likely have to add some imports, at least for the intent class
Also Activities must be registered in the manifest. To add the activity to your manifest check the code below
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.xxx.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="Pagetwo"
android:label="#string/app_name" >
</activity>
</application>
Change the label to something more appropriate than app name once you have it working
End update
Try renaming your xml view id's to meet coding standards by using all lower case letters and separating words with underscores. The Android platform may well be getting confused as Text is a reserved word.
so
<TextView
android:id="#+id/Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="71dp"
android:textSize="70sp" />
becomes
<TextView
android:id="#+id/tv_hallo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="71dp"
android:textSize="70sp" />
and your code becomes
...
public TextView tw;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.eclipse);
btn = (Button) findViewById(R.id.BtnKlick);
tw = (TextView) findViewById(R.id.tv_hallo);
btn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
tw.setText("Hallo");
}
...
Not sure if this is going to have much of an effect but it is just possible android is getting the word Text confused
Also check your logcat output to see if there are any errors.
I have developed an app for page navigation . when a button is clicked the page should navigate to the second page. when i run the project and opened my app its showing unfortunately API demo has stopped. I have posted the entire code of my app . PLS HELP ME OUT...
//MAIN ACTIVITY
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:layout_marginLeft="73dp"
android:layout_marginTop="14dp"
android:text="Click" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button1"
android:layout_alignParentTop="true"
android:layout_marginLeft="14dp"
android:layout_marginTop="34dp"
android:text="Main Activity" />
</RelativeLayout>
//SECONDSCREEN ACTIVITY
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sceond Screen Activity"
tools:context=".SecondScreenActivity"/>
</LinearLayout>
//JAVA CODE FOR MAIN ACTIVITY
package com.example.navigate;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addButtonOnClickEventListener();
}
public void addButtonOnClickEventListener()
{
Button button = (Button)findViewById(R.id.button1);
final Context context = this;
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context,SecondScreenActivity.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.main, menu);
return true;
}
}
// JAVA CODE FOR SECOND SCREEN ACTIVITY
package com.example.navigate;
import android.app.Activity;
public class SecondScreenActivity extends Activity {
}
// MANIFEST FILE
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.navigate"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.navigate.SecondScreenActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name = "android.intent.action.MAIN"/>
<category android:name = "android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
First problem MainActivity is your launcher page here but you define the secondactivity as luncher in manifest.change it first.
Add an oncreate() to secondactivity otherwise there is nothing to show when the button click.
Third change your MainActivity Context to activity which is MainActivity.this not local
Other things are fine , and hope you understand your small problems..
Change your manifest to something like this:
// MANIFEST FILE
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.navigate.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name = "android.intent.action.MAIN"/>
<category android:name = "android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name = ".SecondScreenActivity" />
</application>
And in your SecondScreenActivity add an oncreate method with the contet view like:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondactivity_layout);
}
And also in your mainactivity change the context to MainActivity.this . Here you define the context inside onclicklistener which links to your local method not the class.
All I want to do is a simple Button page switcher between the MainActivity and ArticleGroups pages. I will be implementing a paging class afterwards but I want to get the buttons working first.
Once I run the Application the app will crash and force close so I am certainly doing something wrong.
MainActivity.java
public class MainActivity extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button)findViewById(R.id.articlebtn);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
Intent i = new Intent(MainActivity.this, ArticleGroup.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;
}
}
activity_main.xml
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context=".MainActivity">
<fragment
android:id="#+id/activitymain_fragment"
android:name="com.dharris.mindfulemployer_v4.MainActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/articlebtn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#color/aqua"
android:onClick="onClick" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/articlebtn"
android:layout_alignBottom="#+id/articlebtn"
android:layout_toRightOf="#+id/articlebtn"
android:text="#string/hello_world" />
</RelativeLayout>
ArticleGroup.java
public class ArticleGroup extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.articlegroup);
Button b = (Button) findViewById(R.id.articlebtn);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(ArticleGroup.this, MainActivity.class);
startActivity(i);
}
});
}
}
articlegroup.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="#+id/articlegroupfragment"
android:name="com.dharris.mindfulemployer_v4.ArticleGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/articlebtn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#color/aqua"
android:onClick="onClick" />
</RelativeLayout>
Manifest File
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dharris.mindfulemployer_v4"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<!-- Activity Default -->
<activity
android:name="com.dharris.mindfulemployer_v4.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Activity ArticleGroup -->
<activity
android:name=".ArticleGroup"
android:label="#string/app_name"
></activity>
</application>
</manifest>
If anyone can make sense as to why this is not working then I will be very appreciative.
You have
android:onClick="onClick" />
You need to have a method by name onClick in Activity which takes view as a param
You can remove this
Button b = (Button)findViewById(R.id.articlebtn);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
Intent i = new Intent(MainActivity.this, ArticleGroup.class);
startActivity(i);
}
});
And have
public void onClick(View V)
{
// do something on button click
}
Delete this line from activity_main.xml and articlegroup.xml
android:onClick="onClick"
Here I done code for 5 buttons like Aboutus,development,carriers,services,contactus.
Here I clicked about us button that shows errors as unfortunately app closed.
Here is my code :
MainActivity.java
public class MainActivity extends Activity {
Button aboutus,development,service,carriers,contactus;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button aboutus=(Button) findViewById(R.id.aboutus);
aboutus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(v.getContext(),aboutus.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;
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/aboutus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="About us"
/>
<Button
android:id="#+id/services"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Services"
/>
<Button
android:id="#+id/development"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Development"
/>
<Button
android:id="#+id/carriers"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Carriers"
/>
<Button
android:id="#+id/contactus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Contact Us"
/>
</LinearLayout>
</RelativeLayout>
aboutus.java
public class aboutus extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aboutus);
}
}
aboutus.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Aboutus Page"/>
</LinearLayout>
AndroidManifest.xml is this correct ?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.globalinfosoft"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.globalinfosoft.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<Activity android:name= "aboutus"/>
</application>
</manifest>
MainActivity.java
package com.example.a;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Button aboutus, development, service, carriers, contactus;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button aboutus = (Button) findViewById(R.id.aboutus);
aboutus.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//Starting a new Intent
Intent nextScreen = new Intent(getApplicationContext(),aboutus.class);
startActivity(nextScreen);
}
});
}
}
Here is Androidmanifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.a"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.a.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.a.aboutus"></activity>
</application>
</manifest>
Instead of v.getContext() use 'getApplicationContext()'.
Intent i = new Intent(getApplicationContext(),aboutus.class);
startActivity(i);
Try this ..
Button aboutus=(Button) findViewById(R.id.aboutus);
aboutus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),aboutus.class);
startActivity(i);
}
});
First, you should make sure that you have declared the aboutus acitivity in your AndroidManifest.xml file.
<Activity android:name= "aboutus"/>
Second, you can use
Intent i = new Intent(getApplicationContext(),aboutus.class);
or
Intent i = new Intent(MainActivity.this,aboutus.class);
instead of
Intent i = new Intent(v.getContext(),aboutus.class);
Intent i = new Intent(v.getContext(),aboutus.class);
startActivity(i);
instead of code use following code and instruction:
Intent i = new Intent(MainActivity.this,aboutus.class);
startActivity(i);
you have to declare the About us activity on AndroidManifest.xml like this
<Activity android:name= ".aboutus"/>