I have two button which starts new intents, and they are working fine.
In the same activity i am now trying to make a new button that opens a URL link, but i cant make it work.
The two buttons starting new intents, are btn_Calender and btn_Info. So the new button which should open URL is btn_button4.
Can someone plese see my code and tell me what i am doing wrong.
Thank you all.
package com.xxxxxx;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.chrfugl.stubhuset.R;
public class HomeActivity extends Activity implements OnClickListener{
private Button btn_Calender, btn_Info, btn_button4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
btn_Calender = (Button)findViewById(R.id.btn_Calender);
btn_Info = (Button)findViewById(R.id.btn_Info);
btn_button4 = (Button)findViewById(R.id.button4);
btn_Calender.setOnClickListener(this);
btn_Info.setOnClickListener(this);
btn_button4.setOnClickListener(this);
}
public void onClick(View view) {
Intent intent;
switch (view.getId()) {
case R.id.btn_Calender:
intent = new Intent(this,MainActivity.class);
startActivity(intent);
break;
case R.id.btn_Info:
intent = new Intent(this,InfoActivity.class);
startActivity(intent);
break;
case R.id.button4:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);
break;
}
}
}
Your switch statement in your onClick is wrong..
The ID of your button is R.id.button4:
btn_button4 = (Button)findViewById(R.id.button4);
In your onClick you are checking for the ID:
case R.id.btn_button4:
Which means you never reach your call.. The Intent itself is correct.
EDIT
Replace case R.id.btn_button4: with case R.id.button4: in your onClick-method
Related
I'm quite new to android, however i tried making some simple apps.
But this one is getting worse for me. i searched a lot, they say that we should use "Asynch" method or use "new runnable method", but still not getting the exact solution.
Here is my MainActivity.java:
package com.example.mit;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity implements OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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 onClick(View v) {
switch(v.getId())
{
case R.id.addition:
Intent i1 = new Intent(this,Addition.class);
startActivityForResult(i1, 500);
break;
case R.id.overlap:
Intent i2 = new Intent(MainActivity.this,Overlap.class);
startActivity(i2);
break;
case R.id.bcd:
Intent i3 = new Intent(MainActivity.this,Bcd.class);
startActivity(i3);
break;
case R.id.string1:
Intent i4 = new Intent(MainActivity.this,String1.class);
startActivity(i4);
break;
case R.id.string2:
Intent i5 = new Intent(MainActivity.this,String2.class);
startActivity(i5);
break;
case R.id.mul:
Intent i6 = new Intent(MainActivity.this,Mul.class);
startActivity(i6);
break;
case R.id.mean:
Intent i7 = new Intent(MainActivity.this,Mean.class);
startActivity(i7);
break;
}
}
}
This is pretty simple code and you're probably getting that warning because you're using an emulator. If that is the case, then it is normal because emulators are not as fast as real android devices. You don't need to use AsyncTask or Runnable for this.
I created 2 Activities, Main and Second. And I want to send text, put in the box EditText from Main to Second activity. Id of first EditText is UserName, and of the second is Description.
The code of the Main activity is:
package com.example.test;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.support.v4.app.NavUtils;
import android.content.Intent;
import android.widget.EditText;
public class Main extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onClick(View v) {
Intent intent = new Intent(Main.this, Second.class);
intent.putExtra("username", UserName.getText().toString());
intent.putExtra("gift", Description.getText().toString());
startActivity(intent);
}
public void ButtonOneClick(View v) {
switch (v.getId()) {
case R.id.button1:
Intent i = new Intent(this, Second.class);
startActivity(i);
break;
}
}
}
Full code of the Second Activity is:
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
public class Second extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
TextView txtInfo = (TextView)findViewById(R.id.TextOne);
String user = "ЖЫвотное";
String gift = "дырку от бублика";
user = getIntent().getExtras().getString("username");
gift = getIntent().getExtras().getString("gift");
txtInfo.setText(user + " , вам передали " + gift);
}
}
and errors:
main.java: Description cannot be resolved
main.java: UserName cannot be resolved
sorry for my english
Thanks for help
P.S. I'm studying java and programming for android just third day, please don't throw stones to me.
You'd then refer to your R class file, that is:
EditText desc = (EditText) findViewById(R.id.Description);
...
You have not included
EditText UserName = (EditText) findViewById(R.id.username);
EditText Description = (EditText) findViewById(R.id.description);
in your onCreate() method in MainActivity.
As you reference UserName and Description here refer to them as they are classes. Unless you have such classes an error will be stated because these classes can not be found (or "resolved").
You can even notice this by the syntax highlight. Do you see the words a displayed in a lght green/turquoise like Main or Intent?
public void onClick(View v) {
Intent intent = new Intent(Main.this, Second.class);
intent.putExtra("username", UserName.getText().toString());
intent.putExtra("gift", Description.getText().toString());
startActivity(intent);
}
Instead you want instances of the class EditText which you call userName and description. On these objects you can perform getText() and work your way along. :)
public void onClick(View v) {
Intent intent = new Intent(Main.this, Second.class);
EditText userName = (EditText) findViewById(R.id.username);
EditText description = (EditText) findViewById(R.id.description);
intent.putExtra("username", userName.getText().toString());
intent.putExtra("gift", description.getText().toString());
startActivity(intent);
}
Please notice I've changed the names of your variables to lowerCase starting camelCase. Though not an explicit rule it's considered good practise to start variable names with lower case letters ('userName,description,intent) while **C**lass references like (ÈditText,Main,Second`) are started with upper case letters.
you have not initialize the username & description in your java;
EditText us_name=(EditText) findViewById(R.id.user_name);
EditText us_desp=(EditText) findViewById(R.id.desp);
I made an app with three buttons. one makes a call, one sends an email and the third sends an sms. after making it the first time, i noticed the email button not responding. i tried to find an eroor but couldnt. so i switched the code so that the email button sends an sms and the sms button sends an email. once again the email button which is now supposed to send an sms, doesnt respond. any ideas?
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class ContactDaveActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void buttonhandler(View view)
{
switch(view.getId())
{
case R.id.button1:
{
try {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:xxxxxxxxxx"));
startActivity(callIntent);
}
catch(ActivityNotFoundException activtyException)
{
Throwable e = null;
Log.e("dialingexample", "Call failed", e);
}
break;
}
case R.id.button2:
{
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
String s = "xyz#gmail.com";
i.putExtra(Intent.EXTRA_EMAIL, new String[]{s });
startActivity(Intent.createChooser(i, "Send mail..."));
break;
}
case R.id.button3:
{
String phoneNumber = "+xxxxxxxx";
Intent smsIntent = new Intent(Intent.ACTION_SENDTO);
smsIntent.addCategory(Intent.CATEGORY_DEFAULT);
smsIntent.setType("vnd.android -dir/mms-sms");
smsIntent.setData(Uri.parse("sms:"+phoneNumber));
startActivity(smsIntent);
break;
}
}
}
}
you have android:onClick="buttonhandler" in your main.xml file ?
and also check for all,button1,button2,button3
<Button android:id="#id/button1" android:onClick="buttonhandler" ... />
<Button android:id="#id/button2" android:onClick="buttonhandler" ... />
<Button android:id="#id/button3" android:onClick="buttonhandler" ... />
how do i embed a tab layout within a button. My main layout is a linear layout, but i don't know how to program the main activity.java class. Could anybody help me get started this is what my main.java code looks like right now
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Intent;
public class Remote_DocActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
//private static final String TAG = "Remote_Doc";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View Patient_Button = findViewById(R.id.patientButton);
Patient_Button.setOnClickListener(this);
View Doctor_Button = findViewById(R.id.doctorButton);
Doctor_Button.setOnClickListener(this);
View About_Option = findViewById(R.id.aboutButton);
About_Option.setOnClickListener(this);
View Exit_Option = findViewById(R.id.exit);
Exit_Option.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.aboutButton:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
case R.id.exit:
finish();
break;
}
}
}
you can create tabs with the help of TabActivity class. You can take help from android-wireless-application-development book's unit 3 chapter 8 example.
I'm making my first official application to put on the market and i was wondering would over 120 new activities be too many if I'm only using them to pull text? If so is there a way I could turn MysecondActivity to a giant if or switch statement?
This is the example I found in a book I changed up case 0: I'm going to change up the rest be similar to case 0 once i get an answer.
Thank you in advance and sorry for messy code and writing
AndroidManifest.xml
<activity android:name=".MySecondActivity" />
ListActivityExample.java
import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
public class ListActivityExample extends ListActivity{
static final String[] ACTIVITY_CHOICES = new String[] {
"Open new Actvity",
"Open Contacts",
"Open Phone Dialer Example",
"Search Google Example",
"Start Voice Command"
};
final String searchTerms = "superman";
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, ACTIVITY_CHOICES));
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
getListView().setTextFilterEnabled(true);
getListView().setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3){
switch(arg2) {
case 0: //opens new screen
{Intent intent = new Intent(ListActivityExample.this,MySecondActivity.class);
startActivity(intent);
break;}
case 1: //opens phone dialer and fills in the given number
{
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("content://contacts/people/")));
break;}
case 2:
{
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("tel:12125551212")));
break;}
case 3: //
{
Intent intent1= new Intent(Intent.ACTION_WEB_SEARCH);
intent1.putExtra(SearchManager.QUERY, searchTerms);
startActivity(intent1);
break;}
case 4: //
{startActivity(new
Intent(Intent.ACTION_VOICE_COMMAND));
break;}
default: break;
}
}
});
}
}
MySecondActivity.java
mport android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MySecondActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("text here");
setContentView(tv);
}
}
There's nothing I can see about this code that will cause an issue with a large Activity stack. Start as many Activities as you like, as long as they aren't all stacked together at the same time. With this code you are starting a new Activity as a result of tapping an item in the list, but the user must finish the new Activity (press back) when returning to your list to start something else, so they aren't all in memory at once.
Does that help? Or did I misunderstand your question?