Google Search

Showing posts with label ANDROID ADVANCE TOPIC. Show all posts
Showing posts with label ANDROID ADVANCE TOPIC. Show all posts

How To Sort Month Name Using comparator Interface

==>java Provide comparator and Coprator interface. this interface used to sort a data.
Here i used Comparator Interface to Sort A Month Name.

==>This Interface is very useful when ever i want to Compare two String Object .

Main.xmlfile
 <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.college.sortmonthname.MainActivity">

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="monthname"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>


MainActivity.java
package com.college.sortmonthname;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    TextView name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        name = (TextView) findViewById(R.id.tv_name);
        List<String> month = Arrays.asList("Feb", "Jul", "Jan", "Apr", "Dec",
 "Aug", "Oct", "May", "Sep", "Nov", "Jun", "Mar");
        final Comparator<String> compare = new Comparator<String>() {

            public int compare(String o1, String o2) {

              SimpleDateFormat sdate = new SimpleDateFormat("MMM");
              Date s1 = null;
              Date s2 = null;
              try {
                  s1 = sdate.parse(o1);
                  s2 = sdate.parse(o2);
              } catch (ParseException e) {
                  e.printStackTrace();
              }
              return s1.compareTo(s2);
          }
      };

      Collections.sort(month, compare);
      name.setText(String.valueOf(month));
   }
}

output
Android Sort using Collection

 

How To Add Fragment at Runtime In Activity

-->Hello Friends Today i post one important topic is Fragment. In this Post you Can easily Learn How To Add Fragment At runtime  in our Activity.

-->Fragment is one of the most important topic  in android.whenever i used Fragment So no need to Handle our Activity OnBackPress.

-->in android used Fragment when multiple View or Activity Can Available our Project.


FirstFragment.xmlfile  


<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:background="#da6f66">

 <TextView  android:gravity="center" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:layout_centerHorizontal="true" 
 android:layout_centerVertical="true" 
 android:text="@string/text1" 
 android:textAllCaps="false" 
 android:textColor="@color/text_btn" 
 android:textSize="20dp" />

</RelativeLayout>

SecondFragment.xmlfile  


<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:background="#371f4d">

    <TextView 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true" 
 android:text="@string/text2" 
 android:textAllCaps="false" 
 android:gravity="center" 
 android:textColor="@color/text_btn" 
 android:textSize="20dp" />

</RelativeLayout>

Main.XmlFile 

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout 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:layout_width="match_parent" 
 android:layout_height="match_parent"
     android:background="#79efb0" 
 tools:context="com.college.runtimeaddfragmentdemo.MainActivity">

    <LinearLayout 
 android:id="@+id/ll_first" 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:weightSum="2">

        <Button 
 android:id="@+id/btn_frag1" 
 android:layout_width="0dp" 
 android:layout_height="wrap_content" 
 android:layout_margin="5dp" 
 android:layout_weight="1" 
 android:background="@color/back_btn" 
 android:text="@string/btn1" 
 android:textAllCaps="false" 
 android:textColor="@color/text_btn" />

        <Button 
 android:id="@+id/btn_frag_two" 
 android:layout_width="0dp" 
 android:layout_height="wrap_content" 
 android:layout_margin="5dp" 
 android:layout_weight="1" 
 android:background="@color/back_btn" 
 android:text="@string/btn2" 
 android:textAllCaps="false" 
 android:textColor="@color/text_btn" />
    </LinearLayout>

    <FrameLayout 
 android:id="@+id/ff_layout" 
 android:layout_width="wrap_content" 
 android:layout_height="200dp" 
 android:layout_below="@+id/ll_first" 
 android:orientation="horizontal">

        <FrameLayout 
 android:id="@+id/ll_container_first" 
 android:layout_width="match_parent" 
 android:layout_height="250dp" 
 android:orientation="horizontal"/>


    </FrameLayout>

    <FrameLayout 
 android:id="@+id/ll_container_second" 
 android:layout_width="match_parent" 
 android:layout_height="200dp" 
 android:layout_below="@id/ff_layout">
 </FrameLayout>
</RelativeLayout>
 
String.xmlfile 


<resources>
    <string name="app_name">RuntimeAddFragmentDemo</string>
    <string name="btn1">Load First fragment</string>
    <string name="btn2">Load Second fragment</string>
    <string name="text1">First Fragment</string>
    <string name="text2">Second Fragment</string>
</resources>


color.xmlfile


<?xml version="1.0" encoding="utf-8"?><resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
    <color name="text_btn">#FFFFFF</color>
    <color name="back_btn">#c261ec</color>
    <color name="text_textview">#a42395</color>
</resources>

First_Fragment.java

package com.college.runtimeaddfragmentdemo;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/** * Created by admin on 04-05-2017. */
public class First_Fragment extends Fragment {
    @Nullable    @Override    public View onCreateView
(LayoutInflater inflater, @Nullable ViewGroup 
container, Bundle savedInstanceState)
 {
        View v = inflater.inflate(R.layout.fragmet_first, null);
        return v;
    }
}

Second_Fragment.java
package com.college.runtimeaddfragmentdemo;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/** * Created by admin on 04-05-2017. */
public class Second_Fragment extends Fragment {
    @Nullable    @Override    public View onCreateView
(LayoutInflater inflater, @Nullable ViewGroup container,
 Bundle savedInstanceState) 
{
        View v = inflater.inflate(R.layout.fragment_second, null);
        return v;
    }
}
 MainActivity.java

package com.college.runtimeaddfragmentdemo;

import android.app.FragmentManager;
import android.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 {
    Button first_frag, second_frag;

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        first_frag = (Button) findViewById(R.id.btn_frag1);
        second_frag = (Button) findViewById(R.id.btn_frag_two);

        first_frag.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction
                 = fragmentManager.beginTransaction();
                First_Fragment first = new First_Fragment();
                fragmentTransaction.add(R.id.ll_container_first, first, "FIRST");
                fragmentTransaction.commit();

            }
        });
        second_frag.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction
                 = fragmentManager.beginTransaction();
                Second_Fragment second = new Second_Fragment();
                fragmentTransaction.add(R.id.ll_container_first, second, "SECOND");
                fragmentTransaction.commit();

            }
        });
    }
}

output
 
Android Fragment

Add Android fragment


What is Content Provider



A content provider manage access to a structure state a data.Content providers are standard inteface that process with the data in one process with the code running in another process.

When we want to Access the data we need to acess an object of content provider class.

The content resolver object communicate with the provider object and instance of a class implements content provider.

The provider object request the data request from the clientvand perform requested action and return the result.