so I'm kinda new to android developing.
I read this as my reference to have a clickable button, with some changes in my xml. The problem that I have is, whenever I tried to click it, nothing happens. There's no error message on my phone nor android studio. I wonder what's missing or is there something that I don't know, that I should know?
Here's my java on my project
public class test_button extends Activity {
Button button ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_button);
click();
}
public void click(){
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent browserIntent =
new Intent(Intent.ACTION_VIEW, Uri.parse("http://reddit.com"));
startActivity(browserIntent);
}
});
}
and this is my xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<include layout="@layout/layout_toolbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
android:id="@+id/toolbar"/>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:id="@+id/profile_page_activity_body"
android:layout_below="@id/toolbar"
>
<Button
android:layout_width="wrap_content"
android:layout_height="60sp"
android:text="Do things"
android:textColor="#ffffff"
android:textSize="40sp"
android:id="@+id/button"
android:textAllCaps="false"
android:background="@drawable/btn_greenbox"
android:layout_centerHorizontal="true"
android:layout_marginTop="51dp" />
</RelativeLayout>
Edit1 : on onClick(), i did try Toast, but it had similar outcome, nothing happened.
Edit2 : there's a click animation on button whenever i click it
call intent like this may in your link have a problem:
into click:-
**Intent i = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://www.reddit.com/"));
startActivity(i);**
Just Put a https://www.reddit.com/ instead of http://www.reddit.com/
How about setting click listener as:
public class TestButton extends Activity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// Intent code here;
}
}
Set the onclick listenter inside the onCreate
instead of that method and try.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_button);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent browserIntent =
new Intent(Intent.ACTION_VIEW, Uri.parse("http://reddit.com"));
startActivity(browserIntent);
}
});
}
Also, test_button
is not a good name for a class. Change it to TestButton
. (Rename using refactoring [shift+f6], so that you do not get reference errors)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.