Saturday, 27 August 2016

Implementation Function Handler Android (show anything after a few second)



Implementation Function Handler Android (show anything after a few second)


Hello world. Today its my second post, ya i wanna show you how to implementation function handler on the android programming. If you don't know about handler, handler is like function that you can use, if you want to show anything, with period. For example i wanna show snackbar or anything after 3 second. Lets do it.

Ahha, i've handler function like this.

private void handlerFunction(final View v){
        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Snackbar.make(v, "Hi i am show after  3 second", 
                        Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        }, 3000);
    }

On this function, i just 3000 millisecond its mean 3 second waiting. And after that snackbar will show like this "Hi i am show after 3 second". Like this picture.


Implement Handler Function


If you want to show anything, you put inside run function. Just it. Write comment bellow if still confuse about my code, or comment on my Github page. I hope this is can help you little bit. Lol. Thanks.

Github page.

Share:

Create OnClick Function on The Android



Implementation onClick on the Android

Hello world. Today i wanna show you how to implementation onClick on the android programming. Basically onClick function can use for all widget in android. Something like Textview, Textfield, Button, etc. So on this post i will show you onClick just in Textview. Here we go.

This my basic layout, i call it fragment_one.xml.

<?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:layout_marginTop="@dimen/activity_horizontal_margin_twice">

    <TextView
        android:id="@+id/tvOne"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16dp"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:text="@string/fragmentOne"/>

    <View
        android:id="@+id/vOne"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/colorAccent"
        android:layout_marginTop="@dimen/activity_horizontal_margin"
        android:layout_below="@+id/tvOne"/>

    <TextView
        android:id="@+id/tvOnClick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16dp"
        android:layout_below="@+id/vOne"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:text="@string/onClick"/>

    <View
        android:id="@+id/vTwo"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/colorAccent"
        android:layout_marginTop="@dimen/activity_horizontal_margin"
        android:layout_below="@+id/tvOnClick"/>

</RelativeLayout>

and on this layout i've two textview that i will to implement click. And this is my function to implement my textview onClick.

private void onClick(View view){
        tvOne = (TextView) view.findViewById(R.id.tvOne);
        tvOnClick = (TextView) view.findViewById(R.id.tvOnClick);
        tvOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Snackbar.make(v, "Hi i am function onclick", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        tvOnClick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Snackbar.make(v, "Hi i am function onclick", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

On this function i just declare my text view and call setOnClickListener . And then i put snack bar to show "Hi i am function onClick". It would be like this one.


onClick implementation Function with Snackbar



You can write comment bellow or comment on the my Github page. And also you can download it on my Github page.


Share:

Build Menu Navigation Drawer Android


Android Menu Navigation Drawer

Hello world. Its night to build something little android project and i hope in the future it will be big project for all tricks or tutorial about android project. From now i will create a tutorial on this project. Its mean, for all tutorial will do on this project. And also i will post to my Github page. So you can post comment on my Github page or on my post by post. If you still confused about structure android project you can read on my post, this one.

For first post, i share about Navigation Drawer android. It's a something like menu navigation on android apps. On the navigation drawer, i do interaction one activity and many fragment. Lets do it.

First Activity, i have xml file and one activity. So this is my xml file, i call it activity_main.xml.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

As you can see, on this my xml file i have DrawerLayout, NavigationView, FragmentLayout, and Include. This file will be first show when app run. So we set first User Interface and Navigator User Interface. Include is another my xml file is app_bar_main. First UI it will will show like this. 


First UI Xml File



This is my app_bar_main.xml.

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
    tools:context="com.blogspot.dedyyuristiawan.androidstudioproject.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

On this file, i have CoordinatorLayout, AppBarLayout ( for put Toolbar) , and FloatingActionButton. This file it will be fist UI when you run app for first time. So for Navigation UI i put on the another xml file. Its Item menu on the folder menu, i call it activity_main_drawer.xml.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/fragmentOne"
            android:icon="@drawable/ic_menu_camera"
            android:title="Fragment One" />
        <item
            android:id="@+id/fragmentTwo"
            android:icon="@drawable/ic_menu_gallery"
            android:title="Fragment Two" />
        <item
            android:id="@+id/fragmentThree"
            android:icon="@drawable/ic_menu_slideshow"
            android:title="Fragment Three" />
        <item
            android:id="@+id/fragmentFour"
            android:icon="@drawable/ic_menu_manage"
            android:title="Fragment Four" />
    </group>

    <item android:title="Communicate">
        <menu>
            <item
                android:id="@+id/fragmentFive"
                android:icon="@drawable/ic_menu_share"
                android:title="Fragment Five" />
            <item
                android:id="@+id/fragmentQuit"
                android:icon="@drawable/ic_menu_send"
                android:title="Quit" />
        </menu>
    </item>

</menu>

So i have a lot item menu for navigation. and i have group for make different. It will be show like this.


Navigation UI


On this navigation, i put on the top is a header navigation and i write on this file xml. I call it nav_header_main.xml. 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/nav_header_height"
    android:background="@drawable/side_nav_bar"
    android:gravity="bottom"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:src="@android:drawable/sym_def_app_icon" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:text="Android Studio"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android.studio@android.com" />

</LinearLayout>

As you can see, i've Image and two text_view. You can imagine with Navigation UI Screen. And then as i told you before, i have activity and fragment. Navigation menu work on the one activity and a lot fragment. Its certain. This is my activity, i call it MainActivity.java. On this file will build work with xml, and make it happen like first UI. This is MainActivity.java. 

package com.blogspot.dedyyuristiawan.androidstudioproject;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    private Toolbar toolbar;
    private Fragment fragment = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setToolbar();
        showFAB();
        setDraawerLayout();
        setNavigation();
        setFirstFragment();
    }

    //function for set first fragment run
    private void setFirstFragment(){
        Fragment fragment = null;
        fragment = new FragmentOne();
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.frame_container, fragment).commit();
    }

    //private void set navigation
    private void setNavigation(){
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

    // function to set drawer layout
    private void setDraawerLayout(){
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();
    }

    // function to set toolbar
    private void setToolbar(){
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }

    //function to call fab
    private void showFAB(){
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    // function to detect back button, when user click button back
    // it will close drawer
    // and close app
    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    //function for create menu, on the left top tollbar
    // it declare from xml menu, folder menu
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    // function to create action click when user click menu on the menu folder
    // you can check on the menu folder
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    // function to create action click when user click navigation menu item
    // id it from menu folder
    // and also will change one fragment to another fragment, with replace
    // after change fargment will close drawer too
    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.

        int id = item.getItemId();
        //declare fragment
        Fragment fragment = null;

        //select fragment with id condition
        //set tittle bar
        //call fragment
        if (id == R.id.fragmentOne) {
            Log.d("fragmentOne Test","Works" );
            getSupportActionBar().setTitle(R.string.fragmentOne);
            fragment = new FragmentOne();

        } else if (id == R.id.fragmentTwo) {
            Log.d("fragmentTwo Test","Works" );
            getSupportActionBar().setTitle(R.string.fragmentOne);
            fragment = new FragmentOne();

        } else if (id == R.id.fragmentThree) {
            Log.d("fragmentThree Test","Works" );
            getSupportActionBar().setTitle(R.string.fragmentOne);
            fragment = new FragmentOne();

        } else if (id == R.id.fragmentThree) {
            Log.d("fragmentThree Test","Works" );
            getSupportActionBar().setTitle(R.string.fragmentOne);
            fragment = new FragmentOne();

        } else if (id == R.id.fragmentFour) {
            Log.d("fragmentFour Test","Works" );
            getSupportActionBar().setTitle(R.string.fragmentOne);
            fragment = new FragmentOne();

        } else if (id == R.id.fragmentFive) {
            Log.d("fragmentFive Test","Works" );
            getSupportActionBar().setTitle(R.string.fragmentOne);
            fragment = new FragmentOne();

        } else if (id == R.id.fragmentQuit){
            Log.d("fragmentQuit Test","Works" );
            getSupportActionBar().setTitle(R.string.fragmentOne);
            finish();
        }

        //interaction fragment, with replace
        //close navigator
        if (fragment != null) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.frame_container, fragment).commit();

            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            drawer.closeDrawer(GravityCompat.START);

        } else {
            // error in creating fragment
            Log.e("MainActivity", "Error in creating fragment");
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            drawer.closeDrawer(GravityCompat.START);
        }

        return true;
    }
}

On this file, i've to write comment on the function to so you can learn and understand it. The important is on the top function is a onCreate(Bundle savedInstanceState) . Its first function when app run. After that will call another function like this. 

setToolbar();
        showFAB();
        setDraawerLayout();
        setNavigation();
        setFirstFragment();

simple right? there is another java file, it's a fragment file. As you can see, activity will be make interaction with fragment see on the onNavigationItemSelected function. I can replace it with another function. But i create one fragment. haha its ok, you can create yourself. So this is my fragment class.

package com.blogspot.dedyyuristiawan.androidstudioproject;

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

/**
 * Created by dedyyuristiawan on 8/27/16.
 */
public class FragmentOne extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_one, container, false);

        return rootView;
    }
}

As you can see, on my fragment have xml too. And this is my xml file. I call it fragment_one.xml.

<?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">


    <TextView
        android:id="@+id/tvOne"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16dp"
        android:text="Hello World!"
        android:layout_centerInParent="true"/>

    <TextView
        android:id="@+id/txtLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tvOne"
        android:layout_centerInParent="true"
        android:textSize="16dp"
        android:text="@string/fragmentOne"/>

</RelativeLayout>

That's it. Android Navigator Apps. If you still have confuse about my code you can fill comment bellow, or just sent email to me (bellow my photo). Remember it will change when i write post. Cause all tutorial i will create on this project. If you want to see about previous code, you can search on the commit Github Page. And this is my project, Just download it on my Github page

Github page.

Share:

Monday, 22 August 2016

Moving From Odoo Developer to Android Developer



Moving Odoo to Android Developer


Hello world. Today i just wanna share about my life. Around 4 month ago, i did share about how to develop website with Odoo, but now mostly i will share about how to develop Android apps. It means, i will not share about the Odoo, yeah sometimes. Actually rather choose develop Android than Odoo, cause yesterday i join with company that working with Odoo beside that i have a lot knowledge about android than Odoo. And now i join with company that working with android apps. Yes, its waste if i not share about Android and Odoo.  So don't think i am weird cause for next time  i will mostly do share about develop  android. Ahha this is weird. Lol. 
Share:

Monday, 1 August 2016

How to Create Wizard on Odoo ?



https://dedyyuristiawan.blogspot.com/


Hello world. As i promise on this post How to Call Wizard from XML Button Odoo, i will share about wizard on Odoo. As you can see on previous post i just call wizard on the xml file. Now, i will share how to create wizard on Odoo. You know, wizard on Odoo is like pop up when in website, so in wizard data will save in memory, not in database Odoo. Usually we show wizard is for input additional data. So lets do it.

On this post, i just share wizard, if you want call wizard, you can read on this post How to Call Wizard From XML Button Odoo. So you can on my wizard.xml file bellow.

<?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data>
        <record id="dedy_yuristiawan_wiz" model="ir.ui.view">
            <field name="name">dedy.yuristiawan.wizard.name</field>
            <field name="model">dedy.yuristiawan.wizard</field>
            <field name="arch" type="xml">
                <form string="Hi i am wizard">
                    <group>
                        <field name="name"/>
                    </group>
                    <footer>
                        <button name="create_request" 
                            string="Finished" type="object" 
                            class="btn-primary"/>
                        <button string="Cancel" 
                            class="btn-default" 
                            special="cancel" />
                    </footer>
                </form>
            </field>
        </record>

        <record id="action_view_wizard" model="ir.actions.act_window">
            <field name="name">Hi i am a wizard</field>
            <field name="type">ir.actions.act_window</field>
            <field name="res_model">dedy.yuristiawan.wizard</field>
            <field name="view_type">form</field>
            <field name="view_mode">form</field>
            <field name="view_id" ref="dedy_yuristiawan_wiz"/>
            <field name="target">new</field>
        </record>
    </data>
</openerp>

Ass you can see, just 2 record on my xml file. On the bottom record i have name, type, model, view type, view mode, view_id, and target. The important is :

1. Type ir.actions.act_window it mean will show window like from view usually.

2. Res_model dedy.yuristiawan.wizard it's my model.

3. Target new it will show as new form, you can set current, it mean will show your current form.

4. View Id dedy_yuristiawan_wiz it will call top record as you can see. You know Odoo its run from Top to Bottom, That's why i put dedy_yuristiawan_wiz on the top (it mean, i make declaration first before i call it). 

5. On the Top record at  it look like similar with bottom record, i just add field, that i create on python file, and two button, button Cancel will dismiss this wizard and button Finish will call function create_request on my python file.

And this one, i call it python file.

from openerp import models, fields, api, _
from openerp.exceptions import UserError, ValidationError, Warning


class DedyYuristiawan(models.TransientModel):
    _name = "dedy.yuristiawan.wizard"
    
    name = fields.Char('Name', required=True)
    
    @api.multi
    def create_request(self):
        print "You click finish"

        return True

As you can see, the class have name models.TransientModel it mean wizard. And then i create field name that i call in  my xml file <field name="name"/> . Last i create function def create_request(self). That's i told you before, if want to call this function i create button Finish with this syntax <button name="create_request" string="Finished" type="object" button with name and type object.

That's it. So if want make wizard you can read first, you can create code python first and then xml code. Aha i forget something, Usually these file (python and xml) i put on folder Wizard, and the i declare on the __init__.py with include python file and then on __openerp__.py with this 'wizard/split_wo_wizard.xml' put on the top.

It real that's it. Aha. Just read first, if you still confuse about my code, you can write comment bellow. Thanks.
Share:

Friday, 29 July 2016

How to Inherit Function Odoo ?



https://dedyyuristiawan.blogspot.co.id


Hello wold. Today i got inspiration from my comment's. He just started develop Odoo. You know, actually i am so appreciate that, maybe we feel difficult to learn something new, and we don't have person to help us, just world wide web. I know, because for first time I learn Odoo, i feel so disappointed for myself, I just think 'i can't do it'. But i keep learn Odoo, cause i am still work with company use Odoo aha. But huyaa for time by time i can learn the process Odoo, new syntax from python, qweb, or maybe js, css, etc cause my background its not from website. And you know, now i am realise that how's can develop Odoo is easy, i am so realise that. Just little bit share my misery. Lol

So just go ahead right? today i am gonna share about how to inherit function Odoo, for example in this post i will inherit function create. Just for note, function create is automatically create when we develop view form or tree. Aha you know function create in base Odoo, it always have the name 'create'. And usually have one parameter is self or two parameter is self, vals (variable that we have). For example in Odoo base, we have create like this.

@api.model
def create(self, vals):
    .
    .
    .
    .
    return A

As you can see create have two variable, self and vals. And now i want to inherit that. see my code.

class AddFieldProduct (models.Model):
    _inherit = "product.template"    
 
    info = fields.Char('Info') 

    @api.model
    def create(self, vals):
        if self.info :
            self.info = 'is a field info'
        return super(AddFieldProduct, self).create(vals)

as you can see, the requirement is :

1. We need to create file something like me, i create file inherit_function.py and then we need to create class like 'class inherit_function (models.Model)' and type class 'model'.

2. Declaration class with _inherit = "product.template", it mean we want to inherit model product.product.

3. Write name and parameter, similar with function, see def create(self, vals) and add @api.model above the function.

4. Add what you what you want need. Like this one i just want to set field info is 'is a field info'. if self.info : self.info = 0

5. And last add return with super like this one, return super(inherit_function, self).create(vals). inherit_function is a my class, see on the top.

7. When we inherit create like that, we put there condition on the function, field info will save 'is a field info' in database. trust me.

6. That's it, easy right? 

So the conclusion is Semangat!! like my boss always say to me, when i got problem with my code. Lol


Share:

Wednesday, 27 July 2016

How to add or Ihherit Action on Odoo ?



https://dedyyuristiawan.blogspot.com
Action Odoo


Hello world. Today i've new knowledge about develop Odoo. do you ever see action in odoo, some thing like picture on top, beside print? its action, and now i will share how to inherit or add action on odoo. let's do this.

Actually its very simple, like you inherit field or another view. in this case, i just add xml file that inherit to parent. This is my xml file.

<record model="ir.values" id="set_efaktur">
    <field name="name">My Action</field>
    <field name="value" 
    eval="'ir.actions.act_window,' + str(ref('wizard_action'))" />
    <field name="key">action</field>
    <field name="model">dedy.yuristiawan</field>
</record>

You can read this description :

1. As you can see i set record in the ir.values. and then i put name My Action.
2. And then in the value i just set value, and eval ("'ir.actions.act_window,' + str(ref('wizard_action'))") it will be show my wizard. wizard_action is name of wizard template.
3. Next line i just declaration key with action.
4. And the last, this is really important, i put my action in the my custom model dedy.yuristiawan.
5. Just it

Huya, simple right? so this is post about inherit or add new action in Odoo, if you still confuse about my code you can fill comment bellow. I hope this is can help you. See yaa
Share:

Tuesday, 26 July 2016

How to get Data from JSON Show on List Android



https://dedyyuristiawan.blogspot.com

Hello world, today I am gonna share about get data from the JSON. In this case I will use volley library for make easy develop, when i get data from JSON. Actually this is test for myself, to remain me about android code. And this is my list :

1. A Activity have one button, two edit text.
2. When your click button will move to another activity, I call it Activity B, Activity B will show list data from JSON.
3. When you click your list, it will will be back to Activity B with fill two EditText.
4. Just it.

Hoya, in this tutorial i use JSON from androidhive. You can check here. He share about JSON too but in this post i will make it complete :D.

Now you follow my step by step :

1. Activity A 

In this Activity A I just put One Button and with Two EditText this is my activity_main.xml file :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/btnArrayRequest"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:text="Pick Doctor" />

    <EditText
        android:id="@+id/ETCode"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:layout_below="@id/btnArrayRequest"
        />

    <EditText
        android:id="@+id/ETName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:layout_below="@id/ETCode"
        />

</RelativeLayout>

and this is my MainActivity.java file :


public class MainActivity extends Activity {
 
 private static String TAG = MainActivity.class.getSimpleName();
 private Button btnMakeArrayRequest;
 private EditText ETCOde;
 private EditText ETName;

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

  btnMakeArrayRequest = (Button) findViewById(R.id.btnArrayRequest);
  ETCOde = (EditText)findViewById(R.id.ETCode);
  ETCOde.setEnabled(false);
  ETName = (EditText)findViewById(R.id.ETName);
  ETName.setEnabled(false);
  btnMakeArrayRequest.setOnClickListener(new View.OnClickListener() {

   @Override
   public void onClick(View v) {
    Intent intent = new Intent(MainActivity.this,
MainActivityList.class);
    startActivity(intent);
   }
  });
  if (getIntent() != null){
   if (getIntent().hasExtra("Code")) {
    ETCOde.setText(getIntent().getStringExtra("Code"),                                 TextView.BufferType.EDITABLE);
   }
   if (getIntent().hasExtra("Name")) {
    ETName.setText(getIntent().getStringExtra("Name"),                                 TextView.BufferType.EDITABLE);
   }
  }
 }
}

See on my first list, function button its for move to Activity B. You can see on this code :

btnMakeArrayRequest.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
      Intent intent = new Intent(MainActivity.this,
                        MainActivityList.class);
      startActivity(intent);
   }
});

And then as you can see in the third my list it must fill data from list Activity B.  I just use this code:

if (getIntent() != null){
    if (getIntent().hasExtra("Code")) {
      ETCOde.setText(getIntent().getStringExtra("Code"), 
      TextView.BufferType.EDITABLE);   
    }
    if (getIntent().hasExtra("Name")) {
      ETName.setText(getIntent().getStringExtra("Name"), 
      TextView.BufferType.EDITABLE);   
    }
}

This condition for detection if this activity get intent for another activity.

2. Activity B

Remember in this activity it's our main point. We will show data from JSON and will be show on list activity. So this is my activity_main_list.xml and list_item.xml file.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/list"
     />

</RelativeLayout>

<?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:orientation="vertical" >
    
    <TextView 
        android:layout_width="wrap_content"
     android:layout_height="50dp"
     android:id="@+id/name"
     android:layout_alignParentStart="true"
  android:gravity="center_vertical"  
        android:paddingStart="10dp"
 />
    
    <TextView 
  android:layout_width="wrap_content"
  android:layout_height="50dp"
     android:id="@+id/code"
  android:gravity="center_vertical"  
     android:layout_alignParentEnd="true"
        android:paddingEnd="10dp"
    />

</RelativeLayout>

In this xml file list we need to add item list for set list in xml file list. I mean element list have item list.

So this is my MainActivityList.java file in Activity B


public class MainActivityList extends Activity{

   public ListView list;    
   public ArrayList doc = new ArrayList();    
   public ListAdapter adapter;
   private String urlJsonArry = "http://192.168.88.10/api/medikav1/specialization";   private static String TAG = MainActivity.class.getSimpleName();
      
   @Override   
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);      
      setContentView(R.layout.activity_main_list);      
      makeJsonArrayRequest();
      list = (ListView) findViewById(R.id.list);
      adapter = new ListAdapter(this);
      list.setAdapter(adapter);
      list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        
   @Override
   public void onItemClick(AdapterView parent, View view, int position, long id) {
      Intent intent = new Intent(MainActivityList.this, MainActivity.class);             intent.putExtra("Code", doc.get(position).name);
      intent.putExtra("Name", doc.get(position).code);
      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | 
                      Intent.FLAG_ACTIVITY_CLEAR_TASK);
      startActivity(intent);
      }
      });
   }

   private void makeJsonArrayRequest() {

      JsonArrayRequest req = new JsonArrayRequest(urlJsonArry,
            new Response.Listener() {
               @Override               
               public void onResponse(JSONArray response) {
                  Log.d(TAG, response.toString());
                  try {
                     for (int i = 0; i < response.length(); i++) {
                        JSONObject person = (JSONObject) response.get(i);
                        Model add=new Model();
                        add.name = person.getString("SpecializationID");                                   add.code = person.getString("SpecializationName");                                 doc.add(add);
                     }
                  } catch (JSONException e) {
                     e.printStackTrace();
                     Toast.makeText(getApplicationContext(),                                                           "Error: " + e.getMessage(),                                                        Toast.LENGTH_LONG).show();
                  }
               }
            }, new Response.ErrorListener() {
         @Override
         public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            Toast.makeText(getApplicationContext(),
                  error.getMessage(), Toast.LENGTH_SHORT).show();
         }
      });
      AppController.getInstance().addToRequestQueue(req);   }

}

The point of this activity is i create function for get data from Json. This is my function.


private void makeJsonArrayRequest() {

   JsonArrayRequest req = new JsonArrayRequest(urlJsonArry,
         new Response.Listener() {
            @Override               
            public void onResponse(JSONArray response) {
               Log.d(TAG, response.toString());
               try {
                  for (int i = 0; i < response.length(); i++) {
                     JSONObject person = (JSONObject) response.get(i);
                     Model add=new Model();
                     add.name = person.getString("SpecializationID");                                   add.code = person.getString("SpecializationName");                                 doc.add(add);
                  }
               } catch (JSONException e) {
                  e.printStackTrace();
                  Toast.makeText(getApplicationContext(),                                                           "Error: " + e.getMessage(),                                                        Toast.LENGTH_LONG).show();
               }
            }
         }, new Response.ErrorListener() {
      @Override
      public void onErrorResponse(VolleyError error) {
         VolleyLog.d(TAG, "Error: " + error.getMessage());
         Toast.makeText(getApplicationContext(),
               error.getMessage(), Toast.LENGTH_SHORT).show();
      }
   });
   AppController.getInstance().addToRequestQueue(req);   
}

As you can see I use volley for get data Json with parameter urlJsonArry.


   JsonArrayRequest req = new JsonArrayRequest(urlJsonArry, new Response.Listener()
   String urlJsonArry   = "http://api.androidhive.info/volley/person_array.json"

.As i told you before, i use JSON data from android hive, you can check here. You can goto link JSON, If you want see, it would be like this.


[
 {
 "name" : "Ravi Tamada", 
 "email" : "ravi8x@gmail.com",
 "phone" : {
  "home" : "08947 000000",
  "mobile" : "9999999999"
 }
 },
 {
 "name" : "Tommy", 
 "email" : "tommy@gmail.com",
 "phone" : {
  "home" : "08946 000000",
  "mobile" : "0000000000"
 }
 }
]

You can see I am do looping and set data to my model class. And then I do add to array list with type data model. After that you can see when i set array list into list xml with this code. This is in my ListAdapter.java file.

public class ListAdapter extends BaseAdapter {
   
   MainActivityList main;
      
   ListAdapter(MainActivityList main)
   {
      this.main = main;
   }
   
   @Override
   public int getCount() {
      return  main.doc.size();
   }
 
   @Override
   public Object getItem(int position) {
      return null;   }
 
   @Override   public long getItemId(int position) {
      return 0;   }
   
   static class ViewHolderItem {
      TextView name;
      TextView code;
   }

   @Override   
   public View getView(int position, View convertView, ViewGroup parent){
      ViewHolderItem holder = new ViewHolderItem();

      if (convertView == null) {
          LayoutInflater inflater = (LayoutInflater) 
          main.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.list_item, null);                          holder.name = (TextView) convertView.findViewById(R.id.name);           
            holder.code = (TextView) convertView.findViewById(R.id.code);                      convertView.setTag(holder);
      }
      else
      {
          holder = (ViewHolderItem) convertView.getTag();
      }
      
      holder.name.setText(this.main.doc.get(position).name);
      holder.code.setText(this.main.doc.get(position).code);
 
   return convertView;
  }
}

after adapter have value, this adapter will be add to list :


adapter = new ListAdapter(this);
list.setAdapter(adapter);

And the last remember my third list. List will move back to activity A, with fill 2 EditText. I just use this code.


list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView parent, View view, int position, long id) {
 Intent intent = new Intent(MainActivityList.this, MainActivity.class);
 intent.putExtra("Code", doc.get(position).name);
 intent.putExtra("Name", doc.get(position).code);
 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | 
                        Intent.FLAG_ACTIVITY_CLEAR_TASK);
 startActivity(intent);
 }
});

As you can see, I just use intent and bring data from array list with position. Then with the array list type data model I just add .name or .code it's a getter from model class. Uha

The conclusion is if you have text like this, you can make plan one by one. Like my plan is button click, get data Json, set data Json to list, and onclick list. Hoya you can also download to my GitHub repository with click link below.

GitHub
Share:


<br>


Advice for contacting me at dedyy54@gmail.com