battle programminga forum for elite programmerswith extreme will powerto sharpen theire skills
 
HomeCalendarFAQSearchMemberlistUsergroupsRegisterLog in
Share | 
 

 android studio execises

Go down 
AuthorMessage
Moti Barski

avatar

Posts : 238
Join date : 2011-08-02

PostSubject: android studio execises   Mon Jul 09, 2018 1:43 pm

image based dialer for old people with custom list view:

android manifest xml :
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CALL_PHONE" />

build.gradle module add :

implementation 'com.github.bumptech.glide:glide:4.7.1'


add dimens.xml to res, values :
Code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="edge">16dp</dimen>

</resources>

main xml :

Code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_margin="@dimen/edge"
    >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="My List"
        android:gravity="center"
        android:textSize="22sp"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Add Data"
        android:background="#009fff"
        android:textColor="#fff"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:id="@+id/btnAdd"
        />
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/myLst"/>
</LinearLayout>


partinias xml (for the custom list in the main xml):

Code:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="200dp">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageView
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher_background"
            android:layout_gravity="center"

            android:id="@+id/imgPerson"/>

        <Button
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:background="#009fff"
            android:textColor="#fff"
            android:text="DIAL"
            android:layout_gravity="center"

            android:id="@+id/btnDial"/>
    </LinearLayout>
</FrameLayout>


MyContacts java :


Code:
package com.justforum.yotamarker.lisbaseadapter;

import android.widget.ImageView;

public class MyContacts {
    private String url;
    private String myName;
    private String dialer;

    public MyContacts(String myName, String dialer, String url) {
        this.myName = myName;
        this.dialer = dialer;
        this.url=url;
    }

    public String getMyName() {
        return myName;
    }

    public String getDialer() {
        return dialer;
    }

    public String getUrl()
    {
        return url;
    }
}


MyAdapter java :


Code:
package com.justforum.yotamarker.lisbaseadapter;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v4.view.LayoutInflaterCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import java.util.List;
import java.util.zip.Inflater;

public class MyAdapter extends BaseAdapter {
    List<MyContacts> myList;
    Context context;

    public MyAdapter(List<MyContacts> myList, Context context) {
        this.myList = myList;
        this.context = context;
    }

    @Override
    public int getCount() {
        return myList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        /*
        TextView myText = new TextView(context);
        myText.setText(myList.get(position));
        myText.setTextSize(50);
        return myText;
        */

        View view = LayoutInflater.from(context).inflate(R.layout.partinias,null,false);

        Button btnDial = view.findViewById(R.id.btnDial);
        btnDial.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "you dial to:"+myList.get(position).getDialer(), Toast.LENGTH_SHORT).show();

                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:" + myList.get(position).getDialer()));
                if (ActivityCompat.checkSelfPermission(context,
                        Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    return;
                }
                context.startActivity(callIntent);

            }
        });
        ImageView personImage = view.findViewById(R.id.imgPerson);
        Glide.with(context).load(myList.get(position).getUrl()).into(personImage);
        return view;

    }
}


main java :

Code:
package com.justforum.yotamarker.lisbaseadapter;

import android.Manifest;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

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

public class MainActivity extends AppCompatActivity {
    Context context;
    Button btnAdd;
    ListView lstData;
    private static final int REQUEST_PHONE_CALL = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // ensure enablement of phone calls
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.CALL_PHONE)) {

            new AlertDialog.Builder(this)
                    .setTitle("Permission needed")
                    .setMessage("This permission is needed to make phone calls")
                    .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            ActivityCompat.requestPermissions(MainActivity.this,
                                    new String[]{Manifest.permission.CALL_PHONE}, REQUEST_PHONE_CALL);
                        }
                    })
                    .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    })
                    .create().show();

        } else {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.CALL_PHONE}, REQUEST_PHONE_CALL);

        }

        setPointer();
    }

    private void setPointer() {
        this.context=this;
        btnAdd=findViewById(R.id.btnAdd);
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        lstData=findViewById(R.id.myLst);
        MyAdapter myAdapter=new MyAdapter(getData(),context);
        lstData.setAdapter(myAdapter);
    }

    public List<MyContacts> getData() {
        List<MyContacts> returnList = new ArrayList<>();
        returnList.add(new MyContacts("sarval chan","+972-666666666","http://pacific.network/wp-content/uploads/2017/02/hadoken.jpg"));
        return returnList;
    }
}

scratch

_________________
MB over and out _______________________     
Back to top Go down
View user profile
 
android studio execises
Back to top 
Page 1 of 1
 Similar topics
-
» Trulia's Android Tablet Sweepstakes
» Golden Amazo Vs Super Android 17
» Dr. Dre Beats Giveaway *usa only*
» How to use ANT build to create HTML reports in Eclispe
» The Conduit heads to Android

Permissions in this forum:You cannot reply to topics in this forum
battle programmers alliance :: battle programming alliance :: chit chat-
Jump to: