In This tutorials we will implement dynamic text View design . explain below
MainActivity.java
...................................................................................................................................................................
package com.androidbeginner.testcode;
import java.util.ArrayList;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
LinearLayout ln;
ArrayList<String> ParameterList = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ln = (LinearLayout) findViewById(R.id.layouts);
final TextView tutorial = new TextView(getApplication());
tutorial.setText("AndroidBeginner");
tutorial.setTextSize(40);
tutorial.setGravity(Gravity.CENTER);
tutorial.setTextColor(Color.BLUE);
ln.addView(tutorial);
/*
* Add items in arraylist
*/
ParameterList.add("item1");
ParameterList.add("item2");
ParameterList.add("item3");
ParameterList.add("item4");
ParameterList.add("item5");
ParameterList.add("item6");
for (int i = 0; i < ParameterList.size(); i++) {
String id = ParameterList.get(i);
/*
* create a textView
*/
final TextView text = new TextView(getApplication());
text.setTag(id);
text.setText(ParameterList.get(i));
text.setTextColor(Color.RED);
text.setTextSize(20);
text.setGravity(Gravity.CENTER);
ln.addView(text);
/*
* click on text items change item text Color
*/
text.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
text.setTextColor(Color.BLACK);
}
});
}
}
}
MainActivity.java
...................................................................................................................................................................
package com.androidbeginner.testcode;
import java.util.ArrayList;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
LinearLayout ln;
ArrayList<String> ParameterList = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ln = (LinearLayout) findViewById(R.id.layouts);
final TextView tutorial = new TextView(getApplication());
tutorial.setText("AndroidBeginner");
tutorial.setTextSize(40);
tutorial.setGravity(Gravity.CENTER);
tutorial.setTextColor(Color.BLUE);
ln.addView(tutorial);
/*
* Add items in arraylist
*/
ParameterList.add("item1");
ParameterList.add("item2");
ParameterList.add("item3");
ParameterList.add("item4");
ParameterList.add("item5");
ParameterList.add("item6");
for (int i = 0; i < ParameterList.size(); i++) {
String id = ParameterList.get(i);
/*
* create a textView
*/
final TextView text = new TextView(getApplication());
text.setTag(id);
text.setText(ParameterList.get(i));
text.setTextColor(Color.RED);
text.setTextSize(20);
text.setGravity(Gravity.CENTER);
ln.addView(text);
/*
* click on text items change item text Color
*/
text.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
text.setTextColor(Color.BLACK);
}
});
}
}
}
0 comments