Button is not clickable? [duplicate]
I have created a button in my screen's xml like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@drawable/bg">
<Button android:text="Lets Get Started"
android:background="@drawable/btnbg"
android:id="@+id/btn_nav_let_strtd"android:layout_width="wrap_content"
android:layout_height="35px"
android:textColor="#FFFFFF"
android:layout开发者_StackOverflow中文版_alignParentLeft="true"
android:drawableRight="@drawable/arrow"
android:drawableLeft="@drawable/nav_getstart">
</Button>
set click listenrss
public class NavigationScreen extends Activity implements OnClickListener{ private Button btn_nav_lets_get_strtd;
private LinearLayout ll_get_started;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.navigation);
btn_nav_lets_get_strtd = (Button) findViewById(R.id.btn_nav_let_strtd);
btn_nav_lets_get_strtd.setClickable(true);
}
private void setOnclickListeners() {
btn_nav_lets_get_strtd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
System.out.println("clicked");
Toast.makeText(NavigationScreen.this, "fgd",Toast.LENGTH_SHORT).show();
}
});
}
}
but when i try to click this button it is not clickable not focusable .. how do i set it as clickable ???
and the drawable is not a bitmap it is a statlist drawable btnbg.xmlis here
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:constantSize="true"
android:dither="true"
android:variablePadding="true" >
<item
android:drawable="@drawable/nav_blue_bar"
android:state_pressed="true"
/>
any help will be appreciated ...
look at your Code , you didn't call the method setOnClickListeners()
in your method onCreate()
!! ,
to fixe your problem , you should call your method like this :
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.navigation);
btn_nav_lets_get_strtd = (Button) findViewById(R.id.btn_nav_let_strtd);
btn_nav_lets_get_strtd.setClickable(true);//here you don't need to call this method because the button is clickable by default
//call your method setOnClickListeners() here
setOnClickListeners();
}
private void setOnclickListeners() {
btn_nav_lets_get_strtd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("clicked");
Toast.makeText(NavigationScreen.this, "fgd",Toast.LENGTH_SHORT).show();
}
});
精彩评论