Is it possible to set multiple id values in the same TextView somehow?
I have a long string with multiple clickable links in it and I want to assign ids to those links so I can fetch them in the code, is that achievable?
I tried putting separate TextView elements in a horizontal LinearLayout, but it cannot be wrapped to another row if it's too long.
Is there some custom element/way to achieve this behavior?
Use RecyclerView to create list of TextView with links. For aligning it correctly, you can use flexbox-layout
Add dependency in build.gradle(:app)
dependencies {
implementation 'com.google.android:flexbox:2.0.1'
}
and use FlexboxLayoutManager with RecyclerView as follows:
val flexLayoutManager = FlexboxLayoutManager(activity)
flexLayoutManager.flexDirection = FlexDirection.ROW;
flexLayoutManager.justifyContent = JustifyContent.FLEX_START;
recyclerViewTextLinks!!.layoutManager = flexLayoutManager
val flexAdapter = FlexDemoAdapter(listOfTextLinks, onTextLinkClicked) //onTextLinkClicked for handling link clicks
recyclerViewTextLinks!!.adapter = flexAdapter
Related
I'm trying to create a custom View that contains a list of CheckBoxes based on my database. This means I have to create the Views at run-time, and can't do it in XML. However, the method I'm using for this is very slow. Is there a faster method to create large amount of Views in code?
For example, with the 18 types in my database, it can take over 1 second to create all the CheckBoxes.
class FilterView : LinearLayout {
private fun init(types : List<Type>){
... setup
// Creating the CheckBoxes, this takes all the time.
checkboxes = Array(types.size, {
AppCompatCheckBox(context).apply {
text = types[it].type
CompoundButtonCompat.setButtonTintList(this, ColorStateList(states, intArrayOf(colours[it], colours[it])))
}
})
... add to view
}
What your looking for is a Recyclerview. It can all be explained here. The downvote was likely because this is assumed to be common knowledge or easily googled on your own. I was new once too. Here you go.
I'm trying to build a timetable app and there I will need a lot of dynamical Textviews, the only way I know is to declare every Textview for it self like
MoFa1 = (TextView) findViewById(R.id.MoFa1);
MoFa2 = (TextView) findViewById(R.id.MoFa2);
MoFa3 = (TextView) findViewById(R.id.MoFa3);
MoFa4 = (TextView) findViewById(R.id.MoFa4);
MoFa5 = (TextView) findViewById(R.id.MoFa5);
MoFa6 = (TextView) findViewById(R.id.MoFa6);
MoFa7 = (TextView) findViewById(R.id.MoFa7);
MoFa8 = (TextView) findViewById(R.id.MoFa8);
MoFa9 = (TextView) findViewById(R.id.MoFa9);
MoFa10 = (TextView) findViewById(R.id.MoFa10);
Now I want to know if there is another easier way to declare multiple views.Thanks in advance
How are your views displayed? Since you're creating a timetable I think your views are displayed in a grid. You can use GridLayout or you can use a RecyclerView. Here its an excellent article about using RecyclerView.
EDIT: Here its another article using RecyclerView with GridLayoutManager
I don't know how they are displayed, but you could use a gridlayout and add them dynamically by looping throught cells.
http://www.techotopia.com/index.php/Working_with_the_Android_GridLayout_in_XML_Layout_Resources
http://android-er.blogspot.ca/2014/09/insert-view-to-gridlayout-dynamically.html?m=1
If these links actually help you, I will describe its content here.
Use butter knife http://jakewharton.github.io/butterknife/
#BindView(R.id.MoFa5) TextView mofa5;
That's it. No findviewbyid.
Hello I am iOS developer just started with android, I know basic concepts of android like RecyclerView, ListView.
I want to make a feed page in which each row can contain one photo and multiple comments, no of comments are dynamically changing depending on data.
How to achieve the same using RecyclerView/ListView?
I can achieve the same in iOS like this : http://t.co/z1IRHTTjED
If you already know concepts of ListView and RecyclerView you should know that we use Adapters to provide views for List.
You can create a custom Adapter which will create a view, where, depends on data you have, you will display comments or not.
Edit.
For example you have a layout like this
<LinearLayout>
<ImageView />
<TextView android:id="#+id/comments" />
</LinearLayout>
in your adapter you should call
TextView comments = (TextView) findViewById(R.id.comments);
if(haveComments) {
comments.setVisibility(View.VISIBLE);
//display comments
} else {
comments.setVisibility(View.GONE);
}
If you need to have more complex layout for comments, for example with images, and date field, instead of TextView declare LinearLayout with vertical orientation. You can fill it with items from code.
LinearLayout commentsLayout = (LinearLayout) findViewById(R.id.commentsLayout);
if(haveComments) {
comments.setVisibility(View.VISIBLE);
//display comments
commentsLayout.addView(inflateCommentView());
} else {
comments.setVisibility(View.GONE);
}
private View inflateCommentView() {
View myComment = LayoutInflater.from(context).inflate(R.layout.my_complex_comment, null);
//set listeners, fill with data
return myComment;
}
Not sure if the head line describes its well, what I want is that:
I have let's say 9 TextViews in my layout, named tv1, tv2, ..., tv9.
Of course I can access each of them with
TextView tv11 = (TextView) findViewById(R.id.tv1)
Lot's of typing, so I rather would have a loop and loop through these items, but how can include the findViewbyId into a loop that I can change the value accordingly?
Is there any reason why you can't iterate over the children of the containing ViewGroup? So you get the number of children with getChildCount and then you can access each individual child with getChildAt.
You may have to test whether a particularly child is an instance of TextView if you have other views in the layout, but the basic concept is fairly straightforward.
You could make an array of the IDs you want to loop through.
IE:
public static final int TEXTVIEW_IDS = { R.id.tv1, R.id.tv2, R.id.tv3, etc.. };
In your view code:
for (int id : TEXTVIEW_IDS) {
((Textview) findViewById(id)).CallYourMethod();
}
If you cannot do what #James Holderness suggested, this may be a suitable alternative.
hii every buddy ,
am not getting how to load parsed data into an existing textview(existing layout) in android,
insted of loading data to the new textview like how they r doing in the following tutorial(check the following link)
check this link for tutorial
In the tutorial they do:
TextView something = new TextView(context);
As you don't want to do so (good decision), you get the reference to the existent TextView:
TextView something = (TextView)findViewById(R.id.the_id_you_gave_it_in_the_xml);
// then:
something.setText(theParsedString);
Try this code using existing Layout
Textview name=(TextView)findViewById(R.id.textView1);
String txt="";
for(int i=0;i<sitesList.getName().size();i++)
{
txt=txt+sitesList.getName().get(i).toString();
}
name.setText(txt);
Sowmya It seems that you are creating multiple textviews & trying to update those textviews .In that case you need to use setTag() and getTag() .
There is an example you can find here
Regarding your answer:
we r declaring textview array so am
unable to load to existing textview
I have never implemented such a textview array instead the best practice would be to
for (String s : stringarray) {
TextView tv = new TextView (this);
tv.setText(s);
tv.setTag("1");
linearlayout.addView(tv);
}
*EDIT:*For those enthusiast out there I found 2 more methods:
One is using setId() to manually set ID of that textview & then find it by findViewById()
Another method(is though not implemented by me , but is a suggestion by my colleague so dont kill me if it doesn't works) is to store textview objects in an arraylist & then access them & do whatever you want whoa!