Wednesday, April 20, 2011

Creating Custom Text View in Android?


Step 1: Create a Layout in xml file as i did below

<LinearLayout android:id="@+id/san_tag" android:layout_width="wrap_content" android:layout_height="wrap_content">


</LinearLayout>

Step 2:  Now Map that Layout in Java Code as i Shown Below

LinearLayout navagtion_bar;
String myarray[]={"Foot Ball","Basket Ball","Cricket"};
@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
navagtion_bar= (LinearLayout) findViewById(R.id.san_tag);// Map the Layout
 TextView bs_text[]= new TextView[myarray.length]; // Create TextView according to value in length variable;

for(int z=0;z< myarray.length] ;z++)
{
try
{

bs_text[z] = (TextView) new TextView(this);
bs_text[z].setText( myarray [z]);
bs_text[z].setTextSize(15);
bs_text[z].setBackgroundColor(android.graphics.Color.TRANSPARENT);
bs_text[z].setTextColor(android.graphics.Color.BLACK);
navagtion_bar.addView(bs_text[z]);
}
}
catch(ArrayIndexOutOfBoundsException e)
{
Log.d("ArrayIndexOutOfBoundsException",e.toString());
}
}

Step 3: Set the Click Listener Custom TextView

do
{

    bs_text[y].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
           Log.d("I m clicked","Y Value"+Integer.toString(y));
    });
y--;
}while(y>=0);







No comments:

Post a Comment

Notification in Android Using AlarmManager, BoradCastReceiver & Services

    Our aim is to show notification message to user in a specific time.     Say For example , I have planned to show Notification ...