I have 2 activities:
the first - is just an opening page and it has a button that goes to the second activity
the second activity - will open up the contact list and will allow the user to pick contacts and retrieve their numbers - they will be saved for later use.
My problem is I can not figure out how to get it to work in the second activity. All the examples only have the contact list in the Main/first activity and I am really new to this and don't understand how to get it to move to the second.
my code I have right now
Main.java
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) this.findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick (View v){
Intent i = new Intent(MainActivity.this, Contacts.class);
startActivity(i);
}
});
}
}
Main XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Settings"
android:id="@+id/button1"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="93dp"/>
</RelativeLayout>
Contacts java
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class Contacts extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contacts);
}
}
Contacts XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context="akh.seniorproj.Contacts">
</RelativeLayout>
Check this solution: How to call Android contacts list? Its pretty much what you need to do but instead name you should search the number with the cursor.