I am trying to launch a new activity when a button is pressed but nothing happens and I get no errors. Here is my code:
Main activity
public class CSLearn_Python_AppActivity extends Activity {
String tag = "Events";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
//get content from main.xml
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button login = (Button) findViewById(R.id.loginBtn);
login.setOnClickListener(new OnClickListener(){
public void onClick(View v){
// Intent intent = new Intent("com.Main.Verification");
// startActivity(intent);
Intent myIntent = new Intent(getBaseContext(), Verification.class);
startActivity(myIntent);
}
});
}
The new activity
import android.app.Activity;
import android.os.Bundle;
public class Verification extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.verification);
}
}
Verification XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/linearLayout1" android:layout_width="match_parent" android:baselineAligned="true" android:layout_height="match_parent" android:layout_gravity="center" android:gravity="center" android:orientation="vertical">
<TextView android:text="#string/Verification" android:id="#+id/Verrification" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<EditText android:layout_height="wrap_content" android:id="#+id/password" android:inputType="textPassword" android:layout_width="112dp">
<requestFocus></requestFocus>
</EditText>
<Button android:text="#string/LoginBtn" android:id="#+id/loginBtn" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
I added this to the android manifesto
<activity android:name=".Verification"
android:label="Verification">
<intent-filter>
<action android:name="com.Main.VERIFICATION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
If anyone can point me in the right direction it would be really appreciated.
Try the code below and register your activity in the AndroidManifest.xml file:
Intent myIntent = new Intent(CSLearn_Python_AppActivity.this, Verification.class);
startActivity(myIntent);
Is CSLearn_Python_AppActivity in the same package than Verification.
May be you can try in the Manifest with:
<activity android:name="yourpackage.Verification"
android:label="#string/verification" >
</activity>
The intent-filter in your Verification activity is not necessary.
I believe the problem lies in the getBaseContext(). Use getApplicationContext() instead (the activity's context is also an option but would cause a leak). I havent quite been able to wrap my head around the base context, but it seems to be some sort of proxy doing more or less nothing in its raw implementation.
A more through explanation of the different contexts is given here.
I would try to change it like this:
Button login = (Button) findViewById(R.id.loginBtn);
login.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Intent myIntent = new Intent(v.getContext(), Verification.class);
startActivity(myIntent);
}
});
try removing the intent filter, and everything in between, from your manifest
do you have all your imports?
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
Intent myIntent = new Intent(CSLearn_Python_AppActivity.this, Verification.class);
startActivity(myIntent);
In your Android manifest file,
<activity android:name=".Verification" android:label="Verification"></activity>
You include the other activity in Manifest file:
Intent intent = new Intent(First.this, Second.class);intent.putExtra("userData",registeredUsersData);startActivity(intent);
Related
Please help me sorting out where I am wrong. Because when I start a new activity using a button click the app crashes.
I am unable to figure out. I used another Acitvity from the same application and it launched successfully. But this nelwy created activity isn't starting in any ways.
My codes are:
[secondscreen.java]
package org......android.activities;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import org......android.R;
public class secondscreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_secondscreen);
TextView myAwesomeTextView = (TextView)findViewById(R.id.myAwesomeTextView);
myAwesomeTextView.setText("brown fox");
}
}
[activity_secondscreen.xml]
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.secondscreen">
<TextView
android:id="#+id/myAwesomeTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="secondscreen activity"
android:textSize="34sp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
[starting activity code in a first activity]
buttonTraining = findViewById(R.id.buttonTraining);
buttonTraining.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(),"going to second activity...",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(),secondscreen.class);
startActivity(intent);
}
});
[partial stacktrace]
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
Worked. After Changed
From
<activity
android:name=".activities.secondscreen"
android:exported="false"/>
to
<activity
android:name=".activities.secondscreen"
android:exported="false"
android:theme="#style/Theme.AppCompat.Light.NoActionBar"/>
I have been on this for a while now (3 days). I am trying to use Eclipse to make an android app. I want to have two image buttons. Each one linking to a different site. I haven't been able to do it. I have been able to use webview to open one webpage with a button, but not two. I have moved to trying to use Intent instead because I read somewhere that that was the better way. Ultimately, what I want to do is have the page open in the app and use the back button to go back to the main screen of the app for each button/page. Here is my code so far.
MainActivity.java
package com.modsbyus.onoff;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void Light()
{
Intent intent = new Intent(Intent.ACTION_VIEW, uri.parse("https://agent.electricimp.com/BGSBpog28J0u?led=1"));
startActivity(intent);
}
public void Light1()
{
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://agent.electricimp.com/BGSBpog28J0u?led=0"));
startActivity(intent);
}
}
and my Layout
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android.onClick="Light1"
android:clickable="true"
android:src="#drawable/ic_launcheronswitch" />
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android.onClick="Light"
android:clickable="true"
android:src="#drawable/ic_launcheroffswitch" />
</LinearLayout>
Any help you guys can give would be great.
Thanks!
I would change the onclick in your xml to android:onClick="onClick" for both buttons and call your methods there in once place. Just for looks. Make sure your class implements OnClickListener.
Then your onClick method would be :
#Override
public void onClick(View v) {
Intent iExp = null;
switch (v.getId()) {
case R.id.imageButton1:
iExp = new Intent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://agent.electricimp.com/BGSBpog28J0u?led=1"));
break;
case R.id.imageButton2:
iExp = new Intent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://agent.electricimp.com/BGSBpog28J0u?led=0"));
break;
}
startActivity(iExp);
}
PS onClick on imagebuttons not available till 1.6 and your onClick in the xml has a . when it should be a :
Make sure your manifest has:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Your xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:clickable="true"
android:src="#drawable/ic_launcheronswitch" />
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:clickable="true"
android:src="#drawable/ic_launcheroffswitch" />
</LinearLayout>
Your class:
package com.modsbyus.onoff;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
public class MainActivity extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void onClick(View v) {
Intent iExp = null;
switch (v.getId()) {
case R.id.imageButton1:
iExp = new Intent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://agent.electricimp.com/BGSBpog28J0u?led=1"));
break;
case R.id.imageButton2:
iExp = new Intent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://agent.electricimp.com/BGSBpog28J0u?led=0"));
break;
}
startActivity(iExp);
}
}
In order to use the xml attribute android:onClick="methodName" you need to declare a public method that returns void and accepts a View as parameter, with same name as defined in onClick="methodName".
What you need in your code is only add the parameter View to methods Light and Light1.
So, change:
public void Light()
public void Light1()
to:
public void Light(View v)
public void Light1(View v)
And, as Rick sugested, change android.onClick= to android:onClick=
How can I open another layout xml file when I click on a button in main.xml file?
so if I have main.xml which has a button sying click here and I click it, it opens up second.xml file (layout).
First Create your two layout:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="This is Activity 1" />
<Button android:text="Next"
android:id="#+id/Button01"
android:layout_width="250px"
android:textSize="18px"
android:layout_height="55px">
</Button>
</LinearLayout>
second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="This is Activity 2" />
<Button android:text="Previous"
android:id="#+id/Button02"
android:layout_width="250px"
android:textSize="18px"
android:layout_height="55px">
</Button>
</LinearLayout>
Second Add your Activity to the manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rr"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".Activity1"
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=".Activity2"></activity>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
Activity1.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Activity1 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.Button01);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Activity2.class);
startActivityForResult(myIntent, 0);
}
});
}
}
To switch to Activity2 you have to:
Gets a reference to the button with ID Button01 on the layout using
(Button) findViewById(R.id.Button01).
Create an OnClick listener for the button.
And the most important part, creates an “Intent” to start another
Activity. The intent needs two parameters: a context and the name of
the Activity that we want to start (Activity2.class)
Activity2.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Activity2 extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Button next = (Button) findViewById(R.id.Button02);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
}
-Inflate the button from the xml
-add an onClickListener on it
-set a new layout in the onClick event
Button btn = (Button) findViewById(R.id.myButton);
btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v)
{
MyActivity.setContentView(R.layout.newlayout);
}
});
Something like this should work...
A Kotlin way:
-Add the onClick event directly in the designer.
Open the activity (Activity1.xml for example) file in the designer mode
Select the button that will trigger the transition
Add the function name on the onClick box of the right panel with all the button properties
-Open the Activity .kt file
Add the function with the name that you just defined in the designer
fun openActivity2(view: View) {
intent = Intent(view.context,Activity2::class.java)
startActivity(intent)
}
Now you have the function linked to the onClick event of your button
GET EDITTEXT INPUT TO TEXTVIEW IN ANOTHER CLASS USING A BUTTON
I am fairly new to android and I am trying to use an edittext to get user input on one screen (Activity), actually not just one edittext a few like a couple edit texts and maybe a spinner, kind of like a create a new user screen. But I know how to use a button to getText() and setText() from the edittext to the textview if they are in the same activity but can not find anywhere how to accomplish this. Here is something like what the first class's bare bones would be:
public class UserInput extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText edit = (EditText) findViewById(R.id.editText1);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
}
Now I know the magic will be in the onClick(View v){} method, but what magic exactly do I use to 1-open a new Activity that houses the textview and 2- open the Activity?
Here is the second Activity for visual reference:
public class GetText extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
TextView view = (TextView) findViewById(R.id.textView1);
}
}
Again please if anyone can even chop up the code I will use just trying to get it to work right now. Hopefully everyone can rally and give their input as to help others out that may be stuck as well. Thanks ahead of time.
Here is what I have and if force closes on me:
Main Activity:
package com.mandam.ok;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class UserInput extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText edit = (EditText) findViewById(R.id.editText1);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
EditText edit = (EditText)
findViewById(R.id.editText1);
Intent intent = new Intent(UserInput.this, GetText.class);
intent.putExtra("com.mandam.ok.GETTEXT",
edit.getText().toString());
startActivity(intent);
}
});
}
}
Second Activity:
package com.mandam.ok;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class GetText extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
TextView view = (TextView) findViewById(R.id.textView1);
Intent intent = getIntent();
String name = intent.getStringExtra("com.mandam.ok.MAIN");
//edit.setText(view.getText());
}
}
I will even attach 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="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<EditText
android:id="#+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
Second 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" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
And manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mandam.ok"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".OkActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="#string/app_name"
android:name=".GetText" >
<intent-filter >
<action android:name="com.mandam.ok.GETTEXT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Now I know I am missing something very simple, just don't know what. I appreciate the help so far. :)
Your second activity HAS a textview... saying that it IS a textview is wrong since Activities are not Views.
Here's how you can pass arguments to a new Activity... this code would be in your first Activity's onClick:
EditText edit = (EditText) findViewById(R.id.editText1);
Intent intent = new Intent(UserInput.this, GetText.class);
intent.putExtra("com.package.name.NAME", edit.getText().toString());
startActivity(intent);
where "com.package.name" is your package name. An intent is an abstraction that performs an operation. In this case, the intent tells android to create a new Activity. The putExtra method allows you to put extra information into the intent before telling Android to create a new Activity. When you put an extra variable into an intent, you need to give it a unique string identifier that preferably starts with the package name.
Once the other Activity is created, here's how you can retrieve the string:
Intent intent = getIntent();
String name = intent.getStringExtra("com.package.name.NAME");
// do whatever you want with name
Okay so I'm working on a project and I'm trying to use buttons with text inside of them in an activity, but eclipse can't find the id or chooser.xml
Here's myActivity class
package com.xxxxxxx;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageButton;
import android.view.*;
public class MyActivity extends Activity {`
private Button mButton1;
private Button mButton2;
private Button mButton3;
private Button mButton4;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//chooser is underlined (chooser cannot be resolved or is not a field)
setContentView(R.layout.chooser);
//all ids are underlined(id cannot be resolved or is not a field)
mButton1 = (Button) findViewById(R.id.button1);
mButton2 = (Button) findViewById(R.id.button2);
mButton3 = (Button) findViewById(R.id.button3);
mButton4 = (Button) findViewById(R.id.button4);
mButton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(MyActivity.this, Activity2.class);
startActivity(myIntent);
}
});
mButton2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(MyActivity.this, Activty3.class);
startActivity(myIntent);
}
});
mButton3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(MyActivity.this, Activity4.class);
startActivity(myIntent);
}
});
mButton4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(MyActivity.this, Activity5.class);
startActivity(myIntent);
}
});
}
}
Here's the chooser.xml file
<?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">
<Button
android:id="#+id/button_1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/button_1" />
<Button
android:text="#string/button_2"
android:id="#+id/button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/button_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_3"
/>
<Button
android:id="#+id/button_4"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/button_4"
/>
</LinearLayout>
And here's the string resource xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Test App</string>
<string name="test_image">test.png</string>
<string name="button_1">button_1</string>
<string name="button_2">button_2</string>
<string name="button_3">button_3</string>
<string name="button_4">button_4</string>
</resources>
Your gen files most likely aren't generated yet. Try building it, it should make the lines go away.
1) You need to import your R class. import <android package>.R Ref: R.id cannot be resolved AND R cannot be resolved - Android error
2) Aren't your R.id.button1 supposed to be R.id.button_1 and so forth for other buttons?
Is chooser.xml in the correct folder (the default layout folder provided with the project)? Have you perhaps tried to go to Project and choose Clean? Everything looks okay in your code.
I know this is a very old post, though I still stumbled upon it in my search for a similar problem. When I built the android project, it automatically added:
import android.R;
When I used the advice above,
import <android package>.R;
and I still used R.id....., it still propagated an error. I removed
import android.R;
and no errors.