Monday, 25 September 2017

Android Add Images On Spinner











1) MainActivity.xml

<?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"
    tools:context="spinnerimage.spinnerimage.MainActivity">


    <Spinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="88dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:background="@drawable/spinner"/>

</RelativeLayout>

2) MainActivity.java

public class MainActivity extends AppCompatActivity {

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

        ArrayList<ItemData> list=new ArrayList<>();
        list.add(new ItemData("Bank",R.drawable.bank_icon));
        list.add(new ItemData("Entertainment",R.drawable.entertainment_icon));
        list.add(new ItemData("Fashion",R.drawable.fashion_icon));
        list.add(new ItemData("Govt",R.drawable.govt_icon));
        Spinner sp=(Spinner)findViewById(R.id.spinner);
        SpinnerAdapter adapter=new SpinnerAdapter(this,
                R.layout.spinner_layout,R.id.txt,list);
        sp.setAdapter(adapter);
    }
}

3) ItemData.java

public class ItemData {
    String text;
    Integer imageId;
    public ItemData(String text, Integer imageId){
        this.text=text;
        this.imageId=imageId;
    }

    public String getText(){
        return text;
    }

    public Integer getImageId(){
        return imageId;
    }
}

4) SpinnerAdapter.java

public class SpinnerAdapter extends ArrayAdapter<ItemData> {
    int groupid;
    Activity context;
    ArrayList<ItemData> list;
    LayoutInflater inflater;
    public SpinnerAdapter(Activity context, int groupid, int id, ArrayList<ItemData>
            list){
        super(context,id,list);
        this.list=list;
        inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.groupid=groupid;
    }

    public View getView(int position, View convertView, ViewGroup parent ){
        View itemView=inflater.inflate(groupid,parent,false);
        ImageView imageView=(ImageView)itemView.findViewById(R.id.img);
        imageView.setImageResource(list.get(position).getImageId());
        TextView textView=(TextView)itemView.findViewById(R.id.txt);
        textView.setText(list.get(position).getText());
        return itemView;
    }

    public View getDropDownView(int position, View convertView, ViewGroup
            parent){
        return getView(position,convertView,parent);

    }
}

Thursday, 14 September 2017

Android Recyclerview Cards With Images In Gridview Style


#ref-menu

1)Cards.Java

class Cards {
    Integer Img;
    String category;


    public Cards(Integer img, String category ) {

        Img = img;
        this.category = category;
    }



    public String getCategory() {
        return category;
    }

    public int getImg() {
        return Img;
    }
}

2)Cards_Adapter.java

Class Cards_Adapter extends RecyclerView.Adapter<Cards_Adapter.ViewHolder>
{
    List<Cards> list;
    Context context;

    public Cards_Adapter(List<Cards> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_category_cards, parent, false);
        return new Cards_Adapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
       Cards Cards = list.get(position);
        final int img = Cards.getImg();
        final String categiry = Cards.getCategory();

        holder.sms_cat_img.setImageResource(img);
        holder.sms_category.setText(categiry);

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        ImageView sms_cat_img;
        TextView sms_category;
        public ViewHolder(View itemView) {
            super(itemView);

            sms_cat_img = (ImageView)itemView.findViewById(R.id.sms_cat_img);
            sms_category = (TextView) itemView.findViewById(R.id.sms_category);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, MainActivity.class);
                    Bundle extras = new Bundle();
                    extras.putString("category_msg",sms_category.getText().toString());
                    intent.putExtras(extras);
                    view.getContext().startActivity(intent);
                }
            });
        }
    }
}

3)Cards.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="wrap_content"
    android:background="@drawable/card_shadow">


<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/card_shadow">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="@drawable/card_shadow">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:id="@+id/sms_cat_img"
            android:padding="10dp"
            android:src="@drawable/bank_icon"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:id="@+id/sms_category"
            android:text="Category Name"
            android:textSize="18dp"
            android:textColor="@color/black"
            android:textStyle="bold"/>

    </LinearLayout>



</android.support.v7.widget.CardView>
</LinearLayout>

 4) MyData.Java

public class MyData {
    static String[] nameArray = {"Property","Offers","Travel","Self-Help","GOVT","Bank","Investment",
            "Entertainment","Fashion","OTP","Wallet","Income Tax","Insurance", "Uncategorized"};

    static Integer[] drawableArray = {
            R.drawable.property_icon,
            R.drawable.offers_icon,
            R.drawable.travel_icon,
            R.drawable.self_help_icon,
            R.drawable.govt_icon,
            R.drawable.bank_icon,
            R.drawable.investment_icon,
            R.drawable.entertainment_icon,
            R.drawable.fashion_icon,
            R.drawable.otp_icon,
            R.drawable.wallet_icon,
            R.drawable.tax_icon,
            R.drawable.insurance_icon,
            R.drawable.uncategorized};

}

5) MainActivity.xml

<?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"
    tools:context="sms.smsrec.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/sms_category_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        >
    </android.support.v7.widget.RecyclerView>

</RelativeLayout>


5)MainActivity.java

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private RecyclerView.Adapter adapter;
    private List<Cards> Cardss;
    private RecyclerView.LayoutManager mLayoutManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        recyclerView = (RecyclerView) findViewById(R.id.sms_category_recycler_view);
        recyclerView.setHasFixedSize(true);

       //  To Set Gridview Style
        mLayoutManager = new GridLayoutManager(getApplicationContext(),2);
        recyclerView.setLayoutManager(mLayoutManager);
        Cardss = new ArrayList<>();
        SMS_Category();
    }

    private void SMS_Category()
    {
        for (int i = 0; i < MyData.nameArray.length; i++) {
            Cardss.add(new Cards(
                    MyData.drawableArray[i],
                    MyData.nameArray[i]
            ));
        }

        adapter = new Cards_Adapter(Cardss, getApplicationContext());
        recyclerView.setAdapter(adapter);
    }
}

Friday, 8 September 2017

Android Drag And Drop Image And Buttons



1)  MainActivity.xml

<?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"
    tools:context="sms.moveimage.MainActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"
        android:id="@+id/image"/>


</RelativeLayout>


2)  MainActivity.java

import android.graphics.PointF;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    ImageView image;
    PointF DownPT = new PointF(); // Record Mouse Position When Pressed Down
    PointF StartPT = new PointF(); // Record Start Position of 'image'

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

        image = (ImageView)findViewById(R.id.image);

        image.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int eid = event.getAction();
                switch (eid)
                {
                    case MotionEvent.ACTION_MOVE :
                        PointF mv = new PointF( event.getX() - DownPT.x, event.getY() - DownPT.y);
                        image.setX((int)(StartPT.x+mv.x));
                        image.setY((int)(StartPT.y+mv.y));
                        StartPT = new PointF( image.getX(), image.getY() );
                        break;
                    case MotionEvent.ACTION_DOWN :
                        DownPT.x = event.getX();
                        DownPT.y = event.getY();
                        StartPT = new PointF( image.getX(), image.getY() );
                        break;
                    case MotionEvent.ACTION_UP :
                        // Nothing have to do
                        break;
                    default :
                        break;
                }
                return true;
            }
        });


    }

}

Android Studio - Get Current Latitude And Longitude

1. Add this dependencies in your gradle file     implementation 'com.google.android.gms:play-services-location:18.0.0' 2. Add this p...