activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" android:background="#ff6600" > <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:cacheColorHint="#ff004d" > </ListView> </LinearLayout>
MainActivity.java
package com.blogspot.kantapp.listviewinandroid; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.ContextMenu; import android.view.View; import android.view.ContextMenu.ContextMenuInfo; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends Activity implements OnItemClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView list = (ListView) findViewById(R.id.listView1); //ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), //android.R.layout.simple_list_item_1, names); ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.text, names); list.setAdapter(adapter); list.setOnItemClickListener(this); registerForContextMenu(list); } String[] names = { "name1", "name2", "name3", "name4", "name5", "Second Activity", "name7", "name8", "name9", "name10" , "name11", "name12", "name13", "name14", "name15", "name16", "name17", "name18", "name19", "name20"}; @Override public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "selected name is :"+names[position], Toast.LENGTH_SHORT).show(); if(position ==5 ){ startActivity(new Intent(getApplicationContext(), Second.class)); } } }
text.xml
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:textSize="20dp" android:textColor="#000000" > </TextView>
second.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="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is second activity" android:textAppearance="?android:attr/textAppearanceLarge" /> </LinearLayout>
Second.java
package com.blogspot.kantapp.listviewinandroid; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class Second extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.second); } }
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.blogspot.kantapp.listviewinandroid"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".Second"></activity> </application> </manifest>
No comments:
Post a Comment