Monday, 16 April 2012

Creating HorizontalScrollView in Android

Today i am going to describing a powerful tool of android, Horizontal Listview with complete source in the end.
Horizontal ListView sometimes is very useful and it save our day. HorizontalScrollView is nothing but a widget, If you add more element than its visible area then it start scrolling horizontally. Bit main disadvantage is that you can not perform click on the base of position quite easily

If you need some explanation then read complete step else download code with link in the end and play with it

Step 1) Create one new Project name HorizontalListviewDemo

Step 2) Change your main activity to as follow class

 package com.ahmad;  
 import android.app.Activity;  
 import android.os.Bundle;  
 import android.view.View;  
 import android.view.View.OnClickListener;  
 import android.widget.Button;  
 import android.widget.LinearLayout;  
 import android.widget.Toast;  
 public class SecondActivity extends Activity implements OnClickListener {  
  Button btn1, btn2, btn3, btn4;  
  public LinearLayout middle;  
  @Override  
  public void onCreate(Bundle savedInstanceState) {  
  super.onCreate(savedInstanceState);  
     setContentView(R.layout.main);  
  btn1 = (Button) findViewById(R.id.btn1);  
  btn1.setOnClickListener(this);  
  btn2 = (Button) findViewById(R.id.btn2);  
  btn2.setOnClickListener(this);  
  btn3 = (Button) findViewById(R.id.btn3);  
  btn3.setOnClickListener(this);  
  try {  
   btn4 = (Button) findViewById(R.id.btn4);  
   btn4.setOnClickListener(this);  
  } catch (Exception e) {  
   e.getMessage();  
  }  
  }  
  @Override  
  public void onClick(View v) {  
  if (v.getId() == R.id.btn1) {  
   Toast.makeText(this, "Second Act Btn1 Clicked", Toast.LENGTH_LONG)  
    .show();  
  } else if (v.getId() == R.id.btn2) {  
   Toast.makeText(this, "Second Act Btn 2 Clicked", Toast.LENGTH_LONG)  
    .show();  
  } else if (v.getId() == R.id.btn3) {  
   Toast.makeText(this, "Second Act Btn 3 Clicked", Toast.LENGTH_LONG)  
    .show();  
  } else if (v.getId() == R.id.btn4) {  
   Toast.makeText(this, "Second Act Btn 4 Clicked", Toast.LENGTH_LONG)  
    .show();  
  }  
  }  
 }  
Step 3)Make one Layout and add HorizontalListview and some content inside

  
Now enjoy after running it.complete source link is below.See more article on ListView in blog tag Android ListView
                                                          Download Complete Project


Article You may Like

Android Listactivity with header
Android News and source code