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();
    }
}


   

No comments:

Post a Comment

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