Android open URL onclick certain button - java

How i can send people to my google play on click certain button ?
i already did this in activity_menu.xml
<Button
android:id="#+id/ratebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/morebutton"
android:layout_alignBottom="#+id/morebutton"
android:layout_toLeftOf="#+id/morebutton"
android:layout_toRightOf="#+id/aboutButton"
android:background="#drawable/more"
android:text="#string/rateBtn"
android:textColor="#color/White"
android:textColorHint="#color/White"
android:textSize="18sp" />
but in MenuActivity.java i couldn't know what should i do ?
Any help? Thanks

findViewById(R.id.ratebutton).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String url = "market://details?id=<package_name>";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});

Button rate = (Button)findViewById(R.id.rate);
rate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i2=new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=yourpackagename"));
startActivity(i2);
}
});
// Enter package name of application in place of yourpackagename

In my signin.xml file I put this code:
<Button
android:id="#+id/privacypolicylink"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginBottom="50dp"
android:background="#android:color/transparent"
android:onClick="performSingUpMethod"
android:text="Política de Privacidad"
android:textColor="#A9A9A9"
android:textSize="15dp"
android:textStyle="bold" />
In my signin.java file I used this:
findViewById(R.id.privacypolicylink).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String url = "https://example.net/privacy_policy";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
It works for me. It displays a text in the grey color that I want it. The text is underlined and it is a link. The is the way it looks:
When I click on it, I see this:
Then I can open the link using a web browser on the phone and everything works correctly the way I needed it.

Declare button inside your activity:
Button btnRateButton;
Initialize btnRateButton inside onCreate method:
btnRateButton = (Button) findViewById(R.id.ratebutton);
Set on click listener:
btnRateButton.setOnClickListener(this);
Write onclick method:
#Override
public void onClick(View v) {
Intent i = null;
final String myPackage = getPackageName();
if(v.getId() == R.id.btnRateButton) {
i = new Intent(Intent.ACTION_VIEW,Uri.parse("market://details?id=" + myPackage));
startActivity(i);
}
}
Note that for above code to work, you need to have Play Store app installed on your device.

Related

OnClick Not Firing?

I'm new to android and having trouble getting some simple things going!
Here is some basic code:
MainActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Lets go ahead and set our buttons up.
ServerButton = (Button)this.findViewById(R.id.ServerButton);
ClientButton = (Button)this.findViewById(R.id.ClientButton);
BTServer = new BluetoothServerService(this, mHandler);
}
#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 ServerButtonClick(View v){
Log.d("BLUETOOTH MAIN ACTIVITY", "It got here");
BTServer.start();
}
private final Handler mHandler = new Handler(){
#Override
public void handleMessage(Message msg){
switch (msg.what){
case HANDLER_CHANGE_SERVER_STATUS:
switch(msg.arg1){
case SERVER_STATUS_OFF:
CURRENT_SERVER_STATUS = SERVER_STATUS_OFF;
//We should probably add some logging, and the change to the button
break;
case SERVER_STATUS_ON:
CURRENT_SERVER_STATUS = SERVER_STATUS_ON;
//Again lets add some logging, etc.
break;
case SERVER_STATUS_ON_CONNECTED:
CURRENT_SERVER_STATUS = SERVER_STATUS_ON_CONNECTED;
//Again lets add some logging, etc.
break;
}
}
return;
}
};
My XML Looks like this:
<Button
android:id="#+id/ServerButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="146dp"
android:layout_marginLeft="64dp"
android:text="Connect to Server" />
<Button
android:id="#+id/ClientButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="28dp"
android:text="Start Server"
android:onClick="ServerButtonClick"/>
In my logs, I see the logs saying it creates the BTServer object, but I can't get any logs to show up when I click the button.
Any suggestions?
Thanks!
define a click action for ClientButton inside onCreate method
ClientButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
also for ServerButton define a click action like the above as
ServerButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
For me, I find it's best to have your Activity implement android.view.View.OnClickListener, then set your Button's onClickListener to your Activity.
Here's an example Activity class:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Home extends Activity implements OnClickListener
{
#Override
public void onCreate (Bundle savedInstanceState)
{
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
Button serverButton = (Button)this.findViewById (R.id.ServerButton);
serverButton.setOnClickListener (this);
Button clientButton = (Button)this.findViewById (R.id.ClientButton);
clientButton.setOnClickListener (this);
}
#Override
public void onClick(View button)
{
int buttonId = button.getId ();
if (buttonId == R.id.ServerButton)
{
// Do server stuff.
}
if (buttonId == R.id.ClientButton)
{
// Do client stuff.
}
}
}
In Android when you define the android:onClick="someMethod" attribute it only implements the OnClickListener for you.
So it is your responsibility to define the onCLickListener Method and call your ServerButtonClick method from that.
Add this method in your Activity OnCreate Method. It would work.
ServerButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ServerButtonClick();
}
});
Also, android:onClick method works only if API level > 4 onwards. so if you're targeting Minimum SDK version < 1.6 please do not use it

Android - Need to Pause playing mediafile at particular instance

I have a page consisting of three different Buttons PLAY | PAUSE | STOP.
PLAY | STOP is working fine, but i'm not able to pause at particular instance. I want to pause playing at an instance, by pressing pause button. And then resume it from saved instance by again pressing Play button.
Below is the code from Sound.java file
public class Sound extends Activity {
MediaPlayer mp = null;
String play = "Play!";
String stop = "Stop!";
String pause = "Pause";
int length;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sound);
final ImageButton play = (ImageButton) findViewById(R.id.idplay);
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
managerOfSound("play");
Toast toast_play = Toast.makeText(getBaseContext(),
"Playing First", Toast.LENGTH_SHORT);
toast_play.show();
} // END onClick()
});
final ImageButton pause = (ImageButton) findViewById(R.id.idpause);
pause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
managerOfSound("pause");
Toast toast_pause = Toast.makeText(getBaseContext(),
"Pausing Morning Bhajan-Aarati", Toast.LENGTH_SHORT);
toast_pause.show();
} // END onClick()
});
final ImageButton stop = (ImageButton) findViewById(R.id.idstop);
stop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mp != null) {
mp.stop();
mp.reset();
Toast toast_stop = Toast.makeText(getBaseContext(),
"Stopping First",
Toast.LENGTH_SHORT);
toast_stop.show();
}
} // END onClick()
});
/** Manager of Sounds **/
protected void managerOfSound(String theText) {
if (mp != null){
mp.reset();
mp.release();
}
if (theText == "play")
mp = MediaPlayer.create(this, R.raw.goodbye);
else if (theText == "pause" && mp.isPlaying())
mp.pause();
length = mp.getCurrentPosition();
mp.seekTo(length);
mp.start();
}
Here is my sound.xml file,
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageButton
android:id="#+id/idplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="65dp"
android:layout_marginTop="20dp"
android:background="#drawable/image_play" />
<!-- android:text="#string/idplay" /> -->
<ImageButton
android:id="#+id/idpause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_toLeftOf="#+id/idstop"
android:background="#drawable/image_pause" />
<ImageButton
android:id="#+id/idstop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_toRightOf="#+id/idplay"
android:background="#drawable/image_stop" />
</RelativeLayout>
Your Help is Appreciated..!!
I think your problem is this line:
if (theText == "play")
mp = MediaPlayer.create(this, R.raw.goodbye);
else if (theText == "pause" && mp.isPlaying())
mp.pause();
length = mp.getCurrentPosition();
mp.seekTo(length);
mp.start();´
You are setting the length by clicking the pause button, but you will start it again directly in this if clause.
You should check if the play button is clicked if the media was playing befor, something like
if(length > 0)
and then start the player at the specified position.
Edit:
In addition, you should not use the operator == to compare strings. You should better use string1.equals(string2)
Hope it helps
Best wishes

Phone call click for textview

Okay so I've looked and tried to make sense of some other code that I've found but nothing has been working out for me. I'm trying to get the user to click the textview and have it take them to their phones dial-er.
Here's the Java:
public class Info extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.moreinfo);
Button appoint =(Button) findViewById(R.id.btnAppoint);
TextView phone =(TextView) findViewById(R.id.phoneTxt);
String url = phone.getText().toString();
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
appoint.setOnClickListener(new OnClickListener () {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Info.this, EmailContact.class));
}
});
phone.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:+"+phone.getText().toString().trim()));
startActivity(callIntent );
}
});
}
}
I've also put android: clickable = true so I don't see if that's the problem. Any help would be appreciated!
Change phone.getText().toString().trim() to use the View object supplied to the onclick method:
phone.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel:+"+((TextView)arg0).getText().toString().trim()));
startActivity(callIntent );
}
});
Additionally, if you just want to show the dialer with a phone number loaded, you're using the wrong intent action, Intent.ACTION_DIAL instead of Intent.ACTION_CALL will show the dialer. The Intent.ACTION_CALL that you're using will actually initiate the phone call, and to make that work, you need to add the appropriate permission to your Manifest:
<uses-permission android:name="android.permission.CALL_PHONE" />
Based on your description its red because if you're going to use an class method level object within an anonymous obejct, the object must be defined as final.
final TextView phone =(TextView) findViewById(R.id.phoneTxt);
And for further measure, ensure you are using the right permission to do the call action in your manifest:
<uses-permission android:name="android.permission.CALL_PHONE" />

search a website in android

I am in a development of an android that will search for a particular word in a website..for that I developed some code...but it is not complete...is anyone able to help..it will be a help for me...thank you so much for reading
public class MainActivity extends Activity {
EditText et = new EditText(this);
public void onClick(View v){
Editable searchText = et.getText();
Intent intent = new Intent(this, com.example.app.MainActivity.class);
intent.putExtra("searchQuery", searchText);
startActivity(intent);
}
}
Try it....
<EditText
android:id="#+id/edt_search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:ellipsize="end"
android:hint="Enter Your Text"
android:imeOptions="actionGo"
android:singleLine="true"
android:textSize="14sp" />
EditText edt_search;
edt_search = (EditText) findViewById(R.id.edt_search_book);
edt_search.setImeOptions(EditorInfo.IME_ACTION_GO);
edt_search.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
// Log.i("KeyBoard" ,"Inside the Edit Text");
if (actionId == EditorInfo.IME_ACTION_GO) {
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY, edt_search.getText().toString()); // query contains
startActivity(intent); // search string
}
return false;
}
});

SharedPreference in android(Tell friend + Disable Dialog notification)

i am developing an android application that has a shared preference, containing :
- Ringtone (done)
disable the dialog notification (alarm, SMS, MMS)(not yet,please read the comment inside the code)
-Tell friend (i am sure my code is right but i am not sure how to put it with Shared preference + i am now sure of place and method that i should put at it- please read to the comment inside the code)
public class Setting extends PreferenceActivity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
}
#Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
String ringtonePreference;
// Get the xml/preferences.xml preferences
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
ringtonePreference = prefs.getString("ringtonePref",
"DEFAULT_RINGTONE_URI");
}
//disable all Notification (Alarm , SMS,MMS)
//what is the method that i should put this code at it? onStart?onDestroy?or what?
SharedPreferences sharedPreferences = this.getPreferences(MODE_PRIVATE);
boolean hideNotification = sharedPreferences.getBoolean("checkbox", false);
if(!hideNotification)
alertDialog.show();
}
//tell friend
//i am not sure if this is right place to this code
//if it is the right place then what is the method that i should put this code at it?onStart?onActivityResult?or what?
SharedPreferences prefs1 = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
TellFriendPreference = prefs1.getString("tellfriend","null");
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String item = (String) getListAdapter().getItem(position);
String str= item.toString();
if(str=="Tell Friends"){
Toast.makeText(this,"First", Toast.LENGTH_LONG).show();
//Go to tell friend activity
//Intent i = new Intent (Help.this, .class);
//startActivity(i);
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(android.content.Intent.EXTRA_TEXT, "Check out s Application for Android. Download it today from http://s.com");
startActivity(Intent.createChooser(intent, "Tell friends:"));
startActivity(intent);
}
this is prefs.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="Second Category" >
<RingtonePreference
android:name="Ringtone Preference"
android:key="ringtonePref"
android:summary="Select a ringtone"
android:title="Ringtones" />
<CheckBoxPreference
android:defaultValue="true"
android:key="checkbox"
android:summary="Check the Box"
android:title="Disable Notification" />
<Preference
android:name="tell Preference"
android:key="tellfriend"
android:summary="if u want to tell ur friends by usinf"
android:title="Tell Friend" />
</PreferenceCategory>
</PreferenceScreen>
please help me, i should submit it after some hours
I solved my problem. I just but tell friend in another activity, my code is correct.

Categories