Im using intents to pass the noteId from my database to a different activity for displaying the information it was working and i went to work on another part of the project and now it seems to only pass one value and from playing about i have found that seems to be whatever is the next highest object on the ListView. Sorry if this is badly worded, Im not very good at explaining these things
ViewNote Class
package com.hardy.passnotes;
import java.util.HashMap;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class ViewNote extends Activity {
EditText NewNoteTitle;
EditText NewNoteContent;
TextView tvNoteId;
DBTools dbTools = new DBTools(this);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_note);
NewNoteTitle = (EditText)findViewById(R.id.ETNewNoteTitle);
NewNoteContent = (EditText)findViewById(R.id.ETNewNoteContent);
Intent n = getIntent();
String NoteId = n.getStringExtra("noteId");
Toast.makeText(getApplicationContext(), NoteId,
Toast.LENGTH_LONG).show();
HashMap<String, String> NoteList = dbTools.getNoteInfo(NoteId);
if(NoteList.size() != 0)
{
setTitle("Note: " + NoteList.get("noteTitle"));
NewNoteTitle.setText(NoteList.get("noteTitle"));
NewNoteContent.setText(NoteList.get("noteContent"));
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.view_note, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId())
{
case R.id.action_Editnote:
tvNoteId = (TextView)findViewById(R.id.NoteId);
String NoteValue = tvNoteId.getText().toString();
Intent intent = new Intent(getApplication(),EditNote.class);
intent.putExtra("noteId", NoteValue);
startActivity(intent);
case R.id.action_DelNote:
Intent intent1 = getIntent();
String NoteId = intent1.getStringExtra("noteId");
dbTools.deleteNote(NoteId);
Intent i = new Intent(getApplication(),MyNotes.class);
startActivity(i);
finish();
}
return super.onOptionsItemSelected(item);
}
}
MyNote Class:
package com.hardy.passnotes;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class MyNotes extends ListActivity {
DBTools dbTools = new DBTools(this);
TextView tvNoteId;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_notes);
Log.i("Tag", "OnCreate Started As Normal");
setTitle("My Notes");
ArrayList <HashMap<String, String>> NoteList = dbTools.getAllNotes();
if(NoteList.size() != 0)
{
ListView listView = getListView();
listView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long id) {
tvNoteId = (TextView)findViewById(R.id.NoteId);
String NoteValue = tvNoteId.getText().toString();
Intent intent = new Intent(MyNotes.this,ViewNote.class);
intent.putExtra("noteId", NoteValue);
startActivity(intent);
Log.i("Tag", "if Started As Normal");
}
});
ListAdapter Adapter = new SimpleAdapter(MyNotes.this,NoteList,R.layout.notes_list, new String[]{"noteId","noteTitle",}, new int[]{R.id.NoteId,R.id.NoteTitle} );
setListAdapter(Adapter);
Log.i("Tag", "Arrey Started As Normal");
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my_notes, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId())
{
case R.id.action_Add:
Intent i = new Intent(getApplicationContext(),CreateNote.class);
startActivity(i);
finish();
}
return super.onOptionsItemSelected(item);
}
}
Thanks in advance been searching around and debugging for hours with no luck.
Hi I think the problem is that you use the findViewById() method inappropriately in your onItemClick listener.
You should use:
tvNoteId = (TextView)view.findViewById(R.id.NoteId);
Notice that I call the findViewById method on the view given by the onItemClick method because you want to find the view with the id NoteId from the pressed item and not from the entire's activity content view.. the way you do it will search in the content view of your activity for the first id equal with NoteId ...
I think your note get deleted on the moment edit is clicked from the options menu.
At the end of case R.id.action_Editnote nothing is returned nor break is called so case R.id.action_Delnote will be executed as well. This is where you delete the note from your database.
In your switch, put a break:
switch (item.getItemId())
{
case R.id.action_Editnote:
tvNoteId = (TextView)findViewById(R.id.NoteId);
String NoteValue = tvNoteId.getText().toString();
Intent intent = new Intent(getApplication(),EditNote.class);
intent.putExtra("noteId", NoteValue);
startActivity(intent);
break;
case R.id.action_DelNote:
Intent intent1 = getIntent();
String NoteId = intent1.getStringExtra("noteId");
dbTools.deleteNote(NoteId);
Intent i = new Intent(getApplication(),MyNotes.class);
startActivity(i);
finish();
break;
}
Related
I created a simple demo program in which there is start button.When i click on start button on main (Home) screen "hi all" appended to Text view.It works fine, But when I change the activity from selecting action bar menu and if again come on the home screen by selecting the action bar home menu then it will not show the "hi all " Message when I click on the Start button.
package com.example.testdemo;
import android.app.ActionBar.LayoutParams;
import android.content.Intent;
import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.text.method.ScrollingMovementMethod;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView logArea;
private TextView log;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
ActionBar actionBar = getActionBar();
log = new TextView(MainActivity.this);
log.append("Heollosdfsjdf" + "\n");
#SuppressWarnings("deprecation")
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
params.gravity = Gravity.LEFT;
log.setLayoutParams(params);
log.setGravity(Gravity.CENTER);
LinearLayout chat = (LinearLayout) findViewById(R.id.main_linear_view);
log.setMovementMethod(new ScrollingMovementMethod());
chat.addView(log);
setDefault();
}
public void setDefault(){
Button btn = (Button) findViewById(R.id.start_recording_button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startWriting(v);
}
});
}
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 boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.home:
goHome();
return true;
case R.id.general_setting:
generalSetting();
return true;
case R.id.server_settings:
serverSetting();
return true;
case R.id.audio_settings:
audioSetting();
return true;
default:
break;
}
return true;
}
private void goHome() {
Intent i = new Intent(MainActivity.this, home.class);
startActivity(i);
finish();
}
private void generalSetting() {
Intent i = new Intent(MainActivity.this, general.class);
startActivity(i);
finish();
}
private void audioSetting() {
Intent i = new Intent(MainActivity.this, audio.class);
startActivity(i);
finish();
}
private void serverSetting() {
Intent i = new Intent(MainActivity.this, server.class);
startActivity(i);
finish();
}
public void startWriting(View view) {
logMessage("Hi all");
}
private void logMessage(String msg) {
log.append(msg + "\n");
final int scrollAmount = log.getLayout().getLineTop(log.getLineCount())- log.getHeight();
if (scrollAmount > 0)
log.scrollTo(0, scrollAmount);
else
log.scrollTo(0, 0);
}
}
it's because you called 'finish' when you changed activities. calling the main activity again reloads its view to its initial state.
In addition to MetaSnarf's answer: For a deeper understanding of the "Android Task Stack" and saving of the current Activity state, you might want to take a look at http://developer.android.com/guide/components/tasks-and-back-stack.html
I want to make a button appear in the MenuActivity layout but the deciding if statement is in the CapitalReceiver class. I've tried adding 'static' to various variables but it didn't work. Please help!
import android.support.v4.app.Fragment;
import android.support.v4.content.LocalBroadcastManager;
import android.text.format.DateFormat;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
public class MenuActivity extends Activity {
String status;
Boolean verified = false;
String textColour = "#000000";
TextView mTvCapital;
ArrayAdapter<String> mAdapter;
Intent mServiceIntent;
CapitalReceiver mReceiver;
IntentFilter mFilter;
String country = "7ec47294ff3d8b74";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_layout);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
Button IDButton = (Button) findViewById(R.id.getIt);
Button RefreshButton = (Button) findViewById(R.id.refresh);
long updateTimeMillis = System.currentTimeMillis();
String updateTime = (String) DateFormat.format("hh:mm", updateTimeMillis);
//If application has been submitted//
if(preferences.contains("first_middle_store") & !(verified)) {
status = "Status: Application pending. Last updated: " + updateTime;
IDButton.setVisibility(View.GONE);
RefreshButton.setVisibility(View.VISIBLE);
textColour = "#000000";
}
//If application has not been submitted
else {
status = "Status: Application not yet submitted";
IDButton.setVisibility(View.GONE);
RefreshButton.setVisibility(View.GONE);
textColour = "#000000";
}
TextView text=(TextView)findViewById(R.id.application_status);
text.setTextColor(Color.parseColor(textColour));
text.setText(status);
Button btnNextScreen = (Button) findViewById(R.id.verify);
//Listening to verify event
btnNextScreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent nextScreen = new Intent(getApplicationContext(), VerifyActivity.class);
startActivity(nextScreen);
}
});
Button btnNextScreen2 = (Button) findViewById(R.id.how);
//Listening to HowItWorks event
btnNextScreen2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent nextScreen2 = new Intent(getApplicationContext(), HowItWorksActivity.class);
startActivity(nextScreen2);
}
});
//Listening to IDbutton event
IDButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent nextScreen3 = new Intent(getApplicationContext(), IDActivity.class);
startActivity(nextScreen3);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.menu_layout, container, false);
return rootView;
}
}
public void refresh(View view) {
// Getting reference to TextView
mTvCapital = (TextView) findViewById(R.id.tv_capital);
mTvCapital.setText("hello");
// Creating an intent service
mServiceIntent = new Intent(getApplicationContext(), CapitalService.class);
mServiceIntent.putExtra(Constants.EXTRA_ANDROID_ID, country);
// Starting the CapitalService to fetch the capital of the country
startService(mServiceIntent);
// Instantiating BroadcastReceiver
mReceiver = new CapitalReceiver();
// Creating an IntentFilter with action
mFilter = new IntentFilter(Constants.BROADCAST_ACTION);
// Registering BroadcastReceiver with this activity for the intent filter
LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(mReceiver, mFilter);
}
// Defining a BroadcastReceiver
private static class CapitalReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
String capital = intent.getStringExtra(Constants.EXTRA_APPROVAL);
if(capital == "YES") {
//status = "Status: Application Approved";//
//IDButton.setVisibility(View.VISIBLE);//
}
else if(capital == "NO"){
//status = "Status: Application Denied";//
}
}
}
}
Just glancing over your code, a possible solution (and perhaps not the best) would be to make the ID Button variable global. You would then instantiate it in the onCreate whilst still allowing it to be manipulated in other classes in this MenuActivity.
I hope this helps.
i am trying to make a note app.in an array i want to make id´s.
i want to check the id with an onclicklistener.
in the onclicklistener i make an Intent and an ExtraString
in this Extrastring i want to write the content from the note file i created
this is the code :
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.text.method.ScrollingMovementMethod;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import java.util.ArrayList;
import java.util.List;
public class NotizOeffnungsMenue extends Activity implements View.OnClickListener {
Button[] NoteListBtn ;
String[] NoteList ;
int [] e;
int i;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notiz_oeffnungs_menue);
LinearLayout l1 = (LinearLayout)findViewById(R.id.layout1);
NoteListBtn = new Button[fileList().length];
NoteList = fileList();
i = 0;
while (i < fileList().length)
{
NoteListBtn[i] = new Button(this);
NoteListBtn[i].setText(NoteList[i]);
NoteListBtn[i].setOnClickListener(this);
l1.addView(NoteListBtn[i]);
NoteListBtn[i].setId(i);
e [i] =i;
i++;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.notiz_oeffnungs_menue, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onClick(View view)
{
if(e[] == view.getId())
{
Intent switchintent = new Intent(this,NotizActivity.class);
String EXTRA_INHALT;
startActivity(switchintent);
}
}
}
In onClick() I would recommend u to use switch case statement
switch(v.getId()){
case R.id.butnid:
//do something here
break;
case R.id.butnid2:
//do something here
break;
}
Make as many cases as u want.
And remember that the buttons u use should do this
butnid.setOnClickListener(this);
butnid2.setOnClickListener(this);
//And so on for ever id u use in on click
You can use like this:
#Override
public void onClick(View view)
{
Intent switchintent = new Intent(this,NotizActivity.class);
switchintent.putExtra("EXTRA_INHALT", e[view.getId()]);
switchintent.putExtra("EXTRA_INHALT2", NoteList[view.getId()]);
startActivity(switchintent);
}
Instead of comparing id, you can use that as a position. Because you are already setting that position as Id.
Hope this helps.
I have created a MenuActivity which is having an Action Bar and an Split Action Bar. I want to use this actionbar and splitactionbar view for all activities in my application. I am a newbie to android so can somebody guide me stepwise about this.
Also I am trying to put the search icon on actionbar which is right now appearing on SplitActionBar. I have four icons on SplitActionBar and i want to show search icon on the actionbar not on the SplitActionBar. The search icon is a SearchView item which when clicked expands on ActionBar, which is very untidy. I want it to appear on rightmost position in the ActionBar and expand on the same when clicked.
This is MenuACtivity.java:
package com.example.travelplanner;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.TextView;
import com.example.travelplanner.R;
public class MenuActivity extends Activity implements OnItemClickListener{
Timer t1;
TimerTask tt1;
ImageView slide;
int currindex = 0;
ActionBar actionBar;
ArrayList<ItemDetails> image_details = GetSearchResults();
private int IMAGE_IDS[] = {R.drawable.slide1, R.drawable.slide2, R.drawable.slide3,R.drawable.slide4};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_menu);
final ListView lv = (ListView)findViewById(R.id.listView1);
lv.setAdapter(new MenuAdapter(this,image_details));
lv.setOnItemClickListener(this);
actionBar = getActionBar();
final Handler h = new Handler();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setIcon(R.drawable.ic_social_share);
LayoutInflater inflator = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.apptitle, null);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setCustomView(v);
int delay = 1000;
int period = 4000;
t1 = new Timer();
t1.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
// TODO Auto-generated method stub
h.post(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
animateSlideShow();
}
});
}
}, delay, period);
}
private void animateSlideShow(){
slide = (ImageView)findViewById(R.id.imagearr);
slide.setImageResource(IMAGE_IDS[currindex%IMAGE_IDS.length]);
currindex++;
Animation fade = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
slide.startAnimation(fade);
}
private ArrayList<ItemDetails> GetSearchResults(){
ArrayList<ItemDetails> results = new ArrayList<ItemDetails>();
ItemDetails item_details = new ItemDetails();
item_details.setmenuitem("Featured Tours");
item_details.setItemDescription("Mostly Recommended");
item_details.setImageNumber(1);
results.add(item_details);
item_details = new ItemDetails();
item_details.setmenuitem("Theme Tours");
item_details.setItemDescription("Some amazing experiences");
item_details.setImageNumber(2);
results.add(item_details);
item_details = new ItemDetails();
item_details.setmenuitem("Holiday Packages");
item_details.setItemDescription("Bundles of happiness");
item_details.setImageNumber(3);
results.add(item_details);
item_details = new ItemDetails();
item_details.setmenuitem("Tailor Tours");
item_details.setItemDescription("Custommize your tours");
item_details.setImageNumber(4);
results.add(item_details);
item_details = new ItemDetails();
item_details.setmenuitem("Events");
item_details.setItemDescription("Experience the culture");
item_details.setImageNumber(5);
results.add(item_details);
item_details = new ItemDetails();
item_details.setmenuitem("Enquiry");
item_details.setItemDescription("Ask your queries");
item_details.setImageNumber(6);
results.add(item_details);
return results;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_action_search).getActionView();
return true;
}
#Override
public void onItemClick(AdapterView<?> av, View v, int position, long id) {
// TODO Auto-generated method stub
if(position==0){
Intent i0 = new Intent(this,FeaturedTourActivity.class);
startActivity(i0);
}
else if(position==1){
Intent i1 = new Intent(this,MainActivity.class);
startActivity(i1);
}
else if(position==2){
Intent i2 = new Intent(this,TourCatActivity.class);
startActivity(i2);
}
else if(position==3){
Intent i3 = new Intent(this,TailoredoneActivity.class);
startActivity(i3);
}
else if(position==4){
Intent i4 = new Intent(this,MainActivity.class);
startActivity(i4);
}
else if(position==5){
Intent i5 = new Intent(this,EnquireActivity.class);
startActivity(i5);
}
else if(position==6){
Intent i6 = new Intent(this,MainActivity.class);
startActivity(i6);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case R.id.menu_action_search:
{
Intent intent_search = new Intent(this,MainActivity.class);
startActivity(intent_search);
break;
}
case R.id.menu_action_locate:
{
Intent intent_nearby = new Intent(this,NearbyPlacesActivity.class);
startActivity(intent_nearby);
break;
}
case R.id.menu_action_mail:
{
Intent intent_mail = new Intent(this,EnquireActivity.class);
startActivity(intent_mail);
break;
}
case R.id.menu_action_call:
{
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:9560875725"));
startActivity(callIntent);
break;
}
}
return super.onOptionsItemSelected(item);
}
}
Create BaseActivity which implements action bar.
And all your activities must inherit BaseActivity (not Activity)
public class BaseActiivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// action bar implementation
}
}
public class MainActivity extends BaseAcivity{
//your code
}
This is my first app and my first time posting so bear with me.
I'm trying to make it so the user can choose the background color of the entire app from a Change Skins screen. However with what I have now it only changes the color of the activity until it goes back to the main activity.
Here is the code for the Change Skins screen
package cs.pacificu.mypace;
import android.R.layout;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Layout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class Skin extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
final String[] SKINS = new String[] {"Light", "Dark"};
super.onCreate(savedInstanceState);
setContentView(R.layout.playlist);
final ListView listView = (ListView) findViewById (R.id.playlists);
listView.setAdapter(new ArrayAdapter<String> (this,R.layout.single_list_item,SKINS));
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(),
((TextView) view).getText(), Toast.LENGTH_SHORT).show();
//
View layout = findViewById(R.layout.activity_main);
String selectedFromList = (listView.getItemAtPosition(position).toString ());
if (SKINS [0] == selectedFromList)
{
listView.setBackgroundColor(Color.CYAN);
//layout.setBackgroundColor(android.R.color.darker_gray);
}
else if (SKINS[1] == selectedFromList)
{
listView.setBackgroundColor(Color.BLACK);
//layout.setBackgroundColor(android.R.color.black);
}
//
finish();
}
});
}
}
And here is the main activity code
package cs.pacificu.mypace;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
/*import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.MenuInflater;*/
import android.view.View;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
startPlaylists(null);
}
#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.skin:
changeSkin();
return true;
case R.id.action_settings:
settingsList();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void startPlaylists (View view)
{
Intent intentPlaylists = new Intent();
intentPlaylists.setClassName("cs.pacificu.mypace", "cs.pacificu.mypace.Playlist");
intentPlaylists.setAction("#strings/action_playlists");
startActivity(intentPlaylists);
}
public void changeSkin ()
{
Intent intentSkin = new Intent();
intentSkin.setClassName("cs.pacificu.mypace", "cs.pacificu.mypace.Skin");
intentSkin.setAction("#strings/action_skin");
startActivity(intentSkin);
}
public void settingsList ()
{
Intent intentSettings = new Intent();
intentSettings.setClassName("cs.pacificu.mypace", "cs.pacificu.mypace.Settings");
intentSettings.setAction("#strings/action_settings");
startActivity(intentSettings);
}
}
Thank you!
Put this on your xml
android:background="#025cd8"
Depending on your choice of color.
what you can do is set a SharedPreferences value to what the user selects, and at the start of each of your activities, set that value to the background colour of your layout. That is the best way I see to solving this.
So in all of your activities, you would set them to the value from the SharedPreferences and if not set use your default one. As for storing the possible values, you can either use the XML file or just hard code the values in one global class, although the former is preferable.
Look here and see what they did (Android-page-about-data-storage)
Edit:
If you are going to keep it simple like that, just background colour, then you can do something like this:
|- Keep these somewhat global, maybe in a class and access them as needed,
// Make sure to maintain the connection
String[] SKINS = {"Light", "Dark"};
int[] COLOURS = {Color.CYAN, Color.BLACK};
|- For the onItemClick(),
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
// You may wish to make these two variables class-global
SharedPreferences settings = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("mskin", COLOURS[position]);
editor.commit();
// For on the spot changes
listView.setBackgroundColor(COLOURS[position]);
// Your other code, as you wish, before or after, depends on how you need it.
|- As for your other activities, or at the onCreate() of all activities,
SharedPreferences settings = getPreferences(MODE_PRIVATE);
listView.setBackgroundColor(settings.getInt("mskin", COLOURS[0]));