• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Best practice for inflating

I

Inflating views

Guest
Currently i'm learning android and i'm an application development student.

I have been working on android for quite a bit of hours now and felt like something was rather odd.
When i inflate all my views inside an xml layout, like all my textviews and buttons.
Especially if i have a lot, i end up with way too much code that feels so redundant.

Is there a common method for doing this easier? i think there should be but i can't find it cause i don't quite know to look for it

Currently i have this.

project_name = (TextView) findViewById(R.id.project_name);
mainlist = (TwoWayView) findViewById(R.id.lvItems);
project_name.setText(getIntent().getStringExtra("project_name"));
project_duration = (TextView) findViewById(R.id.project_duration);

total_coding_value = (TextView) findViewById(R.id.total_coding_value);
total_design_value = (TextView) findViewById(R.id.total_design_value);
total_planning_value = (TextView) findViewById(R.id.total_planning_value);


assigned_student_right = (RelativeLayout) findViewById(R.id.student_item_1);
assigned_student_middle = (RelativeLayout) findViewById(R.id.student_item_2);
assigned_student_left = (RelativeLayout) findViewById(R.id.student_item_3);

avatarright = (CircularNetworkImageView) findViewById(R.id.avatar);
avatarmiddle = (CircularNetworkImageView) findViewById(R.id.avatar2);
avatarleft = (CircularNetworkImageView) findViewById(R.id.avatar3);

coding_indicator = (TextView) findViewById(R.id.coding_indicator);
coding_indicator2 = (TextView) findViewById(R.id.coding_indicator2);
coding_indicator3 = (TextView) findViewById(R.id.coding_indicator3);

planning_indicator = (TextView) findViewById(R.id.planning_indicator);
planning_indicator2 = (TextView) findViewById(R.id.planning_indicator2);
planning_indicator3 = (TextView) findViewById(R.id.planning_indicator3);

design_indicator = (TextView) findViewById(R.id.design_indicator);
design_indicator2 = (TextView) findViewById(R.id.design_indicator2);
design_indicator3 = (TextView) findViewById(R.id.design_indicator3);

coding_value = (TextView) findViewById(R.id.coding_value);
coding_value2 = (TextView) findViewById(R.id.coding_value2);
coding_value3 = (TextView) findViewById(R.id.coding_value3);

planning_value = (TextView) findViewById(R.id.planning_value);
planning_value2 = (TextView) findViewById(R.id.planning_value2);
planning_value3 = (TextView) findViewById(R.id.planning_value3);

design_value = (TextView) findViewById(R.id.design_value);
design_value2 = (TextView) findViewById(R.id.design_value2);
design_value3 = (TextView) findViewById(R.id.design_value3);

display_info_left = (TextView) findViewById(R.id.display_placeholder_text_left);
display_info_right = (TextView) findViewById(R.id.display_placeholder_text_right);
display_info_middle = (TextView) findViewById(R.id.display_placeholder_text_middle);


Surely there must be a more minimalistic way to do this ?

Thank you in advance.
 
It doesnot look good... if you inflate these many views and write the application logic on all the views in the same class....oh god...its just........______________
so i want to you to inflate view in CustomView say.....Create CustomLinearLayout Class extend it from LinearLayout.....so inflate the view layout in the Custom class..so you can use this custom class in your main layout as<packagename.customclassname></packagename.customclassname>

So in the main activity class inflate this view and you implement all the method functionalites in custom class ..and use those in main activity class....
by BALU(pgeek.xyz)
 
Last edited:
Upvote 0
Unfortunately this is quite common. If your UI contains a lot of components, and you need to do something (like validation) on those components, then you end up storing the TextViews, or whatever in a local or class variable.
It's just boiler plate code that has to be written. If you had a sequence of the same type of component that could be stored in an array, then a loop could be used to simplify it a bit.
 
Upvote 0
Unfortunately this is quite common. If your UI contains a lot of components, and you need to do something (like validation) on those components, then you end up storing the TextViews, or whatever in a local or class variable.
It's just boiler plate code that has to be written. If you had a sequence of the same type of component that could be stored in an array, then a loop could be used to simplify it a bit.


Thank you for the information, i am confident now in what i am doing and i did think of looping components if obvious.

Thank you for the info, have a nice day.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones