Add views outside a ListView [duplicate] - java

This question already has answers here:
Trouble with ListView
(3 answers)
Closed 4 years ago.
I have a problem while I am trying to create a simple ListView.
Here is what I want : just an activity with 1 ListView with text inside (and buttons in the next steps) AND beside 1 (or more) button.
So in my XML I have a ListView, a TextView (the one inside the ListView's children and a button (the one I don't want inside my ListView). When I lunch my app, i have every views of my activity in my listView... And i don't know where I have to manage that.
Here is my code :
My activity :
ListView listView = findViewById(R.id.MainListDecks);
AdapterDeck adpDeck;
ArrayList<Deck> myDecks = new ArrayList<>();
for (int i = 0; i<5;i++){
myDecks.add(new Deck("Deck " + i));
}
adpDeck = new AdapterDeck(this, 0, myDecks);
listView.setAdapter(adpDeck);
My adapter :
public AdapterDeck(#NonNull Context context, int resource, #NonNull
ArrayList<Deck> decks) {
super(context, resource, decks);
this.decks = decks;
this.context = context;
}
#Override
public View getView(final int position, View convertView, ViewGroup
parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.activity_main, null);
}
TextView listItemText = view.findViewById(R.id.MainDeckNom);
listItemText.setText(decks.get(position).getNom())
return view;
}
My XML :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/Background"
tools:context="activities.MainMenu">
<ListView
android:id="#+id/MainListDecks"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/MainDeckNom"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="20dp"
android:layout_marginBottom="22dp"
android:text="Button" />
</RelativeLayout>
The result : the result i got and i don't want
Thanks for anyone who can help !
Cheers

They're not in your ListView, they're above it.
Your root layout is a RelativeLayout. RelativeLayout allows children to overlap. If you want a ListView with a button on the bottom right and (I'm assuming) some text on the bottom, you should be using a LinearLayout and a FrameLayout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/Background"
android:orientation="vertical"
tools:context="activities.MainMenu">
<ListView
android:id="#+id/MainListDecks"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/MainDeckNom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:layout_marginBottom="22dp"
android:text="Button"
android:layout_gravity="center_vertical|end"
/>
</FrameLayout>
</LinearLayout>

Related

Android ListView background color masks onClick ripple effect

I have a custom ListView where each element has an icon and 3 TextViews. I am able to change the background to white but now I don't see the dynamic effect when I click on the text. I mean the gray color on touch (ripple effect).
this is the MainActivity
ListView mListView = findViewById(R.id.list_layout);
names = new ArrayList<>();
macs = new ArrayList<>();
adapter= new CompleteListAdapter(this, names, macs);
mListView.setAdapter(adapter);
adapter.notifyDataSetChanged();
Method in my CompleteListAdapter which extends BaseAdapter
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = LayoutInflater.from(mContext).inflate(R.layout.two_line_icon, parent, false);
TextView text1 = (TextView) rowView.findViewById(R.id.text1);
TextView text2 = (TextView) rowView.findViewById(R.id.text2);
ImageView icon = (ImageView) rowView.findViewById(R.id.icon);
text1.setText(names.get(position));
text2.setText(macs.get(position));
return rowView;
}
This is part of the main_activity.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
tools:context="com.example.myapp.MainActivity">
<ListView
android:id="#+id/list_layout"
android:layout_width="match_parent"
android:layout_height="400dp"></ListView>
</LinearLayout>
And this is the single entry of the ListView: two_line_icon.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:background="#android:color/white"
android:layout_height="match_parent">
<ImageView android:id="#+id/icon"
android:foregroundTint="#android:color/transparent"
android:foregroundTintMode="add"
android:scaleType="fitStart"
android:src="#drawable/img_logo"
android:tintMode="add"
android:visibility="visible"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Texto 1"/>
<TextView android:id="#+id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Tet2"/>
<TextView android:id="#+id/text3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="text3"/>
</LinearLayout>
</LinearLayout>

How to set parent background on ArrayAdapter

I am building an app and have some difficulties on setting backround on a array adapter, hope someone can help!
public class ListAdapter extends ArrayAdapter {
// List context
private final Context context;
// List values
private final List<RssItem> items;
public ListAdapter(Context context, List<RssItem> items) {
// Set the layout for each item
super(context, R.layout.item, items);
this.context = context;
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Build each element of the list
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.item, parent, false);
// Populate each layout element with the data from the items list
TextView itemPos = (TextView) rowView.findViewById(R.id.position_aa);
itemPos.setText(items.get(position).getPosition());
// Populate each layout element with the data from the items list
TextView itemTitle = (TextView) rowView.findViewById(R.id.textViewTitle);
itemTitle.setText(items.get(position).getTitle());
TextView itemPublishDate = (TextView) rowView.findViewById(R.id.textViewPublishDate);
itemPublishDate.setText(items.get(position).getPublishDate());
// Set the icon base on the post's category
ImageView icon = (ImageView) rowView.findViewById(R.id.imageViewCategoryIcon);
icon.setImageResource(CategoryMapper.getIconIdForCategory(items.get(position).getCategory()));
return rowView;
}
}
And this is the item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/bg">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#0FFF3300"
android:id="#+id/item">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageViewCategoryIcon"
android:layout_width="100dp"
android:layout_height="100dp"
android:padding="5dp"
android:src="#drawable/ic_launcher" />
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="1dp">
<TextView
android:id="#+id/textViewTitle"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="[TITLE-GOES-HERE]"
android:textColor="#000"
android:textStyle="bold"/>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textViewPublishDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:textColor="#555"
android:text="[PUB-DATE-GOES-HERE]"
android:textAppearance="?android:attr/textAppearanceSmall" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/position_aa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:textColor="#555"
android:text="[AA-GOES-HERE]"
android:textAppearance="?android:attr/textAppearanceSmall" />
</TableRow>
</TableLayout>
</TableRow>
</TableLayout>
</LinearLayout>
</LinearLayout>
Here I deploy some rss data into RowViews Via the list adapter class, but how can I set a parent layout so I can give a fixed upon scroll background?
Thanks in advance
P.S I`m stuck for a day at this......
Set background color to your ListItem like
rowView.setBackgroundResource(R.color.yourcolor);

Android ListView and ArrayAdapter implementation

So, I'm trying to create a screen that when clicking a button this takes the data from some EditText and add them to a ListView item, now I know there are a lot of examples on the web and I've done them and they work, but when I took them to what I want to do it just adds one item and stops working it doesn't throw any exception or anything, it just add one item, this is what I got so far...
create_class.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.eduardolaguna.mariela.app.activities.CreateClass">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/cc_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/cc_hint_class_name" />
<View
android:id="#+id/cc_divider1"
style="#style/Divider"
android:layout_below="#+id/cc_name" />
<TextView
android:id="#+id/tv_cc_professors_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/cc_divider1"
android:text="#string/cc_tv_professors_data" />
<EditText
android:id="#+id/cc_professors_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tv_cc_professors_data"
android:hint="#string/cc__hint_professors_name"
android:inputType="textPersonName|textAutoComplete|textAutoCorrect" />
<EditText
android:id="#+id/cc_professors_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/cc_professors_name"
android:hint="#string/cc_hint_professors_email"
android:inputType="textEmailAddress" />
<EditText
android:id="#+id/cc_professors_phonenumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/cc_professors_email"
android:hint="#string/cc_hint_professors_phonenumber"
android:inputType="phone" />
<View
android:id="#+id/cc_divider2"
style="#style/Divider"
android:layout_below="#id/cc_professors_phonenumber" />
<TextView
android:id="#+id/tv_cc_schedule"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/cc_divider2"
android:text="#string/cc_tv_class_schedule" />
<Spinner
android:id="#+id/sp_cc_day_of_the_week"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tv_cc_schedule"
android:entries="#array/dow" />
<EditText
android:id="#+id/cc_from_schedule"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#id/sp_cc_day_of_the_week"
android:hint="#string/cc_hint_from_schedule"
android:inputType="time" />
<EditText
android:id="#+id/cc_to_schedule"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_below="#id/sp_cc_day_of_the_week"
android:layout_toRightOf="#id/cc_from_schedule"
android:hint="#string/cc_hint_to_schedule"
android:inputType="time" />
<EditText
android:id="#+id/cc_floor_number"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_below="#id/sp_cc_day_of_the_week"
android:layout_toRightOf="#id/cc_to_schedule"
android:hint="#string/cc_hint_floor"
android:inputType="number" />
<EditText
android:id="#+id/cc_classroom"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_below="#id/sp_cc_day_of_the_week"
android:layout_toRightOf="#id/cc_floor_number"
android:hint="#string/cc_hint_classroom" />
<Button
android:id="#+id/btn_cc_add_schedule"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/cc_from_schedule"
android:text="#string/cc_add_schedule" />
<ListView
android:id="#+id/cc_schedule_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/btn_cc_add_schedule" />
</RelativeLayout>
</ScrollView>
</LinearLayout>
schedule_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/sch_day_of_the_week"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="MiƩrcoles"
android:textSize="60dp" />
<TextView
android:id="#+id/sch_from"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="5:45"
android:textSize="20dp" />
<TextView
android:id="#+id/sch_to"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="7:15"
android:textSize="20dp" />
<TextView
android:id="#+id/sch_floor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="7"
android:textSize="20dp" />
<TextView
android:id="#+id/sch_clasroom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="lab1"
android:textSize="20dp" />
</LinearLayout>
ScheduleAdapter.java
public class ScheduleAdapter extends ArrayAdapter<Actividad> {
private int layoutResourceId;
private LayoutInflater inflater;
private List<Actividad> shifts;
public ScheduleAdapter(Context context, int resource, List<Actividad> objects) {
super(context, resource, objects);
layoutResourceId = resource;
shifts = objects;
inflater = LayoutInflater.from(context);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
Holder holder = null;
row = inflater.inflate(layoutResourceId, null);
holder = new Holder();
holder.actividad = shifts.get(position);
holder.dow = (TextView) row.findViewById(R.id.sch_day_of_the_week);
holder.fromTXT = (TextView) row.findViewById(R.id.sch_from);
holder.toTXT = (TextView) row.findViewById(R.id.sch_to);
holder.floorTXT = (TextView) row.findViewById(R.id.sch_floor);
holder.roomTXT = (TextView) row.findViewById(R.id.sch_clasroom);
setupItem(holder);
row.setTag(holder);
return row;
}
private void setupItem(Holder holder) {
holder.dow.setText(holder.actividad.getDiaDeLaSemana());
holder.fromTXT.setText(holder.actividad.getDesdeStr());
holder.toTXT.setText(holder.actividad.getHastaStr());
holder.floorTXT.setText(holder.actividad.getPiso());
holder.roomTXT.setText(holder.actividad.getSalon());
}
public static class Holder {
Actividad actividad;
TextView dow;
TextView fromTXT;
TextView toTXT;
TextView floorTXT;
TextView roomTXT;
}
}
And finally the CreateClass.java
public class CreateClass extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_class);
ListView view = (ListView) findViewById(R.id.cc_schedule_list);
final ScheduleAdapter adapter = new ScheduleAdapter(getApplicationContext(), R.layout.schedule_item, new ArrayList<Actividad>());
view.setAdapter(adapter);
Button addSchedule = (Button) findViewById(R.id.btn_cc_add_schedule);
addSchedule.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Resources res = getResources();
ListView scheduleView = (ListView) findViewById(R.id.cc_schedule_list);
ScheduleAdapter adapter1 = (ScheduleAdapter) scheduleView.getAdapter();
Actividad act = new Actividad(TipoEvento.CLASE);
Spinner dow = (Spinner) findViewById(R.id.sp_cc_day_of_the_week);
act.setDiaDeLaSemana(dow.getSelectedItem().toString());
TextView from = (TextView) findViewById(R.id.cc_from_schedule);
act.setDesdeStr(res.getString(R.string.from) + ": " + from.getText().toString());
TextView to = (TextView) findViewById(R.id.cc_to_schedule);
act.setHastaStr(res.getString(R.string.to) + ": " + to.getText().toString());
TextView floor = (TextView) findViewById(R.id.cc_floor_number);
act.setPiso(res.getString(R.string.floor) + ": " + floor.getText().toString());
TextView classroom = (TextView) findViewById(R.id.cc_classroom);
act.setSalon(res.getString(R.string.classroom) + ": " + classroom.getText().toString());
adapter1.add(act);
}
});
}
}
You should instantiate your adapter with the Activity context, not the application context.
Storing a copy of the constructed list in shifts can be dangerous.
Don't store shifts in the Holder. Only views go in there.
When populating the convertView, reference the internal getItem(position) method to obtain the Actividad data...not your shifts variable.
You're also using the ViewHolder paradigm incorrectly. Example here.
Turns out the problem is the ListView is inside the ScrollView, this does not work properly when the list grows dynamically, I move it out the ScrollView and it works like a charm.
This was according to #Romain Guy a developer in the Android project, where he stated that
Using a ListView to make it not scroll is extremely expensive and goes against the whole purpose of ListView. You should NOT do this. Just use a LinearLayout instead.
So I use a LinearLayout to draw the new items on the layout.

Using Fit_XY in listview

holder.item3.setImageResource(custom.getcustomImage());
This is a line of code in my custom ListViewAdapter that holds an imageview. I need to get the images that i put in to shrink down to the size of my ImageView within the ListView.
I want to apply imageview.setImageResource(custom.getcustomImage()); to the item in my adapter but i get an error.
Cannot invoke setScaleType(ImageView.ScaleType) on the primitive type int
My code look like this
holder.item3.setImageResource(custom.getcustomImage().setScaleType(ScaleType.FIT_XY);
How am I suppose to use this FIT_XY, and is there another way I can fit an image the ImageView within my ListView?
My Adapter.
public class CustomAdapter extends ArrayAdapter<Custom> {
private ArrayList<Custom> entries;
private Activity activity;
public CustomAdapter(Activity a, int textViewResourceId,
ArrayList<Custom> entries) {
super(a, textViewResourceId, entries);
this.entries = entries;
this.activity = a;
// TODO Auto-generated constructor stub
}
public class ViewHolder {
public TextView item1;
public TextView item2;
public ImageView item3;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder;
if (v == null) {
LayoutInflater vi = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.rowlayout, null);
holder = new ViewHolder();
holder.item1 = (TextView) v.findViewById(R.id.secondLine);
holder.item2 = (TextView) v.findViewById(R.id.firstLine);
holder.item3 = (ImageView) v.findViewById(R.id.icon);
v.setTag(holder);
} else
holder = (ViewHolder) v.getTag();
final Custom custom = entries.get(position);
if (custom != null) {
holder.item1.setText(custom.getcustomBig());
holder.item2.setText(custom.getcustomSmall());
holder.item3.setImageResource(custom.getcustomImage());
holder.item3.setScaleType(ScaleType.FIT_XY);
}
return v;
}
}
}
Line Calling code
a = new Custom("String one","Stringtwo", R.drawable.image);
Row Layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:background="#FFFFFF"
android:padding="6dip" >
<!-- android:background="#color/Cream" -->
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:contentDescription="TODO"
android:src="#drawable/launchicon" />
<TextView
android:id="#+id/firstLine"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="#id/icon"
android:ellipsize="marquee"
android:singleLine="true"
android:text="Example application"
android:textColor="#000000"
android:textSize="12sp" />
<TextView
android:id="#+id/secondLine"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#id/secondLine"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:layout_toRightOf="#id/icon"
android:gravity="center_vertical"
android:text="Description"
android:textColor="#000000"
android:textSize="16sp" />
</RelativeLayout>
I see that item3 is an ImageView. You are trying to apply an image to that ImageView. Here, your getcustomImage() probably returns an imageResId.
If your getcustomImage() method returns an image res id(which is an integer value), use;
holder.item3.setImageResource(custom.getcustomImage());
to apply image to ImageView.
At the end there is no problem to use setScaleType on ImageView
holder.item3.setScaleType(ScaleType.FIT_XY);
Read more about setImageResource and setScaleType
Edit:
Shortly;
Change this;
holder.item3.setImageResource(custom.getcustomImage().setScaleType(ScaleType.FIT_XY);
With this:
holder.item3.setImageResource(custom.getcustomImage());
holder.item3.setScaleType(ScaleType.FIT_XY);
Edit:
With saying wrap_content to your ImageView at your rowlayout.xml you are eliminating scaleType option because your ImageView is going to resize itself to your image's size. You may want to give a static width to get a better visual on your list(all images width will be equal). And you want fitXY but consider using centerinside as an option(to a better view). And try to use scaleOption at your rowlayout so you can see its output at graphical layout.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:background="#FFFFFF"
android:padding="6dip" >
<!-- android:background="#color/Cream" -->
<ImageView
android:id="#+id/icon"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:contentDescription="TODO"
android:src="#drawable/launchicon"
android:scaleType="fitXY"/>
<TextView
android:id="#+id/firstLine"
android:layout_width="wrap_content"
android:layout_height="26dip"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="#id/icon"
android:ellipsize="marquee"
android:singleLine="true"
android:text="Example application"
android:textColor="#000000"
android:textSize="12sp" />
<TextView
android:id="#+id/secondLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/firstLine"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:layout_toRightOf="#id/icon"
android:gravity="center_vertical"
android:text="Description"
android:textColor="#000000"
android:textSize="16sp" />
</RelativeLayout>

Android Dev: Can't generate multiple layouts dynamically from an XML template

Making a simple program which will generate a multiple choice form. I have an sing_select.xml which acts as the template for making each question. Then, in code I wanted to populate my main.xml with a bunch of these templates customized. Though it works great for the first question, any subsequent questions do not get displayed. Not sure what I'm doing wrong. I know there isn't an overlap as I manually hid the first question.
Java File
public class FormFillerActivity extends Activity
{
private LinearLayout mQuestionList;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//Must come before setContentView or program crashes
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Must set before accessing layout elements or program crashes
setContentView(R.layout.main);
mQuestionList = (LinearLayout) findViewById(R.id.Body_Layout);
initForm();
}
private void initForm()
{
int count = 1;
ArrayList<String> answers = new ArrayList<String>();
answers.add("Single");
answers.add("Married");
answers.add("Separated");
answers.add("Divorced");
mQuestionList.addView(addSingSelectQuestion(count++, "What is your marital status?", answers));
answers.clear();
answers.add("Male");
answers.add("Female");
mQuestionList.addView(addSingSelectQuestion(count++, "What is your gender?", answers));
}
private View addSingSelectQuestion(int count, String question, ArrayList<String> answers)
{
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View container = inflater.inflate(R.layout.sing_select, null);
((TextView) container.findViewById(R.id.Sing_Select_Num)).setText(count + ") ");
((TextView) container.findViewById(R.id.Sing_Select_Text)).setText(question);
RadioGroup rg = (RadioGroup) container.findViewById(R.id.Sing_Select_Answer);
//Generate radio group answers
Iterator<String> it = answers.iterator();
while (it.hasNext())
{
RadioButton rb = new RadioButton(rg.getContext());
RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT);
String ans = it.next();
rb.setId(answers.indexOf(ans));
rb.setLayoutParams(params);
rb.setText(ans);
rb.setTextColor(getResources().getColor(R.color.black));
rb.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.txt_normal));
rg.addView(rb);
}
return container;
}
}
main.xml (stripped out unrelated UI elements)
<?xml version="1.0" encoding="utf-8"?>
...
<ScrollView
android:id="#+id/Body_Scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/Footer"
android:layout_below="#id/Title"
android:scrollbars="vertical" >
<LinearLayout
android:id="#+id/Body_Layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/marg_normal"
android:padding="#dimen/pad_large" >
</LinearLayout>
</ScrollView>
...
sing_select.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/Sing_Select_Layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:padding="8dp" >
<TextView
android:id="#+id/Sing_Select_Num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#) "
android:textColor="#color/black"
android:textSize="#dimen/txt_normal" />
<TextView
android:id="#+id/Sing_Select_Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/Sing_Select_Num"
android:layout_toRightOf="#+id/Sing_Select_Num"
android:text="The Question?"
android:textColor="#color/black"
android:textSize="#dimen/txt_normal" />
<RadioGroup
android:id="#+id/Sing_Select_Answer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Sing_Select_Text"
android:layout_below="#+id/Sing_Select_Text"
android:layout_toLeftOf="#+id/Sing_Select_Trans_Button" >
</RadioGroup>
<Button
android:id="#+id/Sing_Select_Trans_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="#drawable/btn_big"
android:padding="8dp"
android:text="Accept"
android:textSize="#dimen/txt_button" />
</RelativeLayout>
Try to set orientation to vertical for the *Body_Layout* LinearLayout. I think your pushing out of the screen the next rows after the first one(the width for the inflated view is set to fill the parent).

Categories

Resources