I'm a new coder trying to figure out where I've gone wrong. My intent is to make an app with multiple buttons that each link to different websites. I've been using code from the web but am still not sure why it won't compile when I try adding uri.parse and am racking my brains looking for a solution.
So now I'm starting with a baseline example and trying to reverse engineer it to understand. I'm wanting the buttons to lead to a website instead of setting text. I've tried multiple solutions but none are working and I'm guessing you guys could answer this in your sleep. Thanks!
Update figured it out guys thanks!
Working java below
public class MainActivity extends Activity implements OnClickListener {
TextView tvOut;
Button btnOk;
Button btnCancel;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// find View-elements
tvOut = (TextView) findViewById(R.id.tvOut);
btnOk = (Button) findViewById(R.id.btnOk);
btnCancel = (Button) findViewById(R.id.btnCancel);
// assign listeners to buttons
btnOk.setOnClickListener(this);
btnCancel.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// define the button that invoked the listener by id
switch (v.getId()) {
case R.id.btnOk:
// ОК button// Open Website
Intent intent;
try {
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.facebook.com/yourPage"));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
} catch (Exception e) {
Log.e("Exception Caught", e.toString());
}
break;
case R.id.btnCancel:
// Cancel button
Intent intent2;
try {
intent2 = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.facebook.com/yourPage"));
intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent2);
} catch (Exception e) {
Log.e("Exception Caught", e.toString());
}
break;
}
}
}
If you would like to keep the context of the webpage(s) within your app, you'll need to add a webview in your activity layout:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/webView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
then add in onCreate:
final WebView webView = (WebView)findViewById(R.id.webView);
webView.loadUrl("http://www.google.com");
you'll need to add this to the manifest:
<uses-permission android:name="android.permission.INTERNET" />
this way you can keep things in control.. otherwise, you'll need to do it with an Intent. simple example and docs for that are here:
http://developer.android.com/reference/android/webkit/WebView.html
Related
I am writing code for an inbox-like activity which has a button that leads to the messages. This button has a text field that counts how many messages are in the inbox.
My problem is that the button's text field is not changing when the number of messages changes. It is not a problem of the app not checking for updates, and the code with setText is being called with the correct number to update.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.v("onCreate", "Main");
// Checking if there is login
if (ParseUser.getCurrentUser() == null) {
navigateToLogin();
} else {
// Setting pointers for buttons.
// onClick methods follow.
askButton = (Button) findViewById(R.id.buttonAsk);
ansButton = (Button) findViewById(R.id.buttonAnswer);
inboxButton = (Button) findViewById(R.id.buttonCenter);
mUser = ParseUser.getCurrentUser();
updateInbox();
}
This is the method that checks for new messages and updates the button.
private void updateInbox() {
Log.v(TAG, "Updating inbox");
ParseQuery responses = new ParseQuery(ParseConstants.CLASS_ANSWER);
responses.whereMatches(ParseConstants.KEY_SENDER_ID, mUser.getObjectId());
try {
responsesCount = responses.count();
Log.v("responses count" ,""+responsesCount);
if (responsesCount > 0) {
inboxButton.setText(String.valueOf(responsesCount));
}
Log.v("InboxActivity","Label set to " + responsesCount);
} catch (ParseException e) {
Log.v("InboxActivity", e.getMessage());
}
}
updateInbox gets called correctly and in the correct moments, so I only added its code to make this as clean as possible. Here is the 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:background="#0099cc"
tools:context=".MainActivity"
android:clickable="false"
>
<Button
android:layout_width="80sp"
android:layout_height="80sp"
android:id="#+id/buttonCenter"
android:layout_gravity="center"
android:layout_centerInParent="true"
android:textSize="30sp"
android:text=""
android:textColor="#color/black_overlay"
android:background="#drawable/greenbutton"/>
EDIT:
Hi all, thanks for the help. I figured out the problem and posted it as an answer. It was a logical error, nothing to do with Android.
try this:
inboxButton.post(new Runnable(){
#Override
public void run(){
inboxButton.setText(String.valueOf(responsesCount));
}
});
(responsesCount should be final)
Try below code
import android.widget.RemoteViews;
if (responsesCount > 0) {
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.my_layout); //where my_layout is the layout file where the button resides.
remoteViews.setTextViewText(R.id.button, "Set button text here");// where button is id of the button.
}
findViewById doesn't exist for a widget.
Below code will work for you
runOnUiThread(new Runnable() {
#Override
public void run() {
if (responsesCount > 0) {
inboxButton.setText(String.valueOf(responsesCount));
}
}
});
You may use AsyncTask for smoothness of app flow.
I realized that the text was only getting updated when the number of responses was > 0. The button was updating correctly otherwise so I had to put an else statement to make sure the button always got updated. I edited the question with the correct answer in comments.
responsesCount = responses.count();
Log.v("responses count" ,""+responsesCount);
if (responsesCount > 0) {
inboxButton.setText(String.valueOf(responsesCount));
///////////////////////////////////
// MY EDIT HERE
//} else{
// inboxButton.setText("");
///////////////////////////////////
How do you start a new activity with the Google plus sign in button?
I have this in my activity_main.xml file:
<com.google.android.gms.common.SignInButton
android:id="#+id/btn_sign_in"
android:onClick="launchGooglePlusSignIn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
>
</com.google.android.gms.common.SignInButton>
<Button
android:id="#+id/button1"
android:onClick="launchGooglePlusSignIn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/btn_sign_in"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="Button" />
and in my MainActivity, I have this:
public void launchGooglePlusSignIn(View view) { //Must have a view as the ONLY parameter.This will be the view that was clicked
Intent intent = new Intent(this, GooglePlusHandlerActivity.class);
startActivity(intent);
}
When I run the app and click the Google button, nothing happens but when I click the other button, (as expected) am taken to the new activity.I really need to handle the Google plus signin, signout, access revocation etc in another activity, how can i start the activity with the google button?
Follow this tutorial for google plus login.Here you will find a method called
public void onConnected(Bundle arg0){}.
Write your intent code within that method.It will work.
With help from this tutorial I was able to solve this problem.
I needed to initialize the buttons and then set onClickListeners in my MainActivity. My MainActivity now looks like this:
public class MainActivity extends Activity implements OnClickListener{
private SignInButton signInButton;
private Button otherButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
signInButton = (SignInButton)findViewById(R.id.btn_sign_in);
signInButton.setOnClickListener(this);
otherButton = (Button)findViewById(R.id.button1);
otherButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.button1:
//Toast.makeText(this, "Button CLicked", Toast.LENGTH_SHORT).show();
Intent ordinaryButtonClickedIntent = new Intent(this, GooglePlusHandlerActivity.class);
startActivity(ordinaryButtonClickedIntent);
break;
case R.id.btn_sign_in:
//Toast.makeText(this, "Button CLicked", Toast.LENGTH_SHORT).show();
Intent GPlusButtonClickedIntent = new Intent(this, GooglePlusHandlerActivity.class);
startActivity(GPlusButtonClickedIntent);
break;
}
}
I don't need these line in the buttons in my activity_main.xml file anymore:
android:onClick="launchGooglePlusSignIn"
As we can send data from one Activity to another within the app and also from one app to another app by using Intent
I'm able to send data from my one app to another by using following code
just consider there are two apps APP1 and APP2 and I'm sending data from APP1 to APP2 and vice verse.
In APP1 package name: (com.sush.myApp)
public class HomeActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
// to receive data from com.my.demo package and display it to TextView
TextView mText = (TextView)findViewById(R.id.textView1);
String name=getIntent().getStringExtra("User_Message");
mText.setText(name);
// to send data from com.sush.myApp package to com.my.demo package
Button btn1 =(Button)findViewById(R.id.button1);
btn1.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
try
{
Intent i = new Intent(Intent.ACTION_MAIN);
PackageManager manager = getPackageManager();
i = manager.getLaunchIntentForPackage("com.my.demo");
i.putExtra("User_ID", "sush19");
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
}catch(Exception e)
{
e.printStackTrace();
Toast.makeText(v.getContext(), "App Not Found", Toast.LENGTH_LONG).show();
}
}
});
}
and from APP2 package name: (com.my.demo)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// to receive data from com.sush.myApp package and display it to TextView
final TextView txt1 = (TextView)findViewById(R.id.Text1);
String name=getIntent().getStringExtra("User_ID");
txt1.setText(name);
// to send data from com.my.demo package to com.sush.myApp package
Button btnSending = (Button)findViewById(R.id.btnSend);
final EditText myMessage = (EditText)findViewById(R.id.txtMessage);
btnSending.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
if (myMessage.getText().toString().equals(""))
{
Toast.makeText(v.getContext(), "Enter your message", Toast.LENGTH_SHORT).show();
}
else
{
try
{
Intent i = new Intent(Intent.ACTION_MAIN);
PackageManager manager = getPackageManager();
i = manager.getLaunchIntentForPackage("com.sush.myApp");
i.putExtra("User_Message", myMessage.getText().toString());
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
}catch(Exception e)
{
e.printStackTrace();
Toast.makeText(v.getContext(), "Error: "+e, Toast.LENGTH_LONG).show();
}
}
}
});
}
And its working perfect..
Now my problem is I want to do same operation i.e sending data from one app to another but my first app is in Java created using Eclipse and my second app is in ActionScript created using AIR for Android in Adobe Flash Professional CS6
Is there a way to use Intent and PackageManager in Actionscript so that I can send the data easily from AIR app to Android App, if yes then can anyone show me a example
Or else can anyone show me an example on how to send data from Android App to AIR for Android App and vice verse..
Thank you...
There's 2 methods I can think of for this.
The first is to use the Java code you have written (or similar), package it as a Native Extension, and build that into your app.
Another alternative is to use a URI scheme and read the data from the InvokeEvent. This SO question covers that method already.
I am little confused with this code am looking for somenthing similar i have a button that i want it to say rate and when click it takes you to the android market where the user can rate my application, please help
Button bRate = (Button) findViewById(R.id.button7);
bRate.setOnClickListener(new View.OnClickListener() {
//plus the code on the top here is my idea of the code
}
Elipse is saying something is wrong and I understand that iI need to put my market link but the button still doesn't work
I couldn't post it on the same page here is the link for the original question
Use application to rate it on market
You have just add your app package name...
https://play.google.com/store/apps/details?id=your packagename
And call using Intent.ACTION_VIEW
String str ="https://play.google.com/store/apps/details?id=com.zeustechnocrats.quickfoods";
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(str)));
This was helpful for me to do the same thing using an image button. here the code I used.
final ImageButton mybutton = (ImageButton) findViewById(R.id.imageButton);
mybutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String str ="https://play.google.com/store/apps/details?id=ws.crandell.newspaperpuzzles";
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(str)));
}
});
Example code you can use.
Change your_package_name to your.package.name. E.g. com.example.myapp
Java File:
public void ratebutton(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW)
.setData(Uri
.parse("https://play.google.com/store/apps/details?id=your_package_name"));
startActivity(intent);
}
Layout file:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="ratebutton"
android:text="#string/rate"
android:layout_gravity="center_horizontal" />
strings.xml:
<string name="rate">Rate with 5 stars please</string>
I am developing an application for android in which I need to make a call with MMI code in the background of the application. But by default the call application of the android phone is activated and I have to press call button from there. so I want solution for this....
You can try the given simple code this is directly initiating a call to the no provided in code 123456789 and no call button is clicked for this. And yes dont forget to add permission in manifest file:
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
Code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
// TODO Auto-generated method stub
try {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:123456789"));
startActivity(callIntent);
} catch (ActivityNotFoundException activityException) {
Throwable e = null;
Log.e("helloandroid dialing example", "Call failed", e);
}
}});
}
If you would have provided some code than it would be helpful other wise have you declared in your app manifest file about the permission to modify phone state.
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE"></uses-permission>