android - Change the background image of a button -
android - Change the background image of a button -
i using 2 buttons, 1 next news , 1 previous news. in activity when on first news button should set image gray image , when move forwards previous news button should alter gray image colored one. when reach @ lastly news next button should alter gray image.
this xml file.
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffffff" > <textview android:id="@+id/tv_submitdate" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:text="medium text" android:textcolor="#000000" android:padding="5dp"/> <textview android:id="@+id/tv_headline" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv_submitdate" android:text="medium text" android:textcolor="#000000" android:textstyle="bold" android:padding="5dp"/> <view android:id="@+id/viewseperator" android:layout_width="fill_parent" android:layout_height="2dip" android:layout_below="@+id/tv_headline" android:background="#ff909090" /> <scrollview android:id="@+id/scrollview1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@+id/btn_relative_layout" android:layout_below="@+id/viewseperator" android:padding="10dp"> <linearlayout android:id="@+id/linearlayout1" android:layout_width="match_parent" android:layout_height="match_parent" > <textview android:id="@+id/tv_detailnews" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="medium text" android:textcolor="#000000" /> </linearlayout> </scrollview> <linearlayout android:id="@+id/btn_relative_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:background="#dcdcdc" android:padding="10sp" > <button android:id="@+id/btn_prevnews" android:layout_width="120sp" android:layout_height="40sp" android:layout_weight="0.5" android:layout_marginright="5sp" android:background="@drawable/arrow_left" android:clickable="true" /> <button android:id="@+id/btn_nextnews" android:layout_width="120sp" android:layout_height="40sp" android:layout_marginleft="5sp" android:layout_weight="0.5" android:background="@drawable/arrow_right" android:clickable="true" /> </linearlayout> </relativelayout>
this trying alter image of button:
public void onclick(view v) { if (v == btnprevnews && newsid > 0) { if (newsid > 0) { newsid--; putdetailnewsinview(newsid); } else if (newsid == 0) { btnprevnews .setbackgroundresource(r.drawable.disable_arrow_left); btnprevnews.setenabled(false); } // btnprevnews.setvisibility(view.invisible); } else if (v == btnnextnews && newsid < newslistdetail.size() - 1) { if (newsid < newslistdetail.size() - 1) { newsid++; putdetailnewsinview(newsid); if(newsid == 0) btnnextnews.setbackgrounddrawable(getresources().getdrawable( r.drawable.disable_arrow_right)); } else if (newsid == newslistdetail.size()) { // btnnextnews.setvisibility(view.invisible); btnnextnews.setbackgrounddrawable(getresources().getdrawable( r.drawable.disable_arrow_right)); // btnnextnews.setbackgroundresource(r.drawable.disable_arrow_right); btnnextnews.setenabled(false); } } }
please help me out in this.
thanks in advance.
i assume have methods executed if button pressed. utilize these , add together logic alter background of these buttons grey: give both buttons id, can access these buttons with: button mybutton = (button) findviewbyid(r.id.yourid);
now can kinds of things button, changing background.
edit: problem lay in pictures, if you, set breakpoints , debug step-by-step.
i hacked code of mine test functionality, works fine me. here it:
bundle my.namespac; import android.app.activity; import android.os.bundle; import android.view.view; import android.widget.button; public class sometestprojectactivity extends activity { /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); button button = (button)findviewbyid(r.id.btn_prevnews); button.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { button b = (button)v; v.setbackgrounddrawable(getresources().getdrawable(r.drawable.koala)); } }); } }
and layout-xml:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <button android:id="@+id/btn_prevnews" android:layout_width="120sp" android:layout_height="40sp" android:layout_weight="0.5" android:layout_marginright="5sp" android:background="@drawable/ic_launcher" android:clickable="true" /> </linearlayout>
android button background-image
Comments
Post a Comment