I have 2 files: main_activity.xml and home.xml. I made a button in main_activity.xml
Here is the code snippet:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="#drawable/splash_background"
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/Home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="43dp"
android:onClick="home"
android:text="Home" />
</RelativeLayout>
And then, I have my home.xml. I want the button to open up home.xml. How can i do this?
I don't know any java and I am new to android development.
Here is my home.xml below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#drawable/app_bg"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
And below is my AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.idozer"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="false"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.idozer.SplashActivity"
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.idozer.MainActivity"
android:label="#string/app_name" >
</activity>
</application>
</manifest>
And that's all I have. Please, if you reply, tell me where to add the code such as the directory or between code snippets.
For managing click activity in android ,you can do as below
Implement OnClickListener on YourActivity.java class like
public class MainActivity extends Activity implements OnClickListener
Then, declare your button in .java class like
Button btn = (Button) findViewById(R.id.btnPlay);
Then use button btn variable as below
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
myClick(v); /* my method to call new intent or activity */
}
});
Handle the click event:
public void myClick(View v) {
Intent intent = new Intent(**this, Swipe.class**);
startActivity(intent);// for calling the activity
}
you also need to register your activity(.java) in android manifest as below
<activity
android:name=".Swipe"
android:screenOrientation="landscape" >
</activity>
You can use this code.
Android: OnClickListener
In our activity class we add the onclick method.
In our activity class we add the onclick method.
//On click event for button1
public void button1OnClick(View v) {
//Inform the user the button has been clicked
Toast.makeText(this, "Button1 clicked.", Toast.LENGTH_SHORT).show();
}
In the layout file we add a reference to the onclick handler in the
activity. The app will automatically bind the onclick method to the
view (in this case button1)
<?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">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button1"
android:onClick="button1OnClick"/>
</LinearLayout>
create another class goto your project right click and click class and create Home.
In that Home class file extends activity and add code like this
public class Home extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
}
}
in splash activity class add this line
Intent intent = new Intent(SplashActivity.this,Home.class);
startActivity(intent);
add Home activity class in your android manifest file
<activity android:name="com.example.idozer.Home"
android:label="#string/app_name" >
</activity>
I will give you just a little bit to get you started as this answer may help others that are having trouble with using onClick() this way but you really need to learn Java and go through the Android Docs so you can ask better questions
You need to read Here about Actviities and how to create them. Then in your code you will have a function
public void home(View v) //the name of this function comes from where you declared in your manifest `android:onClick="home"
{
Intent intent (MainActivity.this, HomeActivity.class); //MainActivity is the name of current activity and HomeActivity is the name of the activity you want to start
can add intent extras/flags/categories here
startActivity(intent);
}
You also need to add the HomeActivity in your manifest as you have for the other Activities.
But you really need to go through the docs and do some tutorials to get an idea of how the Android framework operates and you need to learn Java to make your life a whole lot easier. Besides the previous two links I have given, also see this post about click events as there are different ways to use onClick()
I hope this is enough to get you started and I really hope you go through the docs to get a better understanding of what you are doing. Good luck to you!
Another important link to get started
Intents
android:onClick was added in API level 4 to make it easier, more Javascript-web-like, and drive everything from the XML. What it does internally is add an OnClickListener on the Button, which calls your home method.
<Button
android:id="#+id/Home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="43dp"
android:onClick="home"
android:text="Home" />
.
public void home(View view){
Intent intent=new Intent (view.getContext(),Luton.class);
this.startActivity(intent);
}
In your activity class
Using java code you can do button click by getting the id of the button from xml.
<Button
android:id="#+id/myHomeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="43dp"
android:text="Home" />
.
Button button= (Button) findViewById(R.id.myHomeButton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do whatever you want on button click
}
});
Both are exactly the same
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Button Clicked", Toast.LENGTH_SHORT).show();
}
});
Related
I started off by trying to make a simple app on Eclipse.
the target for me was to create a button which simply calls a number.
however, after many tutorials and such, I still couldn't figure out where to write the code and what code was the best.
here is my Manifest . I inserted uses permission CALL_PHONE since it is required (As I learned from some tutorials)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.projectrandomfox.randomfox"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.projectrandomfox.randomfox.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>
here is my activity_main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select VAS"
android:textSize="40dp"
android:layout_gravity="center" />
<Button
android:id="#+id/balance"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Check Balance"
android:layout_gravity="center"
/> </LinearLayout>
here is my activity java
package com.projectrandomfox.randomfox;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
THATS ALL.
please help me figure out where to write code in activity, and what code to write
it would mean a lot to me :)
Add this inside your onCreate method:
Button button = (Button) findViewById(R.id.balance);
myButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View view)
{
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + telephoneNumber));
startActivity(intent);
}
}
All you need to do is set the telephoneNumber variable
First you have to get a reference your Button.
Button myButton = findViewById(R.id.balance);
Then add the onClickListener.
myButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View view)
{
// Do whatever
}
}
So I have 2 activities and in the first one I have a menu item that once clicked should open the second activity. The changing part works but it's not changing what it is supposed to, in the second activity which I activate with this :
Intent intent = new Intent(this, EditView.class);
startActivity(intent);
break;
in the first activity onOptionsItemSelected, and I click a button from the menu, in this second activity I have a diferent layout and I do this setContentView(R.layout.second); to change the layout I have a functional xml file because I tried it in another project, in the manifest file I have this :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ro.merca.ionel"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".FileList"
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=".EditView"
android:label="#string/app_name" />
</application>
</manifest>
the problem is that when I click the option from the menu it loads a simple layout without all the things I put in second.xml... I don't know that the problem is...
public class EditView extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Intent intent = getIntent();
TextView tv = new TextView(this);
final TextView name = (TextView) findViewById(R.id.name);
final TextView text = (TextView) findViewById(R.id.text);
setContentView(tv);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(1,1,1,"Salveaza Nota").setIcon(android.R.drawable.btn_default);
menu.add(1,2,2,"Anuleaza Modificari").setIcon(android.R.drawable.btn_default);
menu.add(1,3,3,"Sterge Nota").setIcon(android.R.drawable.btn_default);
menu.add(1,4,4,"Share").setIcon(android.R.drawable.btn_default);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case 1 :
Intent intent = new Intent(this, EditView.class);
this.startActivity(intent);
break;
case 2 :
break;
case 3 :
break;
case 4 :
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Nume" />
<EditText
android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Text" />
<EditText
android:id = "#+id/text"
android:inputType="text|textMultiLine"
android:minLines="5"
android:gravity="top"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
You are calling setContentView twice in onCreate(). Remove the second one and try again.
I am bit confused over here. If you are just moving from 1st activity to 2nd without passing any values then what's the use of using getIntent() etc?
Write simply this much, and you will land on your desired activity:
public class EditView extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
TextView tv = new TextView(this);
final TextView name = (TextView) findViewById(R.id.name);
final TextView text = (TextView) findViewById(R.id.text);
}
}
I have used multiple activities to handle mutiple views in android . I found that some where in the blog but I'm lost inside it.
I'm not able to switch between 2 views , my code is as follows:
main file
public class MultiViewActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next = (Button) findViewById(R.id.button1);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), MultiViews2.class);
startActivityForResult(myIntent, 0);
}
});
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button android:text="View 2"
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
another activity:
public class MultiViews2 extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
Button next = (Button) findViewById(R.id.button1);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), MultiViewActivity.class);
startActivityForResult(myIntent, 0);
}
});
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button android:text="View 1"
android:id="#+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.multiview.org"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".MultiViewActivity"
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=".MultiViews2"></activity>
</application>
</manifest>
whenever I click on button2 it shows me error the application has stopped unexpectedly.Anything I missed above. I'm very new to android programming.
Problem in Below line in MultiViews2 Class
Button next = (Button) findViewById(R.id.button2);
Instead Of
Button next = (Button) findViewById(R.id.button1);
Well considering your views are confusing and I cant say which one is main.xml and which one is main2.xml - the error is in one of the activities, both of them if you see have button 1 being referenced.
Button next = (Button) findViewById(R.id.button1);
So obviously for one it should be
Button next = (Button) findViewById(R.id.button2);
make that change and it should work. And for ease of understanding change view 1 to correspond to button 1 and similarly for 2. Else you'll run into more such problems
So I just started learning to develop Android apps, and I have a programming background (Python mainly), so I somewhat know what I'm doing. I'm having a problem with using startActivity(). I've commented code to figure out exactly where the error gets thrown, and it happens right as startActivity() is encountered. The error I get is from the emulator and it is just a popup window that says, "Unfortunately, Test has stopped." (Test is my program name) after I click my button. My code is this
package com.test.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class TestActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.test.test.MESSAGE";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public class DisplayMessageActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
}
public void sendMessage(View view) {
setContentView(R.layout.main);
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.textBox1);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
Now I know that won't do anything yet, but why is it crashing?
My XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<EditText android:id="#+id/textBox1"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/textBox1Hint" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button1"
android:onClick="sendMessage" />
</LinearLayout>
My manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".TestActivity"
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=".DisplayMessageActivity" >
</activity>
</application>
</manifest>
I saw that a lot of other people were having problems with this because they had not declared their activities in the manifest, but I think I have done so properly. Any help would be so greatly appreciated.
One possible problem is that
EditText editText = (EditText) findViewById(R.id.textBox1);
returns null because you dont set anywhere the layout for your activity (with setContentView)
If your editetext is included in view (passed to sendMessage), you can find it with
EditText editText = (EditText) view.findViewById(R.id.textBox1);
I think problem is you are on DisplayMessageActivity and starting the same activity.
What you need to do is start test activity and call DisplayMessageActivity from intent.
Use separate files for TestActivity and DisplayMessageActivity classes. In your given code you are not specifying a layout for your DisplayMessageActivity activity using setContentView.
the problem is this,
you declare in the manifest that the main class is TestActivity:
<activity
android:name=".TestActivity"
android:label="#string/app_name" >
but you wanted to start DisplayMessageActivity,
so, change this to the following:
public class TestActivity extends Activity {
#Override
I can't understand what you are actually trying to achieve by starting the same activity again. Try this,
public class DisplayMessageActivity extends Activity {
private String message ;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setLayout(R.layout.<name of the xml file> ); // change the name if it is not in layout folder
EditText editText = (EditText) findViewById(R.id.textBox1);
String message = editText.getText().toString();
Button submitBtn = (Button) findViewById(R.id.button1);
submitBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast toast = Toast.makeText(DisplayMessageActivity.this,
message,
Toast.LENGTH_SHORT);
toast.show();
}});
}
}
The above code simply displays the String, This is just a sample code... Hope that helps!!!
EDIT
To run this code you must also add id tag to your Button Tag inside XML file having value button1..
Actullay you have created the Activity inside activity. Which is not much pref-able but if want to stick with that then you have to change the android:name=".DisplayMessageActivity" to
I have an application that has a start up screen and when the user clicks a button it starts the real app in a new activity using an intent. When I try to add admob adds to the second activity the app just crashes.
I took a sample app off admobs web site and modified it to replicate what I am doing. I get the same results. The adds work on the first actiivity, but when I start the second activity it just crashes.
Here is my code
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.ads.example"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name="BannerXML"
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.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation"/>
</application>
<uses-sdk android:minSdkVersion="3" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
</manifest>
Main XML from original sample code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res/com.google.ads.example"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:text="Button"
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<com.google.ads.AdView android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="MY_AD_UNIT_ID"
ads:adSize="BANNER" />
</LinearLayout>
Original Source code with my added code to start new activity
public class BannerXML extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button myBtn = (Button) findViewById(R.id.button1);
myBtn.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
start_new_activity();
}
});
// Look up the AdView as a resource and load a request.
AdView adView = (AdView)this.findViewById(R.id.adView);
adView.loadAd(new AdRequest());
}
private void start_new_activity()
{
Intent i = new Intent(BannerXML.this, test_add.class);
this.startActivity(i);
finish();
}
}
My new activity class
public class test_add extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
AdView adView = (AdView)this.findViewById(R.id.adView1);
adView.loadAd(new AdRequest());
}
}
My XML for second layout activity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:text="This is a test"
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<com.google.ads.AdView android:id="#+id/adView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="a14e449d41a7dbe"
ads:adSize="BANNER" />
</LinearLayout>
The problem seems to be something with the XML for my new layout. Even if I never do an add request in the second activity it still crashes when it starts
Any Help Would Be Appreciated
Thanks
In your Manifest, there is no declaration for the test_add activity..
Also I suggest respecting the CamelCase convention for naming Java classes