How use ListView in android Tab? - android

Good day.
In my app I have three tab (one Activity extend TabActivity and others activitys provides access to content). In first tab I have ImageView, a few TextView and it is works. But when I add ListView and in activity that contain ListView I add a few rows it was not show in may tab.
Can someone tell me where I was wrong? Here my code:
In StartActivity:
intent = new Intent().setClass(this, GoodsAndShopsActivity.class);
spec = tabHost.newTabSpec("shops").setIndicator("Shops",
res.getDrawable(R.drawable.ic_tab_shops))
.setContent(intent);
tabHost.addTab(spec);
In GoodsAndShopsActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.descriptions);
m_shopsLayout = (ListView) findViewById(R.id.shops);
m_shopList = new ArrayList<Shop>();
m_shopAdapter = new ShopAdapter(m_shopList, this);
m_shopsLayout.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
m_shopsLayout.setAdapter(m_shopAdapter);
for (int i = 0; i<3; i++) {
m_shopList.add(new Shop("new description"));
m_shopAdapter.notifyDataSetChanged();
}
}
In class that extends BaseAdapter:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = m_inflater.inflate(R.layout.shop, null);
holder = new ViewHolder();
holder.descriptions = (TextView) convertView.findViewById(R.id.shop);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
String textOnView = m_shops.get(position).getDescription();
holder.descriptions.setText(textOnView);
return convertView;
}
static class ViewHolder{
TextView descriptions;
}
And my xml where define ListView (Sorry that so much):
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/full_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10px"
android:src="#drawable/icon">
</ImageView>
<LinearLayout
android:id="#+id/short_info"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/icon"
android:layout_alignParentRight="true">
<TextView
android:id="#+id/name_for_good"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="Наименование товара">
</TextView>
<TextView
android:id="#+id/best_price"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="marquee"
android:text="Лучшая цена: ">
</TextView>
<TextView
android:id="#+id/worst_price"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:text="Худшая цена: ">
</TextView>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/description_and_shop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/full_info">
<TextView
android:id="#+id/description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Большое подробное описание товара с всякими деталями, нюансами и т.п.">
</TextView>
<ScrollView
android:id="#+id/ScrollView"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="#+id/shops"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
</ScrollView>
</LinearLayout>
UPDATE: After coffe brake I was find mistake: in my xml I add in last LinearLayout "orientation=vertical" and my rows appeared.
SOLVED

Also, remember that the ListView is already scrollable, so you shouldn't need to put it under ScrollView.
Im glad you solved your problem youself :)

Related

Cant change View background colour programmatically

Im an android newbie so please forgive me...
This is my code:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.adapter_who_row, null);
viewHolder.nameTextView = (TextView) convertView.findViewById(R.id.text);
viewHolder.companyTextView = (TextView) convertView.findViewById(R.id.whoRowCompany);
viewHolder.dotView = convertView.findViewById(R.id.whoRowDot);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
String textToShow = dataArray.get(position);
viewHolder.nameTextView.setText(textToShow);
viewHolder.dotView.setBackgroundColor(getResources().getColor(R.color.red));
viewHolder.companyTextView.setText("Test");
return convertView;
}
class ViewHolder {
TextView nameTextView;
TextView companyTextView;
View dotView;
}
dotView doesn't change its colour...
However, the two textVies does change their text.
What could it be?
Edit:
The xml is holding two editTexts and one View.
Below is my xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_centerVertical="true">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.85"
android:layout_gravity="center"
android:gravity="center">
<View
android:id="#+id/whoRowDot"
android:layout_width="40px"
android:layout_height="40px"
android:backgroundTint="#color/noteColor">
</View>
<!--android:background="#drawable/round_button"-->
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:background="#color/clear"
android:layout_weight="0.15">
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="0dp"
android:text="User Name"
android:fontFamily="Roboto-Light"
android:background="#color/clear"/>
<TextView
android:id="#+id/whoRowCompany"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="15dp"
android:text="Company"
android:textColor="#color/lightGrey"
android:background="#color/clear"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Thanks
remove this line from your xml view and try:
android:backgroundTint="#color/noteColor"

Custom ListView Item barely visible

I'm having a problem that all my ListViews Items are showing like this:
As you guys can see Hello World! is a TextView and is showing perfectly.
But the ListView itens are showing like they are disabled.
This is my Activity's onCreate method:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_open_answers);
ctx = getApplicationContext();
Intent intent = getIntent();
usersurvey_id = intent.getIntExtra("usersurvey_id", -1);
list = (ListView) findViewById(R.id.listview_answers);
getAnswers();
AnswersArrayAdapter adapter = new AnswersArrayAdapter(ctx, answersList);
list.setAdapter(adapter);
list.setClickable(false);
list.setEnabled(true);
}
This is my Custom Adapter:
#Override
public View getView(int position, View convertView, ViewGroup parent){
Answers answerItem = answersList.get(position);
ViewHolder holder;
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.listitem_answers, null);
holder = new ViewHolder();
holder.question = (TextView) convertView.findViewById(R.id.tv_texto_pergunta);
holder.answer = (TextView) convertView.findViewById(R.id.tv_texto_resposta);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.question.setText(getQuestionById(answerItem.getAnswer_question_id()));
holder.answer.setText(answerItem.getAnswer_answer());
return convertView;
}
Activity layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.seven.questionario.OpenAnswers">
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:id="#+id/listview_answers"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
And ListView Item Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tv_pergunta"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Pergunta:"
android:background="#D0D0D0"/>
<TextView
android:id="#+id/tv_texto_pergunta"
android:layout_width="fill_parent"
android:layout_height="60sp"
android:layout_below="#id/tv_pergunta"
android:text="Texto da pergunta"
android:background="#D0D0D0"/>
<View
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tv_texto_pergunta"
android:background="#drawable/dotted"
/>
<TextView
android:id="#+id/tv_resposta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tv_texto_pergunta"
android:text="Resposta:"
android:textStyle="bold"/>
<TextView
android:id="#+id/tv_texto_resposta"
android:layout_width="fill_parent"
android:layout_height="80sp"
android:layout_below="#id/tv_resposta"
android:text="Texto da resposta" />
<View
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/back"
android:layout_below="#id/tv_texto_resposta"
/>
I now this is a bit old but i just stumbled across this post.
Could it be that you're using the wrong context? I think you should pass the current context of your Activity, like so:
ctx = getContext();
or like this:
AnswersArrayAdapter adapter = new AnswersArrayAdapter(this, answersList);
try this.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#D0D0D0">
<TextView
android:id="#+id/tv_pergunta"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Pergunta:"/>
<TextView
android:id="#+id/tv_texto_pergunta"
android:layout_width="fill_parent"
android:layout_height="60sp"
android:layout_below="#id/tv_pergunta"
android:text="Texto da pergunta"/>
<View
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tv_texto_pergunta"
android:background="#drawable/dotted"
/>
<TextView
android:id="#+id/tv_resposta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tv_texto_pergunta"
android:text="Resposta:"
android:textStyle="bold"/>
<TextView
android:id="#+id/tv_texto_resposta"
android:layout_width="fill_parent"
android:layout_height="80sp"
android:layout_below="#id/tv_resposta"
android:text="Texto da resposta" />
<View
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/back"
android:layout_below="#id/tv_texto_resposta"
/>
Show your theme(application/activity) selected in AndroidManifest.xml. I guess it's the problem.
I followed #Aun tip and solved my problem.
I just had to set manually all my app's colors.
try this line
android:textColor="#000000" in your TextView

Some parts of TextViews don't show and are cut off by the edge of the screen

I have a query results from a database and corresponding variables are filled.
I think that the problem is the layout, I am using incorrect layouts in my XML file. Maybe it is the ArrayAdapter, I don't know. I've spent a lot of hours trying to solve this problem and haven't been able to. Thanks for your help!
This is my XML:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow>
<TextView android:id="#+id/precio_offers_charger"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textStyle="bold"
android:textSize="20px"
android:textColor="#FFFFFF"
android:background="#FF0000" />
<TextView android:id="#+id/cabecera_offers_charger"
android:paddingLeft="10px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18px"
android:textColor="#FFFFFF"
android:background="#010201" />
</TableRow>
<TableRow>
<ImageView android:id="#+id/imagen"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:id="#+id/descripcion_offers_charger"
android:paddingLeft="10px"
android:paddingTop="10px"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textStyle="normal"
android:textSize="14px" />
</TableRow>
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</TableLayout>
This is a part of my class:
public class OffersChargerActivity extends ListActivity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.offers_charger);
...
public class AdaptadorTitulares extends ArrayAdapter<Oferta> {
Activity context;
AdaptadorTitulares(Activity context) {
super(context, R.layout.offers_charger, ofertas);
this.context = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = linflater.inflate(R.layout.offers_charger, null);
}
// poblamos la lista de elementos
TextView precio = (TextView)view.findViewById(R.id.precio_offers_charger);
precio.setText(ofertas.get(position).getPrecio().concat("€"));
ImageView imagen = (ImageView) view.findViewById(R.id.imagen);
imagen.setImageBitmap(new GetImages().downloadFile(ofertas.get(position).getImagen()));
TextView cabecera = (TextView)view.findViewById(R.id.cabecera_offers_charger);
cabecera.setText(ofertas.get(position).getCabecera());
TextView descripcion = (TextView)view.findViewById(R.id.descripcion_offers_charger);
descripcion.setText(ofertas.get(position).getDescripcion());
return view;
}
}
...
}
This worked for me:
<TextView
android:id="#+id/nameText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:textColor="#android:color/black" />
Try adding in 'layout_weight' and change your 'layout_width' to "fill_parent".
Hope this helps!
don't wrap content, use fill_parent for width, and also set android:ellipsize="true"

problem with custom ListView showing 3 vertical TextViews

im trying to display 3 TextViews as a custom row design in a ListView. Unfortunatly, only 2 TextViews ( row_title and row_postcode_city) are visible...
Maybe somebody has an idea?
Regards,
float
Code of my ArrayAdapter:
private class PartnerAdapter extends ArrayAdapter<Partner> {
private ArrayList<Partner> items;
public PartnerAdapter(Context context, int textViewResourceId, ArrayList<Partner> items) {
super(context, textViewResourceId, items);
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.partner_suche_ergebnis_row, parent, false);
}
Partner o = items.get(position);
if (o != null) {
TextView title = (TextView) v.findViewById(R.id.row_title);
TextView subtitle = (TextView) v.findViewById(R.id.row_subtitle);
TextView postcode_city = (TextView) v.findViewById(R.id.row_postcode_city);
if (title != null) {
title.setText(o.getTitle());
}
if(subtitle != null){
subtitle.setText(o.getSubtitle());
}
if(postcode_city != null){
subtitle.setText(o.getPostcode() + " " + o.getCity());
}
}
return v;
}
}
XML of the custom row design:
<?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="?android:attr/listPreferredItemHeight"
android:padding="0dip"><!--
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="6dip"
android:src="#drawable/icon" />
--><LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:id="#+id/row_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="bold"/>
<TextView
android:id="#+id/row_subtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/row_postcode_city"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
Try to use the following xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="0dip">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:id="#+id/row_title"
android:text="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="bold"/>
<TextView
android:id="#+id/row_subtitle"
android:text="2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:text="3"
android:id="#+id/row_postcode_city"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
Check it now.
Change your LinearLayout to "wrap_content" on layout_height
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content">
Also, I don't know why you have a LinearLayout inside a LinearLayout. Since this xml is only for individual rows. Each row will be a horizontal LinearLayout, the second one being unnecessary as ListView automatically lists rows vertically.
Finally, the root LinearLayout should use minHeight for the default Item Height, not layout_height.
android:minHeight="?android:attr/listPreferredItemHeight"
This should be your xml file (unless you really need the second LinearLayout for some odd reason)
<?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="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:padding="0dip">
<TextView
android:id="#+id/row_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="bold"/>
<TextView
android:id="#+id/row_subtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/row_postcode_city"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Sorry for offtopic, but I here some useful tips from me
You have a LOT of needless (and flat out wrong) stuff going on here:
Partner o = items.get(position);
if (o != null) {
You should get something for every position, if item retuns null that means you are doing something wrong. In your case, if you get null you just return reused old, or brand new row, which will confuse the user by displaying the old data, or no data at all.
if (title != null) {
You have 2 ways of getting View object of your row, by inflating, or getting it from recycler (convertView). And recycler will ALLWAYS return the right type of View, so those check are useless since your components will always be found.

How to make a view appear below

Hey I want to make a view appear below the last that has been created.
It is showing like this:
I want to make the next view show below the last one, so for each new view added, it shows below. Understand?
Here is my code. The xml file and the java file.
listitem.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:id="#+id/linearId"
android:padding="6dip"
>
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="6dip"
android:src="#drawable/icon"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip" android:layout_weight="1"
android:layout_height="fill_parent"
>
<TextView
android:id="#+id/txt1"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:textSize="20sp"
android:gravity="center_vertical"
android:text="My Application"
/>
<TextView
android:id="#+id/txt2"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="marquee"
android:text="Simple application that shows how to use RelativeLayout"
android:textSize="10sp"
/>
</LinearLayout>
</LinearLayout>
RatedCalls.java
public class RatedCalls extends Activity {
private static final String LOG_TAG = "RatedCalls";
private TableLayout table;
private CallDataHelper cdh;
private TableRow row;
private TableRow row2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listitem);
Log.i(LOG_TAG, "calling from onCreate()");
cdh = new CallDataHelper(this);
startService(new Intent(this, RatedCallsService.class));
Log.i(LOG_TAG, "Service called.");
Log.i(LOG_TAG, "before call fillList");
List<String> ratedCalls = new ArrayList<String>();
ratedCalls = this.cdh.selectTopCalls();
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout ll = (LinearLayout) this.findViewById(R.id.linearId);
for (int i = 0; i < 1; i++) {
View item = inflater.inflate(R.layout.listitem, null);
TextView x = (TextView) item.findViewById(R.id.txt1);
x.setText(ratedCalls.get(0));
TextView ht = (TextView) item.findViewById(R.id.txt2);
ht.setText(ratedCalls.get(1));
ll.addView(item, ViewGroup.LayoutParams.WRAP_CONTENT);
}
}
Your problem is that you should be using listitem.xml from a list adapter. What you're doing is, first setting your content view to an instance of listitem.xml, then trying to insert an instance of listitem INTO listitem's first LinearLayout. You should use listitem.xml as the view that you inflate in your custom adapter's getView() method. See the QuoteAdapter class from this question for an example, or just Google search "custom ArrayAdapter Android" for tons of results on this topic.
To work so you need to create two different xml for each inflater
In your layout folder,new xml layout/main2.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">
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="6dip"
android:src="#drawable/icon"
/>
<TextView
android:id="#+id/txt1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20sp"
android:gravity="center_vertical"
android:text="My Application"
/>
<TextView
android:id="#+id/txt2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="marquee"
android:text="Simple application that shows how to use RelativeLayout"
android:textSize="10sp"
/>
</LinearLayout>
Then your layout/main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/linearId"
android:orientation="vertical"
>
<TextView android:id="#+id/rowCount" android:layout_width="wrap_content"
android:layout_height="wrap_content" ></TextView>
<LinearLayout
android:id="#+id/linearId2"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
</LinearLayout>
</LinearLayout>
Then you can set you activity like this
private int ELEMENTINEACHROW=2 ;
private int NOOFROW = 4;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout ll = (LinearLayout) this.findViewById(R.id.linearId);
for (int j = 0; j < NOOFROW; j++) {
View itemMain = inflater.inflate(R.layout.main, null, false);
TextView rowCount = (TextView) itemMain.findViewById(R.id.rowCount);
rowCount.setText("Row:"+(j+1));
LinearLayout ll2 = (LinearLayout) itemMain.findViewById(R.id.linearId2);
for (int i = 0; i < ELEMENTINEACHROW; i++) {
View item = inflater.inflate(R.layout.main2, null, false);
TextView x = (TextView) item.findViewById(R.id.txt1);
x.setText("test");
TextView ht = (TextView) item.findViewById(R.id.txt2);
ht.setText("good");
ll2.addView(item, ViewGroup.LayoutParams.FILL_PARENT);
}
ll.addView(itemMain, ViewGroup.LayoutParams.FILL_PARENT);
}
}
Hope it give you the idea.
mmm...I did not understand fully what is required, but may be you need to add android:orientation="vertical" to LinearLayout with id="linearId".
In your xml file, set the android:orientation attribute to vertical on your <LinearLayout/>:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:id="#+id/linearId"
android:padding="6dip"
android:orientation="vertical"/>
Check out the LinearLayout class documentation and the orientation documentation for more information.

Categories

Resources