mergeDebugResources (String-array in strings.xml) - android

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!

Related

My activity is unable to resolve a symbol to the layout file

I have the following activity
package com.example.responsivelayout;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class DisplayText extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_displaytext);
Intent intent = getIntent();
String message = intent.getStringExtra("com.example.responsivelayout.MESSAGE");
TextView tv = (TextView)findViewById(R.id.user_text);
tv.setText(message);
}
}
But the line setContentView(R.layout.activity_displaytext); gives error as "cannot resolve symbol". I have the following layout file as activity_displaytext.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.constraint.ConstraintLayout>
which is present in the layout/ folder. After trying all the efforts, i am clueless why i am getting this error. I tried project clean, make, eveything. Please help.
Make sure below import is not there
import android.R; //Resource class
It may be issue with import
Import should be
import com.example.responsivelayout.R;

Android Studio creates blank layout xml file on create new resource xml file

I am developing my project in Android Studio. While creating ListView application, I am trying to create a new layout .xml file for list_item_row
What I did is right click on /res/layout then LayoutResourceFile then given the name.
But it generates a blank file.
Even the root node is not available, just a blank screen.
The same thing happening while creating new menu xml file.
What is the problem in the Android Studio and how can I rectify it?
Hope now my questions is clear.
In android studio, goto File -> Editor -> File and Code Templates and make sure you have something like
If you dont have this, may be you have to UPDATE/REINSTALL android studio.
In android studio, goto File -> Editor -> File and Code Templates ->other add the following codes...
In AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="">
<application android:icon="#drawable/icon">
</application>
</manifest>
In valueResourceFile.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
</resources>
In resourceFile.xml
<?xml version="1.0" encoding="utf-8"?>
<${ROOT_TAG} xmlns:android="http://schemas.android.com/apk/res/android">
</${ROOT_TAG}>
In layoutResourceFile.xml
<?xml version="1.0" encoding="utf-8"?>
<${ROOT_TAG} xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="${LAYOUT_WIDTH}"
android:layout_height="${LAYOUT_HEIGHT}">
</${ROOT_TAG}>
In layoutResourceFile_vertical.xml
<?xml version="1.0" encoding="utf-8"?>
<${ROOT_TAG} xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="${LAYOUT_WIDTH}"
android:layout_height="${LAYOUT_HEIGHT}">
</${ROOT_TAG}>
In Activity.java
package ${PACKAGE_NAME};
import android.app.Activity;
import android.os.Bundle;
public class ${NAME} extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
In Fragment.java
package ${PACKAGE_NAME};
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ${NAME} extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return null;
}
}
In Application.java
package ${PACKAGE_NAME};
import android.app.Application;
public class ${NAME} extends Application {
}
In Service.java
package ${PACKAGE_NAME};
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class ${NAME} extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
}
In Broadcast_Receiver.java
package ${PACKAGE_NAME};
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class ${NAME} extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
}
}
In Remote_Interface.aidl
package ${PACKAGE_NAME};
interface ${NAME} {
}
I hope this will help you...

Android: fill actionbar list dynamically

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

Android - adapter code does not compile

I have some code which I think should compile, but doesn't:
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import com.problemio.ViewSolutionsActivity.DownloadWebPageTask;
import com.problemio.data.Discussion;
import com.problemio.data.DiscussionMessage;
import com.problemio.data.SuggestedSolution;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class TopicActivity extends Activity
{
ArrayAdapter<Discussion> adapter;
ArrayList<Discussion> discussion = new ArrayList <Discussion>( );
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.discussion);
// Have to display the topic, and the existing discussion, and the form field.
Discussion d = new Discussion ();
d.setDiscussionTopicName( "Please wait while the discussion comments load" );
discussion.add(d);
adapter = new ArrayAdapter<Discussion>( this,R.layout.discussion_comments, discussion);
setListAdapter(adapter);
ListView lv = getListView();
lv.setTextFilterEnabled(true);
There is more code below but the line with setListAdapter(adapter); gives this error:
The method setListAdapter(ArrayAdapter<Discussion>) is undefined for the type TopicActivity
Any idea why? I actually copied this code from another class and it worked well there.
Thanks!
You can only user setListAdapter() in a ListActivity
I think this should work extends your class to ListActivity.
You need to extend your activity with the ListActivity. Right now you have
public class TopicActivity extends Activity
Change it to
public class TopicActivity extends ListActivity
setListAdapter(adapter) works with
ListActivity or
if the layout has atleast one list view with id android.R.id.list

Formatting TextView

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))));

Categories

Resources