Mayur Saini
Mayur Saini

Reputation: 229

how to set layout_weight of the textview in a fragment in LinearLayout

I am making a program on ListView + Checkbox in a fragment, but the presentation is creating problem... Actually i have set layout_weight of the textview and checkbox but it is not working, so i want to set it programatically, And the checkbox+textview that i am refering are not in the current layout,actually i am inflating it in the current layout. And i didnt find any of the previous example, implementing in a fragment... please have a look at my code and please help me

Here is my code:

fragment_packages.XML:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/activity_horizontal_margin"
android:orientation="vertical">
<ListView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:paddingBottom="30dp"
android:id="@+id/packages_list"/>
<LinearLayout
android:layout_weight="0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="start|end"
android:layout_gravity="bottom"
android:orientation="horizontal"
android:paddingBottom="8dp"
>
<TextView
android:id="@+id/tv_total"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="9"
android:text="@string/totalcost"
android:textSize="20sp"/>
<TextView
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/price"
android:textSize="20sp"
android:layout_gravity="end"
/>
</LinearLayout>
</LinearLayout>

packages_items.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:layout_alignParentTop="true"
    android:orientation="horizontal"
    android:id="@+id/linearlayout1"
    >

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:id="@+id/checkbox"
    android:layout_weight="6"
        />
<TextView
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:id="@+id/items"
    android:textColor="@color/colorPrimary"
    android:textSize="25sp"
    />
</LinearLayout>

Fragment:

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;


public class PackagesFragment extends Fragment
{
MyCustomAdpater adpater=null;
ArrayList<PackagesItems> packagesList=null;
ListView listView;
TextView priceView;
private float totalPrice=0f;

private class ViewHolder
{
    TextView textView;
    CheckBox checkBox;
}



@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    final ViewHolder holder=new ViewHolder();
    View rootView;


    rootView = inflater.inflate(R.layout.fragment_packages,container,false);
    listView=(ListView)rootView.findViewById(R.id.packages_list);
    priceView=(TextView)rootView.findViewById(R.id.price);


    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

           PackagesItems packagesItems = (PackagesItems) parent.getItemAtPosition(position);

holder.checkBox = (CheckBox)view.findViewById(R.id.checkbox);

if(holder.checkBox.isChecked())
{
holder.checkBox.setChecked(false);
packagesItems.setSelected(!holder.checkBox.isChecked());
                subtractPrice(packagesList.get(position));
            }
            else {
                holder.checkBox.setChecked(true);
                packagesItems.setSelected(holder.checkBox.isChecked());
                addPrice(packagesList.get(position));
            }
            }
    });
    displayListView();

    return rootView;
}



public void displayListView()
{

    packagesList = new ArrayList<PackagesItems>();
    PackagesItems packagesItems = new PackagesItems("Hair Cut",150.00f,false);
    packagesList.add(packagesItems);
    packagesItems = new PackagesItems("Hair Spa",300.00f,false);
    packagesList.add(packagesItems);
    packagesItems = new PackagesItems("Facial",300.00f,false);
    packagesList.add(packagesItems);
    packagesItems = new PackagesItems("Make Up",300.00f,false);
    packagesList.add(packagesItems);

    adpater = new MyCustomAdpater(getActivity(),R.layout.packages_items,packagesList);

    listView.setAdapter(adpater);

}

private class MyCustomAdpater extends ArrayAdapter<PackagesItems>
{

    private ArrayList<PackagesItems> packagesList;

    public MyCustomAdpater(Context context, int textViewResourceId, ArrayList<PackagesItems> packagesList)
    {

        super(context,textViewResourceId,packagesList);

        this.packagesList = packagesList;


    }




    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;

        if(convertView==null)
        {
            LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.packages_items,null);
            holder = new ViewHolder();
            holder.textView = (TextView)convertView.findViewById(R.id.items);
            holder.checkBox=(CheckBox)convertView.findViewById(R.id.checkbox);
            holder.checkBox.setTextSize(25);
            holder.checkBox.setTextColor(getActivity().getResources().getColor(R.color.colorPrimary));

            convertView.setTag(holder);


            holder.checkBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    CheckBox cb= (CheckBox)v;
                    PackagesItems packagesItems = (PackagesItems)cb.getTag();
                    ;
                    if(cb.isChecked())
                    {
                        addPrice(packagesList.get(position));
                        packagesItems.setSelected(cb.isChecked());
                    }
                    else
                    {
                        subtractPrice(packagesList.get(position));
                        packagesItems.setSelected(!cb.isChecked());
                    }

                }
            });
        }
        else
        {
            holder=(ViewHolder)convertView.getTag();
        }
        PackagesItems packagesItems = packagesList.get(position);
        holder.textView.setText(String.valueOf(packagesItems.getPrice()));
        holder.checkBox.setText(packagesItems.getItem());
        holder.checkBox.setChecked(packagesItems.isSelected());
        holder.checkBox.setTag(packagesItems);
        return convertView;
    }


}

public void addPrice(PackagesItems packagesItems)
{float currentItemPrice;
    currentItemPrice= packagesItems.getPrice();

    totalPrice=totalPrice+currentItemPrice;

    priceView.setText(String.valueOf(totalPrice));
}
public void subtractPrice(PackagesItems packagesItems)
{float currentItemPrice;
    currentItemPrice= packagesItems.getPrice();

    totalPrice=totalPrice-currentItemPrice;

    priceView.setText(String.valueOf(totalPrice));
}

public void setView()
{

    //I WANT TO DO THE STUFF HERE, IN THIS METHOD - setView() !!!
}

}

Upvotes: 0

Views: 968

Answers (3)

Shubham Jain
Shubham Jain

Reputation: 2383

change your packages_items.XML: file with

<?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:weightSum="10"
    android:layout_alignParentTop="true"
    android:orientation="horizontal"
    android:id="@+id/linearlayout1"
    >

<CheckBox
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:id="@+id/checkbox"
    android:layout_weight="2"
        />
<TextView
    android:layout_weight="8"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:id="@+id/items"
    android:textColor="@color/colorPrimary"
    android:textSize="25sp"
    />
</LinearLayout>

here in you can find the difference that in checkbox you define the width wrap content but when be use layout weight then we have to define height/width one of them 0dp

as per our last comment you can do one thing change our xml and

<?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:layout_alignParentTop="true"
        android:orientation="horizontal"
        android:id="@+id/linearlayout1"
        >

  <CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/checkbox"

        />
    </LinearLayout>

and in your adpater get the reference of checkbox and use checkbox..setText("your text"); i hope it work if it dont so please post the view in the form of image what you exactly looking for

Upvotes: 0

Parveen Prajapati
Parveen Prajapati

Reputation: 83

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:weightSum="5"
    android:gravity="center"
    android:orientation="vertical"
    android:layout_height="match_parent">


    <TextView
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:layout_height="0dp"
        android:text="Hello"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:layout_height="0dp"
        android:text="Hello"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:layout_height="0dp"
        android:text="Hello"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:layout_height="0dp"
        android:text="Hello"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:layout_height="0dp"
        android:text="Hello"/>

</LinearLayout>

Upvotes: 1

saibbyweb
saibbyweb

Reputation: 3254

How will android:weight work without android:weightSum ?

Example of how android:weightSum works :

You have a LinearLayout with horizontal orientation and you have 3 ImageViews inside it and you want these ImageViews always to take equal space.

    <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:weightSum="3"
    android:orientation="horizontal"
    android:layout_gravity="center">

You can set the layout_weight of each ImageView to 1 and the weightSum in LinearLayout to 3 to achieve this.

    <ImageView
    android:layout_height="wrap_content"       
    android:layout_weight="1"
    android:layout_width="0dp"/>

The good point is that this will work correctly for any device, which will not happen if you set width and height directly.

Source

Upvotes: 0

Related Questions