Free Download Software and Android Tutorials

Wednesday, August 5, 2015

Basic Android studio tutorial: to know the interaction between the fragment

Android Studio tutorial will discuss about the interaction between the fragment, where in previous articles we have discussed about the sense fragment and benefits in making an android based applications, then on this occasion I will specifically address the interaction between the fragments through direct practice by making a new project through the android studio. Interaction between fragment intentions is eventually to see which one can be determined fragment of other fragment interactions. For more details, we immediately practice just that by creating a new project through the android studio, and then the project is named ContohFragment. This ContohFragment project will contain some java and xml files as the layout. Here are some java and xml files contained in the project ContohFragment:
  • ContohFragmentActivity.java
  • UtamaFragment.java
  • RincianFragment.java
  • RincianActivity.java
  • activity_contoh_fragment.xml
  • activity_contoh_rincianfragment.xml
  • activity_contoh_landscapefragment.xml
  • AndroidManifest.xml
Source code ContohFragmentActivity.java
package com.dsoccerupdate.contohfragment;

import android.os.Bundle;
import android.app.Activity;
import android.content.res.Configuration;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;

public class ContohFragmentActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getResources().getConfiguration().orientation==
                Configuration.ORIENTATION_LANDSCAPE) {
            setContentView(R.layout.activity_contoh_landscapefragment);
        } else {
            setContentView(R.layout.activity_contoh_fragment);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_contoh, menu);
        return true;
    }
}

Source code UtamaFragment.java
package com.dsoccerupdate.contohfragment;

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

public class UtamaFragment extends ListFragment{
    String[] buahbuahan = {
            "belimbing",
            "mangga",
            "jambu",
            "sirsak",
            "rambutan",
            "durian",
            "manggis",
            "kedongdong"
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setListAdapter(new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1,
                buahbuahan));
    }

    public void onListItemClick(ListView parent, View v, int position, long id) {
        Toast.makeText(getActivity(),
                "Yang di pilih  adalah buah : " + buahbuahan[position],
                Toast.LENGTH_SHORT).show();
        String strbuahdipilih =  buahbuahan[position];

        RincianFragment rincianFragment = (RincianFragment)
                getFragmentManager().findFragmentById(R.id.rincianFragment);

        if (rincianFragment != null && rincianFragment.isInLayout()) {
            rincianFragment.setBuahTerpilih(strbuahdipilih);
        } else {
            Intent intent = new Intent(getActivity(), RincianActivity.class);
            intent.putExtra("buah", strbuahdipilih);
            startActivity(intent);
        }
    }
}

Source code RincianFragment.java
package com.dsoccerupdate.contohfragment;

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 RincianFragment extends Fragment{
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

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

    public void setBuahTerpilih(String name) {
        TextView view = (TextView) getView().findViewById(R.id.txtBuahbuahan);
        view.setText("Yang dipilih adalah " + name);
    }
}

Source code RincianActivity.java
package com.dsoccerupdate.contohfragment;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.widget.TextView;

public class RincianActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contoh_rincianfragment);

        if (getResources().getConfiguration().orientation ==
                Configuration.ORIENTATION_LANDSCAPE) {
            finish();
            return;
        }

        String name = getIntent().getStringExtra("buah");
        TextView view = (TextView) findViewById(R.id.txtBuahbuahan);
        view.setText("Yang dipilih adalah buah : " + name);
    }
}
Source code activity_contoh_fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
    <fragment
        android:id="@+id/UtamaFragment"
        android:name="com.dsoccerupdate.contohfragment.UtamaFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>
Source code activity_contoh_rincianfragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView
        android:id="@+id/txtBuahbuahan"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="" />

</LinearLayout>
Source code activity_contoh_landscapefragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <fragment
        android:id="@+id/UtamaFragment"
        android:name="com.dsoccerupdate.contohfragment.UtamaFragment"
        android:layout_width="150dp"
        android:layout_height="match_parent" />
    
    <fragment
        android:id="@+id/rincianFragment"
        android:name="com.dsoccerupdate.contohfragment.RincianFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>
Basic, Android, Studio, Tutorial, interaction, fragment

Basic, Android, Studio, Tutorial, interaction, fragment

Results of the project ContohFragment display will differ depending on the orientation of which is used, if it is run with the vertical orientation of the home screen will then display extends from top to bottom, whereas if it is run with a horizontal orientation, the home screen will display extends from left to right. Similarly, android tutorial studio for a basic level of interaction between the fragment, the next article I will discuss about what is Google Admob as well as how to create an ad through Google Admob.

Basic Android studio tutorial: to know the interaction between the fragment Rating: 4.5 Diposkan Oleh: Unknown

0 comments:

Post a Comment