K
K
KingOfNothing2013-11-25 06:17:59
Java
KingOfNothing, 2013-11-25 06:17:59

Why is my component based on LinearLayout shown only the first time (although called 2 times)?

There is a component that extends SeekBar by adding some TextView to it. In the layout I need to place two components:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res/com.alexrudenko.general"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    
    <com.alexrudenko.general.ui.InfoSeekBar
        android:id="@+id/seekBarMapSize"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        custom:maxText="100"
        custom:minText="50"
        custom:titleText="@string/map_size_description" />
    
     <com.alexrudenko.general.ui.InfoSeekBar
        android:id="@+id/seekBarNumberOfCountries"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        custom:maxText="20"
        custom:minText="2"
        custom:titleText="@string/number_of_countries_description" />
    
</LinearLayout>

My com.alexrudenko.general.ui.InfoSeekBar component looks like this:
package com.alexrudenko.general.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.alexrudenko.general.R;

public class InfoSeekBar extends LinearLayout {

  // contols
  public TextView title;
  public TextView min;
  public TextView max;
  public TextView result;
  public SeekBar seekBar;
  // data
  public String titleText;
  public Integer minText;
  public Integer maxText;

  public InfoSeekBar(Context context, AttributeSet attrs) {
    super(context, attrs);
    initView(context, attrs);
  }

  private void initView(Context context, AttributeSet attrs) {
    TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
        R.styleable.InfoSeekBar, 0, 0);
    try {
      titleText = a.getString(R.styleable.InfoSeekBar_titleText);
      minText = Integer.valueOf(a.getString(R.styleable.InfoSeekBar_minText));
      maxText = Integer.valueOf(a.getString(R.styleable.InfoSeekBar_maxText));
    } finally {
      a.recycle();
    }
    /*View v = LayoutInflater.from(context).inflate(R.layout.seek_bar, this);
    addView(v);*/
    
    LayoutInflater.from(context).inflate(R.layout.seek_bar, this);
  }

  public InfoSeekBar(Context context) {
    super(context);
    LayoutInflater.from(context).inflate(R.layout.seek_bar, this);
  }

  public InfoSeekBar(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    initView(context, attrs);
  }

  @Override
  protected void onFinishInflate() {
    super.onFinishInflate();
    title = (TextView) this.findViewById(R.id.seekBarTextView);
    title.setText(titleText);
    min = (TextView) this.findViewById(R.id.minText);
    min.setText(String.valueOf(minText));
    max = (TextView) this.findViewById(R.id.maxText);
    max.setText(String.valueOf(maxText));
    result = (TextView) this.findViewById(R.id.resultText);
    result.setText(String.valueOf(minText));
    seekBar = (SeekBar) this.findViewById(R.id.seekBar);
    OnSeekBarChangeListener l = new OnSeekBarChangeListener() {
      
      @Override
      public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub
        
      }
      
      @Override
      public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub
        
      }
      
      @Override
      public void onProgressChanged(SeekBar seekBar, int progress,
          boolean fromUser) {
        float level = (float) progress / (float) seekBar.getMax();
        int position = (int) ((maxText - minText)*level + minText);
        result.setText(String.valueOf(position));
      }
    };
    seekBar.setMax(100);
    seekBar.setProgress(10);
    seekBar.setOnSeekBarChangeListener(l);
  }

}

and component layout:
<merge xmlns:android="http://schemas.android.com/apk/res/android" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/seekBarTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text=""
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <SeekBar
            android:id="@+id/seekBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/seekBarTextView" />

        <TextView
            android:id="@+id/minText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/seekBar"
            android:layout_marginLeft="10dp"
            android:text="" />
        
        <TextView
            android:id="@+id/resultText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/seekBar"
         	android:layout_centerHorizontal="true"
            android:text="" />

        <TextView
            android:id="@+id/maxText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/seekBar"
            android:layout_marginRight="10dp"
            android:text="" />
    </RelativeLayout>

</merge>

As a result, only the android:id="@+id/seekBarMapSize" component is displayed. What is the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bimeg, 2013-11-25
@KingOfNothing

Maybe the first one occupies all the space?

android:layout_width="match_parent"
android:layout_height="match_parent"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question