battle programmers alliance

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

Display results as :
 
Rechercher Advanced Search
Latest topics
» android mobile app dev APK
Thu May 17, 2018 6:22 am by Moti Barski

» links/suggestions to become a programmer
Mon May 07, 2018 11:01 pm by Moti Barski

» java eclipse oxygen grimoire
Thu May 03, 2018 8:24 pm by Moti Barski

» forum logo by tf monkey
Wed May 02, 2018 10:17 pm by Moti Barski

» java and MySQL
Mon Apr 30, 2018 3:02 pm by Admin

» mini A.I puzzles
Mon Apr 23, 2018 11:38 am by Moti Barski

» java convert string to unicode
Sun Mar 25, 2018 8:40 pm by Admin

» problem java dynamic web
Sat Mar 24, 2018 6:10 am by Admin

» auto programming dev
Wed Mar 14, 2018 8:42 pm by Admin

May 2018
SunMonTueWedThuFriSat
  12345
6789101112
13141516171819
20212223242526
2728293031  
CalendarCalendar
Social bookmarking
Social bookmarking digg  Social bookmarking delicious  Social bookmarking reddit  Social bookmarking stumbleupon  Social bookmarking slashdot  Social bookmarking yahoo  Social bookmarking google  Social bookmarking blogmarks  Social bookmarking live      

Bookmark and share the address of battle programmers alliance on your social bookmarking website
Share | 
 

 android mobile app dev APK

Go down 
AuthorMessage
Admin
Admin
avatar

Posts : 108
Join date : 2011-08-01

PostSubject: android mobile app dev APK   Tue May 01, 2018 10:23 pm



1 android studio
2 JDK
3A in android studio - virtual mobile machine :
tool strip, tools, AVD manager, in the now appeared window : +create device, select devices with
the google play store symbol, next, select version : oreo (download), wait... wait, wait some more

3B tool strip, tools, SDK manager, SDK platforms tab : those are the SDKs your app will support
choose : 4.3 and above: corresponding to API lvl : 15,18,25,27 those are the criticle ones.

3c choose the SDK tools tab, check the : sdk build tools, android emulator, android sdk platform tools,
android sdk tools, google play servicesgoogle play APK, google play licencing library, instant apps deployment SDK
intel x86 emulator

3d : click apply, wait.
4 alternative to VM : GenyMotion
it simulates physical devices as if they are actually connected to your machine.
so when you run the app it will show on connected devices.
https://www.genymotion.com/download/
choose the installation with VirtualBox
5 install Vysor to cast your android screen on to the computer
7 enable developer mode & USB debugging mode on your phone :
open your mobil device for developing search the walkthrough for your phone.
mostly it is : all apps, settings, build number, click build number 7 times, rego to
settings, developer options, enable debugging and usb debugging.

on android studio, tool strip, file, settings, appearance (for IDE visual customization)

8 enable virtual devices :
windows key + R
type msinfo32
search the field with hyper v virtualization, this must be enabled Fix VT-x is disabled in BIOS :

press and hold shift key and restart your computer
choose trouble shoot, advanced options, UEFI firmware options, restart.
choose bios settings, go to advanced tab, enable virtualization technology, exit with save changes.

9 with android studio some of the files it downloads need to be installed externally.
install : intelhaxm-android from : "C:\Users\Lenovo\AppData\Local\Android\Sdk\extras\intel\Hardware_Accelerated_Execution_Manager"
Lenovo = comuter name
(HAXM). now the emulator should work.

drunken

_________________
kurayami no kagi  _____    
Back to top Go down
View user profile https://yotamarker.justforum.net
Moti Barski

avatar

Posts : 118
Join date : 2011-08-02

PostSubject: android studio hello world   Tue May 08, 2018 8:15 pm


if you leave the project name as the default exercise it will not be uploadable to the google play store.

in the solution explorer window :
app :
java : where your activity code
res : layout : the activities xml files (how the app windows look (widgets))


the xml can be manipulated by drag droping in the design tab or with the text tab :
Code:
<?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=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:onClick="TestHelloWorld"
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:layout_marginStart="63dp"
        android:text="Button" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="16dp"
        android:layout_marginTop="81dp"
        android:ems="10"
        android:inputType="textPersonName" />

</RelativeLayout>
notice you need to change the 2nd line to RelativeLayout as you see above and
it automatically changes the corresponding closing tag.
also notive I added a btn and a textbox.

when you add in the btn the line :
android:onClick="TestHelloWorld"
it offers you to generate onClick event code in the X activity (main activity here)
in the java folder, 1st folder main activity make the code :
Code:
package com.example.lenovo.myapplication1;

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

public class MainActivity extends AppCompatActivity {
    EditText t1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toast.makeText(getApplicationContext(), "moti", Toast.LENGTH_LONG).show();
        t1 = findViewById(R.id.editText); // links var to textbox
    }
// btn click event :
    public void TestHelloWorld(View view) {
        String str1 = t1.getText().toString();
        Toast.makeText(this,"moti rulz " + str1,Toast.LENGTH_LONG).show();
    }
}
run the code.

in the next code I harness the button and make its text display it's click count :
Code:
package com.example.lenovo.myapplication1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    EditText t1;
    Button btn1;
    int c =0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toast.makeText(getApplicationContext(), "moti", Toast.LENGTH_LONG).show();
        t1 = findViewById(R.id.editText);
        btn1 = findViewById(R.id.button); // btn name id = button
    }

    public void TestHelloWorld(View view) {
        String str1 = t1.getText().toString();
        Toast.makeText(this,"moti rulz " + str1,Toast.LENGTH_LONG).show();
        c++;
        String sTemp = c + "";
        btn1.setText(sTemp);
    }
}
sometimes the IDE will mark some code with red as an error cause it needs to import stuff or what ever
so press alt + enter to fix it.

to create classes right click the solution explorer window, new, java class
press alt + insert to add setters getters and constructores geek

_________________
MB over and out
Back to top Go down
View user profile
Moti Barski

avatar

Posts : 118
Join date : 2011-08-02

PostSubject: changing android studio theme   Tue May 08, 2018 9:56 pm


_________________
MB over and out
Back to top Go down
View user profile
Moti Barski

avatar

Posts : 118
Join date : 2011-08-02

PostSubject: controllers (activeX)   Sun May 13, 2018 6:27 pm

the following app builds a toast message using radio group buttons +
pile of check boxes.
the cassier will select his problem (radio)
the cassier will select person to summon (check box)
a message is built accordingly
the controllers (radio group, check boxes are reset)

xml design (solution explorer, res folder, layout):
Code:
<?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=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/txtName"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <!-- this is a comment -->
    <!-- android:layout_below = set controller location -->
    <!--android:id = controller name-->
    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/radioGroup"
        android:layout_below="@id/txtName">

        <RadioButton
            android:id="@+id/rbChange"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="need change" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="permission to return product"
            android:id="@+id/rbReturn"/>

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="else"
            android:id="@+id/rbElse"/>
    </RadioGroup>
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="send to vice"
        android:id="@+id/chkVice"
        android:layout_below="@id/radioGroup"/>
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="send to manager"
        android:id="@+id/chkManager"
        android:layout_below="@id/chkVice"/>
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="send to supervisor"
        android:id="@+id/chkSupervisor"
        android:layout_below="@id/chkManager"/>
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="send to coWorker"
        android:id="@+id/chkTall"
        android:layout_below="@id/chkSupervisor"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="summon"
        android:id="@+id/btnOrder"
        android:textAllCaps="false"
        android:layout_below="@id/chkTall"
        android:layout_centerHorizontal="true"
        android:onClick="summon"
        />
</RelativeLayout>
main activity (fire up when the app starts) :
(solution explorer, java folder, 1st sub folder):


Code:
package com.example.lenovo.cassiersummoner;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    Button btnCbOrder;
    RadioGroup radioCbGroup;

    TextView txtCbName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnCbOrder = findViewById(R.id.btnOrder);
        radioCbGroup = findViewById(R.id.radioGroup);
        txtCbName = findViewById(R.id.txtName);
    }
    public void summon(View view) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("Hello, " + "\n");
        buffer.append(((CheckBox)(findViewById(R.id.chkManager))).isChecked() ? "\nmanager" : "");
        buffer.append(((CheckBox)(findViewById(R.id.chkVice))).isChecked() ? "\nvice" : "");
        buffer.append(((CheckBox)(findViewById(R.id.chkSupervisor))).isChecked() ? "\nsupervisor" : "");
        buffer.append(((CheckBox)(findViewById(R.id.chkTall))).isChecked() ? "\nworker" : "");
        buffer.append(" " + getSummon());
        Toast.makeText(this,buffer.toString() ,Toast.LENGTH_LONG).show();
        resetForm();
    }
    public void resetForm(){
        ((CheckBox)(findViewById(R.id.chkManager))).setChecked(false);
        ((CheckBox)(findViewById(R.id.chkSupervisor))).setChecked(false);
        ((CheckBox)(findViewById(R.id.chkTall))).setChecked(false);
        ((CheckBox)(findViewById(R.id.chkVice))).setChecked(false);
        radioCbGroup.clearCheck();
        txtCbName.setText("");
    }

    public String getSummon(){
        switch (radioCbGroup.getCheckedRadioButtonId()){
            case R.id.rbChange:
                return "change needed";
            case R.id.rbReturn:
                return "customer wants to return a product";
            case R.id.rbElse:
                return "need you at the register";
            default:
                return "test message";
        }
    }
}

_________________
MB over and out
Back to top Go down
View user profile
Moti Barski

avatar

Posts : 118
Join date : 2011-08-02

PostSubject: spinner controller   Sun May 13, 2018 8:13 pm



spinner controller (see comments for Xplnation)

xml design :
Code:
<?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=".MainActivity">

    <Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:id="@+id/spnNames"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btnAdd"
        android:text="Add"
        android:textAllCaps="false"
        android:layout_below="@id/spnNames"
        android:layout_alignParentRight="true"
        android:onClick="newName"
        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtName"
        android:layout_below="@id/spnNames"
        android:hint="Enter Name..."
        android:layout_toLeftOf="@id/btnAdd"
        android:textAlignment="center"/>

</RelativeLayout>

main activity :
Code:
package com.example.lenovo.spinner;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

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

public class MainActivity extends AppCompatActivity {
    // shadow clone the controllers declaration
    Spinner spnCbNames;
    EditText txtCbName;
    List<String> lstNames;
    ArrayAdapter<String> arrayAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lstNames = new ArrayList<>();
        initControllers(); // connect shallow clones to controllers
        arrayAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_dropdown_item,
                lstNames); // link spinner to its components :
        spnCbNames.setAdapter(arrayAdapter);
        // get the selected item
        http://spnCbNames.getSelectedItem();
        // listener - to perform an action when item is selected
        spnCbNames.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this,
                        lstNames.get(position), Toast.LENGTH_SHORT).show();
                // lstNames.get(position) returns the item itself not a position
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    private void initControllers(){
        spnCbNames = findViewById(R.id.spnNames);
        txtCbName = findViewById(R.id.txtName);
    }

    public void newName(View view) {
        // btn on click event summoned from the xml file of the activity
        lstNames.add(txtCbName.getText().toString());
        txtCbName.setText("");
        arrayAdapter.notifyDataSetChanged(); // updates the spinner organs
    }
}

_________________
MB over and out
Back to top Go down
View user profile
Moti Barski

avatar

Posts : 118
Join date : 2011-08-02

PostSubject: change the apps background picture using code   Mon May 14, 2018 7:08 am



xml design code (activity_main.xml):
Code:
<?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=".MainActivity"
    android:id="@+id/mainActivityLayout"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</RelativeLayout>

notice in the above code I named the layout :
android:id="@+id/mainActivityLayout"
and the 1st line was changed to : <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

main activity code (mainActivity.java):
Code:
package com.example.lenovo.screenbg;

import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RelativeLayout;

public class MainActivity extends AppCompatActivity {
    RelativeLayout r1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        r1 = findViewById(R.id.mainActivityLayout);
        r1.setBackgroundDrawable(ContextCompat.getDrawable(this,R.drawable.aoitori));
    }
}

finally the aoitori.jpg picture was added to the res, drawable in the solution explorer window

_________________
MB over and out
Back to top Go down
View user profile
Moti Barski

avatar

Posts : 118
Join date : 2011-08-02

PostSubject: simple list view rapid walkthrough   Tue May 15, 2018 7:38 pm

simple list view :

xml
Code:
<?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=".MainActivity">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtItem"
        android:textAlignment="center"
        android:hint="enter Item"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="save"
        android:textAllCaps="false"
        android:id="@+id/btnSave"
        android:layout_below="@id/txtItem"
        android:layout_centerHorizontal="true"
        android:onClick="newItem"
        />
    <!--Divider code in ListView-->
    <ListView
        android:id="@+id/simpleListView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="#f00"
        android:dividerHeight="1dp"
        android:layout_below="@id/btnSave"
        />

</RelativeLayout>
activity code :
Code:
package com.example.lenovo.mylistview2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

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

public class MainActivity extends AppCompatActivity {
    private List<String> words;
    private ListView lv1;
    private EditText txtItem;
    private ArrayAdapter<String> arrayAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        words = new ArrayList<>();
        lv1 = findViewById(R.id.simpleListView);
        txtItem = findViewById(R.id.txtItem);
        arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_activated_1,words);
        lv1.setAdapter(arrayAdapter);
    }


    public void newItem(View view) {
// btn xml onClick event
        words.add(txtItem.getText().toString());
        txtItem.setText("");
// update list view (lv1) with the words in the list words
        arrayAdapter.notifyDataSetChanged();
    }
}



_________________
MB over and out
Back to top Go down
View user profile
Moti Barski

avatar

Posts : 118
Join date : 2011-08-02

PostSubject: android studio custom list view walkthrough   Thu May 17, 2018 6:22 am



polimarization !!!







exmple : contact list
each contact has : image name phone number

main activity xml :
Code:
<?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=".MainActivity">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtName"
        android:textAlignment="center"
        android:hint="Enter Item"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtPhone"
        android:textAlignment="center"
        android:layout_below="@id/txtName"
        android:hint="Enter Item"/>
    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/radioGroupImg"
        android:layout_below="@id/txtPhone"
        android:layout_centerHorizontal="true"
        android:orientation="horizontal">
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="work"
            android:id="@+id/rbWork"
            android:checked="true"/>
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="cell"
            android:id="@+id/rbCell"/>
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Home"
            android:id="@+id/rbHome"/>

    </RadioGroup>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save"
        android:textAllCaps="false"
        android:id="@+id/btnSave"
        android:layout_below="@id/radioGroupImg"
        android:layout_centerHorizontal="true"
        android:onClick="newContact"/>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lstItems"
        android:divider="#f00"
        android:dividerHeight="1dp"
        android:layout_below="@id/btnSave"/>

</RelativeLayout>

partinias xml = custom list view element inflation appearans:

Code:
<?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="wrap_content">

    <ImageView
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:id="@+id/imgContact"
        android:src="@mipmap/ic_launcher"
        android:layout_margin="5dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/lblName"
        android:layout_toRightOf="@id/imgContact"
        android:layout_toEndOf="@id/imgContact"
        android:text="Contact Name"
        android:textSize="16dp"
        android:layout_centerVertical="true"
        android:layout_marginLeft="8dp"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtContactName"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@id/imgContact"
        android:layout_toEndOf="@id/imgContact"
        android:visibility="invisible"
        android:layout_toLeftOf="@id/btnEdit"
        android:layout_toStartOf="@id/btnEdit" />

    <ImageButton
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:id="@+id/btnEdit"
        android:src="@drawable/edit"
        android:layout_toLeftOf="@id/btnDel"
        android:layout_centerVertical="true"
        android:visibility="invisible"
        />
    <ImageButton
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:id="@+id/btnDel"
        android:src="@drawable/del"
        android:layout_toLeftOf="@id/btnDone"
        android:layout_centerVertical="true"
        android:visibility="invisible"
        />
    <ImageButton
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:id="@+id/btnDone"
        android:src="@drawable/save"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:visibility="invisible"
        />


</RelativeLayout>

contact java class :

Code:
package com.justforum.yotamarker.contacts;

import android.support.annotation.DrawableRes;

public class Contact {

    private String name;
    private String phone;
    // Will limit the image variable to get only R.drawable.something as int value
    private @DrawableRes int image;

    public Contact(String name, int image, String phone) {
        this.name = name;
        this.image = image;
        this.phone = phone;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public @DrawableRes int getImage() {
        return image;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public void setImage(@DrawableRes int image) {
        this.image = image;
    }
}

regex java class

Code:
package com.justforum.yotamarker.contacts;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class regexifier {
    public static String regexChecker(String theRegex, String str2Check) {
        // You define your regular expression (REGEX) using Pattern
        Pattern checkRegex = Pattern.compile(theRegex);
        // Creates a Matcher object that searches the String for
        // anything that matches the REGEX
        Matcher regexMatcher = checkRegex.matcher(str2Check);
        // Cycle through the positive matches and print them to screen
        // Make sure string isn't empty and trim off any whitespace
        while (regexMatcher.find()) {
            if (regexMatcher.group().length() != 0) {
                return regexMatcher.group().trim();
//                System.out.println(regexMatcher.group().trim());
//                // You can get the starting and ending indexs
//                System.out.println("Start Index: " + regexMatcher.start());
//                System.out.println("Start Index: " + regexMatcher.end());
            }
        }
        return "err";
    }
}

example partinias java class (adjusted for the contact class):
fuses main xml's listview with partinias xml

Code:
package com.justforum.yotamarker.contacts;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

public class partinias extends ArrayAdapter {

    private ArrayList<Contact> arrayList;
    private Activity mActivity;
    public partinias(@NonNull Activity context, int resource, ArrayList<Contact> arrayList) {
        super(context, resource);
        this.arrayList = arrayList;
        this.mActivity = context;
    }

    // will help the ListView to know how many items need to be created
    @Override
    public int getCount() {
        return arrayList.size();
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View view, @NonNull ViewGroup parent) {

        LayoutInflater inflater = mActivity.getLayoutInflater();
        // attachToRoot - if the layout is inside the root layout, or in a different layout
        view = inflater.inflate(R.layout.partinias, parent, false);

        TextView lblCbName = view.findViewById(R.id.lblName);
        ImageView imgCbContact = view.findViewById(R.id.imgContact);

        Contact contact = arrayList.get(position);

        lblCbName.setText(contact.getName() + " " + contact.getPhone());
        imgCbContact.setImageDrawable(ContextCompat.getDrawable(mActivity, contact.getImage()));

        return view;
    }
}

main activity :

Code:
package com.justforum.yotamarker.contacts;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MainActivity extends AppCompatActivity {
    // Class Variables, will live through the Class life cycle.
    EditText txtCbName;
    EditText txtPhone;
    RadioGroup radioGroupCbImg;
    ListView lstCbContacts;
    ArrayList<Contact> contactsList;
    partinias adapter;
    int lastPosition = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Point to all ID's in XML, using variables
        initControllers();
        contactsList = new ArrayList<>();
        adapter = new partinias(this,
                R.layout.partinias, contactsList);
        lstCbContacts.setAdapter(adapter);
        setListViewListener();
    }



    public void initControllers(){
        // Assign the variables to the object using findViewById to find the object with ID
        txtCbName = findViewById(R.id.txtName);
        txtPhone = findViewById(R.id.txtPhone);
        radioGroupCbImg = findViewById(R.id.radioGroupImg);
        lstCbContacts = findViewById(R.id.lstItems);
    }


    public void setListViewListener(){
        lstCbContacts.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                // Will clear the last clicked item and return it back to normal;
                clearLastCheckedItem();
                ImageButton btnCbDel = view.findViewById(R.id.btnDel);
                ImageButton btnCbEdit = view.findViewById(R.id.btnEdit);
                ImageButton btnCbDone = view.findViewById(R.id.btnDone);
                btnCbDel.setVisibility(View.VISIBLE);
                btnCbEdit.setVisibility(View.VISIBLE);
                btnCbDone.setVisibility(View.VISIBLE);
                final EditText txtCbContactName = view.findViewById(R.id.txtContactName);
                final TextView lblCbName = view.findViewById(R.id.lblName);
                btnCbDel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        contactsList.remove(position);
                        adapter.notifyDataSetChanged();
                    }
                });
                btnCbEdit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        txtCbContactName.setText(lblCbName.getText().toString());
                        txtCbContactName.setVisibility(View.VISIBLE);
                        lblCbName.setVisibility(View.INVISIBLE);
                    }
                });
                btnCbDone.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String value = txtCbContactName.getText().toString();
                        String newphone = regexifier.regexChecker("([0][0-9]{9})",value);
                        value = value.substring(0,value.indexOf(" "));

                        if(!value.isEmpty()){
                            contactsList.get(position).setName(value);
                            contactsList.get(position).setPhone(newphone);
                        }
                        adapter.notifyDataSetChanged();
                    }
                });
                lastPosition = position;
            }
        });
    }

    // Check which Radio Button checked by the user
    public int chosenImage(){
        switch (radioGroupCbImg.getCheckedRadioButtonId()){
            case R.id.rbHome:
                return R.drawable.home;
            case R.id.rbCell:
                return R.drawable.phone;
            case R.id.rbWork:
                return R.drawable.work;
            default:
                return R.mipmap.ic_launcher;
        }
    }
    public void newContact(View view) {
        String phoneNumInput = regexifier.regexChecker("([0][0-9]{9})", txtPhone.getText().toString());
        if(phoneNumInput=="err"){
            Toast.makeText(this,"phone number must be 10 digits and start with 0", Toast.LENGTH_LONG).show();
            txtPhone.setText("");
        }else{
            contactsList.add(new Contact(txtCbName.getText().toString(), chosenImage(), phoneNumInput));
            txtCbName.setText("");
            txtPhone.setText("");
            adapter.notifyDataSetChanged();
        }
    }

    public void clearLastCheckedItem(){
        try{
            View lastChild = lstCbContacts.getChildAt(lastPosition);
            lastChild.findViewById(R.id.btnDel).setVisibility(View.INVISIBLE);
            lastChild.findViewById(R.id.btnEdit).setVisibility(View.INVISIBLE);
            lastChild.findViewById(R.id.btnDone).setVisibility(View.INVISIBLE);
            lastChild.findViewById(R.id.txtContactName).setVisibility(View.INVISIBLE);
            lastChild.findViewById(R.id.lblName).setVisibility(View.VISIBLE);

        }
        catch (NullPointerException e){
            e.printStackTrace();
        }

    }
}

_________________
MB over and out
Back to top Go down
View user profile
 
android mobile app dev APK
Back to top 
Page 1 of 1
 Similar topics
-
» [Hỏi đáp] Làm sao vào giao diện mobile trên pc
» Licensed VPS[sale/Rent]+UAE Unblock Mobile dialer
» UAE Anti blocking pc2phone & mobile dialer for Rent/Purchase
» VPS with relay server based PC2Phone & mobile dialer at $499
» Mobile dialer certified by NOIKA for sale/Rent

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