Kenston Miller

Greenhorn
+ Follow
since Nov 07, 2015
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Kenston Miller

Keith Lynn wrote:Can you tell us what compile errors are reported?



Well that's part of the problem I get different errors each time because I'm not sure I'm placing this MainActivity.java code in the right spot or for that matter if anything else might be missing. Your best bet is to just look at my dropbox MainActivity file above and then show me where to place this code below exactly and maybe see if there is anything else I need to add for this to work. Because I'm a total newb at this but I'm learning a lot the last couple days.

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//open up browser or play store with the provided link
String url = linkToYourDeveloperPage;
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
}
});
}
8 years ago
I figure this would probably be easier for everyone just to see my MainActivity file code as a whole. (This is the code in a working state before I try to post the part from my original post in the MainActivity file part)

https://www.dropbox.com/s/t8t2j584qv3f5x1/MainActivity.java?dl=0
8 years ago

Keith Lynn wrote:Did you include the import for Button?

Also did you replace this line and put your URL there?




what do you mean by "import for button" ? do you mean "import android.widget.Button;" added somewhere in the MainActivity file? Also do I need to import android.view.View.OnClickListener; ?

Idk Everytime I add this code above to the MainActivity file it fails to compile, so either it's incorrect in some form or I'm placing it in the wrong way in the file (most likely)

Also for the URL thing it should look like this right ? -> String url = http://www.example.com/xxxx;



I just found this tutorial which is very similar to the code provided to me but I'm seeing small differences, not sure if they mean anything.
http://www.mkyong.com/android/android-button-example/


Like where does "public class MyAndroidAppActivity extends Activity { " come from near the top in that website example ? In my MainActivity file I see this " public class MainActivity extends AbstractMainActivity implements Memory.OnMemoryListener
{
"
8 years ago
Hey guys I'm new to Android development, I was wanting to add a button to the bottom of one of my apps that when clicked takes the user to my GooglePlay page. I had someone provide me with the code below as a solution for this, the first part with displaying the button in the Main.xml was easy enough for me to figure out. But the second part where I was told to add the onclick code to the MainActivity.java file I can't quite figure out. For one I'm not even sure if that code supplied to me is error proof, but then I'm not even sure where exactly I should place the code in the MainActivity.java file without getting possible errors. No matter where I paste the code in that file it fails to compile so something is obviously off, I just can't figure out what exactly (I'm total newbie at this).

Here is the code below for this button in question, any help would be greatly appreciated.

First, you need to add a Button to your layout which should contain the Button. In your case it's main.xml from the layout folder. Then your main.xml should look similar to this one:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!--other Views-->

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="8dp"
android:text="My Apps" />

</RelativeLayout>

In your MainActivity you assign an OnClickListener to this Button:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//open up browser or play store with the provided link
String url = linkToYourDeveloperPage;
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
}
});
}

Inside onClick you create an Intent with an url which shows your developer page or your apps in the Play Store.
8 years ago