PLZ help! how to make the app save what the user write in edittext? - android-edittext

As shown in my picture, I want to save what the user writes as note. So, when the user reopens the app, the notes he wrote will stay under the calendar, btw i used EditText Multiline
https://i.stack.imgur.com/jcMcJ.jpg
package com.example.test;
import androidx.appcompat.app.AppCompatActivity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private EditText editText;
SharedPreferences sharedPreferences;
private static final String SHARED_PREF_NAME= "mypref";
private static final String KEY_NAME = "name";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=findViewById(R.id.editTextTextMultiLine);
sharedPreferences=getSharedPreferences(SHARED_PREF_NAME ,MODE_PRIVATE);
String name = sharedPreferences.getString(KEY_NAME,null);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(KEY_NAME, editText.getText().toString());
editor.apply();
editor.clear();
editor.commit();
}
}

I think you should move editor.clear() below editor.commit()

Related

How to make a button to do something in Blackberry

I am trying to make a button that shows a message when it is pressed, but cannot get it to work.
Here is my button:
HorizontalFieldManager buttonManager =
new HorizontalFieldManager(ButtonField.FIELD_HCENTER);
messageButton = new ButtonField("Press me", ButtonField.CONSUME_CLICK);
messageButton.setChangeListener(this);
buttonManager.add(messageButton);
add(buttonManager);
And here is the method that prints the message:
public void fieldChanged(Field field, int context) {
if (field == messageButton) {
showMessage();
}
}
private void showMessage() {
Dialog.inform("The button was pressed");
}
Am I doing something wrong in the showMessage() method, or id the error elsewhere?
// just paste this class and run
package mypackage;
import java.io.IOException;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
public final class MyScreen extends MainScreen
{
public MyScreen()
{
// Set the displayed title of the screen
HorizontalFieldManager horizontalFieldManager= new HorizontalFieldManager();
ButtonField buttonField= new ButtonField("click to show dialog",Field.FIELD_VCENTER);
horizontalFieldManager.add(buttonField);
buttonField.setChangeListener(buttonchangelisners);
add(horizontalFieldManager);
}
private FieldChangeListener buttonchangelisners= new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
// TODO Auto-generated method stub
showDialog("show your message");
}
};
public void showDialog(final String message) {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
Dialog.alert(message);
}
});
}
}
Check the event log (Alt + LGLG), I suspect you'll see an error about pushing a modal screen on a non-event thread. If that is the case just change showMessage to
private void showMessage()
{
UiApplication.getUiApplication.invokeLater(new Runnable()
{
public void run()
{
Dialog.inform("The button was pressed");
}
});
}
I think the problem might be the flag CONSUME_CLICK you are passing to the constructor. If you want to do something with the button this is certainly not a flag you'd want to use.
Other than this, I'd advise against using FieldChangeListener for fields. Maybe for buttons is ok, but for other components the fieldChanged method might be called without user interaction during layout. This is why you almost always want to filter out those calls whose second parameter (context in your code) is equals to FieldChangeListener.PROGRAMMATIC.
A better alternative is to override navigationClick, which also works both for tactile and regular screens.
invokeLater is nevertheless useful, because fieldChanged is invoked at some point within a chain of UI Engine actions, and you'd better let it finish the job before displaying the Dialog. I suspect not using invokeLater is the main reason for the problem.
CONSUME_CLICK is in fact necessary when using fieldChangeListener, otherwise context menu will be invoked. If you switch to using navigationClick within your button field, you can return true to achieve the same behavior (though I prefer using navigationUnclick).

Blackberry ObjectChoiceField Not Showing drop down list

ObjectChoiceField not working because the below code:
protected boolean navigationUnclick(int status, int time) {
return true;
}
I have added this code to remove the menus on touchevent. Means I have made a custom bottom tab and adding and deleting verticalfields on screen. When i touch any HorizontalField it shows menu that's why i added the above code.
This is my code contains ObjectChoiceField added in horizontalfield:
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.EncodedImage;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.TouchEvent;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.ObjectChoiceField;
import net.rim.device.api.ui.container.VerticalFieldManager;
import com.ec.pleasewaitpopup.PleaseWaitPopupScreen;
import com.np.naijapings.ApplicationFont;
import com.np.naijapings.Constant;
import com.np.naijapings.TabControlScreen;
import com.np.naijapings.intermediateclasses.GetUserListIntermediator;
import com.tv.servercommunication.WebServiceDetails;
public class FindUsersScreen extends VerticalFieldManager implements FieldChangeListener{
private VerticalFieldManager _mainVfm;
private VerticalFieldManager _contentVfm;
private BitmapField _headerBmp;
private EncodedImage _bitmap;
private LabelField _gender;
private LabelField _age;
private LabelField _religion;
private ObjectChoiceField _genderChoiceField;
private ObjectChoiceField _ageChoiceField;
private ObjectChoiceField _religionChoiceField;
private ButtonField _findUser;
private static String[] _genderChoices={"Both gender","Male","Female"};
private static String[] _ageChoices={"Any age","18-25","26-30","31-35","36-45","46-50"};
private String[] _religionChoices={"Any religion","Hindu","Muslim"};
public FindUsersScreen(){
//HEADER IMG
_bitmap = EncodedImage.
getEncodedImageResource("find-user_header.png");
_headerBmp = new BitmapField(Constant.sizePic(_bitmap, _bitmap.getHeight(), _bitmap.getWidth()));
//MAIN VFM
_mainVfm=new VerticalFieldManager();
//CONTENT VFM
final Bitmap tabBackGroundImage = Bitmap
.getBitmapResource("finduserscr_bg.png");
_contentVfm=new VerticalFieldManager(){
protected void paint(Graphics graphics) {
int y = FindUsersScreen.this.getManager().getVerticalScroll();
graphics.drawBitmap( 0, y, tabBackGroundImage.getWidth(), tabBackGroundImage.getHeight(), tabBackGroundImage, 0, 0 );
super.paint( graphics );
}
};
//CREATE WIDGETS
_gender=new LabelField("Gender");
_genderChoiceField=new ObjectChoiceField("Gender", _genderChoices,0){
protected boolean touchEvent(TouchEvent message) {
return super.touchEvent(message);
}
};
_age=new LabelField("Age");
_ageChoiceField=new ObjectChoiceField("Age", _ageChoices,0);
_religion=new LabelField("Religion");
_religionChoiceField=new ObjectChoiceField("Religion", _religionChoices,0);
_findUser=new ButtonField(" Find Users ",ButtonField.CONSUME_CLICK);
_findUser.setChangeListener(this);
//SET FONT TYPE
/*_gender.setFont(ApplicationFont.labelFont_16);
_genderChoiceField.setFont(ApplicationFont.labelFont_16);
_ageChoiceField.setFont(ApplicationFont.labelFont_16);
_age.setFont(ApplicationFont.labelFont_20);
_religionChoiceField.setFont(ApplicationFont.labelFont_20);
_religion.setFont(ApplicationFont.labelFont_20);
*/
//SET MARGIN
/*_gender.setMargin(5,20,5,20);
_age.setMargin(5,20,5,20);
_religion.setMargin(5,20,5,20);
*/
_contentVfm.setMargin(15,30,15,0);
_genderChoiceField.setMargin(10,5,5,5);
_religionChoiceField.setMargin(10,5,5,5);
_ageChoiceField.setMargin(10,5,5,5);
_findUser.setMargin(10,80,20,80);
_contentVfm.setMargin(30,10,30,10);
//ADD FIELDS TO CONTENT VFM
//_contentVfm.add(_gender);
_contentVfm.add(_genderChoiceField);
//_contentVfm.add(_age);
_contentVfm.add(_ageChoiceField);
//_contentVfm.add(_religion);
_contentVfm.add(_religionChoiceField);
_contentVfm.add(_findUser);
_mainVfm.add(_headerBmp);
_mainVfm.add(_contentVfm);
add(_mainVfm);
}
public void fieldChanged(Field field, int context) {
if(field==_findUser){
Object obAgeRange = _ageChoiceField.getChoice(_ageChoiceField.getSelectedIndex());
String ageRange = obAgeRange.toString();
Object obgender = _genderChoiceField.getChoice(_genderChoiceField.getSelectedIndex());
String gender = obgender.toString();
Object obReligion = _religionChoiceField.getChoice(_religionChoiceField.getSelectedIndex());
String religion = obReligion.toString();
GetUserListIntermediator getUserListIntermediator=new GetUserListIntermediator(ageRange,gender,religion);
PleaseWaitPopupScreen.showScreenAndWait(getUserListIntermediator, Constant.PLEASE_WAIT_TEXT);
}
}
}
could anyone answer me how to solve this problem.
You don't get drop-down list because the field code does not process unclick event. You have intercepted and consumed this event (via return true;) before your field can process it.
Try this code for your event handler.
protected boolean navigationUnclick(int status, int time) {
super.navigationUnclick(status, time);
return true;
}

Trying to write a hello world CLDC blackberry program

I got the book Advance Black Berry 6 Development.
I was able to get the midlet example to work, but not the first example for a CLDC program. It seems like it never gets to the code and when I run the app I get a blank white screen.
I tried to put a break point but it never went off.
Here is the code
package test.org;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
public class cHelloUniverses extends UiApplication{
public static void main(String[] args)
{
(new cHelloUniverses()).start();
}
public void start()
{
MainScreen main = new MainScreen();
LabelField label= new LabelField("Hello Ted");
main.add(label);
UiApplication app = UiApplication.getUiApplication();
app.pushScreen(main);
app.enterEventDispatcher();
}
}
Replace your start() method with this:
public void start()
{
MainScreen main = new MainScreen();
LabelField label= new LabelField("Hello Ted");
main.add(label);
this.pushScreen(main);
this.enterEventDispatcher();
}

BlackBerry app "no Application instance" error

I want this non-ui app to open from an icon press and then invoke the memopad to make a new memo.
But when I run it from the icon click I get,
""Uncaught exception: no Application instance""
What am I doing wrong? I extended the Application to say it is non-ui. The Invoke.invoke ... code is correct I know. It has something to do with the struct and instance of the app. But I'm stumped.
package mprn;
import net.rim.blackberry.api.invoke.*;
import net.rim.device.api.system.Application;
public class memopadrn extends Application
{
public static void main(String[] args)
{
Invoke.invokeApplication(Invoke.APP_TYPE_MEMOPAD, new MemoArguments(MemoArguments.ARG_NEW));
}
}
Your application never enters into the Event Dispatcher, try this (untested):
import net.rim.blackberry.api.invoke.Invoke;
import net.rim.blackberry.api.invoke.MemoArguments;
import net.rim.device.api.ui.UiApplication;
public class Memopadrn extends UiApplication {
public static void main(String[] args) {
new Memopadrn().enterEventDispatcher();
}
public Memopadrn() {
Invoke.invokeApplication(Invoke.APP_TYPE_MEMOPAD, new MemoArguments(MemoArguments.ARG_NEW));
System.exit(0);
}
}

How can I lock the keypad in Blackberry application using "lockSystem" method in J2ME?

How can I lock the keypad in Blackberry application using "lockSystem" method in J2ME ??
And also the brightness of the blackberry should reduce to Zero ??
its really easy. I know the answer. We can just use the method "lockSystem". I have coded as following to lock the keypad. It takes long time for me to find it, but u got this.
package net.asem;
import net.rim.device.api.system.ApplicationManager;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;
public class LockBlackberry extends UiApplication
{
public static void main(String[] args)
{
LockBlackberry lockB = new LockBlackberry();
lockB.enterEventDispatcher();
}
LockBlackberry()
{
pushScreen(new myBlackBerryClass());
}
}
final class myBlackBerryClass extends MainScreen implements FieldChangeListener<br>
{
LabelField title;
ButtonField btn1;
myBlackBerryClass()
{
LabelField title = new LabelField("Title : Locking the Device.",LabelField.USE_ALL_WIDTH | LabelField.USE_ALL_WIDTH);
setTitle(title);
btn1 = new ButtonField("KeyPad Loack ?",ButtonField.CONSUME_CLICK);
btn1.setChangeListener(this);
add(btn1);
}
public void fieldChanged(Field field, int context)
{
if(field == btn1)
{
Click();
}
}
private void Click()
{
ApplicationManager manager = ApplicationManager.getApplicationManager();
manager.lockSystem(true);
}
}

Resources