Today i am going to describing a powerful tool of android, Horizontal Listview with complete source in the end.
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();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFF"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/top"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:layout_alignParentTop="true"
android:background="@drawable/header" >
<Button
android:id="@+id/btn_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:text="@string/back" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:text="@string/title"
android:textColor="#FFF"
android:textSize="20dp"
android:textStyle="bold" />
<Button
android:id="@+id/btn_unknown"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:text="@string/unknow" />
</RelativeLayout>
<HorizontalScrollView
android:id="@+id/bottom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:fadeScrollbars="false"
android:scrollbars="none" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/header"
android:orientation="horizontal"
android:paddingTop="5dp" >
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn1" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn2" />
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn3" />
<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn4" />
</LinearLayout>
</HorizontalScrollView>
<LinearLayout
android:id="@+id/middlebar"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@id/bottom"
android:layout_below="@id/top" >
</LinearLayout>
</RelativeLayout>
Horizontal ListView sometimes is very useful and it save our day.
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)Change your main.xml to following
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFF"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/top"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:layout_alignParentTop="true"
android:background="@drawable/header" >
<Button
android:id="@+id/btn_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:text="@string/back" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:text="@string/title"
android:textColor="#FFF"
android:textSize="20dp"
android:textStyle="bold" />
<Button
android:id="@+id/btn_unknown"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:text="@string/unknow" />
</RelativeLayout>
<HorizontalScrollView
android:id="@+id/bottom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:fadeScrollbars="false"
android:scrollbars="none" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/header"
android:orientation="horizontal"
android:paddingTop="5dp" >
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn1" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn2" />
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn3" />
<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn4" />
</LinearLayout>
</HorizontalScrollView>
<LinearLayout
android:id="@+id/middlebar"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@id/bottom"
android:layout_below="@id/top" >
</LinearLayout>
</RelativeLayout>
Now enjoy after running it.complete source link is below.See more article on ListView in blog tag Android ListView
0 comments:
Post a Comment