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=
Related
hello I want put one button . when click that button from this page go to another page. I think I use Relative layout but I don't know exactly how to use it. please put complete code from "main.xml" and "activity" because I'm an amateur!!! thanks
Oh silverboy...
MainActivity:
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, SecondActivity.class));
}
});
}
}
activity_main.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/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go to second page"/>
</RelativeLayout>
I'm beginner in Android. I'm trying to make an app where when user tap on button he should go to another activity let say if user tap on button 1 he see new activity with information-A and if he press button 2 he see information-B in same activity he saw information A.
How can I set that.
If I am understanding your question correctly, here's a solution.
You can use the putExtras method of Intent to pass data in key/value pairs to the next activity.
Add the following to activity 1 layout:
<Button
android:id="#+id/btnA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button A"
android:onClick="goToActivityFromButtonA" />
<Button
android:id="#+id/btnB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/btnA"
android:text="Button B"
android:onClick="goToActivityFromButtonB" />
Add the following methods to Activity 1:
public void goToActivityFromButtonA(View view) {
Intent intent = new Intent(this, ActivityTwo.class);
intent.putExtra("buttonData", "You clicked button A");
startActivity(intent);
}
public void goToActivityFromButtonB(View view) {
Intent intent = new Intent(this, ActivityTwo.class);
intent.putExtra("buttonData", "You clicked button B");
startActivity(intent);
}
Add the following to Activity 2 layout:
<TextView
android:id="#+id/txtText"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Add the following to the onCreate method of activity 2:
TextView txtText = (TextView)findViewById(R.id.txtText);
txtText.setText(getIntent().getExtras().getString("buttonData"));
I just created a sample project for you:
https://drive.google.com/file/d/0B0S6sddMC_rMSUNieUxkR2lFRzQ/view?usp=sharing
you have first create ui for first activity like this
<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=".FirstActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="77dp"
android:text="Button 1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="110dp"
android:layout_marginLeft="38dp"
android:layout_toRightOf="#+id/button1"
android:text="Button 2" />
</RelativeLayout>
ui of second activity like this
<?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:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="second actvity" />
</LinearLayout>
activity first code where i set index with intent for both button click(you also put string double ling and message with intent)
package com.example.teststart;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class FirstActivity extends Activity implements OnClickListener{
Button b1,b2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
b1=(Button)findViewById(R.id.button1);
b2=(Button)findViewById(R.id.button2);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int id=v.getId();
switch(id){
case R.id.button1:
Intent i1=new Intent(FirstActivity.this,secondactivity.class);
i1.putExtra("one", 1);
startActivity(i1);
break;
case R.id.button2:
Intent i2=new Intent(FirstActivity.this,secondactivity.class);
i2.putExtra("one", 2);
startActivity(i2);
break;
}
}
}
and second activity i get that integer value to identify which button is clicked and fire operation on that condition
package com.example.teststart;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class secondactivity extends Activity{
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.secondactvity);
tv=(TextView)findViewById(R.id.textView1);
Bundle extras = getIntent().getExtras();
int index = extras.getInt("one");
if(index==1){
tv.setText("nformation-A "+index);
}else if(index==2){
tv.setText("nformation-B "+index);
}
}
}
this my xml file main
<?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"
android:background="#raw/christmas"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/by_kostas"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="83dp"
android:text="#string/by_kostas"
android:textSize="20sp"
android:textColor="#F2F3F4"/>
<ImageView
android:id="#+id/settings"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:onClick="nameOfMethod"
android:src="#raw/settings" />
</RelativeLayout>
and my java Main
package com.kostas.mytorch;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
//start our layout
super.onCreate(savedInstanceState);
setContentView(R.layout.main);{
final ImageView diskView = (ImageView) findViewById(R.id.settings);
diskView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
//my codes
}
});
diskView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
// System.out.println("image clicked...");//in my logcat
startActivity(new Intent("com.kostas.standroid.settings"));
}
});
}
}
my problem is when i click in the settings icon, my program crashes instead of what i wanted to create a new layout (settings xml is just black page),can someone be kind enough to help me out
You can attach click listener to any view by two ways:
In xml, write onClick attribute of the view and pass the method
name you have implemented in the activity.
Example: android:onClick="someMethod" and in the activity code, declare a method
public void someMethod(View view)
{
// handle click here
}
In activity, use setOnClickListener() to the view.
For now, try this:
diskView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
// System.out.println("image clicked...");//in my logcat
startActivity(new Intent(Main.this, settings.class));
}
});
Also remove this line from xml: android:onClick="nameOfMethod"
Some time there is contextual problem. Try this.
Intent intent = new Intent(CurrentActivity.this, UpcomingActivity.class);
startActivity(intent);
Also don't forget to define class in Manifest file
try to remove
android:onClick="nameOfMethod"
from the xml layout. I believe this is the method that it's called when you click the settings button and it doesn't exist so it crashes.
This is the java code and xml file for a program I am writing, and it force closes whenever i try to invoke the plusCalc button in-program. Could someone please tell me why?
Thank you!
Java file:
package org.example.knittingframe;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Intent;
public class KnittingFrame extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View plusCalc = findViewById(R.id.plus_calc_button);
plusCalc.setOnClickListener(this);
View exitbutton = findViewById(R.id.exit_button);
exitbutton.setOnClickListener(this);
}
public void onClick(View v) {
switch(v.getId()) {
case R.id.plus_calc_button:
startPlusCalc();
break;
case R.id.exit_button:
finish();
break;
}
}
public void startPlusCalc() {
Intent i = new Intent(this, PlusCalc.class);
startActivity(i);
}
}
Here is the XML file:
<?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">
<TextView
android:text="#string/main_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="25dip"
android:textSize="24.5sp" />
<Button
android:id="#+id/plus_calc_button"
android:layout_width="300px"
android:layout_height="wrap_content"
android:text="#string/plusCalc_label"
android:layout_gravity="center" />
<Button
android:id="#+id/exit_button"
android:layout_width="300px"
android:layout_height="wrap_content"
android:text="Exit"
android:layout_gravity="center" />
</LinearLayout>
Without the stack trace it appears that the problem is with declaring the PlusCalc.class class in the App Manifest file, because as you say, the error only appears when you click on the PlusCalc button.
If this doesn't help please post the stack trace.
I am doing the simplest of onClick models and cannot get the onClick method to fire. I know it is something simple, and I am new to Android. Any help is appreciated.
package com.bordeloniphone.timeentry;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class TimeEntryActivity extends Activity implements OnClickListener{
/** Called when the activity is first created. */
Button okButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
okButton = (Button) findViewById(R.id.btnOK);
okButton.setText(":)");
okButton.setOnClickListener(this);
//setContentView(okButton);
}
public void onClick(View v) {
Log.d("TEST", "TEST");
Toast.makeText(this, "TEST", Toast.LENGTH_SHORT).show();
}
}
Here is the main.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:id="#+id/btnOK"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="OK" />
</LinearLayout>
After much angst and gnashing of teeth, I figured it out. I had to delete the emulator device and add an new one and now it works like a champ. I appreciate everyone trying to help.
Instead of setting the onClicklistener to this, try this approach:
okButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.d("TEST", "TEST");
Toast.makeText(this, "TEST", Toast.LENGTH_SHORT).show();
}
});
You should try Cleaning your project or try to restart your Eclipse or any other Editor you are using as it is a valid code and should work fine.
UPDATE:
Also, you should check your Logcat, are you getting the output of Log.d("TEST", "TEST"); because your Toast seems to be implemented in a wrong manner.
Toast.makeText(this, "TEST", Toast.LENGTH_SHORT).show(); // wrong
Toast.makeText(Activity_name.this, "TEST", Toast.LENGTH_SHORT).show(); // correct
Using this in Toast inside the Listener means you are Referencing the Listener, which indeed should not be the case. You have to reference to the Activity itself so better use Activity_name.this.
Button iv_StyleInspiration_Back = (Button) findViewById(R.id.iv_StyleInspiration_Back);
iv_StyleInspiration_Back.setOnClickListener(this);
Try this Whenever you implement onclick in your activity you need to set as above to make it work for all controls and onclick should look something as below
#Override
public void onClick(View pView) {
if (null != pView) {
switch (pView.getId()) {
case R.id.iv_StyleInspiration_Back:
//do what you want
break;
default:
break;
}
}
}
Check for Three Steps:
find the button by id correctly
link the button to a listener. (adding the actionListener)
you specify a condition for this button in case of implementing an actionListener class.
try making the button clickable
<?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:id="#+id/btnOK"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="OK"
android:clickable="true" />