Adding a raw .txt file to a layout - java

I'm trying to add a text file for a help page on my app but with the code I have it's just crashing the app when I select help from the options menu. I know it's to do with this code as when I comment it out it opens the help page fine.
The code is in my HelpActivity class:
public class HelpActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.help_page);
}
InputStream iFile = getResources().openRawResource(R.raw.gamehelp);
private String inputStreamToString(InputStream iFile) {
TextView helpText = (TextView) findViewById(R.id.tvHelpText);
String strFile = inputStreamToString(iFile);
helpText.setText(strFile);
return strFile;
}
}
Can anyone see any problem with how I'm trying to do this?
Thanks

Since you didn't include any error logs, I'm judging just from your code:
InputStream iFile = getResources().openRawResource(R.raw.gamehelp);
You are calling this in class body, but getResources() is not available here. What you should do is:
InputStream iFile;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.help_page);
iFile = getResources().openRawResource(R.raw.gamehelp);
}

Related

Message (toast) not showing

I have a list of buttons and what I want to do is display some information on press. Here´s the code:
public class ActividadDos extends AppCompatActivity {
private Map<CharSequence, String> info = new HashMap();
private Button btnYogur;
private Button btnChocolate;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_actividad_dos);
btnYogur = (Button) findViewById(R.id.btnYogur);
btnChocolate = (Button) findViewById(R.id.btnChocolate);
info.put(btnYogur.getText(), "Lácteos");
info.put(btnChocolate.getText(), "Grasas, aceites y dulces");
}
public void displayMensaje(View v) {
Button button = (Button) v;
String nombre = (String) button.getText();
String mensaje = "Clasificación: " + info.get(nombre);
Toast.makeText(ActividadDos.this, mensaje, Toast.LENGTH_SHORT).show();
}
}
It doesn´t show any errors but when I press the buttons it doesn´t display the message.
Similar code is working on another activity.
You forgot to call displayMensaje() method.
Add this to .xml for corresponding button(As per requirement)
android:onClick="displayMensaje"
It isn't displaying anything because you aren't calling displayMensaje() method anywhere in your code at all. To solve this, please add a call to this method and your problem will be solved.

How to use MediaMetadataRetriever SetDataSource for file in external storage

I'm trying to extract Album art from MP3 file But I not able to give proper location of song, which I can give it to SetDataSource method. I tried everything but still it gives me error IllegalArgumentException.
It all happens when this line metaRetriver.setDataSource(MainActivity.this, Uri.parse("/sdcard/song.mp3")); gets executed
My Code.
public class MainActivity extends AppCompatActivity {
MediaMetadataRetriever metaRetriver;
byte[] art;
ImageView album_art;
Bitmap songImage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getInit();
metaRetriver = new MediaMetadataRetriever();
metaRetriver.setDataSource(MainActivity.this, Uri.parse("/sdcard/song.mp3"));
art = metaRetriver.getEmbeddedPicture();
if (art != null) {
songImage = BitmapFactory
.decodeByteArray(art, 0, art.length);
album_art.setImageBitmap(songImage);
} else {
String error = "art is null";
Log.i("lolol", error);
}
}
public void getInit() {
album_art = (ImageView) findViewById(R.id.album_art);
}
}
Can anyone solve this problem.
From the Android Documentation:
Throws IllegalArgumentException if the Uri is invalid
Double check that the song.mp3 file is in the right place, and the app has permission to see it.

How to change the font in my app based on Locale?

public class MainActivity extends AppCompatActivity {
TextView hi;
Typeface ty;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hi = (TextView) findViewById(R.id.hi);
ty = Typeface.createFromAsset( getAssets() , "fonts/Spirax-Regular.ttf");
hi.setTypeface(ty);
}
}
this code worked very well for me.
My question is if my application support two languages which are (Arabic and English), is there any way to add two types of fonts to the same text? one for the Arabic Text and the other for the English one?
Try this
if(Locale.getDefault().getISO3Language().equals("ar")){
//LANGUAGE IS ARABIC
text.setTypeface(arabicTypeface);
}
else{
//LANGUAGE IS ENGLISH
text.setTypeface(englishTypeface);
}
This is not the proper way but this will solve your problem
textView.setText(Html.fromHtml(text));
This type of example is use to simply change the text font.
Solution :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
hi = (TextView) findViewById(R.id.tvTime);
setContentView(R.layout.activity_main);
if(getResources().getConfiguration().locale.getLanguage().equals("ar"))
hi.setTypeface(Typeface.createFromAsset( getAssets() , "YOUR ARABIC .TTF FILE HERE"));
else if(getResources().getConfiguration().locale.getLanguage().equals("en"))
hi.setTypeface(Typeface.createFromAsset( getAssets() , "YOUR ENGLISH .TTF FILE HERE"));
}

How to fix error with rich text editor(android-RTEditor)?

I would like to use RTEditor in my app. but I have some errors in code. My question is what can I do with that? Instruction about RTEditor is here: https://github.com/1gravity/Android-RTEditor
and that's part of my code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.RTE_BaseThemeDark);
setContentView(R.layout.activity_editor);
newRowEdit = (EditText) findViewById(R.id.editNote);
newRowEdit1 = (EditText) findViewById(R.id.rtEditText);
// create RTManager
RTApi rtApi = new RTApi(this, new RTProxyImpl(this), new RTMediaFactoryImpl(this, true));
RTManager rtManager = new RTManager(rtApi, savedInstanceState);
// register toolbar
ViewGroup toolbarContainer = (ViewGroup) findViewById(R.id.rte_toolbar_container);
RTToolbar rtToolbar = (RTToolbar) findViewById(R.id.rte_toolbar);
if (rtToolbar != null) {
rtManager.registerToolbar(toolbarContainer, rtToolbar);
}
// register editor & set text
RTEditText rtEditText = (RTEditText) findViewById(R.id.rtEditText);
rtManager.registerEditor(rtEditText, true);
rtEditText.setRichTextEditing(true, message);
String text = rtEditText.getText(RTFormat.HTML);
}
Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mRTManager.onSaveInstanceState(outState);
}
#Override
public void onDestroy() {
super.onDestroy();
mRTManager.onDestroy(isFinishing());
}
The error is: cannot resolve symbol: message and cannot resolve symbol:mRTManager.
Can anyone help me please?
I can't see the whole code but it seems that both variables are not declared inside the corresponding methods. Make sure they are declared as class member variables
Replace mRTManager with rtManager, and make sure rtManager is declared outside onCreate method.

Transliteration in Android Using GoogleAPI

Need Help..! I am working on Android Transliteration and I'm getting following 3 errors in my code.
1)GoogleAPI cannot be resolved
2)Translate cannot be resolved
3)Language cannot be resolved
I have properly imported all the required packages and also have added required external jar files. But unable to know where actually I'm going wrong..
Following is my code snippet-->
public class MainActivity extends Activity
{
EditText myInputText;
Button myTranslateButton;
TextView myOutputText;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myInputText = (EditText)findViewById(R.id.inputText);
myTranslateButton = (Button)findViewById(R.id.translateButton);
myOutputText = (TextView)findViewById(R.id.outputText);
myTranslateButton.setOnClickListener(MyTranslateButtonOnClickListener);
}
private Button.OnClickListener MyTranslateButtonOnClickListener
= new Button.OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
String InputString;
String OutputString = null;
InputString = myInputText.getText().toString();
try
{
GoogleAPI.setHttpReferrer("http:\\www.google.com");
GoogleAPI.setKey("API_KEY");
OutputString = Translate.DEFAULT.execute(InputString,Language.ENGLISH, Language.HINDI);
}
catch (Exception ex)
{
ex.printStackTrace();
OutputString = "Error";
}
Typeface customF = Typeface.createFromAsset(getAssets(), "akshar.ttf");
//final TextView textV = (TextView) findViewById(...);
myOutputText.setTypeface(customF);
myOutputText.setText(OutputString);
}
};
}
For your better understanding please have a look on following screenshots
Image-1 Packages Imported
Image-2 Code that Contains Error
Image-3 My Build Configuration
Please help.. Thanks...!!
Import these two:
import com.google.api.translate.Language;
import com.google.api.translate.Translate;
& I think it would be better if you use the translation as a separate method.Check this out.

Categories

Resources