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.
Related
When I am trying to run my app on my phone it is getting successfully installed in my phone but I am getting white screen instead of my splash page . Attaching my xml code and java code for your reference . Your help is highly appreciated !!
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="100dp"
android:fontFamily="sans-serif-black"
android:text="MY APP"
android:textColor="#color/Black"
android:textSize="50sp" />
</Relativelayout>
activity_main.java
package com.example.kriova;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(getApplicationContext(),
MainActivity.class);
startActivity(intent);
finish();
}
}
The finish() method is called and the activity destroys and returns to the home screen.
Your code should be like this
package com.example.kriova;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}}
remove the last line which is :-
finish();
I'm trying to create a button to reload a Webview but ( sorry I'm new in android developement ) I didn't find a way to do it ( even in stackoverflow )
Here is the Mainactivity.java
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.widget.Button;
import static com.d4rkunicorn.partyhard.R.id.webView;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webview = new WebView(this);
setContentView(webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("url");
}
}
The webview.xml :
<?xml version="1.0" encoding="utf-8"?>
<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"
android:id="#+id/webview.xml">
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="REFRESH"
android:id="#+id/button" />
</RelativeLayout>
Thanks for helping
In Activity class :
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.widget.Button;
import static com.d4rkunicorn.partyhard.R.id.webView;
public class MainActivity extends ActionBarActivity {
WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview = new WebView(this);
setContentView(webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("url");
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
webview.reload();
}
});
}
}
In Layout XML :
<?xml version="1.0" encoding="utf-8"?>
<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"
android:id="#+id/webview.xml">
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:id="#+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="REFRESH"
android:id="#+id/button" />
</RelativeLayout>
There many possible way to reload your webview:
you can use webview.loadUrl( "javascript:window.location.reload( true )" ); inside any method/action event
webview.loadUrl(webview.toString); //getting the webview URL and use to reload ur web page
webview.loadUrl(url); // you can use your desire URL to reload your web page
webview.loadUrl( "onclick=\"window.history.back()"\");
You can reload your web view or you can use below code :
yourActivity.this.webView.loadUrl(url);
on your button click.
I am trying to get an integer array into ListView using an adapter but do not seem to know where to begin.
I want the user to be able to open the listview, select a number from 1-99 and have the selected number be deducted from the current year and result displayed in a TextView.
Any help in the right direction would be fantastic!
first you have to create xml of listview given below
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
</RelativeLayout>
make one arraylist<String> list=new arraylist<string>;
add value 1-99 using for loop
and then set the array adpter on that listview as like
package com.example.arraydemo;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
ArrayList<String> list;
ListView li;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list=new ArrayList<String>();
li=(ListView)findViewById(R.id.listView1);
for(int i=1;i<=99;i++){
list.add(""+i);
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
list );
li.setAdapter(arrayAdapter);
li.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
// TODO Auto-generated method stub
}
});
}
}
and ontemclicklistener you get current date from system deduct position of listview from that date
Hello im making an assignment list with database to add assignments. When i push on the add button i can type my assignment. But i only see half of it. When i click add, it correctly adds the new assignment to the list and i can see the whole text.
How can i solve this ?
This is my XML layout file
<?xml version="1.0" encoding="utf-8"?>
<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="be.ehb.android.arnojansens.DetailGroupsActivity"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:id="#+id/listDetails"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="4.60"></ListView>
<EditText
android:id="#+id/editDetails"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:hint="Details Assignment"/>
<Button
android:id="#+id/btnAddDetailsOpdracht"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Add Details Assignment"/>
</LinearLayout>
</RelativeLayout>
And this is the java of the class
package be.ehb.arnojansens.fragmentexampleii;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
public class DetailGroupsActivity extends Activity implements View.OnClickListener {
static Dbhelper opdrDbHelper;
static Button btnAddDetailsOpdracht;
static EditText editDetails;
static ListView listDetails;
static ArrayList<Detail> details;
static long detailId;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail_groups);
btnAddDetailsOpdracht = (Button)findViewById(R.id.btnAddDetailsOpdracht);
editDetails = (EditText)findViewById(R.id.editDetails);
listDetails = (ListView)findViewById(R.id.listDetails);
btnAddDetailsOpdracht.setOnClickListener(this);
opdrDbHelper = new Dbhelper(this);
detailId = (Long)getIntent().getSerializableExtra("detailId");
String opdrachtName = (String)getIntent().getStringExtra("opdrachtName");
this.setTitle(opdrachtName);
if(detailId != -1)
details = opdrDbHelper.findDetailsForOpdracht(detailId);
else
details = new ArrayList<Detail>();
loadDetails();
}
public void loadDetails(){
ArrayAdapter<Detail> adapter =
new ArrayAdapter<Detail>(
this,
android.R.layout.simple_list_item_1,
details);
listDetails.setAdapter(adapter);
}
#Override
public void onClick(View v) {
String detailString = editDetails.getText().toString();
editDetails.setText("");
Detail alb = opdrDbHelper.addAlbum(detailId,detailString);
details.add(alb);
loadDetails();
}
}
Sound to me like the text doesn't fit in the EditText container. Make it wrap_content instead of a fixed size:
<EditText
android:id="#+id/editDetails"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Details Assignment"/>
Also keep in mind that fill_parent was deprecated, use match_parent instead.
Activity_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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="#+id/engadget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Engadget" />
</RelativeLayout>
bestblog.java
package com.example.bestblog;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class bestblog extends Activity {
Button button;
#Override
public void onCreate(Bundle saveInstanceState) {
super.onCreate(saveInstanceState);
setContentView(R.layout.activity_main);
addListemerOnButton();
}
private void addListemerOnButton() {
button = (Button) findViewById(R.id.engadget);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent browserIntent =
new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.engadget.com"));
startActivity(browserIntent);
}
});
}
}
If I use a Android Simulator to run my application, I get a error message.
I don't know how to fix it, I get this error message "unfortunately "name application" has stopped".
Change this line of your layout xml...
tools:context=".MainActivity"
to...
tools:context=".bestblog"