-1

I have a base activity and main activity but in main activity I have applied weightsum but its not working properly. Below is the expected and current output enter image description here

MainActivity.java

public class MainActivity extends BaseActivity {
    LinearLayout dynamicContent,bottonNavBar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dynamicContent = (LinearLayout)  findViewById(R.id.dynamicContent);
        bottonNavBar= (LinearLayout) findViewById(R.id.bottonNavBar);
        View wizard = getLayoutInflater().inflate(R.layout.activity_main, null);
        dynamicContent.addView(wizard);
}
}

Mainactivity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:weightSum="2"
    android:layout_height="match_parent"
    android:background="#F5F5F5"
    tools:context="com.creativeframe.arun.pro.MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/cbseschool"
        android:orientation="vertical">

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/college"
        android:orientation="vertical">

        </LinearLayout>

</LinearLayout>

BaseActivity.java

public class BaseActivity extends AppCompatActivity {


    RadioGroup radioGroup1;
    RadioButton home,deals,account,settings;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);
        home = (RadioButton)findViewById(R.id.homebtn);
        deals = (RadioButton)findViewById(R.id.dealsbtn);
        account = (RadioButton)findViewById(R.id.accountbtn);
        settings = (RadioButton)findViewById(R.id.settingbtn);

        home.setCompoundDrawablesWithIntrinsicBounds( 0,R.mipmap.ic_home_white_24dp, 0,0);
       deals.setCompoundDrawablesWithIntrinsicBounds( 0,R.mipmap.ic_navigation_white_24dp, 0,0);
       account.setCompoundDrawablesWithIntrinsicBounds( 0,R.mipmap.ic_about, 0,0);
       settings.setCompoundDrawablesWithIntrinsicBounds( 0,R.mipmap.ic_call_white_24dp, 0,0);

        radioGroup1=(RadioGroup)findViewById(R.id.radioGroup1);
        radioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId)
            {
                switch (checkedId)
                {
                    case R.id.homebtn:
                        home.setTextColor(Color.parseColor("#FF4081"));
                        startActivity(new Intent(getBaseContext(),MainActivity.class));
                        finish();
                        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                        break;
                    case R.id.dealsbtn:
                        deals.setTextColor(Color.parseColor("#FF4081"));
                        startActivity(new Intent(getBaseContext(), location.class));
                        finish();
                        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);

                        break;
                    case R.id.settingbtn:
                        settings.setTextColor(Color.parseColor("#FF4081"));
                        startActivity(new Intent(getBaseContext(), contact.class));
                        finish();
                        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                        break;
                    case R.id.accountbtn:
                        account.setTextColor(Color.parseColor("#FF4081"));
                        startActivity(new Intent(getBaseContext(), about.class));
                        finish();
                        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                        break;
                    default:
                        break;
                }
            }
        });
    }
}
codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • `weightSum` is **totally optional**. Moreover the weighted size (only one! choose between width OR height) must be **exactly 0dp**. Furthermore, nesting layouts affects performances. **Negatively**. Someone didn't do their homework, huh? ;) – Phantômaxx Sep 05 '17 at 15:50
  • 1
    Change the height of each child view to 0dp – codeMagic Sep 05 '17 at 15:52
  • Can you post the layout you inflate in `BaseActivity`? I think the problem is there as your other xml looks correct. – codeMagic Sep 05 '17 at 16:37

2 Answers2

1

Try the following in your Mainactivity.xml

change your child LinearLayout layout_height from match_parent to 0dp.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:weightSum="2"
android:layout_height="match_parent"
android:background="#F5F5F5"
tools:context="com.creativeframe.arun.pro.MainActivity">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="@drawable/cbseschool"
    android:orientation="vertical">

</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="@drawable/college"
    android:orientation="vertical">

    </LinearLayout>

  </LinearLayout>

In MainActivity set your Layout View as below.

public class MainActivity extends BaseActivity {
LinearLayout dynamicContent,bottonNavBar;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    dynamicContent = (LinearLayout)  findViewById(R.id.dynamicContent);
    bottonNavBar= (LinearLayout) findViewById(R.id.bottonNavBar);

}
}
Amrutha Saj
  • 1,408
  • 16
  • 35
  • 2
    It would be better to explain *what* you have changed and *how* it will fix the issue so others can learn better. – codeMagic Sep 05 '17 at 15:52
1

For Horizontal divide use

android:layout_width="0dp"

and For vertical use:

android:layout_height="0dp"

Example:

       <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:orientation="vertical">
       <LinearLayout
           android:layout_width="match_parent"
           android:layout_height="0dp"
           android:layout_weight="1">
           <ImageView
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:scaleType="fitXY"
               android:src="@mipmap/ic_launcher"/>
       </LinearLayout>
       <LinearLayout
           android:layout_width="match_parent"
           android:layout_height="0dp"
           android:layout_weight="1">
           <ImageView
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:scaleType="fitXY"
               android:src="@mipmap/ic_launcher"/>
       </LinearLayout>
   </LinearLayout>
Vinay
  • 732
  • 5
  • 8