How to use same Custom Layout with different Image and Text in android Preference class

163 views Asked by At

I want to insert custom layout in android Preference class. My custom layout contains one ImageView and one TextView. There are 7 preferences and all preferences have same custom layout but different images and texts. Now one way to implement such Preference Screen is, make 7 .xml files one for each preference. Is there any other way so that I need to make only one .xml file and I can set image source and text string programatically ?

**EDIT : I need to show all 7 preferences simultaneously. **

2

There are 2 answers

0
NehaK On

You can use same layout and changing then at runtime. For example,

TextView tv1 = findViewById(R.id.textview);
tv1.setText("tv1");
layout.addView(tv1);

TextView tv2 = findViewById(R.id.textview);
tv1.setText("tv2");
layout.addView(tv2);
.
.
TextView tv5 = findViewById(R.id.textview);
tv1.setText("tv5");
layout.addView(tv5);

Or you can just change their data at runtime by using only single reference to use same layout.

9
blueware On

You can use one layout with all your views for all preferences, but you need to control them at run time by using:

yourView.setVisibility(View.VISIBLE);
otherView.setVisibility(View.GONE);
thirdView.setVisibility(View.GONE);
fourthView.setVisibility(View.GONE);
. . .