Saturday, 15 October 2016

MultiAutoCompleteTextView example in android studio

Do the following in sequence:-
Step 1:- activity_main.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"
    android:layout_margin="10dp"
    android:gravity="center_vertical">
   <TextView
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="Select City:-"
       android:textSize="30sp"
       android:textColor="@android:color/black"/>

   <MultiAutoCompleteTextView
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:id="@+id/multilist"
       android:layout_marginTop="16dp"
       android:singleLine="true"/>
   <Button
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="Submit"
       android:layout_marginTop="30dp"
       android:onClick="submit"/>
</LinearLayout>

Step 2:- MainActivity.java
package supriya.com.multiautocompletetextview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.MultiAutoCompleteTextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    MultiAutoCompleteTextView completeTextView;
    String[] list={"Hyderabad","Delhi","Goa","Patna","Telangana","Jaipur"};


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

        completeTextView=(MultiAutoCompleteTextView)findViewById(R.id.multilist);
        completeTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
        ArrayAdapter<String> adp=new ArrayAdapter<String>(getApplicationContext(),R.layout.multitextview_design,list);
        completeTextView.setThreshold(1);
        completeTextView.setAdapter(adp);
       completeTextView.setDuplicateParentStateEnabled(false);

    }
    public void submit(View v){
        completeTextView.setText("");
        Toast.makeText(getApplicationContext(),"Submitted",Toast.LENGTH_LONG).show();
    }
}
Step 2:- multitextview_design.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="?attr/spinnerDropDownItemStyle"
    android:singleLine="true"
    android:layout_width="match_parent"
    android:layout_height="?attr/dropdownListPreferredItemHeight"
    android:ellipsize="marquee"
    android:textColor="#000000"
    android:text=""
    android:layout_margin="5dp"/>

Step 3:-Output


Sunday, 9 October 2016

Add Tabs in Android Studio | TabLayout


Requirements:-
1.MainActivity.java | activity_main.xml
2.OneFragment.java | fragment_one.xml
3.TwoFragment.java | fragment_two.xml
4.ThreeFragment.java | fragment_three.xml

Points nees to follow:-
1. open build_gradle-> add following
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.1.1'
    compile 'com.android.support:design:24.1.1'
}

Step1:-activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

   <android.support.design.widget.TabLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       app:tabMode="fixed"
       android:layout_alignParentBottom="true"
       app:tabGravity="fill"
       android:id="@+id/tabs"
       android:background="#FA5882"
       app:tabTextColor="#F6CED8"
       app:tabSelectedTextColor="#ffffff">
   </android.support.design.widget.TabLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager1"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v4.view.ViewPager>
</android.support.design.widget.CoordinatorLayout>

Step2:-MainActivity.java
package supriya.com.tabsexample;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

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

public class MainActivity extends AppCompatActivity {
    TabLayout tabLayout;
    ViewPager viewPager;

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tabLayout=(TabLayout)findViewById(R.id.tabs);
        viewPager=(ViewPager) findViewById(R.id.viewPager1);
        tabLayout.setupWithViewPager(viewPager);

        /*methods..*/        setupViewPager(viewPager);
    }

    public void setupViewPager(ViewPager v){
        ViewPagerAdapter adapter=new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new OneFragment(),"One");
        adapter.addFragment(new TwoFragment(),"Two");
        adapter.addFragment(new ThreeFragment(),"Three");
        v.setAdapter(adapter);
    }

    /*class*/    class ViewPagerAdapter extends FragmentPagerAdapter{
        List<Fragment> listFragment=new ArrayList<>();
        List<String>listString=new ArrayList<>();
        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override        public Fragment getItem(int position) {
            return listFragment.get(position);
        }

        @Override        public int getCount() {
            return listFragment.size();
        }
        public void addFragment(Fragment fragment, String title) {
            listFragment.add(fragment);
            listString.add(title);
        }
        public CharSequence getPageTitle(int position) {
            return listString.get(position);
        }
    }
}
Step3:-fragment_one
<?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:gravity="center_vertical">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="One"
        android:textSize="40dp"
        android:textStyle="bold"
        android:textAlignment="center"/>
</LinearLayout>
step4:-OneFragment.java
package supriya.com.tabsexample;

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;

/** * Created by Supriya on 10/9/2016. */public class OneFragment extends Fragment {

    public OneFragment() {
    }

    
    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_one,container,false);
    }
}
step5:-fragment_two
<?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:gravity="center_vertical">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Page Two"
        android:textSize="40dp"
        android:textStyle="bold"
        android:textAlignment="center"/>
</LinearLayout>
Step6:-TwoFragment.java
package supriya.com.tabsexample;

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;

/** * Created by Supriya on 10/9/2016. */
public class TwoFragment extends Fragment {

    public TwoFragment() {
    }
   

    @Nullable    @Override    
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_two,container,false);
    }
}
Step7:-fragment_three
<?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:gravity="center_vertical">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Page Three"
        android:textStyle="bold"
        android:textSize="30sp"
        android:textAlignment="center"/>

</LinearLayout>
Step8:-ThreeFragment.java
package supriya.com.tabsexample;

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;

/** * Created by Supriya on 10/9/2016. */public class ThreeFragment extends Fragment {

    public ThreeFragment() {
    }

    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_three,container,false);
    }
}
Output:-