Android Creating Views Programmatically not getting separated instances - android

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.

Related

Adding onClick to buttons that are created programmatically

A TextView and Button are programmatically created and added to a pre-existing vertical layout making it look like a vertical list of views. These views are only created based off the user entering data into an edittext and that data being saved into an ArrayList.
How can I add an onClick function to my 'trash' buttons that are programmatically created that would allow them to remove the view they are associated with.
public static ArrayList<String> deckNameArray = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout mainLayout = (LinearLayout)findViewById(R.id.mainLayout);
for(int i = 0; i < deckNameArray.size(); i++)
{
LinearLayout layout = new LinearLayout(this);
if ((i % 2) == 0) {
layout.setBackgroundColor(Color.CYAN);
} else {
layout.setBackgroundColor(Color.WHITE);
}
layout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(layoutParams);
layout.setPadding(10, 5, 10, 5);
layout.setWeightSum(5);
mainLayout.addView(layout);
LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT, 4f);
TextView deckName = new TextView(this);
deckName.setText(deckNameArray.get(i));
deckName.setTextColor(Color.BLACK);
deckName.setTextSize(18);
deckName.setGravity(Gravity.CENTER_VERTICAL);
deckName.setLayoutParams(textViewParams);
layout.addView(deckName);
LinearLayout.LayoutParams imageButtonParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT, 1f);
ImageButton remove = new ImageButton(this);
remove.setImageResource(R.mipmap.trash);
if ((i % 2) == 0) {
remove.setBackgroundColor(Color.CYAN);
} else {
remove.setBackgroundColor(Color.WHITE);
}
remove.setLayoutParams(imageButtonParams);
layout.addView(remove);
}
remove.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
See http://developer.android.com/reference/android/widget/Button.html
You can set a click listener on any View, using code similar to the sample below:
View myView;
myView.setOnClickListener(new OnClickListener() {
#Override public void onClick(View v) {
// TODO your logic here
}
}

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

Set the text of a toast using a position in an array that is the iteration of the loop

This code programatically generates a layout which consists of views and TextViews in a way that is defined in an array. However, I am trying to make each TextView clickable which displays a toast of a bigger explanation of that TextView . Since the toast needs to be specific to the particular TextView , I am trying to set the TextView using another array of explanations at position i. However, when I try to do this I get the error "Cannot refer to a non final variable inside an inner class defined in a different method."
So I am wondering, how do I get around this? i can't be final because it changes on each loop iteration?
for ( int i = 0; i < formulae.length; i++){
if (formulae[i].equals("horizontalrule")){
View rule = new View(FormulaeActivity.this);
rule.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 2));
rule.setBackgroundColor(0xFF000000);
View spacerbig = new View(FormulaeActivity.this);
spacerbig.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 20));
spacerbig.setBackgroundResource(android.R.color.transparent);
container.addView(rule);
container.addView(spacerbig);
}
else{
View spacersmall1 = new View(FormulaeActivity.this);
spacersmall1.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 5));
spacersmall1.setBackgroundResource(android.R.color.transparent);
tx[i] = new TextView(FormulaeActivity.this);
tx[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 50));
tx[i].setText(Html.fromHtml(formulae[i]));
tx[i].setTextSize(20);
tx[i].setPadding(10, 0, 0, 0);
tx[1].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), Html.fromHtml(explanations[i]), Toast.LENGTH_SHORT).show();
// ^this is the offending line
}
});
container.addView(tx[i]);
container.addView(spacersmall1);
}
Why not have a final variable which will hold the value of i on each iteration.
for ( int i = 0; i < formulae.length; i++){
final int pos = i;
And use it like this on the onClick()
Toast.makeText(getApplicationContext(), Html.fromHtml(explanations[pos])..
Try this,
for ( int i = 0; i < formulae.length; i++){
if (formulae[i].equals("horizontalrule")){
View rule = new View(FormulaeActivity.this);
rule.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 2));
rule.setBackgroundColor(0xFF000000);
View spacerbig = new View(FormulaeActivity.this);
spacerbig.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 20));
spacerbig.setBackgroundResource(android.R.color.transparent);
container.addView(rule);
container.addView(spacerbig);
}
else{
View spacersmall1 = new View(FormulaeActivity.this);
spacersmall1.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 5));
spacersmall1.setBackgroundResource(android.R.color.transparent);
tx[i] = new TextView(FormulaeActivity.this);
tx[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 50));
tx[i].setText(Html.fromHtml(formulae[i]));
tx[i].setTextSize(20);
tx[i].setPadding(10, 0, 0, 0);
container.addView(tx[i]);
container.addView(spacersmall1);
}
}
for(int i=0;i<tx.length;i++)
{
if(tx[i]!=null)
{
tx[i].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),Html.fromHtml(explanations[i]), Toast.LENGTH_SHORT).show();
// ^this is the offending line
}
});
}
}

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..

Android Twitter Application Development and Usage of TextView and Linkify

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.

Categories

Resources