Wednesday 2 August 2017

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.




Step 1: -Open your project and design layout of your app in activity-main.xml.

activity_main.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:id="@+id/number_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.hp.myframepart2.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <Button
        android:id="@+id/fragment_one"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/fragment_one"
        android:onClick="sendframe"/>
    <Button
        android:id="@+id/fragment_two"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="@string/fragment_two"
        android:onClick="sendframe"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <FrameLayout
        android:id="@+id/color_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </FrameLayout>

    </LinearLayout>
</LinearLayout>


Step 2: -Create a new xml which act as the fragment which attaches to our frame layout.

fragment_one.xml
<?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">
    <TextView
        android:id="@+id/fragment_field"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="@dimen/textsize"
        android:padding="@dimen/textpadding"
        android:gravity="center"
        android:textColor="@android:color/white"/>

</LinearLayout>

string.xml
<resources>
    <string name="app_name">MyFramepart2</string>
    <string name="fragment_one">FRAGMENT ONE</string>
    <string name="fragment_two">FRAGMENT TWO</string>
</resources>


Step 3: -Create another java class to attach our fragment to frame layout.

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

public class TextFragment extends Fragment {
    private View view;


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

    @Override
    public void onStart() {
        super.onStart();
        Bundle bundle=getArguments();
        if(bundle!=null){
            String data=bundle.getString(MainActivity.TEXT);
            int color=bundle.getInt(MainActivity.COLOR);
            TextView textView=(TextView)view.findViewById(R.id.fragment_field);
            textView.setText(data);
            textView.setBackgroundColor(color);
        }
    }
}


Step 4: - To give text size or dimension we give it dynamically in dimen.xml.In it, we mention size or dimension and call it dynamically in a project. To create dimen.xml go to res→values(right click)→Values resource file then press ok.Inside dimen.xml you mention your dimension related requirement.

dimen.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
   <dimen name="textsize">25sp</dimen>
   <dimen name="textpadding">16dp</dimen>
</resources>


Step 5: -Open your MainActivity.java class and type the code mention below to perform the operation.

MainActivity.java
package com.example.hp.myframepart2;

import android.graphics.Color;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    public final static String TEXT = "org.mz.Activity.text";
    public final static String COLOR = "org.mz.Activity.color";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    public void sendframe(View view) {
        TextFragment changeFragmentOnClick = new TextFragment();
        Button button = (Button) view;
        Bundle bundle = new Bundle();
        String buttonText = button.getText().toString();
        if (buttonText.equals("FRAGMENT ONE")) {
            bundle.putInt(COLOR, (Color.RED));
        } else {
            bundle.putInt(COLOR, (Color.BLUE));
        }

        bundle.putString(TEXT, buttonText);
        changeFragmentOnClick.setArguments(bundle);
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.color_fragment, changeFragmentOnClick);
        transaction.commit();
    }
}


   

Save File in External Storage in Android


There are two categories of files we might save in External Storage.

1.) Public files→Files that are freely available to other app and user.When the user uninstalls app these files should remain available to the user.
2.) Private files→Files that rightfully belong to the app and should be deleted when user uninstall your app.

To save the public file in external storage we need a method getExternalStoragePublicDirectory() method.e file
To save the private file in external storage we need method getExternalFilesDir().
Here we are dealing with external storage.To work with external storage we need permission.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

This permission is required to read or write date from external storage.This permission is placed in AndroidManifest.xml.I give you the example as mention below.


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hp.publicfileexternalstorage">
   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <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>
    </application>

</manifest>




Step 1: -Write code in activity_main.xml to design your layout.                                                      

activity_main.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"
    android:orientation="vertical"
    tools:context="com.example.hp.publicfileexternalstorage.MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <EditText
            android:id="@+id/entername"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/save"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="SAVE"
            android:onClick="savedata"/>
        <Button

            android:id="@+id/retreive"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="RETRIEVE"
            android:onClick="retreivdata"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/display"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textSize="15sp"/>
    </LinearLayout>


</LinearLayout>


Step 2: -Type code in MainActivity.xml to save the file in External storage.

MainActivity.xml
package com.example.hp.publicfileexternalstorage;

import android.content.Context;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class MainActivity extends AppCompatActivity {
private static final String LOG_TAG=MainActivity.class.getSimpleName();
    TextView display;
    File file;
    File file1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.e(LOG_TAG,"create");
        display=(TextView)findViewById(R.id.display);
        file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "file"); //To save public file in external storage;
        file = new File(getExternalFilesDir("MyDocument"), "appfile.txt"); //To save private file in external storage;      
 if (!file.mkdir()) {
            Log.e(LOG_TAG,"Directory not created");
        }else{
            Log.e(LOG_TAG,"Directory created");
        }

    }
    public void savedata(View view){
         file1=new File(file,"Mytext.txt");
        EditText name=(EditText)findViewById(R.id.entername);
        String username=name.getText().toString();
        try {
            FileOutputStream outputStream=new FileOutputStream(file1);
            outputStream.write(username.getBytes());
            outputStream.close();
            name.setText("");

        }
        catch (FileNotFoundException e){
            e.printStackTrace();

        }

        catch (IOException e){
            e.printStackTrace();
        }

    }
    public void retreivdata(View view) {
        try {
            FileInputStream inputStream = new FileInputStream(file1);
            if (inputStream != null) {
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                String temp, temp1 = "";
                try {
                    while ((temp = bufferedReader.readLine()) != null)
                        temp1 += temp;
                } catch (Exception e) {
                    e.printStackTrace();
                }
                display.setText(temp1);
                inputStream.close();
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }


    }



}





Tuesday 1 August 2017

Save File in Internal Storage in Android


In Android, we have to way to store data.

1.) Internal storage
2.) External storage

In the below example we use internal storage to store data and retrieve date from the same location.I am using internal storage so we don't need for permission to access internal storage.But when we use external storage we need permission to read or write on external storage.In this example on click of save button value of edit-text saved in internal storage and value retrieve on click of a retreive button.






Step 1: -Open your project and in activity_main.xml create the design of your own requirement.To give you the idea I am creation simple design.      

activity_main.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"
    android:orientation="vertical"
    tools:context="com.example.hp.mysavefile.MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <EditText
        android:id="@+id/entername"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <Button
        android:id="@+id/save"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="SAVE"
        android:onClick="savedata"/>
        <Button

            android:id="@+id/retreive"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="RETRIEVE"
            android:onClick="retreivdata"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/display"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textSize="15sp"/>
    </LinearLayout>

</LinearLayout>


Step 2: -Open your MainActivity.java and write code MainActivity.java I am writing code for save button and retrieve button.

MainActivity.java
package com.example.hp.mysavefile;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class MainActivity extends AppCompatActivity {
 TextView display;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        display=(TextView)findViewById(R.id.display);

    }
                public void savedata(View view){
                EditText name=(EditText)findViewById(R.id.entername);
                String username=name.getText().toString();
                try {
                    FileOutputStream outputStream=openFileOutput("amit.txt", Context.MODE_PRIVATE);
                    outputStream.write(username.getBytes());
                    outputStream.close();

                    name.setText("");
                }
                catch (FileNotFoundException e){
                    e.printStackTrace();

                }

                catch (IOException e){
                    e.printStackTrace();
                }
            }
            public void retreivdata(View view) {
                try {
                    FileInputStream inputStream = openFileInput("amit.txt");
                    if (inputStream != null) {
                        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                        String temp, temp1 = "";
                        try {
                            while ((temp = bufferedReader.readLine()) != null)
                                temp1 += temp;
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        display.setText(temp1);
                        inputStream.close();
                    }
                }
            catch (Exception e) {
            e.printStackTrace();
            }


                        }
                    }








                                                                 


Custom Listview in Android


  Listview: -

It is a view group that displays a list of scrollable items. The list items are automatically inserted to the list using an Adapter pulls content from a source such as an array or database query and converts each item result into a view that's placed on the list.

 

Step 1: - Open your project.In your activity.xml creates list view as mention below.                          
         
activity_main.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.mylistview.MainActivity">
   <ListView
       android:id="@+id/product_list"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       />
</LinearLayout>

          
Step 2: -Now create another xml which attach to our list view.

row.xml
<?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"
    >
      <TextView
          android:id="@+id/product_name"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" />
      <TextView
          android:id="@+id/product_price"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" />
</LinearLayout>

In it, I am using two text-view to show two texts in our list view.

Step 3: -Now create a bean class to describe our component.

Product.java

public class Product {
    private String productName;
    private String productPrice;
    public Product(String productName,String productPrice){
        this.productName=productName;
        this.productPrice=productPrice;
    }

    public String getProductName() {
        return productName;
    }

    public String getProductPrice() {
        return productPrice;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public void setProductPrice(String productPrice) {
        this.productPrice = productPrice;
    }
}

Step 4: - I am creating custom Listview.In it I am using two text view for that we need to create an adapter class.If I need to display only one text in one row then we do not need to create custom adapter there is default adapter that we use.

ListAdapter.java

public class ListAdapter extends ArrayAdapter<Product>{
    private Context context;
    private List<Product> productList;
    private int id;
    public ListAdapter(Context context,int id,List<Product> productList){
        super(context,id,productList);
        this.context=context;
        this.id=id;
        this.productList=productList;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        if (convertView==null){
            LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView=inflater.inflate(id,parent,false);
        }
        TextView productName=(TextView) convertView.findViewById(R.id.product_name);
        TextView productPrice=(TextView) convertView.findViewById(R.id.product_price);
        productName.setText(productList.get(position).getProductName());
        productPrice.setText(productList.get(position).getProductPrice());
        return  convertView;
    }
}

Step 5: -Now time to write code in MainActivity.java.

MainActivity.java
package com.example.hp.mylistview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        List<Product> productList=new ArrayList<>();
        productList.add(new Product("Kurkure","10"));
        productList.add(new Product("Kurkure","20"));
        productList.add(new Product("Kurkure","30"));
        productList.add(new Product("Kurkure","40"));
        productList.add(new Product("Kurkure","50"));
        productList.add(new Product("Kurkure","60"));
        productList.add(new Product("Kurkure","70"));
        ListView listView=(ListView)findViewById(R.id.product_list);
        ListAdapter adapter=new com.example.hp.mylistview.ListAdapter(this,R.layout.row,productList);
        listView.setAdapter(adapter);



    }
}


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. ...