I'm new to android and the problem feels simple, but I can't place it. I tried to look in logcat, but I can't follow.
Here is the main activity which loads just fine.
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Intent intent;
Button newsButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void sendMessage(View view) {
Intent intent = new Intent(this, NewsActivity.class);
startActivity(intent);
}
}
Here is the corresponding xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:orientation="vertical" >
<Button
android:id="#+id/newsButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="104dp"
android:onClick="#layout/news_activity"
android:text="News" />
<Button
android:id="#+id/contactButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Contact Us"
android:onClick="#layout/contact_activity" />
<Button
android:id="#+id/findUs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Social Media"
android:onClick="#layout/social_activity" />
<Button
android:id="#+id/fundButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fundraising" />
</LinearLayout>
<TextView
android:id="#+id/titleName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
android:text="Send the Scots to Scotland"
android:textSize="24sp" />
</RelativeLayout>
The crash happens on the API 14 device I debug with. After clicking any one of my buttons to change to a new activity that looks like this :(which are all essentially the same as the one below)
package com.gconcode.scotstoscotland;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
public class NewsActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news_activity);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.news, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up- vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
Sorry I removed the logcat, I wasn't sure if I posted it right. Didn't want too cause the post to be inconvenient thanks for the help!
I think you misunderstand android:onClick. It looks like you are expecting it to load a certain layout file when you click the Button but that is not how it works. The name you give android:onClick should be the name of a function which doesn't return anything and accepts a View as its one and only parameter. It looks like you pulled that from the example in the docs with
public void sendMessage(View view) {
Intent intent = new Intent(this, NewsActivity.class);
startActivity(intent);
}
There are different ways you could do this. Since you already have the android:onClick and you have a method in your Activity, you could change that to look like
android:onClick="sendMessage"
then add a switch statement to that method to open the correct Activity. So something like
public void sendMessage(View view)
{
switch (view.getId()) // get the id of the View clicked
{
case (R.id.newsButton):
Intent intent = new Intent(this, NewsActivity.class);
startActivity(intent);
break;
case (R.id.contactButton):
// assuming ContactActivity.java is an Activity name
Intent intent = new Intent(view.getContext(), ContactActivity.class);
startActivity(intent);
break;
...
}
}
This answer shows a little different way to do the Intent so you cut down on some of the reusable code. If it looks too complicated then don't worry about it until you have a better understanding but just an idea.
Related
I have an android app which starts out at the main activity that contains a navigation view, and a fragment called fragment_home which has my start button. When I hit that button, another activity is started. This works fine in the device emulator, but it also works when I plug my phone in and build directly to it (Pixel 3XL with Android 9).
However, if I manually install the APK and run it with my phone unplugged, the main activity opens up fine and displays the home fragment contents, but when I press the button the app immediately crashes. Here is the code for my
MainActivity.java
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
mToggle = new ActionBarDrawerToggle(this,mDrawerLayout, R.string.open,R.string.close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
/*
* Avoid reloading fragment when device is rotated
*/
if(savedInstanceState == null)
{
getFragmentManager().beginTransaction().replace(R.id.placeholder_home, new HomeFragment()).commit();
navigationView.setCheckedItem(R.id.fragment_home);
}
}
/* Add functionality to the navigation drawer items */
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch(menuItem.getItemId()) {
case R.id.home:
getFragmentManager().beginTransaction().replace(R.id.placeholder_home, new HomeFragment()).commit();
break;
case R.id.history:
getFragmentManager().beginTransaction().replace(R.id.placeholder_home, new HistoryFragment()).commit();
break;
case R.id.settings:
getFragmentManager().beginTransaction().replace(R.id.placeholder_home, new SettingsFragment()).commit();
break;
case R.id.help:
getFragmentManager().beginTransaction().replace(R.id.placeholder_home, new HelpFragment()).commit();
break;
}
mDrawerLayout.closeDrawer(GravityCompat.START);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(mToggle.onOptionsItemSelected(item))
return true;
return super.onOptionsItemSelected(item);
}
public void buttonClick(View v) {
switch(v.getId()) {
case R.id.startButton:
/* Start the survey activity */
Intent myIntent = new Intent(MainActivity.this, SurveyClass.class);
startActivity(myIntent);
break;
}
}
}
The buttonClick() function is the one that is run when the button from my home fragment is clicked.
Here is my home fragment XML code
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragment_home"
android:background="#color/colorPrimary">
<Button
android:id="#+id/startButton"
android:layout_width="256dp"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="120dp"
android:background="#drawable/button_rounded_corners_white"
android:fontFamily="#font/segoeuib"
android:gravity="center"
android:includeFontPadding="false"
android:onClick="buttonClick"
android:text="FIND PRODUCTS"
android:textColor="#color/colorAccent"
android:textSize="26sp" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="326dp"
android:layout_height="188dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="29dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="18dp"
app:srcCompat="#drawable/logo_white" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView2"
android:layout_centerHorizontal="true"
android:textColor="#color/white"
android:textSize="24dp"
android:textStyle="bold"
android:text="#string/subtitle" />
</RelativeLayout>
Do you work with android support v4 Fragment ?
If it's the case you need to use
getSupportFragmentManager().beginTransaction().replace(R.id.placeholder_home, new HomeFragment()).commit();
instead of
getFragmentManager().beginTransaction().replace(R.id.placeholder_home, new HomeFragment()).commit();
Can you also post the error obtained ?
This might be android 9 error!
Add this line in your Manifest file within application
<uses-library android:name="org.apache.http.legacy" android:required="false"/>
I'm learning to code apps for android and I'm trying to code an app that reads QR codes, but I want to add an options menu. I have a small code that does it, but when I try to add it the QR reader app the menu does not show. Can you tell me what is wrong with what I have so far?
MainActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import android.view.View;
import android.app.AlertDialog;
import android.util.Log;
import com.google.zxing.Result;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
public class MainActivity extends Activity implements ZXingScannerView.ResultHandler {
private ZXingScannerView mScannerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// Here I added the menu to the qr app
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item1:
Toast.makeText(getApplicationContext(),"Item 1 Selected",Toast.LENGTH_LONG).show();
return true;
case R.id.item2:
Toast.makeText(getApplicationContext(),"Item 2 Selected",Toast.LENGTH_LONG).show();
return true;
case R.id.item3:
Toast.makeText(getApplicationContext(),"Item 3 Selected",Toast.LENGTH_LONG).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
//Here ends the part of the menu
public void onClick(View v){
mScannerView = new ZXingScannerView(this);
setContentView(mScannerView);
mScannerView.setResultHandler(this);
mScannerView.startCamera();
}
#Override
protected void onPause() {
super.onPause();
mScannerView.stopCamera();
}
#Override
public void handleResult(Result result) {
Log.w("handleResult", result.getText());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Scan result");
builder.setMessage(result.getText());
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
Menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/item1"
android:title="Item 1"/>
<item android:id="#+id/item2"
android:title="Item 2"/>
<item android:id="#+id/item3"
android:title="Item 3"/>
</menu>
activity_main.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">
<GridLayout
android:layout_width="match_parent"
android:layout_height="114dp">
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="3"
android:onClick="onClick"
android:text="Scan QR" />
</GridLayout>
</LinearLayout>
I know that the options menu works by itself, because I tested alone as a different app, and also the qr reader works by itself, but if I try to combine them the menu is not there. I'm a newbie on coding android apps, so I will appreciate your guidance. Thanks
your menu name you supplied is menu.xml but instead you are giving getMenuInflater().inflate(R.menu.main, menu);
you should replace
getMenuInflater().inflate(R.menu.main, menu);
with
getMenuInflater().inflate(R.menu.menu, menu);
I want to make a ListView that gets its elements(strings) from user input. I have a button that directs the user to another activity and while in it, the user enters a name and presses another button to come back to the original activity. The same button gets and adds a string to the ArrayAdapter that the ListView uses and displays it as an element in the ListView. It doesn't seem to work and I know it's a stupid mistake, but I'm fresh to android development and this in particular I haven't done before.
Here's all the code:
the MainActivity
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.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> simpleArray =new ArrayList<String>();
ListAdapter simpleAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
simpleArray);
ListView lv = (ListView) findViewById(R.id.lv);
lv.setAdapter(simpleAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void enterActivity(View view) {
Intent toEnterSecond = new Intent(this, SecondActivity.class);
startActivity(toEnterSecond);
}
}
The second acitivity
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import java.util.ArrayList;
public class SecondActivity extends Activity{
private EditText projectName;
ArrayList simpleArray;
ArrayAdapter simpleAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
projectName = (EditText) findViewById(R.id.eTxt);
}
public void getBack(View view) {
String projectCalling = String.valueOf(projectName.getText());
simpleArray.add(projectCalling);
simpleAdapter.notifyDataSetChanged();
Intent comeBack = new Intent(this, MainActivity.class);
startActivity(comeBack);
}
}
And the layouts
<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"
tools:context=".MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="80dp"
android:id="#+id/simpleButton"
android:text="click me plox"
android:onClick="enterActivity"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/simpleButton"
android:id="#+id/lv">
</ListView>
</RelativeLayout>
//
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="20dp"
android:text="Enter the name of your project:"
android:id="#+id/txt"/>
<EditText
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_below="#+id/txt"
android:id="#+id/eTxt"/>
<Button
android:layout_width="100dp"
android:layout_height="60dp"
android:layout_below="#+id/eTxt"
android:text="click"
android:onClick="getBack"/>
</RelativeLayout>
Explanation:
You can start SecondActivity with startActivityForResult() instead of startActivity as answered here and in onResultActivity method you can call simpleAdapter.notifyDataSetChanged();. Don't call simpleAdapter.notifyDataSetChanged(); in SecondActivity.
To learn how to use startActivityForResult check this.
Also use finish() instead of
Intent comeBack = new Intent(this, MainActivity.class);
startActivity(comeBack);
in SecondActivity to go back to MainActivity.
Solution:
Change MainActivity enterActivity method to this:
...
public void enterActivity(View view) {
Intent toEnterSecond = new Intent(this, SecondActivity.class);
startActivityForResult(toEnterSecond,1);
}
Add this to end of SecondActivity getBack method:
MainActivity.simpleArray.add(projectCalling);
Intent returnIntent = new Intent();
setResult(RESULT_OK,returnIntent);
finish();
Finally add this new method to MainActivity:
protected void onActivityResult(int requestCode, int resultCode, Intent data{
if (requestCode == 1) {
if(resultCode == RESULT_OK){
((ArrayAdapter) simpleAdapter).notifyDataSetChanged();
}
}
}
To be able to access simpleAdapter, you need to define it outside of onCreate:
public class MainActivity extends ActionBarActivity {
private ListAdapter simpleAdapter;
public static ArrayList<String> simpleArray;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
simpleArray =new ArrayList<String>();
simpleAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
simpleArray);
...
}
i am just working on a test app.
this is the xml file of the home page.
<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=".MainActivityTEST2" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:id="#+id/button12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="72dp"
android:text="Button12" />
</RelativeLayout>
i have added a button listener to go to other page. here is my code for that.
package com.example.test2;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivityTEST2 extends Activity {
Button but;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity_test2);
but=(Button)findViewById(R.id.button12);
but.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
setContentView(R.layout.activity123);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_activity_test2, menu);
return true;
}
when i add a button listener in the second page to display text in the textfield, nothing works.
xml file of the second page
<?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" >
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<EditText
android:id="#+id/edittext1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
</LinearLayout>
when i add a button listerner to display text in the textfiled, nothing works.
the app neighter stops working nor does anything after that.
my second window's java content is
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.example.test2.R;
public class activity_main_activty_test2 extends Activity {
Button but2;
EditText edit1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity123);
edit1=(EditText)findViewById(R.id.edittext1);
but2=(Button)findViewById(R.id.button2);
but2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
edit1.setText("hellow");
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_activity_test2, menu);
return true;
}
}
am i missing something. do i have to address something. please help i am just a biggener.
i have added a button listener to go to other page No, apparently you haven't.
You cannot go to the other activity through this code
setContentView(R.layout.activity123);
You need to use Intent
#Override
public void onClick(View arg0) {
Intent intent = new Intent(MainActivityTEST2.this,activity_main_activty_test2.class);
startActivity(intent);
finish(); //optional
}
ALSO remove import com.example.test2.R; from your second activity.
if you want to go to the next view, you dont use setContentView(R.layout.activity123); in your button listener it will not transfer you to the next activity, instead make use of intent since you have a 2nd activity.
Intent intent = new Intent(getBaseContext(), activity_main_activty_test2.class);
startActivity(intent)
in your MainActivityTEST2
but.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(getBaseContext(), activity_main_activty_test2.class);
startActivity(intent)
}
});
Please Change your first page as following. Android use Intent to jump the activity from one to another.
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivityTEST2 extends Activity {
Button but;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity_test2);
but = (Button) findViewById(R.id.button12);
but.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setClass(this, activity_main_activty_test2.class);
startActivity(intent);
finish();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_activity_test2, menu);
return true;
}
}
OK so the trick that works with a textView doesn't work with this.
So I know this has got to be simple.... Right?
because i might help me out with adding pictures for this later.
<?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:background="#drawable/logo"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_alignParentBottom="true"
android:layout_below="#+id/textView1"
android:paddingLeft="8dp"
android:paddingRight="8dp" >
</ListView>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="#string/welcome"
android:textColor="#b70000"
android:textSize="16sp" />
</RelativeLayout>
I use this to call it and add to the listview inside the java code.
package com.example.boonehallfrightnightsapp;
import android.os.Bundle;
import android.view.Menu;
import android.app.ListActivity;
import android.content.Intent;
import android.view.View;
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 ListActivity
{
static final String[] CHOICES = new String[]
{
"Haunted House",
"Amy's Nightmare",
"Zombie Town",
"Haunted Hayride",
"Quit"
};
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fn_main);
//found this part on an example
//Set up ArrayAdaptor for the options
setListAdapter(new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, CHOICES));
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
getListView().setTextFilterEnabled(true);
//part of example
//Set up the listener for user clicks on the list
setListClickListener();
//this toast is for when it opens
Toast.makeText(this, "I see your fear...", Toast.LENGTH_SHORT).show();
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void setListClickListener()
{
//Set up the click listener for the options
getListView().setOnItemClickListener
(
new OnItemClickListener()
{
//#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
switch(arg2)
{
case 0: launchHousePage();
break;
case 1: launchNightmarePage();
break;
case 2: launchZombiePage();
break;
case 3: launchHayridePage();
break;
case 4: finish();
break;
default: break;
}
}
}//END OnItemClickListener
);//END setOnItemClickListener
}//END setListClickListener
//goes to haunted house
protected void launchHousePage()
{
//Set up Intent
Intent launchHouse = new Intent(this, HauntedHouseList.class);
startActivity(launchHouse);
}//END launchHousePage
//goes to Amy's Nightmare
protected void launchNightmarePage()
{
//Set up Intent
Intent launchnightmare = new Intent(this, NightmareList.class);
startActivity(launchnightmare);
}//END launchNightmarePage
//goes to Amy's Nightmare
protected void launchZombiePage()
{
//Set up Intent
Intent launchzombies = new Intent(this, ZombieTownList.class);
startActivity(launchzombies);
}//END launchZombiePage
//goes to haunted house
protected void launchHayridePage()
{
//Set up Intent
Intent launchhayride = new Intent(this, HauntedHayrideList.class);
startActivity(launchhayride);
}//END launchHayridePage
}
You've specified a built-in Adapter (ArrayAdapter) using item layout android.R.layout.simple_list_item_1.
if you want custom layout, you can copy the simple_list_item_1.xml layout from Android SDK (look in the platforms/android-18/data/res/layout folder) into your project and modify it. For example you call it my_simple_list_item_1.xml.
Then modify your code to use your layout, and not android.R.layout.simple_list_item_1:
setListAdapter(new ArrayAdapter<String>(this, R.layout.my_simple_list_item_1, CHOICES));
You'll see that Androids simple_list_item_1 layout is just a TextView, and you can add the textColor attribute to it and modify to your liking.