I have a problem with the preferences.
I created this simple project :
start.java:
package example.ex;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class start extends Activity {
Intent intent;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void preferences (View view){
intent = new Intent(getApplicationContext(), Preferences.class);
startActivity(intent);
}
}
Preferences.java:
package example.ex;
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class Preferences extends PreferenceActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
the main activity is the start, from which we go (via a button) to Preferences.
Preferences display the preferences( a list with a pop-up) .
Layout :
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="preferences"
android:onClick="preferences" />
</LinearLayout>
preferenze.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableRow>
<TextView
android:text="List Selection:"
android:paddingRight="5px"
/>
<TextView android:id="#+id/list"
/>
</TableRow>
</TableLayout>
in values i put arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="country">
<item>U.S. (United States of America)</item>
<item>Italia (Italy)</item>
</string-array>
<string-array name="codice_paese">
<item>US</item>
<item>IT</item>
</string-array>
</resources>
Finally, I created a preferences.xml (in a folder xml in res) :
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="Simple Preferences">
<ListPreference android:key="list" android:title="Selection Dialog"
android:summary="Click to pop up a list to choose from"
android:entries="#array/country" android:entryValues="#array/codice_paese"
android:dialogTitle="Choose a country" />
</PreferenceCategory>
</PreferenceScreen>
then: I have a main and then I go to a page of Preferences.
I would like to put directly into the main preferences (instead of the button) and use one Activity
but I can not do it in any way ..
please you have any suggestions?
thanks!
try something like this using SharedPreferences and Spinner instead
package com.example.spinnerpref;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
public class SpinnerPref extends Activity
{
int countrypos;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
countrypos = prefs.getInt("countrypos", 0);
setContentView(R.layout.main);
Spinner countryspinner = (Spinner) findViewById(R.id.country);
ArrayAdapter<CharSequence> countryadapater = ArrayAdapter.createFromResource(
this, R.array.country, android.R.layout.simple_spinner_item);
countryadapater.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
countryspinner.setAdapter(countryadapater);
countryspinner.setOnItemSelectedListener(new countrySelector());
class countrySelector implements OnItemSelectedListener
{
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
countrypos = pos;
}
public void onNothingSelected(AdapterView<?> parent) {}
}
PreferenceManager.getDefaultSharedPreferences(getApplicationContext())
.edit().putInt("countrypos", countrypos).commit();
}
}
something along those lines should be neater for what you are doing.
Related
I am following a online course and I can across this exercise that for some reason is not working. The onListItemClick() is not being invoked. I have tried troubleshooting this but I couldn't get rid of the issue
Please help me solve this issue.
My code for MainActivity.java is
package hk.ust.cse.comp107x.greetfriend;
import android.app.ListActivity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends ListActivity{
String names[];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
names=getResources().getStringArray(R.array.friends);
//setListAdapter((ListAdapter)new ArrayAdapter<String>(this,R.layout.friend_item,names));
setListAdapter( new ArrayAdapter<String>(this, R.layout.friend_item, names));
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent in=new Intent(this,ShowMessage.class);
in.putExtra("message","Good Day "+names[(int) id]+"!!");
startActivity(in);
}
}
Code for ShowMessage.java is
package hk.ust.cse.comp107x.greetfriend;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class ShowMessage extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_message);
Intent in=getIntent();
String message=in.getStringExtra("message");
TextView textMessage=(TextView)findViewById(R.id.textMessage);
textMessage.setText(message);
}
}
My code for friend.xml is
<?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:text="Friend Name"
android:gravity="center_vertical|center_horizontal"
android:autoText="true"
android:textSize="24sp"
android:padding="20dp"
android:id="#+id/textview">
</TextView>
And finally my code for strings.xml is
<resources>
<string name="app_name">GreetFriend</string>
<string-array name="friends">
<item>John</item>
<item>Paul</item>
<item>George</item>
<item>Ringo</item>
</string-array>
</resources>
Use the following code for your Friend.xml and run the code...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView android:layout_width="match_parent" android:layout_height="match_parent"
android:text="Friend Name"
android:gravity="center_vertical|center_horizontal"
android:textSize="24sp"
android:padding="20dp"
android:id="#+id/textviewfriend">
</TextView>
</LinearLayout>
I'm trying to get a ListView to show the contents of a simple array but my program crashes at the setAdapeter line. As I understand this is supposed to be because the final value comes out as null but I don't see why that would be the case or how to fix it. I have already tried using ArrayAdapeter instead as well as catching the null exception with a try block. The former changes nothing and the latter simply shows a blank activity with no list to speak of. Please help.
Here's Main Activity
package ballton.fowdeckbuilder;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ArrayAdapter;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnDecks = (Button) (findViewById(R.id.btnDecks));
Button btnCards = (Button) (findViewById(R.id.btnCards));
btnDecks.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, DeckCatalogue.class));
}
});
btnCards.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, CardCatalogue.class));
}
});
}
}
Here's the activity with the Problem
package ballton.fowdeckbuilder;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
public class DeckCatalogue extends AppCompatActivity {
ListView Ldecks;
String TAG = "TAG";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_deck_catalogue);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Ldecks = (ListView) (findViewById(R.id.DeckList));
String Decks[] = new String[] {"Faria","Melgis"};
ListAdapter feed = (new ArrayAdapter<String>(this, R.layout.show_deck, Decks));
Ldecks.setAdapter(feed);
}
}
Here's the Layout for that activity
<?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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="ballton.fowdeckbuilder.DeckCatalogue"
tools:showIn="#layout/activity_deck_catalogue">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/DeckList"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
And Here's the layout XML I use for the list times
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="#+id/Deck" />
</LinearLayout>
Use this code instead of yours:
ListAdapter feed = (new ArrayAdapter<String>(this, R.layout.show_deck, R.id.Deck, Decks));
This happened because you must supply the resource ID (as your xml: R.id.Deck) for the TextView you used for ListView.
I am trying to make a tab bar application in android, after searching in google i found some tutorial and i succeeded. I want 3 tabs, when i click on 1st tab some data will be taken from server using asyntask and show the progressdialog in frontend, but the progressdialog is capturing the whole screen(with tabbar) and because of that i can't be able to switch the 2nd or 3rd tab.
Expected Output: Whenever i click on any tab a data will be taken from server in background, as i know data is loaded in background so for that time i want to switch 2nd tab or any other tab.
package com.tabexample;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
public class ArrowsActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.arrowspage);
Button back = (Button) findViewById(R.id.BackButton2);
back.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
ProgressDialog p = new ProgressDialog(getParent());
p.setMessage("Loading");
p.setCancelable(false);
p.show();
p.getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
}
});
}
}
package com.tabexample;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
public class EditActivity extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, mListContent));
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
startActivity(new Intent(EditActivity.this, OptionsActivity.class));
}
});
}
private static String[] mListContent={"Item 1", "Item 2", "Item 3"};
}
package com.tabexample;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class OptionsActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.optionspage);
Button back = (Button) findViewById(R.id.BackButton1);
Button next = (Button) findViewById(R.id.NextButton1);
}
}
package com.tabexample;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
public class TabExmapleActivity extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tablayout);
TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("OPT")
.setContent(new Intent(this, ArrowsActivity.class)));
tabHost.addTab(tabHost.newTabSpec("tab2")
.setIndicator("EDIT")
.setContent(new Intent(this, EditActivity.class)));
tabHost.setCurrentTab(0);
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout02" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:id="#+id/TextView02" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Arrows Page">
</TextView>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/BackButton2"
android:text="Back" android:layout_below="#+id/TextView02">
</Button>
<Button android:id="#+id/Button01" android:layout_below="#id/BackButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Indeterminate Dialog"></Button>
<Button android:id="#+id/Button02" android:layout_below="#id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Select"></Button>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout01" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:id="#+id/TextView01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Options Page">
</TextView>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/BackButton1"
android:text="Back" android:layout_below="#+id/TextView01">
</Button>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/NextButton1"
android:text="Next" android:layout_toRightOf="#+id/BackButton1"
android:layout_below="#+id/TextView01"></Button>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" >
</FrameLayout>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
</TabWidget>
</RelativeLayout>
</TabHost>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tabexample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".TabExmapleActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="ArrowsActivity"></activity>
<activity android:name="EditActivity"></activity>
<activity android:name="OptionsActivity"></activity>
</application>
</manifest>
Just Put Progressbar for the child Activitys you are adding in TabHost .
First it will show progress bar of first activity in onCreate which you set as current activity and if you click on next tab it will show its progress dialog
if i get you correctly, you need to access the below activity components when the progress dialog is active.
for this you need to make non-modal/modeless progress dialog. non-modal dialogs will allow you to accept events by behind ui components
please refer the post below
timed modeless dialog
I'm trying to create a simple to-do list application from a book on Android development. When I try to run it, I get a "this application has stopped working" message. I have absolutely no clue why, except for the fact that it has something to do with a fatal error at setContentView(R.layout.main). Can someone help me fix it?
package com.test.tadalist;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
public class TadaList extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ListView listView = (ListView)findViewById(R.id.listView);
final EditText editText = (EditText)findViewById(R.id.editText);
final ArrayList<String> todoItems = new ArrayList<String>();
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, todoItems);
listView.setAdapter(adapter);
editText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent keyEvent) {
if (keyCode == KeyEvent.KEYCODE_ENTER)
{
todoItems.add(0, editText.getText().toString());
adapter.notifyDataSetChanged();
editText.setText("");
return true;
}
return false;
}
});
}
}
layout/main.xml:
<?xml version="1.0" encoding="utf-8"?>
<!-- -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android_layout_height="fill_parent">
<EditText
android:id="#+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Entry"
/>
<ListView
android:id="#+id/listView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
You're missing a lot in your layout file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:id="#+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Entry"
/>
<ListView
android:id="#+id/listView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
A stack trace would help, but I imagine it's likely because you don't have a container for your two views. Try enclosing your two views within something like a LinearLayout.
In my application I want to change the list view text color. In this case I am using XML file for list view. Is it possible? If yes then give the example.
gender.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/topelg"
>
<ListView
android:id="#+id/android:list"
android:layout_marginTop="60dip"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#000000"
/>
</LinearLayout>
Egender.java
package com.Elgifto;
import android.app.ListActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
public class Egender extends ListActivity{
Button b1;
ListView lv;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gender);
// b1=(Button) findViewById(R.id.butt1);
b1=new Button(this);
b1.setText("Done");
//b1.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
lv=(ListView) findViewById(android.R.id.list);
lv.addHeaderView(b1);
b1.setOnClickListener(new OnClickListener(){
public void onClick(View v)
{
// To send a result, simply call setResult() before your
// activity is finished.
finish();
}
});
lv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_single_choice, GENDER));
lv.setBackgroundColor(Color.BLACK);
lv.setItemsCanFocus(false);
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lv.setTextFilterEnabled(true);
}
private static final String[] GENDER = new String[] {
"Male","Female"
};
}
If you would like to change color of all ListView items, instead of passing default android.R.layout.simple_list_item_single_choice to ArrayAdapter you should pass custom list item XML, which has different TextColor attribute.
For example, created custom_list_item.xml under folder Layout:
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text1"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorSingle"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:textColor="#FF0000"
/>
Then passed it to adapter:
new ArrayAdapter<String>(this, R.layout.custom_list_item, stringList)
I've got list, were all items are red.