I have bulky-multi paragraph TextView. Contents of the TextView is loaded from Strings.xml.
I would like to show show multiple paragraphs. When I use <P></P> tag in strings.xml it is not reflected, the TextView shows all the script as one single paragraph.
Is there a way to break the text into several paragraphs?
The contents of strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, sirukathaikal!</string>
<string name="app_name">Eelathu Sirukathaikal</string>
<string name="main_title">®ÆòÐî º¢Ú¸¨¾¸û</string>
<string name="continue_label">njhlu;f...</string>
<string name="about_label">nrayp cjtp</string>
<string name="about_content">,e;j md;nuhapl; nrayp %yk; ,yq;ifiar; Nru;e;j kA+Nurd; vOjpa %d;W rpWfijfis ntspapLfpd;Nwhk;. njhlu;f vd;w nghj;jhidr; nrhLf;fp fijfis thrpf;fTk;. </string>
<string-array name="story_list">
<item>Story 1</item>
<item>Story 2</item>
<item>Story 3</item>
</string-array>
<string name="story_body">
<p>þ¼õ: ¦¸¡ØõÒ</p>
<p>§¿Ãõ: ¸¡¨Ä 7.30</p>
<p>¬ñÎ: 2019</p>
<p>Å¢ó¾¢Â¡ ¾ÉÐ ¨¸Â¢ø þÕìÌõ ¨À¨Â þÚì¸ «¨½ò¾ÀÊ ¿¼óÐ ¦¸¡ñÊÕó¾¡û. Å£¾¢ ¦ÅȢ¡Êì ¸¢¼ó¾Ð. ¾ÉÐ ¿¨¼¨Â ¦ÁøÄ ¦ÁøÄ §Å¸ôÀÎò¾¢ì¦¸¡ñ§¼ ţΠ§¿¡ì¸¢ ¿¼ì¸ò ¦¾¡¼í¸¢É¡û.
¨¸Â¢ø þÕìÌõ «ó¾ô ¨À¢ý ¦ÀÚÁ¾¢ «ÅÙìÌò¾¡ý ¦¾Ã¢Ôõ. Å£ðÊø «¨ÉÅÕõ þó¾ô ¨À¢ø þÕôÀ¨¾ò¾¡ý ±¾¢÷À¡÷òÐ ¸¡ò¾¢Õ츢ýÈ¡÷¸û. ¡÷ ¸ñ½¢Öõ ÀðÎÅ¢¼Á¡ø ¦ºýÚÅ¢¼§ÅñÎõ ±ýÀ¾¢ø ÌȢ¡¸ þÕó¾¡û. ÀòÐ ¬ñθÙìÌ Ó¾ø þó¾ þ¼ò¾¢ø þó§¿Ãò¾¢ø ºÉõ ÍõÁ¡ §ƒ.. §ƒ… ±ýÚ ¿¼Á¡Îõ. þô§À¡ ±øÄ¡õ ¾¨Ä¸£ú.
º¢ó¾¨É¸Ç¢ø ÍÆýÈÅ¡Ú ¿¼óЦ¸¡ñÊÕó¾¡û Å¢ó¾¢Â¡. «ó¾ ºó¾¢¨Âì ¸¼óÐÅ¢ð¼¡ø ¾ý ţ𨼠«Îò¾ 5 ¿¢Á¢¼ò¾¢ø «¨¼óÐÅ¢¼Ä¡õ ±ýÈ ¿õÀ¢ì¨¸Â¢ø ¿¨¼Â¢ý Å¢¨Ã¨Åì ÜðÊÂÅ¡Ú ºó¾¢¨Â §¿ì¸¢ ¿¼ì¸ò ¦¾¡¼í¸¢É¡û.
¾¢Ë÷ ±ýÚ ±í¸¢Õ󧾡 ´Õ «ÊÀðÎ ¦¿Ç¢ó¾ ¼¡¼¡ þýʸ¡ ¸¡Ã¢ø ¿¡ý¨¸óÐ þ¨Ç»÷¸û. ¸¡÷ ºò¨¾ò¨¾ Å¢¼ «Å÷¸û §À¡ð¼ ºò¾§Á «¾¢¸Á¡¸ þÕó¾Ð. ´Õò¾ý ¸¡Ð Ìò¾¢Â¢Õó¾¡ý ÁüÈÅý ¸ñþ¨Á¢ø ²§¾¡ Ìò¾¢Â¢Õó¾¡ý.
“§¼ö «í¸ À¡Õ¼¡! ²ö……….. ±ýÉ ¨¸Â¢Ä?” ¸¡Ã¢ø þÕó¾ ´Õò¾ý °¨Ç¢ð¼¡ý.</p>
<p>ÌÉ¢ó¾ ¾¨Ä ¿¢Á¢Ã¡Áø Å¢ó¾¢Â¡ «ó¾ þ¼ò¾¢ø þÕóÐ ¦ÁøÄ ¿¸Ãò ¦¾¡¼í¸¢É¡ø. «Å¨Ç º¢È¢Ð ¸¼óÐ ¿¢ýÚ þÕó¾ ¸¡÷ þô§À¡Ð, ¸¢Ã£î ±ýÈ ºò¾§¾¡Î «Åû ÓýÉ¡ø ÅóÐ ¿¢ýÈÐ. ¸¡Ã¢ø þÕóÐ ¿¡ýÚ þ¨Ç»÷¸Ùõ ¾¼ ¾¼¦ÅÉ þÈí¸¢É÷. ´ù¦Å¡Õò¾ý ¸ñ½¢Öõ ¦ÅÈ¢ ¾¡ñ¼ÅÁ¡ÊÂÐ.
´Õ «Ê ÓýÛìÌ ±ÎòÐ ¨Åò¾ ´Õò¾ý Å¢ó¾¢Â¡ ¨¸Â¢ø þÕó¾ ¨À¨Â ÀÈ¢ò¾¡ý. ¨À¨Â ¾¢ÈóÐ ¯û§Ç À¡÷ò¾Åý, ÁüÈÅ÷¸¨Çô À¡÷òÐ Òýɨ¸Ô¼ý ¾¨Ä¡ðÊÉ¡ý.
“§¼ö… ¾¡¼¡!!!” Å¢ó¾¢Â¡ ÀÈ¢ò¾Åý ¸ýÉò¾¢ø º¼¡÷ ±ýÚ ´Õ «¨È Å¢ð¼¡û. «¨Èó¾ ºò¾õ µöžüÌû ´Õ ºò¾õ ‘ÎÁ£ø’. Å¢ó¾¢Â¡Å¢ý ¯¼ø ¸£§Æ ºÃ¢Â «Åû ¯¼Ä¢ø þÕóÐ þÃò¾ý ÌÒ ÌÒ ±É ¦ÅÇ¢§ÂÈò ¦¾¡¼í¸¢ÂÐ.
Å¢ó¾¢Â¡Å¢üÌ Í ¿¢¨É× ¦ÁøÄ ¦ÁøÄ «¸Äò ¦¾¡¼í¸¢ÂÐ. ¸ñ¸û þÕð¼ò ¦¾¡¼í¸¢ÂÐ. ¸¨¼º¢Â¡¸ ¸ñ ã¼ ÓýÉ÷, ¾ý ¨À¢ø þÕó¾ À¡ñ Ðñ¨¼ «ó¾ì ¸ÂÅ÷¸û Å¢Äí̸¨Çô §À¡Ä À¢öòÐ ¯ñÀ¨¾ì ¸ñ¼¡û. ¦ÁøÄ ¦ÁøÄ «Åû ¯Ä¸õ þÕÇò ¦¾¡¼í¸¢ÂÐ.
À¢.Ì: Ôò¾õ Å¢¨ÃÅ¢ø µÂ¡Å¢ð¼¡ø ´Õ¿¡û ŠÃ£ Äí¸¡Å¢ø þÐ ¿¼ì¸ô §À¡ÅÐ ¿¢îºÂÁ</p>
</string>
</resources>
BodyStory1.java:
package com.mayuonline.com.esiru;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.TextView;
public class BodyStory1 extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.story_body);
Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/kaviri.TTF");
TextView tvBody = (TextView) findViewById(R.id.story_body);
tvBody.setTypeface(tf);
}
}
storyBody1.java after adding setText():
package com.mayuonline.com.esiru;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Html;
import android.widget.TextView;
public class BodyStory1 extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.story_body);
setText(Html.fromHtml(getString(R.string.story_body)));
Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/kaviri.TTF");
TextView tvBody = (TextView) findViewById(R.id.story_body);
tvBody.setTypeface(tf);
}
}
When setting the text of the TextView, try using
Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/kaviri.TTF");
TextView tvBody = (TextView) findViewById(R.id.story_body);
tvBody.setTypeface(tf);
tvBody.setText(Html.fromHtml(getString(R.string.story_body))));
Related
I am watching a android studio tutorial but have an error message.
i found out strings.xml have string-array tag with name of "items". However, they cannot be brought to MainActivity.java.
Error is "mergeDebugResources"
strings.xml
<resources>
<string name="app_name">List App</string>
<String-array name="items">
<item>peach</item>
<item>tomato</item>
<item>squash</item>
</String-array>
</resources>
MainActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
ListView myListView;
String[] items;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources res=getResources();
myListView=(ListView) findViewById(R.id.myListView);
items=res.getStringArray(R.array.items);
}
}
Please explain to me why the error happened! Thank you so much!
Hi I am making a converter app (just like the one when you google "temperature converter") So I have one spinner making two other spinners show the same array. I know theres code out there for this but i have not found some that deals with many different arrays. For instance I need one Spinner to be able to make the two other spinners show the same array but i have many different arrays to pic from (temperature/length/mass/speed/volume/area/time)
strings.xml
<string-array name="units">
<item>Temperature</item>
<item>Length</item>
<item>Mass</item>
<item>Speed</item>
<item>Volume</item>
<item>Area</item>
<item>Time</item>
</string-array>
<string-array name="Temperature">
<item>Celsius</item>
<item>Fahrenheit</item>
</string-array>
<string-array name="Mass">
<item>Metric Ton</item>
<item>Kilogram</item>
<item>Gram</item>
<item>Milligram</item>
<item>Pound</item>
<item>Ounce</item>
</string-array>
<string-array name="Length">
<item>Kilometer</item>
<item>Meter</item>
<item>Centimeter</item>
<item>Millimeter</item>
<item>Mile</item>
<item>Yard</item>
<item>Foot</item>
<item>Inch</item>
</string-array>
<string-array name="Speed">
<item>Miles/Hour</item>
<item>Feet/Sec</item>
<item>Meters/Sec</item>
<item>Km/Hour</item>
<item>Knot</item>
</string-array>
<string-array name="Time">
<item>Nanosecond</item>
<item>Microsecond</item>
<item>Meters/Sec</item>
<item>Km/Hour</item>
<item>Knot</item>
</string-array>
<string-array name="Area">
<item>Square km</item>
<item>Square meter</item>
<item>Square mile</item>
<item>Hectare</item>
<item>Acre</item>
<item>Square yard</item>
<item>Square foot</item>
<item>Square inch</item>
</string-array>
<string-array name="Area2">
<item>Square km</item>
<item>Square meter</item>
<item>Square mile</item>
<item>Hectare</item>
<item>Acre</item>
<item>Square yard</item>
<item>Square foot</item>
<item>Square inch</item>
</string-array>
Layout Xml File
<Spinner
android:id="#+id/selector1"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/editText1" />
<Spinner
android:id="#+id/spinner_unit"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="73dp"
android:entries="#array/units" />
<Button
android:id="#+id/equelsbutton"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/editText1"
android:background="#color/clear"
android:text="#string/equels"
android:textSize="40sp" />
<Spinner
android:id="#+id/selector2"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/selector1" />
<TextView
android:id="#+id/textView1"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editText1"
android:layout_alignLeft="#+id/selector2"
android:layout_marginLeft="15dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
Converter_Activity
import android.app.Activity;
import android.os.Bundle;
import android.widget.Spinner;
public class Converter_Activity extends Activity {
public Spinner spinnerunits;
public Spinner spinnerpicker1;
public Spinner spinnerpicker2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.conveter_activity);
spinnerunits = (Spinner) findViewById(R.id.spinner_unit);
spinnerpicker1 = (Spinner) findViewById(R.id.selector1);
spinnerpicker2 = (Spinner) findViewById(R.id.selector2);
}
}
Thanks
Hi i modified the Converter_Activity.java class. Please try with the latest source code. Its working fine for me. Hope it should helpful for you. Thanks.
Converter_Activity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
public class Converter_Activity extends Activity {
public static Spinner spinnerunits;
public static Spinner spinnerpicker1;
public static Spinner spinnerpicker2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinnerlayout);
spinnerunits = (Spinner) findViewById(R.id.spinner_unit);
spinnerpicker1 = (Spinner) findViewById(R.id.selector1);
spinnerpicker2 = (Spinner) findViewById(R.id.selector2);
ArrayAdapter <String> arrayUnits = new ArrayAdapter <String> (getBaseContext(),android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.units));
arrayUnits.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerunits .setAdapter(arrayUnits);
spinnerunits.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
if(spinnerunits.getSelectedItem().equals("Temperature"))
{
ArrayAdapter <String> s1 = new ArrayAdapter <String> (getBaseContext(),android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.Temperature));
s1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerpicker2.setAdapter(s1);
spinnerpicker1.setAdapter(s1);
}else if(spinnerunits.getSelectedItem().equals("Mass"))
{
ArrayAdapter <String> s2 = new ArrayAdapter <String> (getBaseContext(),android.R.layout.simple_spinner_item,getResources().getStringArray(R.array.Mass));
s2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerpicker2.setAdapter(s2);
spinnerpicker1.setAdapter(s2);
}
else if(spinnerunits.getSelectedItem().equals("Speed"))
{
ArrayAdapter <String> s3 = new ArrayAdapter <String> (getBaseContext(),android.R.layout.simple_spinner_item,getResources().getStringArray(R.array.Speed));
s3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerpicker2.setAdapter(s3);
spinnerpicker1.setAdapter(s3);
}
else if(spinnerunits.getSelectedItem().equals("Area"))
{
ArrayAdapter <String> s4 = new ArrayAdapter <String> (getBaseContext(),android.R.layout.simple_spinner_item,getResources().getStringArray(R.array.Area));
s4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerpicker2.setAdapter(s4);
spinnerpicker1.setAdapter(s4);
}
else if(spinnerunits.getSelectedItem().equals("Time"))
{
ArrayAdapter <String> s5 = new ArrayAdapter <String> (getBaseContext(),android.R.layout.simple_spinner_item,getResources().getStringArray(R.array.Time));
s5.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerpicker2.setAdapter(s5);
spinnerpicker1.setAdapter(s5);
}
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
}
}
Please try this code and let me know. Thanks.
I am getting this error. It says "edit_message can not be ressolved or is not a field" Can somebody tell me why I am getting this error is how can I fix it.
LAYOUT OF MAIN ACTIVITY: -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="horizontal">
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
</LinearLayout>
CODE OF MAIN ACTIVITY
package com.practice.firstapp1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE= "com.practice.fristapp1.MESSAGE";
#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;
}
/** Called when the user clicks the Send button */
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message); ///********ERRORR***********
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
LAYOUT OF SECOND ACTIVITY: -
<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=".DisplayMessageActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
CODE OF SECOND ACTIVITY: -
package com.practice.firstapp1;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
public class DisplayMessageActivity extends Activity {
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Make sure we're running on Honeycomb or higher to use ActionBar APIs
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
STRING RESOURCES:-
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">My First App</string>
<string name="edit_message">Enter a message</string>
<string name="button_send">Send</string>
<string name="action_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
<string name="title_activity_display_message">My Message</string>
<string name="hello_world">Hello world!</string>
</resources>
It looks like your code is right. Check the following things:
1) Save the layouts in xml, one reason could be its not saved.
2) Check there is no error in your xmls and project is build.
3) If you are using eclipse, go to Projects->Clean All Projects and then projects->Build Automatically.
4) Open your activity and press "cmd + shift + o" or ctrl+ shift+o (in windows). It shall import the R.java, select the com..R in the options.
I hope it shall solve the error.
You're missing import com.practice.firstapp1.R in MainActivity.java.
See the docs on Accessing Resources for more information.
I'm using SherlockActionbar and I wanted to fill dynamically the ListNavigation spinner in the actionbar. The problem is that my list navigation doesn't display anything. However the app is running on the emulator without any errors.
Here is the activity code:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.content.Context;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
public class TestView extends SherlockFragmentActivity {
private ArrayAdapter<String> listnav;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.test_view_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final ActionBar ab = getSupportActionBar();
ab.setDisplayShowTitleEnabled(false);
ab.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
Context context = ab.getThemedContext();
setContentView(R.layout.test_view);
List<String> items = new ArrayList<String>(Arrays.asList(getResources().getStringArray(R.array.testarray)));
listnav = new ArrayAdapter<String>(context, R.layout.sherlock_spinner_item, items);
listnav.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
}
}
Here is the code for the array resource:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="testarray">
<item >2011</item>
<item >2012</item>
</string-array>
</resources>
I solved this problem. The spinner in the actionbar can be accessed with the setListNavigationCallbacks.
For more Information about this method click here
I solved this problem. The spinner in the actionbar can be accessed with the setListNavigationCallbacks. For more Information about this method click here
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.