Android App Development With AndroidCource: multiple use single activity
Showing posts with label multiple use single activity. Show all posts
Showing posts with label multiple use single activity. Show all posts

Monday, 21 August 2017

Fragment Example in Android

FragmentsActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:tools="http://schemas.android.com/tools"
             android:id="@+id/activity_frame_layouta"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:paddingBottom="@dimen/activity_vertical_margin"
             android:paddingLeft="@dimen/activity_horizontal_margin"
             android:paddingRight="@dimen/activity_horizontal_margin"
             android:paddingTop="@dimen/activity_vertical_margin"
             tools:context="com.bhaumik.programmingstudy.FragmentsActivity">

    <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:src="@drawable/icon_android"
            android:scaleType="fitCenter"
    />
    <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:text="Frame Layout"
            android:textSize="25sp"
            android:textColor="#000000"
    />

</FrameLayout>
FragmentsActivity.java
package com.bhaumik.programmingstudy;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class FragmentsActivity extends AppCompatActivity implements View.OnClickListener{

    Button android,apple,google,microsoft,whatsapp;

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

        FindViewId();

        android.setOnClickListener(this);
        apple.setOnClickListener(this);
        google.setOnClickListener(this);
        microsoft.setOnClickListener(this);
        whatsapp.setOnClickListener(this);
    }
    private void FindViewId() {
        android = (Button) findViewById(R.id.btn_android);
        apple = (Button) findViewById(R.id.btn_apple);
        google = (Button) findViewById(R.id.btn_google);
        microsoft = (Button) findViewById(R.id.btn_microsoft);
        whatsapp = (Button) findViewById(R.id.btn_whatsapp);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.btn_android :
                AndroidListener();
                break;
            case R.id.btn_apple :
                AppleListener();
                break;
            case R.id.btn_google :
                GoogleListener();
                break;
            case R.id.btn_microsoft :
                MicrosoftListener();
                break;
            case R.id.btn_whatsapp :
                WhatsappListener();
                break;
        }
    }
    private void AndroidListener() {
        TestFragment tf = new TestFragment();
        Bundle bundle = new Bundle();
        bundle.putString("test","android");
        tf.setArguments(bundle);

        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,tf,"tags").commit();

    }

    private void AppleListener() {
        TestFragment tf = new TestFragment();
        Bundle bundle = new Bundle();
        bundle.putString("test","apple");
        tf.setArguments(bundle);

        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,tf,"tags").commit();

    }

    private void GoogleListener() {
        TestFragment tf = new TestFragment();
        Bundle bundle = new Bundle();
        bundle.putString("test","google");
        tf.setArguments(bundle);

        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,tf,"tags").commit();
    }

    private void MicrosoftListener() {
        TestFragment tf = new TestFragment();
        Bundle bundle = new Bundle();
        bundle.putString("test","microsoft");
        tf.setArguments(bundle);

        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,tf,"tags").commit();
    }

    private void WhatsappListener() {
        TestFragment tf = new TestFragment();
        Bundle bundle = new Bundle();
        bundle.putString("test","whatsapp");
        tf.setArguments(bundle);
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,tf,"tags").commit();
    }
}

Ads