How to create image button in android?

后端 未结 3 954
栀梦
栀梦 2021-02-19 19:18

So I\'m new to android development...How can I create an image that acts like button, so when I press that image, image starts a specific activity. So I want this to show as ima

3条回答
  •  孤街浪徒
    2021-02-19 19:35

    Create ImageButton as:

    In main.xml:

    
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />
    

    In Code part:

    ImageButton ib=(ImageButton)findViewById(R.id.ib);
    ib.setOnClickListener(ibLis);
        }
        private OnClickListener ibLis=new OnClickListener(){
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //START YOUR ACTIVITY HERE AS
                 Intent intent = new Intent(YOUR_CURRENT_ACTIVITY.this,NextActivity.class);
                 startActivity(intent, 0);
            }
        };
    

    EDIT:

    and second option if you want to create an image like button using Button View then Create an Custom button as:

    First put your all images like for pressed,focused and default in res/drawable folder and then add an newbtn.xml in drawable/newbtn.xml as:

      
      
           
           
           
    
    

    Finally in button XML set android:background as :

    See this tutorial for Creating Custom Button with images

    Creating custom, fancy buttons in Android

提交回复
热议问题