Android Twitter Application Development and Usage of TextView and Linkify - android

I'm on developing a twitter kind of Application where in I want that the user would be displayed the timelines and the Textview in the Lists require to perform clicks on (http://)URLs, (#)usernames, and (#)hasTags and I want to invoke custom methods over these actions, I have used the Linkify class and the actions but where of no use because the customization that i require cannot be incorporated.

I have a solution to the problem to check it out go to the below mentioned link http://www.orangeapple.org/?p=354

Here is my solution.
The main idea is to split the text words and creating a TextView for each one, wrapping each line with horizontal LinearLayout and the lines into vertical LinearLayout:
private LinearLayout mDescription; // vertical LinearLayout
private void setDescriptionText(String twitterText){
String[] splitted;
String regexp = "(#[-a-zA-Z0-9_]*)|(#[-a-zA-Z0-9_]*)|(http://[-a-zA-Z0-9/._]*)|(https://[-a-zA-Z0-9/._]*)|( )";
TextSplitter splitter = new TextSplitter(regexp);
splitted = splitter.split(twitterText);
TextView[] textViews = new TextView[splitted.length];
for (int i = 0; i < splitted.length; i++) {
final String str = splitted[i];
TextView textView = new TextView(mDescription.getContext());
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
textViews[i] = textView;
textView.setText(str);
textView.setTypeface(roboReg);
textView.setTextColor(Color.WHITE);
if (str.startsWith("#")){
textView.setTextColor(mLinkColor);
textView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startWebViewActivity("https://twitter.com/"+str.substring(1));
}
});
}else if (str.startsWith("#")){
textView.setTextColor(mLinkColor);
textView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startWebViewActivity("https://twitter.com/#!/search/?q="+str.substring(1) + "&src=hash");
}
});
}else if (str.startsWith("http://") || str.startsWith("https://")){
textView.setTextColor(mLinkColor);
textView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startWebViewActivity(str);
}
});
}
}
int maxWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 210, getResources().getDisplayMetrics());
populateText(mDescription, maxWidth , textViews, mDescription.getContext());
}
private void populateText(LinearLayout ll,int maxWidth, View[] views, Context mContext) {
ll.removeAllViews();
LinearLayout.LayoutParams params;
LinearLayout newLL = new LinearLayout(mContext);
newLL.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
newLL.setGravity(Gravity.LEFT);
newLL.setOrientation(LinearLayout.HORIZONTAL);
int widthSoFar = 0;
for (int i = 0; i < views.length; i++) {
LinearLayout LL = new LinearLayout(mContext);
LL.setOrientation(LinearLayout.HORIZONTAL);
LL.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
LL.setLayoutParams(new ListView.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
views[i].measure(0, 0);
params = new LinearLayout.LayoutParams(views[i].getMeasuredWidth(),
LayoutParams.WRAP_CONTENT);
LL.addView(views[i], params);
LL.measure(0, 0);
widthSoFar += views[i].getMeasuredWidth();// YOU MAY NEED TO ADD THE MARGINS
if (widthSoFar >= maxWidth) {
ll.addView(newLL);
newLL = new LinearLayout(mContext);
newLL.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
newLL.setOrientation(LinearLayout.HORIZONTAL);
newLL.setGravity(Gravity.LEFT);
params = new LinearLayout.LayoutParams(LL
.getMeasuredWidth(), LL.getMeasuredHeight());
newLL.addView(LL, params);
widthSoFar = LL.getMeasuredWidth();
} else {
newLL.addView(LL);
}
}
ll.addView(newLL);
}
private class TextSplitter {
private Pattern pattern;
private boolean keep_delimiters;
public TextSplitter(Pattern pattern, boolean keep_delimiters) {
this.pattern = pattern;
this.keep_delimiters = keep_delimiters;
}
public TextSplitter(String pattern, boolean keep_delimiters) {
this(Pattern.compile(pattern == null ? "" : pattern), keep_delimiters);
}
public TextSplitter(String pattern) {
this(pattern, true);
}
public String[] split(String text) {
if (text == null) {
text = "";
}
int last_match = 0;
LinkedList<String> splitted = new LinkedList<String>();
Matcher m = this.pattern.matcher(text);
while (m.find()) {
splitted.add(text.substring(last_match, m.start()));
if (this.keep_delimiters) {
splitted.add(m.group());
}
last_match = m.end();
}
splitted.add(text.substring(last_match));
return splitted.toArray(new String[splitted.size()]);
}
}

There are many addLinks() methods on Linkify, one of which may let you accomplish your aims, if your goal is to start an activity from those links.
You can also examine the source code to Linkify to see how you might create your own that meets your needs.

Related

Android Creating Views Programmatically not getting separated instances

I got the following code
for (int i = 0; i < cantcuenta ; i++) {
textview = new ArrayList<>();
final Integer nro = i + 1;
Hmayor = new LinearLayout(view.getContext());
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
param.weight = pesoLayout;
Hmayor.setOrientation(LinearLayout.VERTICAL);
Hmayor.setWeightSum(1);
Hmayor.setLayoutParams(param);
scrollview = new ScrollView(view.getContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0);
params.weight = 0.65f;
scrollview.setLayoutParams(params);
linearinterno = new LinearLayout(view.getContext());
linearinterno.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
linearinterno.setOrientation(LinearLayout.VERTICAL);
scrollview.addView(linearinterno);
for (int j = 0; j < 10; j++)
{
test = new TextView(view.getContext());
test.setId(j);
test.setText("Lorem Ipsum" + j);
test.setVisibility(View.GONE);
textview.add(test);
}
boleta = new Button(view.getContext());
RelativeLayout.LayoutParams parambo = new RelativeLayout.LayoutParams(100, 50);
boleta.setLayoutParams(parambo);
boleta.setText("BOLETA");
boleta.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
boleta.setTypeface(Typeface.DEFAULT_BOLD);
boleta.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
boleta.setTextColor(getResources().getColor(white, null));
boleta.setBackgroundColor(getResources().getColor(colorAccent, null));
boleta.setId(nro);
boleta.setPadding(0,0,0,0);
boleta.setVisibility(View.GONE);
boleta.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
factura = new Button(view.getContext());
RelativeLayout.LayoutParams paramfa = new RelativeLayout.LayoutParams(100, 50);
paramfa.addRule(RelativeLayout.RIGHT_OF, boleta.getId());
factura.setLayoutParams(paramfa);
factura.setText("FACTURA");
factura.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
factura.setTypeface(Typeface.DEFAULT_BOLD);
factura.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
factura.setTextColor(getResources().getColor(white, null));
factura.setBackgroundColor(getResources().getColor(colorAccent, null));
factura.setPadding(0,0,0,0);
factura.setVisibility(View.GONE);
factura.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
ncuenta = new Button(view.getContext());
LinearLayout.LayoutParams paramb = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0);
paramb.weight = 0.15f;
ncuenta.setLayoutParams(paramb);
ncuenta.setPadding(0,0,0,0);
ncuenta.setText("CUENTA " + nro);
ncuenta.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
ncuenta.setTypeface(Typeface.DEFAULT_BOLD);
ncuenta.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START);
ncuenta.setTextColor(getResources().getColor(letras, null));
ncuenta.setBackgroundColor(getResources().getColor(transparent, null));
ncuenta.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Boolean sw = false;
for (TextView este : textview)
{
if (este.getVisibility() == View.GONE)
{
este.setVisibility(View.VISIBLE);
linearinterno.addView(este);
factura.setVisibility(View.VISIBLE);
boleta.setVisibility(View.VISIBLE);
sw = true;
}
}
if (!sw)
{
for (TextView este : textview)
{
este.setVisibility(View.GONE);
factura.setVisibility(View.GONE);
boleta.setVisibility(View.GONE);
}
linearinterno.removeAllViews();
}
}
});
Hmayor.addView(ncuenta);
Hmayor.addView(scrollview);
linearinternodw = new LinearLayout(view.getContext());
LinearLayout.LayoutParams paramdw = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,0);
paramdw.weight = 0.2f;
linearinternodw.setLayoutParams(paramdw);
Hmayor.addView(linearinternodw);
relainterno = new RelativeLayout(view.getContext());
RelativeLayout.LayoutParams paramr = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
relainterno.setLayoutParams(paramr);
relainterno.addView(factura);
relainterno.addView(boleta);
linearinternodw.addView(relainterno);
theParent.addView(Hmayor);
}
it creates this distribution
when i press the button "CUENTA 1" with the name "ncuenta" in the code it should make all the text in the its own layout gone along with the buttons "BOLETA" and "FACTURA" also shown in the picture but instead, it makes it gone for the "CUENTA 2" views, I have done this kind of coding for recyclerviews and everything get instanced separatedly, would think that with a FOR everytime it does create a new instance of any view it would get attached to the corresponding cycle of the FOR.
if I divide the screen in 4 the first 3 buttons will show and hide the "CUENTA 4" wich tells me the the setOnclickListener its only taking the last set of items instanced..
Any ideas on how could solve this?
thanks in advance
Your onClickListener executes this code
for (TextView este : textview)
{
if (este.getVisibility() == View.GONE)
{
este.setVisibility(View.VISIBLE);
linearinterno.addView(este);
factura.setVisibility(View.VISIBLE);
boleta.setVisibility(View.VISIBLE);
sw = true;
}
}
When executed, the contents of the textview ArrayList will be the elements you add in the last iteration, that's why only the views in the last section are targeted.
Hope it helps.

pass value from stringbuilder to textview

I am trying to get words from several EditTexts and display them using TextView without using any layout. But the program is force closed every-time I run it.
LOGCAT: java.lang.RuntimeException: Unable to instantiate activity
ComponentInfo{com.example.tablelayout/com.example.tablelayout.MainActivity}:
java.lang.NullPointerException
public class MainActivity extends Activity {
private List<EditText> editTextList = new ArrayList<EditText>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout linearLayout = new LinearLayout(this);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(FILL_PARENT, WRAP_CONTENT);
linearLayout.setLayoutParams(params);
linearLayout.setOrientation(VERTICAL);
int count = 10;
linearLayout.addView(tableLayout(count));
linearLayout.addView(submitButton());
setContentView(linearLayout);
}
private Button submitButton() {
Button button = new Button(this);
button.setHeight(WRAP_CONTENT);
button.setText("Submit");
button.setOnClickListener(submitListener);
return button;
}
TextView txt=new TextView(this);
// Access the value of the EditText
private View.OnClickListener submitListener = new View.OnClickListener() {
public void onClick(View view) {
StringBuilder stringBuilder = new StringBuilder();
for (EditText editText : editTextList) {
stringBuilder.append(editText.getText().toString());
}
txt.setText(stringBuilder.toString().trim());
}
};
// Using a TableLayout as it provides you with a neat ordering structure
private TableLayout tableLayout(int count) {
TableLayout tableLayout = new TableLayout(this);
tableLayout.setStretchAllColumns(true);
int noOfRows = count / 5;
for (int i = 0; i < noOfRows; i++) {
int rowId = 5 * i;
tableLayout.addView(createOneFullRow(rowId));
}
int individualCells = count % 5;
tableLayout.addView(createLeftOverCells(individualCells, count));
return tableLayout;
}
private TableRow createLeftOverCells(int individualCells, int count) {
TableRow tableRow = new TableRow(this);
tableRow.setPadding(0, 10, 0, 0);
int rowId = count - individualCells;
for (int i = 1; i <= individualCells; i++) {
tableRow.addView(editText(String.valueOf(rowId + i)));
}
return tableRow;
}
private TableRow createOneFullRow(int rowId) {
TableRow tableRow = new TableRow(this);
tableRow.setPadding(0, 10, 0, 0);
for (int i = 1; i <= 5; i++) {
tableRow.addView(editText(String.valueOf(rowId + i)));
}
return tableRow;
}
private EditText editText(String hint) {
EditText editText = new EditText(this);
editText.setId(Integer.valueOf(hint));
editText.setHint(hint);
editTextList.add(editText);
return editText;
}
}
TextView txt=new TextView(this);
you can't declare and initialize a class member View at the same time, because the Context is not yet valid
change it to:
public class MainActivity extends Activity {
private List<EditText> editTextList = new ArrayList<EditText>();
private TextView txt;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
txt = new TextView(this);
// other code
also I noticed that you are not adding this View to any of the containers that your provided to setContentView. The suggestion will fix the crash but you will not see the TextView's content
Exception shows problem is in your manifest file.
Your package name is manifest tag and your activity are in different locations.

Add ImageButton dynamically from Database not displayed

I'm here today for my soundboard app ! son to make it simple i have a G_Son object which is the controller of the "Son" model. i get the list of sound from my database (everything is fine until here) but then when I dynamically try to create ImageButtons and add them on my activity (manageLayout() ), I have absolutely nothing appearing on my activity ! not even a single button. So if you have any Idea, or want to give me a hand, I'm aware of any suggestion
private G_Son gson;
private OurNiceSoundPlayer soundPlayer;
private List<Son> sons;
RelativeLayout gameBoard;
private Son selectedSound;
private View.OnClickListener mSoundOnClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
soundPlayer.setSound(sons.get(Integer.parseInt(v.getTag().toString())));
Log.i("Board", "Clicked on ImgButton ->" + v.getTag());
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_board);
gson = G_Son.getInstance();
sons = gson.getSons(getApplicationContext());
soundPlayer = new OurNiceSoundPlayer(getApplicationContext());
gameBoard = (RelativeLayout) findViewById(R.id.soundboard);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.CENTER_IN_PARENT,1);
manageLayout();
gameBoard.invalidate();
}
private void manageLayout() {
if (sons.size()>0)
{
int rawNbr = (int) Math.ceil((double) sons.size() / 3);
int currentSon = 0;
Son displayed = sons.get(currentSon);
for (int i = 0; i < rawNbr; i++)
{
LinearLayout row = new LinearLayout(this);
row.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
for (int j = 0; j < 3; j++)
{
ImageButton btnTag = new ImageButton(this);
if (displayed.getIsPerso()) {
File imgFile = new File(displayed.getPathImage());
if (imgFile.exists()) {
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
btnTag.setImageBitmap(myBitmap);
} else {
btnTag.setImageResource(R.drawable.defaultimage);
}
}
else
{
int id = getResources().getIdentifier("com.example.m.sbst:drawable/" + displayed.getPathImage(), null, null);
btnTag.setImageResource(id);
}
btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
btnTag.setOnClickListener(mSoundOnClickListener);
btnTag.setBackgroundColor(Color.TRANSPARENT);
btnTag.setTag(currentSon);
btnTag.setId(i);
row.addView(btnTag);
currentSon++;
if (currentSon>=sons.size())
{
break;
}
else
{
displayed = sons.get(currentSon);
}
}
gameBoard.addView(row);
}
}
}
Forgot to mention a size, moreover, after months of learning android, I should advice people to use a grid view to do so, it allows with custom layout to do event better than creating your own layout with dynamic image buttons

refresh activity with dynamic content

I have an activity with some dynamic content.
For each item of an array, there is a different content (number of textViews, checkboxes, ..)
The user clicks on a "save" button and then the screen has to refresh itself to display the content of the next item.
I was wondering if it was a good practice to reload my activity like this :
Intent refresh = new Intent(this, conflictActivity.class);
startActivity(refresh)
finish()
instead of removing all the views in my layouts, etc ...
I think it's easier to reload it.
Is there another way to do it ?
EDIT : added Code.
public class ConflicFieldTest extends Activity {
private int nbField;
private int nbChecked;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.conflict);
final Button saveButton = (Button) findViewById(R.id.save);
final LinearLayout leftLayout = (LinearLayout) findViewById(R.id.leftLayout);
final LinearLayout rightLayout = (LinearLayout) findViewById(R.id.rightLayout);
final TextView fieldName = new TextView(this);
final LinearLayout editLayoutRight = new LinearLayout(this);
final LinearLayout editLayoutLeft = new LinearLayout(this);
final LinearLayout fieldRight = new LinearLayout(this);
final LinearLayout fieldLeft = new LinearLayout(this);
final ArrayList<String> allIdInConflict = getAllIdInConflict();
for (int i = 0; i < allIdInConflict.size(); i++) {
new AccountSyncTask() {
#Override
public void onPostExecute(
final ArrayList<ArrayList<String>> result) {
nbField = 0;
nbChecked = 0;
final Map<String, Object> fieldsInConflict = conflict
.conflictWithFields(memoAccountMap,
serverAccountMap, modifiedAccountMap);
for (Iterator<Map.Entry<String, Object>> field = fieldsInConflict
.entrySet().iterator(); field.hasNext();) {
final Map.Entry<String, Object> entry = field.next();
fieldName.setText(entry.getKey() + " : ");
final EditText[] editTextArrayRight = new EditText[fieldsInConflict
.size()];
final EditText[] editTextArrayLeft = new EditText[fieldsInConflict
.size()];
final CheckBox[] checkboxRight = new CheckBox[fieldsInConflict
.size()];
final CheckBox[] checkboxLeft = new CheckBox[fieldsInConflict
.size()];
checkboxRight[nbField] = new CheckBox(
ConflicFieldTest.this);
checkboxLeft[nbField] = new CheckBox(
ConflicFieldTest.this);
editLayoutLeft.setGravity(Gravity.CENTER_HORIZONTAL);
editLayoutRight.setGravity(Gravity.CENTER_HORIZONTAL);
fieldRight.setGravity(Gravity.CENTER_HORIZONTAL);
fieldLeft.setGravity(Gravity.CENTER_HORIZONTAL);
editLayoutRight.setOrientation(LinearLayout.HORIZONTAL);
fieldRight.setOrientation(LinearLayout.VERTICAL);
fieldLeft.setOrientation(LinearLayout.VERTICAL);
rightLayout
.addView(
editLayoutRight,
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
leftLayout
.addView(
editLayoutLeft,
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
editTextArrayRight[nbField] = new EditText(
ConflicFieldTest.this);
editTextArrayRight[nbField].setText((String) entry
.getValue());
editTextArrayRight[nbField].setTextColor(Color.BLACK);
editTextArrayRight[nbField].setFocusable(false);
editTextArrayRight[nbField].setClickable(false);
editTextArrayRight[nbField].setWidth(180);
editTextArrayRight[nbField].setPadding(0, 10, 0, 0);
editTextArrayLeft[nbField] = new EditText(
ConflicFieldTest.this);
editTextArrayLeft[nbField]
.setText((String) serverAccountMap.get(entry
.getKey()));
editTextArrayLeft[nbField].setTextColor(Color.BLACK);
editTextArrayLeft[nbField].setFocusable(false);
editTextArrayLeft[nbField].setClickable(false);
editTextArrayLeft[nbField].setWidth(180);
editTextArrayLeft[nbField].setPadding(0, 10, 0, 0);
final int i1 = nbField;
checkboxLeft[i1]
.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
checkboxRight[i1]
.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
editLayoutLeft.addView(fieldName);
editLayoutRight
.addView(
editTextArrayRight[nbField],
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
editLayoutLeft
.addView(
editTextArrayLeft[nbField],
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
editLayoutRight.addView(checkboxRight[nbField]);
editLayoutLeft.addView(checkboxLeft[nbField]);
nbField++;
}
saveButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
//save data & update UI
}
}
.execute(appState.getSessionName(), id, "getObjectOnServer");
}
}
Sorry for the code, it is a litle bit dirty (i have deleted some parts), i am reworking it.
int flag = 0;
public void updateUI() {
if(flag == 0) {
//your normal codes...
} else {
//display view with user given data
}
}
Call this function in oncreate. At first flag will be set to zero. When u enter the data in textview and tick checkboxes, update the flag with some value. Then on save Button click call this function again. Now flag is set. So it will display the updated UI.
I think this will give u an idea..

How can we create dynamic textview?

how to create the textview in code not in xml file. It is because number of textviews will be changing in my application according to some integer.
This is the code to create TextView Dynamically
LinearLayout layout = (LinearLayout ) findViewById(R.id.llayout);
for (int i = 0; i < 3; i++) {
TextView dynamicTextView = new TextView(this);
dynamicTextView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
dynamicTextView.setText("NewYork");
layout.addView(tstate);
}
Maybe thats what you need:
LinearLayout lin = (LinearLayout) findViewById(R.id.myLinear);
for (int i = 0; i <= 10 ; i++)
{
TextView myText = new TextView(this);
myText.setText("textview# "+ i);
lin.addView(myText);
}
Something like the following should be what you need:
final int N = 10; // total number of textviews to add
final TextView[] myTextViews = new TextView[N]; // create an empty array;
for (int i = 0; i < N; i++) {
// create a new textview
final TextView rowTextView = new TextView(this);
// set some properties of rowTextView or something
rowTextView.setText("This is TextView #" + i);
// add the textview to the linearlayout
myLinearLayout.addView(rowTextView);
// save a reference to the textview for later
myTextViews[i] = rowTextView;
}
private LinearLayout ll;
private TextView tv;
// in oncreate()
onCreate()
{
int WrapWidth = LinearLayout.LayoutParams.WRAP_CONTENT;
int WrapHeight = LinearLayout.LayoutParams.WRAP_CONTENT;
tv = new TextView(this);
ll.addView(tv,WrapWidth,WrapHeight);
}
Code is here
final int c = 12;
final TextView[] mtext = new TextView[c];
for (int i = 0; i < c; i++) {
TextView rowtxt = new TextView(this);
rowtxt.setText("Hello" + i);
myLinearLayout.addView(rowtxt);
myTextViews[i] = rowtxt;
myTextViews[i].setOnClickListener(onclicklistener);//textview click
}
OnClickListeners code is here
OnClickListener onclicklistener = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v == myTextViews[0]){
//do whatever you want....
}
}
};
Hope it is helpful for you
You use TextView textView = new TextView(CurrentActivity.this);
and then you add setter arguments that come with the TextView class

Categories

Resources