Monday, 31 July 2017

Build Dynamic UI using Fragment.


Fragment: -It is a modular section of an activity which has its own lifecycle.We can add or remove fragment in an activity while activity is running.We can combine multiple fragments in a                      single activity to build a multi-pane UI.







Step 1: -In your project go to activity.xml and type the below code.                                        


<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.hp.myfragment.MainActivity">

</FrameLayout>

Step 2: -Create a java class.Here I am  giving it name

ArticleContent


package com.example.hp.myfragment;

/**
 * Created by hp on 6/5/2017.
 */

public class ArticleContent {
    public static final String[] HEADLINES={"Sachin","Bradman"};
    public static final String[] ARTICLES={"Sachin Data","Bradman Data"};
}


Step 3: -Create a new XML and give it a name.I am giving it a name.To create it                  go to res→layout→new→layout resource file.

Articleview.xml


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

    <TextView
        android:id="@+id/article"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="18sp"
        android:padding="16dp"
        android:textColor="#000000"/>
</ScrollView>

Step 4: -Create another java class and give it the name.To create java class goes to res→java→in MainActivity right click on main activity new go to java class.

HeadLineFragment
package com.example.hp.myfragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * Created by hp on 6/5/2017.
 */

public class HeadLinesFragment extends ListFragment {
    public OnHeadLinesSelectedListener selectedListener;
    public interface  OnHeadLinesSelectedListener{
        void onArticleSelected(int position);
    }

    @Override
    public void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, ArticleContent.HEADLINES));
    }

    @Override
    public void onStart() {
        super.onStart();
        getListView().setSelector(R.drawable.color_on_click);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        selectedListener=(MainActivity)context;
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        l.setSelected(true);
        selectedListener.onArticleSelected(position);
    }

    }



Step 5: - It's time to create another activity.xml but that time its orientation is landscape.Its name is same as that of the original activity.xml but it work when orientation change that is from portrait to landscape.

land|\activity.xml

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

    <fragment
    android:id="@+id/headlines_fragment"
        android:name="com.example.hp.myfragment.HeadLinesFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>
    <FrameLayout
        android:id="@+id/article_layout"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:background="@color/white"
         style="@style/Divider"/>

</LinearLayout>

Step 6: -Now create a java class and give it the name.

ArticalFragment.java

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * Created by hp on 6/5/2017.
 */

public class ArticleFragment extends Fragment {
    private View view;


    @Override
    public View onCreateView(LayoutInflater inflater,  ViewGroup container, Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.article_view,container,false);
        return  view;
    }

    @Override
    public void onStart() {
        super.onStart();
        Bundle bundle=getArguments();
        if(bundle!=null){
            String data=ArticleContent.ARTICLES[bundle.getInt(&quot;position&quot;)];
            TextView textView=(TextView)view.findViewById(R.id.article);
            textView.setText(data);
        }
    }
}



Step 7: - To create partitioning effect need to write code in style.xml file.

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    <style name="Divider">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">1dp</item>
        <item name="android:background">@color/colorAccent</item>

    </style>

</resources>


Step 8: -Now go to MainActivity.java and type code mention below.

package com.example.hp.myfragment;

import android.app.FragmentManager;
import android.content.res.Configuration;
import android.nfc.Tag;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity implements HeadLinesFragment.OnHeadLinesSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       /* HeadLinesFragment headLinesFragment=new HeadLinesFragment();
        getSupportFragmentManager().beginTransaction().add(R.id.fragment_container,headLinesFragment).commit();*/
        if(findViewById(R.id.fragment_container)!=null){
            if (savedInstanceState!=null){
                return;
            }
            HeadLinesFragment headLinesFragment=new HeadLinesFragment();
            getSupportFragmentManager().beginTransaction().add(R.id.fragment_container,headLinesFragment).commit();
        }
    }

    @Override
    public void onArticleSelected(int position) {
        ArticleFragment articleFragment=new ArticleFragment();
        Bundle bundle=new Bundle();
        bundle.putInt("position",position);
        articleFragment.setArguments(bundle);
        FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
        /*transaction.replace(R.id.fragment_container , articleFragment);
        transaction.addToBackStack(null);
        transaction.commit();
*/
        if (findViewById(R.id.article_layout)!=null){
            transaction.replace(R.id.article_layout,articleFragment);
        }else {
            transaction.replace(R.id.fragment_container , articleFragment);
            transaction.addToBackStack(null);
        }
        transaction.commit();
    }


    }




Friday, 28 July 2017

Use Intent to pass value from one to other Activity in Android

An intent is used as message carrier between two activities in Android.It is an object that provides runtime binding between separate component.There are one parent activity and other activity that is invoked by parent activity act as child Activity.







Step 1: - Open your project in Android Studio.In your activity.xml create the layout of your requirement.Here I am giving an example. 

activity.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:layout_height="match_parent"
    tools:context="com.example.hp.simpleuserinterface.MainActivity">

    <EditText
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/enter_message" />

    <Button
        android:id="@+id/send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/click"
        android:onClick="sendMessage"/>


</LinearLayout>


String.xml


<resources>
    <string name="app_name">SimpleUserInterface</string>
    <string name="enter_message">Enter a message</string>
    <string name="click">Send</string>
</resources>



Step 2: - Create a new blank activity in Android.Go to app→java→open first content in Mainactivity right click→new→Activity→Empty activity.Give the name to your new activity.I am giving it name display_message.when we click finish new xml and java class creates.

Step 3: - In Androidmanifest.xml we type some statement to put navigation bar in the new activity that we have created display_message.

<activity android:name=".DisplayMessageActivity"    
android:parentActivityName=".MainActivity">
    <meta-data       
 android:name="android_support.PARENT_ACTIVITY"       
 android:value=".MainActivity"/>
</activity>

Put this code in manifest.xml.


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hp.simpleuserinterface">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".DisplayMessageActivity"
            android:parentActivityName=".MainActivity">
            <meta-data
                android:name="android_support.PARENT_ACTIVITY"
                android:value=".MainActivity"/>
        </activity>
    </application>

</manifest>



Design activity_display_message.xml according to your requirement.I am designing this according to my example.

<?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:layout_height="match_parent"
    tools:context="com.example.hp.simpleuserinterface.DisplayMessageActivity">

     <TextView
         android:id="@+id/display_text"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:textSize="25sp"
         android:textColor="@android:color/holo_blue_dark"/>
</LinearLayout>


Step 4: - Open your MainActivity.java and paste the below code.

package com.example.hp.simpleuserinterface;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.view.ViewPropertyAnimatorCompatSet;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE="org.mz.activity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void sendMessage(View view){
        Intent intent=new Intent(this,DisplayMessageActivity.class);
        EditText editText=(EditText)findViewById(R.id.text);
        String message=editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE,message);
        startActivity(intent);
    }
}


Step 5: - Open your DisplayMessageActivity.java and paste the below code.


package com.example.hp.simpleuserinterface;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class DisplayMessageActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_message);
        Intent intent=getIntent();
        String message=intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
        TextView textView=(TextView)findViewById(R.id.display_text);
        textView.setText("Welcome " + message);
    }
}




Wednesday, 26 July 2017

Design of Scientific Calculator in Android

Layout to design Scientific calculator in Android.





Step-1:- While creating my project I am using empty activity.You can use any activity according to your requirement.                                   


        All written content of my project I use string.xml file.I also give string.xml file for scientific               calculator.

        string.xml
     
<resources>
    <string name="app_name">MyModernCalculator</string>
    <string name="C">C</string>
    <string name="B">B</string>
    <string name="mod">%</string>
    <string name="divide">÷ </string>
    <string name="MD7">7</string>
    <string name="MD8">8</string>
    <string name="MD9">9</string>
    <string name="MCX">×</string>
    <string name="MC4">4</string>
    <string name="MC5">5</string>
    <string name="MC6">6</string>
    <string name="MCsub">−</string>
    <string name="MC1">1</string>
    <string name="MC2">2</string>
    <string name="MC3">3</string>
    <string name="MCplus">+</string>
    <string name="MC.">.</string>
    <string name="MC0">0</string>
    <string name="MCequal">=</string>
    <string name="slash">+/−</string>
    <string name="powerXy">"X<sup><small>y</small></sup>"</string>
    <string name="powerex">"e<sup><small>x</small></sup>"</string>
    <string name="powerYx">"Y<sup><small>x</small></sup>"</string>
    <string name="power10x">"10<sup><small>x</small></sup>"</string>
    <string name="powerLogy">"log<sup><small>y</small></sup>"</string>
    <string name="powerX2">"X<sup><small>2</small></sup>"</string>
    <string name="power2nd">"2<sup><small>nd</small></sup>"</string>
    <string name="power2x">"2<sup><small>x</small></sup>"</string>
    <string name="ln">ln</string>
    <string name="pi">π</string>
    <string name="lbracket">(</string>
    <string name="Rbracket">)</string>
    <string name="MC">MC</string>
    <string name="Mplus">M+</string>
    <string name="Mminus">M-</string>
    <string name="MR">MR</string>
    <string name="divide1x">1/x</string>
    <string name="xnotequal">x!</string>
    <string name="log">log</string>
    <string name="yunderootx">"<sup><small>y</small></sup>√X"</string>
</resources>



Step-2:- In your activity_main.xml paste the code mention below.This activity_main.xml work for portrait mode for mobile.
                                                                                                                                                                
<?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:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="3"
    tools:context="com.example.hp.mymoderncalculator.MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#444444"
        android:layout_weight="1"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:layout_weight="2"
        android:weightSum="4">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="5"
        >
        <Button
            android:id="@+id/BC"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:textSize="30sp"
            android:textColor="#ffffff"
            android:background="@drawable/button1"
            android:text="@string/C"
            android:layout_margin="0.2dp"/>
        <Button
            android:id="@+id/B7"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"

            android:textSize="30sp"
            android:textColor="#000000"
            android:background="@drawable/button"
            android:text="@string/MD7"
            android:layout_margin="0.2dp"/>
        <Button
        android:id="@+id/B4"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"

        android:textSize="30sp"
        android:textColor="#000000"
        android:background="@drawable/button"
        android:text="@string/MC4"
            android:layout_margin="0.2dp"/>
        <Button
            android:id="@+id/B1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"

            android:textSize="30sp"
            android:textColor="#000000"
            android:background="@drawable/button"
            android:text="@string/MC1"
            android:layout_margin="0.2dp"/>
        <Button
            android:id="@+id/B."
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:textStyle="bold"
            android:textSize="30sp"
            android:textColor="#000000"
            android:background="@drawable/button"
            android:text="@string/MC."
            android:layout_margin="0.2dp"/>

    </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:weightSum="5">
            <ImageButton
                android:id="@+id/BB"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:textSize="30sp"
                android:src="@drawable/ic_backspace"
                android:textColor="#ffffff"
                android:background="@drawable/button1"
                android:text="@string/B"
                android:layout_margin="0.2dp"
                />

            <Button
                android:id="@+id/B8"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"

                android:textSize="30sp"
                android:textColor="#000000"
                android:background="@drawable/button"
                android:text="@string/MD8"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/B5"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"

                android:textSize="30sp"
                android:textColor="#000000"
                android:background="@drawable/button"
                android:text="@string/MC5"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/B2"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:textSize="30sp"
                android:textColor="#000000"
                android:background="@drawable/button"
                android:text="@string/MC2"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/B0"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:textSize="30sp"
                android:textColor="#000000"
                android:background="@drawable/button"
                android:text="@string/MC0"
                android:layout_margin="0.2dp"/>

        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="1">
            <Button
                android:id="@+id/Bmod"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:textSize="30sp"
                android:textColor="#ffffff"
                android:background="@drawable/button1"
                android:text="@string/mod"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/B9"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"

                android:textSize="30sp"
                android:textColor="#000000"
                android:background="@drawable/button"
                android:text="@string/MD9"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/B6"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"

                android:textSize="30sp"
                android:textColor="#000000"
                android:background="@drawable/button"
                android:text="@string/MC6"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/B3"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:textSize="30sp"
                android:textColor="#000000"
                android:background="@drawable/button"
                android:text="@string/MC3"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/Bequal"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:textSize="30sp"
                android:textColor="#000000"
                android:background="@drawable/button"
                android:text="@string/MCequal"
                android:layout_margin="0.2dp"/>

        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="1">
            <Button
                android:id="@+id/Bdivide"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:textSize="30sp"
                android:textColor="#ffffff"
                android:background="@drawable/button2"
                android:text="@string/divide"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/Bmulti"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"

                android:textSize="30sp"
                android:textColor="#ffffff"
                android:background="@drawable/button2"
                android:text="@string/MCX"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/Bsubtract"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"

                android:textSize="30sp"
                android:textColor="#ffffff"
                android:background="@drawable/button2"
                android:text="@string/MCsub"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/Bplus"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="2"
                android:textSize="30sp"
                android:textColor="#ffffff"
                android:background="@drawable/button2"
                android:text="@string/MCplus"
                android:layout_margin="0.2dp"/>

        </LinearLayout>
    </LinearLayout>

</LinearLayout>

Step-3:- To give the button pressing effect I am creating 3 xml file.To create xml file go to app→res-               →drawable.Right click on drawable go to new then drawable resource file onclick open                          popwindow open.Type name just as i am using button.xml.

    button.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape>
            <solid
                android:color="#aaaaaa" />
            <stroke
                android:width="0dp"
                android:color="#222222" />

        </shape>
    </item>
    <item>
        <shape>
            <gradient
                android:startColor="#ffffff"
                android:endColor="#ffffff"
                android:angle="270" />
            <stroke
                android:width="0dp"
                android:color="#000000" />


        </shape>
    </item>
</selector>

button1.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape>
            <solid
                android:color="#00ccff" />
            <stroke
                android:width="0dp"
                android:color="#222222" />

        </shape>
    </item>
    <item>
        <shape>
            <gradient
                android:startColor="#66c2ff"
                android:endColor="#66c2ff"
                android:angle="270" />
            <stroke
                android:width="0dp"
                android:color="#000000" />


        </shape>
    </item>
</selector>


button3.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" >
        <shape>
            <solid
                android:color="#c2c3c6" />
            <stroke
                android:width="0dp"
                android:color="#222222" />

        </shape>
    </item>
    <item>
        <shape>
            <gradient
                android:startColor="#4fe1dedf"
                android:endColor="#4fe1dedf"
                android:angle="270" />
            <stroke
                android:width="0dp"
                android:color="#000000" />


        </shape>
    </item>
</selector>


I am using three button of different color thats why to give click effect or change color onclick i am using 3 xml file like button.xml,button1.xml and button3.xml.You can give these xml name of your own choice.



Step-4:- To add thid delete symbol I am using vector asset an inbuilt feature of android studio.
               To add this in your button go to drawable →(right click ) new→select vector asset double                  click on android icon button search desired icon from list and press Ok.Then press next and               then finish.A new component add in drawable.Now you can add this icon to desired place                   using syntax android:src="@drawable/" .In inverted comas use name of component newly               added from adding icon.In my project i am using this.You can watch my code.

Step-5:- Its time to change orientation from portrait to landscape.For this we need to create new                         activity_main.xml.In res go to layout (right click) layout drawable click on that.Apop box                   open in this box type infront of file name type activity_main.xml.Name should same as                       default xml file of project.Infront of directory name after layout use(-land) and combination                 of both look like (layout-land) to create landscape orientation.Inside this file type or paste                    the below code.


<?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="match_parent"
    android:weightSum="3">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#444444"
            android:layout_weight="1"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:layout_weight="2"
        android:weightSum="10">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:weightSum="5"
            android:layout_weight="1"
            >
            <Button
                android:id="@+id/Lbracket"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:textSize="20sp"
                android:textColor="#000000"
                android:fontFamily="Times new Roman"
                android:background="@drawable/button3"
                android:text="@string/lbracket"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/second"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:textAllCaps="false"
                android:fontFamily="Times new Roman"
                android:textSize="20sp"
                android:textColor="#000000"
                android:background="@drawable/button3"
                android:text="@string/power2nd"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/onebyx"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:fontFamily="Times new Roman"
                android:textSize="15sp"
                android:textColor="#000000"
                android:background="@drawable/button3"
                android:text="@string/divide1x"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/Rand"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:textAllCaps="false"
                android:textSize="15sp"
                android:textColor="#000000"
                android:fontFamily="Times new Roman"
                android:background="@drawable/button3"
                android:text="Rand"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/Deg"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:textSize="15sp"
                android:textColor="#00ffff"
                android:background="@drawable/button3"
                android:textAllCaps="false"
                android:fontFamily="Times new Roman"
                android:text="Deg"
                android:layout_margin="0.2dp"/>

        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:weightSum="5"
            android:layout_weight="1"
            >
            <Button
                android:id="@+id/Rbracket"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:fontFamily="Times new Roman"
                android:textSize="20sp"
                android:textColor="#000000"
                android:background="@drawable/button3"
                android:text="@string/Rbracket"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/multiplyx"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:fontFamily="Times new Roman"
                android:textAllCaps="false"
                android:textSize="20sp"
                android:textColor="#000000"
                android:background="@drawable/button3"
                android:text="@string/power2x"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/notequal"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:fontFamily="Times new Roman"
                android:textSize="15sp"
                android:textColor="#000000"
                android:background="@drawable/button3"
                android:text="@string/xnotequal"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/tan"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:textAllCaps="false"
                android:textSize="15sp"
                android:textColor="#000000"
                android:fontFamily="Times new Roman"
                android:background="@drawable/button3"
                android:text="tan"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/tanh"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:textSize="15sp"
                android:textAllCaps="false"
                android:textColor="#000000"
                android:background="@drawable/button3"
                android:text="tanh"
                android:fontFamily="Times new Roman"
                android:layout_margin="0.2dp"/>

        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:weightSum="5"
            android:layout_weight="1"
            >
            <Button
                android:id="@+id/Mc"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:fontFamily="Times new Roman"
                android:textSize="15sp"
                android:textColor="#000000"
                android:background="@drawable/button3"
                android:text="@string/MC"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/Xy"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:fontFamily="Times new Roman"
                android:textAllCaps="false"
                android:textSize="15sp"
                android:textColor="#000000"
                android:background="@drawable/button3"
                android:text="@string/powerXy"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/yunderroot"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:textAllCaps="false"
                android:fontFamily="Times new Roman"
                android:textSize="15sp"
                android:textColor="#000000"
                android:background="@drawable/button3"
                android:text="@string/yunderootx"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/sin"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:textAllCaps="false"
                android:textSize="15sp"
                android:fontFamily="Times new Roman"
                android:textColor="#000000"
                android:background="@drawable/button3"
                android:text="sin"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/sinh"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:textAllCaps="false"
                android:textSize="15sp"
                android:fontFamily="Times new Roman"
                android:textColor="#000000"
                android:background="@drawable/button3"
                android:text="sinh"
                android:layout_margin="0.2dp"/>

        </LinearLayout>


        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="1">
            <Button
                android:id="@+id/Mplus"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:textSize="15sp"
                android:textColor="#000000"
                android:fontFamily="Times new Roman"
                android:background="@drawable/button3"
                android:text="@string/Mplus"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/xsquare"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:textAllCaps="false"
                android:textSize="15sp"
                android:textColor="#000000"
                android:fontFamily="Times new Roman"
                android:background="@drawable/button3"
                android:text="@string/powerX2"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/underrooty"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:textAllCaps="false"
                android:textSize="15sp"
                android:textColor="#000000"
                android:fontFamily="Times new Roman"
                android:background="@drawable/button3"
                android:text="√X"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/cos"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:textSize="15sp"
                android:fontFamily="Times new Roman"
                android:textAllCaps="false"
                android:textColor="#000000"
                android:background="@drawable/button3"
                android:text="cos"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/cosh"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:textSize="15sp"
                android:textAllCaps="false"
                android:fontFamily="Times new Roman"
                android:textColor="#000000"
                android:background="@drawable/button3"
                android:text="cosh"
                android:layout_margin="0.2dp"/>

        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="1">
            <Button
                android:id="@+id/Mminus"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:fontFamily="Times new Roman"
                android:textSize="15sp"
                android:textColor="#000000"
                android:background="@drawable/button3"
                android:text="@string/Mminus"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/e"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:textAllCaps="false"
                android:textSize="20sp"
                android:fontFamily="Times new Roman"
                android:textColor="#000000"
                android:background="@drawable/button3"
                android:text="e"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/pi"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:textAllCaps="false"
                android:fontFamily="Times new Roman"
                android:textSize="20sp"
                android:textColor="#000000"
                android:background="@drawable/button3"
                android:text="@string/pi"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/Yx"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:textSize="20sp"
                android:textColor="#000000"
                android:textAllCaps="false"
                android:background="@drawable/button3"
                android:fontFamily="Times new Roman"
                android:text="@string/powerYx"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/logy"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:textSize="15sp"
                android:textAllCaps="false"
                android:textColor="#000000"
                android:fontFamily="Times new Roman"
                android:background="@drawable/button3"
                android:text="@string/powerLogy"
                android:layout_margin="0.2dp"/>

        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="1">
            <Button
                android:id="@+id/MR"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:fontFamily="Times new Roman"
                android:textSize="15sp"
                android:textColor="#000000"
                android:background="@drawable/button3"
                android:text="@string/MR"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/ex"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:textAllCaps="false"
                android:fontFamily="Times new Roman"
                android:textSize="20sp"
                android:textColor="#000000"
                android:background="@drawable/button3"
                android:text="@string/powerex"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/x10"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:textAllCaps="false"
                android:textSize="20sp"
                android:textColor="#000000"
                android:fontFamily="Times new Roman"
                android:background="@drawable/button3"
                android:text="@string/ln"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/B3"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:textSize="15sp"
                android:textColor="#000000"
                android:fontFamily="Times new Roman"
                android:background="@drawable/button3"
                android:text="@string/power10x"
                android:textAllCaps="false"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/Bequal"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:textSize="15sp"
                android:textAllCaps="false"
                android:fontFamily="Times new Roman"
                android:textColor="#000000"
                android:background="@drawable/button3"
                android:text="@string/log"
                android:layout_margin="0.2dp"/>

        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="1">
            <Button
                android:id="@+id/Bmod"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:textSize="30sp"
                android:fontFamily="Times new Roman"
                android:textColor="#ffffff"
                android:background="@drawable/button1"
                android:text="@string/C"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/B9"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:textAllCaps="false"
                android:textSize="30sp"
                android:textColor="#000000"
                android:fontFamily="Times new Roman"
                android:background="@drawable/button"
                android:text="@string/MD7"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/B6"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:fontFamily="Times new Roman"
                android:textSize="30sp"
                android:textColor="#000000"
                android:background="@drawable/button"
                android:text="@string/MC4"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/B3"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:textSize="30sp"
                android:fontFamily="Times new Roman"
                android:textColor="#000000"
                android:background="@drawable/button"
                android:text="@string/MC1"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/Bequal"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:textSize="30sp"
                android:textColor="#000000"
                android:fontFamily="Times new Roman"
                android:background="@drawable/button"
                android:text="@string/MC."
                android:layout_margin="0.2dp"/>

        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="1"
            android:weightSum="5">
            <ImageButton
                android:id="@+id/BB"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:textSize="30sp"
                android:src="@drawable/ic_backspace"
                android:textColor="#ffffff"
                android:background="@drawable/button1"
                android:text="@string/B"
                android:layout_margin="0.2dp"
                />

            <Button
                android:id="@+id/B8"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:fontFamily="Times new Roman"
                android:textSize="30sp"
                android:textColor="#000000"
                android:background="@drawable/button"
                android:text="@string/MD8"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/B5"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:fontFamily="Times new Roman"
                android:textSize="30sp"
                android:textColor="#000000"
                android:background="@drawable/button"
                android:text="@string/MC5"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/B2"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:fontFamily="Times new Roman"
                android:textSize="30sp"
                android:textColor="#000000"
                android:background="@drawable/button"
                android:text="@string/MC2"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/B0"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:textSize="30sp"
                android:textColor="#000000"
                android:fontFamily="Times new Roman"
                android:background="@drawable/button"
                android:text="@string/MC0"
                android:layout_margin="0.2dp"/>

        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="1">
            <Button
                android:id="@+id/Bmod"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:textSize="30sp"
                android:textColor="#ffffff"
                android:background="@drawable/button1"
                android:text="@string/mod"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/B9"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:fontFamily="Times new Roman"
                android:textSize="30sp"
                android:textColor="#000000"
                android:background="@drawable/button"
                android:text="@string/MD9"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/B6"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:fontFamily="Times new Roman"
                android:textSize="30sp"
                android:textColor="#000000"
                android:background="@drawable/button"
                android:text="@string/MC6"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/B3"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:textSize="30sp"
                android:textColor="#000000"
                android:background="@drawable/button"
                android:text="@string/MC3"
                android:fontFamily="Times new Roman"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/Bequal"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:textSize="30sp"
                android:textColor="#000000"
                android:background="@drawable/button"
                android:text="@string/MCequal"
                android:fontFamily="Times new Roman"
                android:layout_margin="0.2dp"/>

        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="1">
            <Button
                android:id="@+id/Bdivide"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:textSize="30sp"
                android:textColor="#ffffff"
                android:background="@drawable/button2"
                android:text="@string/slash"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/Bmulti"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"

                android:textSize="30sp"
                android:textColor="#ffffff"
                android:background="@drawable/button2"
                android:text="@string/divide"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/Bsubtract"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"

                android:textSize="30sp"
                android:textColor="#ffffff"
                android:background="@drawable/button2"
                android:text="@string/MCX"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/Bplus"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:textSize="30sp"
                android:textColor="#ffffff"
                android:background="@drawable/button2"
                android:text="@string/MCsub"
                android:layout_margin="0.2dp"/>
            <Button
                android:id="@+id/Bplus"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:textSize="30sp"
                android:textColor="#ffffff"
                android:background="@drawable/button2"
                android:text="@string/MCplus"
                android:layout_margin="0.2dp"/>

        </LinearLayout>
    </LinearLayout>


</LinearLayout>


design of scientific calculator in Android finish.Now you can write code according to your  requirement to make this calculator functioning.

Change Fragment on button click in Android

In example mention below on click of a button, we attach the desired fragment in our frame layout which was present in activity_main.xml. ...