Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trying to add button in Android App - having problem with MainActivity.java part

 
Kenston Miller
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you include the import for Button?

Also did you replace this line and put your URL there?
 
Kenston Miller
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
{
"
 
Kenston Miller
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you tell us what compile errors are reported?
 
Kenston Miller
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}
});
}
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would start by replacing the onCreate method in MainActivity.java with that method.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic