Easier way to declare a lot of Views - android

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.

Related

Multiple ids in the same TextView

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

Android: android textview recognition

This code is within a method in a class (not main):
TextView moodouttw = (TextView) findViewById(R.id.moodouttw);
The textview seemes not to be recognized in this part: R.id.moodouttw
The textview moodouttw is located within a layout xml.
Can someone please helpout with code to correctly format the code above?
(I've tried to minimize code at first if it could be solved with any further info)
LOGCAT: 940Kb cant post
is not iR.d is just R.id
the proper way will be
findViewById(R.id.moodouttw);
this is the complete correct sentence
TextView moodouttw = (TextView) findViewById(R.id.moodouttw);
ok as you stated at your xml file, the problem is that your TextView id is moodtw and you are doing R.id.moodouttw;
so you need to do this
TextView moodouttw = (TextView) findViewById(R.id.moodtw);

setVisibility(View.Gone) with SwipeyTabs

I'm a beginner android programmer, and I'm trying to figure out how to work with SwipeyTabs. First of all, is this a good way to find the view:
rb2 = (RadioButton) getView().findViewById(R.id.rb2);
rb2.setOnClickListener(this);
My second question is about how to set the visibility of a TextView or an EditText with SwipeyTabs. I've made a simple application without SwipeyTabs, and there I use the code:
tvPlayer1.setVisibility(View.GONE);
Once I use this same line of code within SwipeyTabs, it crashes. Can someone explain me why I can't get this to work?
Thank you in advance!
Turned out I had a grammar mistake in my line of code.
etPlayer1 = (TextView) getView().findViewById(R.id.etPlayer1);
had to be:
etPlayer1 = (EditText) getView().findViewById(R.id.etPlayer1);

Faster and shorter way of finding views

Is there a quicker or shorter way to initialize all view in my layout than this:
row2[0] = (RelativeLayout) findViewById(R.id.ll22);
row2A[0] = (RelativeLayout) findViewById(R.id.ll22alt);
row2B[0] = (RelativeLayout) findViewById(R.id.ll22blank);
mOffsiteDataBackup[0] = (TextView) findViewById(R.id.ll22_backup);
mRam[0] = (TextView) findViewById(R.id.ll22_ram);
mCpu[0] = (TextView) findViewById(R.id.ll22_cpu);
mHdd[0] = (TextView) findViewById(R.id.ll22_hdd);
mOs[0] = (TextView) findViewById(R.id.ll22_system);
mStatusIcon[0] = (ImageView) findViewById(R.id.ll22_image);
.
.
.
It is really annoying to write lot of lines only to find views. Until I find all of them and initialize some listeners, my onCreate has more than 400 lines, which is something I certainly don't want.
Thanks for your tips !
Any way you do it it's going to be around the same amount of typing, in some cases even more. 50> an extreme amount of views in an activity...
The only way I can think of making this smaller is splitting them up into Fragments and use the updated framework... 70 elements shouldn't be in one ui.. a ui is meant to be simple and easy to use. Not with lots of elements to it that distracts the user from the main task they want it for.
If few UI elements are functional unity(like your progresBar and textView), you can create custom view with these elements. That makes code is much more clear, especially if you use more instances of this view in one activity.

how to load parsed data into an existing textview(existing layout) in android

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!

Categories

Resources