Showing posts with label fragment. Show all posts
Showing posts with label fragment. Show all posts

Monday, August 7, 2017

Android Create A Dynamic Fragment On ViewPager

Today i will show you how to add dynamic fragment in viewpager. Dynamic means the size of fragment can change everytime.

Example :

In a list there are 10 items, it means there will 10 fragments appears in viewpager. If there are 5 items, then 5 fragments will appears.

Result

So for create a dynamic fragment on viewpager, please read carefully the following steps :

File Drawable


Inside folder drawable, create a new xml file that will used as indicator. Look the following image :

Indicator viewpager
Create the following xml file and place it insideres/drawable on your project :

selected_item.xml

nonselected_item.xml

Layouting


This project use 2 layouts (1 for activity and 1 for fragment):
  1. activity_main.xml 
  2. fragment_dinamis.xml

activity_main.xml

This file contains viewpager and its indicator. here is the code :

fragment_dinamis.xml

This file contains attributes that will appears on fragment. Attributes will appears on fragment depend on its data in List. In this proheject i use textview as fragment's marker number (you can change as you wish). Here is the code :

Code in Class Java


In this part, we will use 3 java classes, they are :
  1. DinamisFragment.java
  2. FragmentAdapter.java
  3. MainActivity.java
 
DinamisFragment.java

This class is a fragment class. We just create on fragment class, other fragments will create automatically depend on list size. Here is the code :

There is not special code in fragment above, just like usual. The increment of fragment will happen in its adapter.

Take a look at method setDetail(). This method used in adapter, Why?

Because data got from activity, then data will sent to adapter, from adapter data will set into its fragment.

FragmentAdapter.java

Take a look at attribute fragments. That attribute created as an array ([]), it because the size of data will dynamic, so the fragment also created dynamic (as an array).

Take a look again to method getItem(int position). There is a function : dinamisFragment.setDetail(items). Method setDetail() came from DinamisFragment.

MainActivity.java

In this file as usual is place where we set the data into a List, hosting the viewpager, etc. Here is the code :

That's all about how to create a dynamic fragment on viewpager. Hope it helps. Thank you.

Thursday, July 27, 2017

Tutorial How to move from Activity to Fragment and Fragment to Activity

For those who learning android programming for the first time, they will learn about android components like Java Class, XML, Activity, Fragment, Adapter, etc.


After that the next step is learn how to move from one Activity to another Activity using Intent. After learn about Activity, that they will learn about how to move from Activity to Fragment, and the Fragment to Activity

Move From Activity To Fragment


On this part, i will show you two ways how to move from Activity to Fragment:

1. Using Activity For Place a Fragment


We call the fragment in Activity's layout. So when you try to access a Fragment, just call the Activity where you set the fragment using Intent.

Here is the example :

Create a XML file name it as activity_main.xml

Layout activity_main.xml will used on class MainActivity. Code for class MainActivity.java looks like the follows :

Well, above is a class that we used as the first page when application is opened

Create a class Fragment and name it as NameFragment.java, Here is the code :

Next create layout for Fragment NameFragment name it as fragment_name.xml, here is the code :

OK, class Fragment already done. So how can we call Fragment NameFragment from MainActivity?

Create a new layout name it as activity_for_fragment_name.xml

Take a look at above code, inside tag there is an attribute class, that contains class Fragment. From where the class com.putuguna.fragmenttoactivity.NameFragment? that is the name of class Fragment that we create before, that is NameFragment.

com.putuguna.fragmenttoactivity.NameFragment contains name of package project and name of class.
com.putuguna.fragmenttoactivity : name of package project that we created
NameFragment : name of class Fragment that we created

Then create a java class for Activity NameFragment, name it as ActForFragmentNameActivity.java, the code is looks like the follows :

As we can see there is not special initialize code inside class MainActivity, it just initialize of its layout. But when this class is loaded, by using attribute class inside tagline fragment, then automatically its fragment will loaded


Calling Fragment NameFragment from MainActivity, you can do it look like the following code :

The part of the code above available on MainActivity, inside button onClick

2.  Without using Class Activity


If you create fragment without create its Activity, simple do like the following code :

Move from Fragment To Activity


If you try access an Activity from a Fragment, simply like the following code :

Yang perlu diperhatikan adalah :

.... new Intent(getActivity(), .....), getActivity() as Activity of Fragment, because on Intent's constructor required Activity and destination class , so we use getActivity() to take fragment's activity.
And also before you do startActivity(), first write getActivity() then startActivity().
That's all. Sorry for worst english. Thank you.

Thursday, January 12, 2017

Android Working With ButterKnife On Activity And Fragment

In this tutorial i going to share about one awesome library that help you easy manage your code (and i think you code more cleaner that before), the library called ButterKnife.

ButterKnife is a library that makes you more easy to cast your view. For example if you usually spent much time to find the id of your view (Red : findViewById(R.id.your_id)), this library will save a lot of your time. ButterKnife was developed by Jake Wharton.

ButterKnife

In normal code, you may do like the following code :

In code above, you will work twice, why?

First, you have to define your view : private TextView tvName; It's fine if your view is not too much, but if your view is much, you have to define you view first, after that you also have to define the id inside onCreate().

If you need more efficient work, you must use this library. As i wrote above, this library save your time much.

Library

Compile the library in your build.gradle (module : App) :

//add this line for butterknife
compile 'com.jakewharton:butterknife:7.0.1'
Usage

Define the ButteKnife inside the onCreate(), exactly below the setContentView() :

ButterKnife.bind(this);

Implementation on Activity

The code below will show you how to use butterknife to make you work more efficient. Here the code of MainActivity.java

@Bind = Use to define your view by its id. Make sure under @Bind is the view that owner of the id that has defined inside @Bind.

@OnClick = Use to the define the id that do onClickListener. In this project i use two buttons, so inside @OnClick there are an array id of the buttons.

if you just use one button (one OnClick), you don't need to use an array, just define like the following code snippet :

@OnClick(R.id.button_set_name)
public void onClickButton(){
       //Your Action
    }
}
Implementation on Fragment

Look at the code above, there is a different how to define ButterKnife. In fragment you have to write like this : ButterKnife.bind(this, view); . You have to include the view.

Hope the tutorial above is useful for you. Thank you.