Saturday, February 20, 2016

Implement Gestures in Android Phone Android Studio

Hello everyone today I will be showing you how to capture touch events in your Android application

For detecting gesture you will need to import few classes in your code so import the following classes


1:  import android.view.MotionEvent;  
2:  import android.view.GestureDetector;  
3:  import android.support.v4.view.GestureDetectorCompat;  

You will also need to implement the following Interfaces

1. GestureDetector.OnGestureListener

2. GestureDetector.OnDoubleTapListener

Like this

1:  public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener {  
Now we will need to implement all methods for these interfaces so press Alt + Insert this will list all methods available to be implemented Click on OK In you onCreate Method paste the following code

1:  txtans=(TextView) findViewById(R.id.txtans);  
2:      gestureDetector = new GestureDetectorCompat(this,this);  

Now for all the implemented method change the return type to true from false
We will also need to implement one last method so again press Alt + Insert and select onTouchEvent

1:    public boolean onTouchEvent(MotionEvent event) {  
2:      this.gestureDetector.onTouchEvent(event);  
3:      return super.onTouchEvent(event);  
4:    }  

I hope that it was informative for you and I would like to Thank you for reading.

Sunday, February 14, 2016

Android Multiple Event Handling Java

In this tutorial I will show you how to handle multiple events in Java for Android Development.

We will use a button and use their onClick and OnLongClick events to change the value of TextView


1:    protected void onCreate(Bundle savedInstanceState) {  
2:      super.onCreate(savedInstanceState);  
3:      setContentView(R.layout.activity_main);  
4:      client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();  
5:      Button btn = (Button) findViewById(R.id.btnequal);  
6:      btn.setOnClickListener(new Button.OnClickListener(){  
7:        public void onClick(View v){  
8:          btnonClick();  
9:        }  
10:      });  
11:      btn.setOnLongClickListener(new Button.OnLongClickListener(){  
12:        public boolean onLongClick(View c){  
13:          longClick();  
14:          return true;  
15:        }  
16:      });  
17:    }  
18:    public void longClick(){  
19:      TextView txt = (TextView)findViewById( R.id.txtans) ;  
20:      txt.setText("That was a long click");  
21:    }  
22:  public void btnonClick(){  
23:    TextView txt = (TextView) findViewById(R.id.txtans);  
24:    txt.setText("Dont touch me");  
25:  }  


I hope that it was informative for you and I would like to Thank you for reading

Android Event Handling Java

Hello everyone, in this tutorial I will show you how to implement event handling in Java for Android Development.

The example I will provide will include two controls a button and a TextView.

When user click the the button the value of textView will change.



1:   protected void onCreate(Bundle savedInstanceState) {  
2:      super.onCreate(savedInstanceState);  
3:      setContentView(R.layout.activity_main);  
4:      client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();  
5:      Button btn = (Button) findViewById(R.id.btnequal);  
6:      btn.setOnClickListener(new Button.OnClickListener(){  
7:        public void onClick(View v){  
8:          btnonClick();  
9:        }  
10:      });  
11:    }  
12:  public void btnonClick(){  
13:    TextView txt = (TextView) findViewById(R.id.txtans);  
14:    txt.setText("Dont touch me");  
15:  }  


I hope that it was informative for you and I would like to Thank you for reading

Android Set Control Width Dynamically Android Studio Java

In this tutorial I will show you how to create a Control with Width that will be same on all device regardless of there screen size.

So doesn't matter if your screen size is 5' or 4' your control will adjust itself automatically according to the screen size.

1:      super.onCreate(savedInstanceState);  
2:      RelativeLayout PageLayout = new RelativeLayout(this);  
3:      RelativeLayout.LayoutParams txtLayout = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);  
4:      txtLayout.addRule(RelativeLayout.CENTER_HORIZONTAL);  
5:      txtLayout.addRule(RelativeLayout.CENTER_VERTICAL);  
6:      EditText txtLogin = new EditText(this);  
7:      Resources r = getResources();  
8:      int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,200,r.getDisplayMetrics());  
9:      txtLogin.setWidth(px);  
10:      PageLayout.addView(txtLogin,txtLayout);  
11:      setContentView(PageLayout);  
Note that instead of defining width 200 static we are converting this into pixel according to device screen see line no. 8

After we get the px value which is of type in we can pass this value to setWitdh method.

I hope that it was informative for you and I would like to Thank you for reading.

Android Create Dynamic UI (Multiple Controls)

In this tutorial I have created Dynamic UI using Java. There are 3 Controls A button which is at 
the center of the screen, just above this button there is a textField and at the left of the 
textfield there is a label.
1:      RelativeLayout PageLayout = new RelativeLayout(this);  
2:      RelativeLayout.LayoutParams btnLayout = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);  
3:      btnLayout.addRule(RelativeLayout.CENTER_HORIZONTAL);  
4:      btnLayout.addRule(RelativeLayout.CENTER_VERTICAL);  
5:      Button btn = new Button(this);  
6:      btn.setId(1);  
7:      btn.setText("Login");  
8:      RelativeLayout.LayoutParams txtLayout = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);  
9:      txtLayout.addRule(RelativeLayout.CENTER_HORIZONTAL);  
10:      txtLayout.addRule(RelativeLayout.ABOVE, btn.getId());  
11:      EditText txtLogin = new EditText(this);  
12:      txtLogin.setId(2);  
13:      txtLogin.setWidth(300);  
14:      RelativeLayout.LayoutParams lblLayout = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);  
15:      //lblLayout.addRule(RelativeLayout.RIGHT_OF, txtLogin.getId());  // to put it in right position of textbox  
16:      lblLayout.addRule(RelativeLayout.LEFT_OF, txtLogin.getId());  
17:      lblLayout.addRule(RelativeLayout.ALIGN_BASELINE, txtLogin.getId());  
18:      TextView lblID = new TextView(this);  
19:      lblID.setId(3);  
20:      lblID.setText("User Name");  
21:      PageLayout.addView(btn, btnLayout);  
22:      PageLayout.addView(txtLogin,txtLayout);  
23:      PageLayout.addView(lblID,lblLayout);  
24:      setContentView(PageLayout);  

Saturday, February 13, 2016

Android Create Dynamic UI using Java


1:  super.onCreate(savedInstanceState);  
2:  RelativeLayout.LayoutParams layout= new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);  
3:  layout.addRule(RelativeLayout.CENTER_HORIZONTAL);   // setting Height  
4:  layout.addRule(RelativeLayout.CENTER_VERTICAL);    // Setting Width   
5:  RelativeLayout myLayout = new RelativeLayout(this);  // using this for changing background colormyLayout.setBackgroundColor(Color.GREEN);      // setting background color to green  
6:  Button btn = new Button(this);  
7:  btn.setBackgroundColor(Color.BLUE);         // Defining Btn Background Color to Bluebtn.setText("Touch ME");              // Defining button textbtn.setLayoutParams(layout);  
8:  //ViewPropertyAnimator anim=btn.animate();  
9:  myLayout.addView(btn);  
10:  setContentView(myLayout);  


i hope that it was informative for you and I would like to thank you for reading.