ikbal
ikbal

Reputation: 1894

Aligning objects in custom LinearLayout?

I have custom coded layout to hold all the childs. Here is the code:

public abstract class ItemView extends LinearLayout{
    private ImageView icon;//cell icon
    public static final int iconID=12345;
    private Button accessorButton;//this will not be button ?
    public static final int accessorID=54321;



    public ItemView(Context context) {
        super(context);
    }

    public  void arrange(){

    }
    public  void setView(Context context){  
        int l=super.getLeft();
        int r=super.getRight();
        int t=super.getTop();
        int b=super.getBottom();
        icon=new ImageView(context);
        icon.setImageResource(R.drawable.star);
        addView(icon);
        accessorButton=new Button(context);
        accessorButton.setText("accessor");
        addView(accessorButton);
       // icon.layout(l+5, t+5, l+100, b-5);
        //accessorButton.layout(r-100, t+5, r-10, b-5);


    }


    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        // TODO Auto-generated method stub
        super.onLayout(changed, l, t, r, b);


    }


}

and I have child layouts, one of them is:

public class ItemPlainView extends ItemView{

    private TextView text;

    public ItemPlainView(Context context) {
        super(context);
        setView(context);
    }
    public void setView(Context context){
      super.setView(context);
       text=new TextView(context);
       text.setText("plain view text");
       addView(text);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        // TODO Auto-generated method stub
    super.onLayout(changed, l, t, r, b);


    }
    public TextView getText() {
        return text;
    }
    public void setText(TextView text) {
        this.text = text;
    }

    @Override
    public void arrange() {
        // TODO Auto-generated method stub

    }
}

When I run the code it naturally gives me a star icon, a button and after that a TextView. I need to replace and resize my objects. For this example I need to put star icon at the most left TextView in the middle and my button on the most right side. Considering this hierarchical view, how can I do that?

Thanks

Upvotes: 0

Views: 821

Answers (2)

dylan murphy
dylan murphy

Reputation: 1370

@dymmeh is exactly right, every thing you are doing could be in your main.xml as follows:

@Steve i added another TextView as suggested, you would want to choose either the TextView before the OR or the TextView & ImageView after the OR

<?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:orientation="horizontal">

    <TextView
        android:drawableLeft="@drawable/star"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="plain view text"
        android:weight="1"
        />

    <<--OR-->>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableLeft="@drawable/star"
        android:weight="1"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="plain view text"
        android:weight="1"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="accessor"
        android:weight="1"
        />     
</LinearLayout>

then your java class would just have to call setContentView(R.layout.main); in your onCreate after super.onCreate();

much much easier than what you had. you can easily arrange any of those items any way you want also. see LinearLayout you could also use TableLayout but i think those are too complicated for most situations.

Upvotes: 1

Steve Prentice
Steve Prentice

Reputation: 23514

This is much easier using the XML layouts. I would suggest just using that. However, if you really need to do it in code, you can set the middle TextView to have a layout parameter with a layout_weight = 1.

Upvotes: 0

Related Questions