Android – Create Rounded Corner Buttons

Skill Assumptions:

  1. You have configured your Android development environment on Eclipse or other tools
  2. You know how to create a new Android project

Step 1 – Create a new Eclipse project

Step 2 - Add a default Activity named Main which will add main.xml under layout folder and Main.java under your namespace folder under src.

Step 3 – Add two new shape Xml files under your drawable-hdpi folder named

  1. round_button.xml – this will create a rounded corner rectangle shape. Copy and paste following xml in your file.
  2. // drawable-hdpi/round_button.xml
    
    
    
        
    
        
        
    
    
  3. oval_button.xml – this will create an oval shape. Copy and paste following xml in your file.
  4. // drawable-hdpi/oval_button.xml
    
    
    
        
        
    
    
    

Step 4 – Copy paste following Xml on your main.xml file.

// layout/main.xml




		


		

	

Copy past following code in your Main.java file. This is trivial code to show toast when button is clicked.

// main.java
public class Main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button btnRectangle = (Button) findViewById(R.id.btnRectangle);
        btnRectangle.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				Toast.makeText(getApplicationContext(),
						" You clicked Rectangular button", Toast.LENGTH_SHORT).show();
			}
		});

        Button btnOval = (Button) findViewById(R.id.btnOval);
        btnOval.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				Toast.makeText(getApplicationContext(),
						" You clicked Oval button", Toast.LENGTH_SHORT).show();
			}
		});
    }
}

Step 5 – Run the application as Android Application and you should see following output.

Tags: , , ,

http://downloadpart.com