Using Android API Level : 7

  • 0

Using Android API Level : 7

Category : Uncategorized

In this example we shall see a very simple ways to work with
Shape API from Android platform.

As shown below, the image depicts a rectangular shape of a text view
on screen. In this example there is a customized user defined shape
that is showing a text label on screen.

The main Activity class of this example has the onCreate method overriden
in order to set the background of the textview with a shape as drawable.

MainActivity.java

/**
* This code is provided on “AS IS” basis without guarantee/warranty
* of any kind. Use of this code subjected to user’s own risk.
* @Author: Ishtek at usingframeworks@gmail.com
* @Date : 27th Feb 2013.
*/
package com.example.customshapeexample;

import android.os.Bundle;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources res = getResources();
Drawable shape = res. getDrawable(R.drawable.textview_background);
TextView txtView = (TextView) findViewById(R.id.txtView);
txtView.setBackgroundDrawable(shape);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}

This activity_main XML file is the configuration for using the with some parameters values.

activity_main.xml


Following configuration XML file is the descriptive way of defining
Shape with gradient, stroke, corners, padding etc.

textview_background.xml




For the text mentioned on the Button, following style can be used for
showing appropriate style items.

If every items of this example are setup properly, then the runtime
screen of this example would look something like this, as shown below:

By slightly altering background specific configuration file, the shape
of TextView’s background can be made as shown below:

Here the shape is changed to oval in the “textview_background.xml” file.




Leave a Reply