Open top menu

In this blog we will implement custom Number Picker step by step.
1. first create activity_main.xml class.
2. second create view gradient xml class.
3.MainActivity java class. explain below.




activity_main.xml
Create the  view main xml class inside res/layout folder and drop the two button with Horizontal LinearLayout.
...................................................................................................................................................................

<?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="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/edit_text_gradient"
        android:orientation="horizontal"
        android:padding="5dp"
        android:gravity="center" >

        <Button
            android:id="@+id/button_decrement"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="-" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="1"
            android:textSize="30dp" />

        <Button
            android:id="@+id/button_increment"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="+" />
    </LinearLayout>

    <Button
        android:id="@+id/pickerBtn"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:text="OK" />

</LinearLayout>
...................................................................................................................................................................

edit_text_gradient.xml
Create the UI Gradient Design xml class inside the res/drawable folder .
...................................................................................................................................................................



<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="15dp">
 <solid android:color="#FFFFFF"/>

    <corners
     android:bottomRightRadius="10dp"
     android:bottomLeftRadius="10dp"
     android:topLeftRadius="10dp"
     android:topRightRadius="10dp"/>

  <stroke android:width="2dip" android:color="#dce0e2" />
</shape>
...................................................................................................................................................................

MainActivity.java.
..................................................................................................................................................................

package com.example.androidtestcode;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
Context context;  
TextView tv;
Button increment,decrement,pickerBtn;

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);

   setContentView(R.layout.activity_main);
   tv=(TextView)findViewById(R.id.textView1);
 
   increment=(Button)findViewById(R.id.button_increment);
   decrement=(Button)findViewById(R.id.button_decrement);
   pickerBtn=(Button)findViewById(R.id.pickerBtn);
 
   context=this;

   increment.setOnClickListener(new View.OnClickListener()
   {

       @Override
       public void onClick(View v)
       {
           String present_value_string = tv.getText().toString();
           int present_value_int = Integer.parseInt(present_value_string);
           present_value_int++;
           tv.setText(String.valueOf(present_value_int));        
       }
   });
 
 
   decrement.setOnClickListener(new View.OnClickListener()
   {

       @Override
       public void onClick(View v)
       {
           String present_value_string = tv.getText().toString();
           int present_value_int = Integer.parseInt(present_value_string);
           if(present_value_int>1){
           present_value_int--;
           tv.setText(String.valueOf(present_value_int));
           }
       }
   });

 
   pickerBtn.setOnClickListener(new View.OnClickListener()
   {

       @Override
       public void onClick(View v)
       {
           String present_value_string = tv.getText().toString();
           Toast.makeText(getApplication(), "present value:-"+present_value_string,                                                Toast.LENGTH_LONG).show();
         
           }
     
   });
 
}
}
.................................................................................................................................................................

0 comments