show an element based on what in a edittext in android - if-statement

I am a newcomer in android. Please forgive me if i am asking anything stupid.
I want to show/hide an element (TextView) based on the user input in an EditText element.
Basically there are 3 textview . Based on what is being entered in the edittext, only one of them should be shown ( in this example if mometasone is entered in the edittext, the textview with id strongsteroidtext should be shown and others should hide)
Here is the code pattern that I am using.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText creamname = (EditText) findViewById(R.id.creamname);
TextView nosteroid = (TextView) findViewById(R.id.nosteroidtext);
TextView weaksteroid = (TextView) findViewById(R.id.weaksteroidtext);
TextView strongsteroid = (TextView) findViewById(R.id.strongsteroidtext);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final String cream = (creamname.getText().toString().trim());
weaksteroidlist = R.string.weaksteroidlist;
{if (cream.equals("beclomethasone")) {
weaksteroid.setVisibility(View.VISIBLE);
nosteroid.setVisibility(View.INVISIBLE);
}
if (cream.equals("mometasone")) {
strongsteroid.setVisibility(View.VISIBLE);
nosteroid.setVisibility(View.INVISIBLE);
} else weaksteroid.setVisibility(View.INVISIBLE);
strongsteroid.setVisibility(View.INVISIBLE);
nosteroid.setVisibility(View.VISIBLE);
}
Now, when I am running this, apparently the "else" statement is only working, which means the "nosteroid" text that is supposed to be visible by this is visible. But when mometasone is being entered, nothing happens ( means the if statement is not working).
What am I getting wrong? please guide me.

OnCreate() method is called only the first time the view is created, not every time the text changes in ur EditText. Study android activity life circle first.
What you want can be achieved by registering ur editText with onKeyListener or onTextChangeListener
And aware this:
else
statement 1;
statement 2;
Is equal to
else {
statement 1;
}
statement 2;

Related

wicket I'm using three panels, but only one is displayed at a time, when the third panel is added to the code the second never appears

I have a Modal class, when this modal is opened, it shows a panel asking the user if the user wants to proceed with the operation. If the user selects Yes the request is sent to the DB, which takes some time, during this time the first panel should be replaced by the second (which displays a spinner). This indeed happens if we do not use the third panel. Although I want to replace the second panel by the third panel in order to inform the user if the operation was successful or not (which depends of the message object,if it is null or have an error message).
So when I use addNewPanel(panel3, target) I never see panel2. I put a thread.sleep(5000) instruction after addNewPanel(panel2, target) and even so, this panel didn't appeared, I only get the initial and panel3 in the end.
If I do not use panel3 I see panel2.
Does anyone have an idea why is this happening?
Below I have the code of the Modal class
public class DetailsModal2 extends Modal<IModel<UserDomain>>{
#SpringBean
private IService service;
private BootstrapAjaxLink<String> noButton;
private ResponseMessage message;
private ProcessingPanel panel2;
private AlertPanel panel3;
private Panel replacedPanel;
public DetailsModal2(String id, IModel<UserDomain> model){
super(id);
replacedPanel = new AreYouSure("replacedPanel");
replacedPanel.setOutputMarkupId(true);
add(replacedPanel);
panel2 = new ProcessingPanel("replacedPanel");
panel3 = new AlertPanel("replacedPanel");
addButton(new BootstrapAjaxLink<String>("button", null, Buttons.Type.Warning, new ResourceModel("details")){
private static final long serialVersionUID = 1L;
#Override
public void onClick(AjaxRequestTarget target) {
// TODO Auto-generated method stub
//I was expecting to see this panel
addNewPanel(panel2,target);
// this puts this button invisible
this.setVisible(false);
target.add(this);
//this changes the label of the No button to Close
noButton.setLabel(Model.of("Close"));
target.add(noButton);
if(!service.retrieveData())
{
message = service.addUser("X");
if(message == null){
panel3.updateClassAndText(true);
addNewPanel(panel3,target);
}
else {
panel3.updateClassAndText(false);
addNewPanel(panel3,target);
System.out.println(""+ message.getError());
}
}//close if
else if(service.retrieveData())
{
message = service.removeUser("X");
if(message == null){
panel3.updateClassAndText(true);
addNewPanel(panel3,target);
}
else{
panel3.updateClassAndText(false);
addNewPanel(panel3,target);
System.out.println(""+ message.getError());
}
}
else{
System.out.println("It was not possible to access the db");
}
}
}
});
noButton = new BootstrapAjaxLink<String>("button", null, Buttons.Type.Primary){
private static final long serialVersionUID = 1L;
#Override
public void onClick(AjaxRequestTarget target) {
close(target);
}
}.setLabel(Model.of("No"));
addButton(noButton);
}
public void addNewPanel(Panel addpanel, AjaxRequestTarget target ){
Panel newPanel = null;
newPanel = addpanel;
newPanel.setOutputMarkupId(true);
replacedPanel.replaceWith(newPanel);
target.add(newPanel);
}
}//close class
HTML
<wicket:extend>
<div><span wicket:id="replacedPanel"> </span></div>
</wicket:extend>
Wicket atmosphere is deprecated from wicket 8 and will not be supported anymore, so do not use it ..

Highlight Button onClick in if statement

I am recently struggling with creating a java based code on Android Studio which I am working on since 2days now. Unfortunately, I cannot find any resolution for my problem on the stackoverflow or any other forum..
I try to highlight a button when clicked in red or green depending on if the buttons text is text1 or text2 of a string-array.
If I run the application it always shows me an error or the button simply doesn't change it's color when I press it. I guess that this has to do with the if-clause, but I don't really know how to solve this problem in any other way.
I would really appreciate any help on this matter!
XML code for green button
<item
android:state_pressed="true"
android:drawable="#81c784"/>
<item
android:state_selected="true"
android:drawable="#color/#4caf50"/>
XML code for red button
<item
android:state_pressed="true"
android:drawable="#FF4081"/>
<item
android:state_selected="true"
android:drawable="#d50000"/>
Java code:
Button button1;
Button button2;
String[] txtString;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_layout);
txtString = getResources().getStringArray(R.array.optiontxt);
String random = txtString[new random().nextInt(txtString.length)];
button1 = (Button)findViewById(R.id.btn1);
button2 = (Button)findViewById(R.id.btn2);
if (random == txtString[0])
{
button1.setText(txtString[0]);
button1.setBackgroundResource(R.drawable.button_green);
button2.setText(txtString[1]);
button2.setBackgroundResource(R.drawable.button_red);
}
else
{
button1.setText(txtString[1]);
button1.setBackgroundResource(R.drawable.button_red);
button2.setText(txtString[0]);
button2.setBackgroundResource(R.drawable.button_green);
} button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {}}; button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {}};
I also tried varies other codes for setting the background of the buttons which didn't work aswell, e.g.
button1.setBackground(Drawable.createFromPath("/drawable/button_green"));
or
button1.getBackground().setState(new int[]{R.drawable.button_green});
or
button1.setBackgroundResource(R.drawable.button_green);
I would be very thankful for any help.
Thank you very much in advance!
Did you try to call button1.setsetBackgroundResource in the onCreate method?

how to do that when i click on the map, the marker disappeared?

I use asmdroid 4.2 and bonuspack 4.5.
my code add marker:
Marker startMarker = new Marker(mapView);
startMarker.setPosition(new GeoPoint(locationA,locationB));
startMarker.setIcon(getResources().getDrawable(drawable));
startMarker.setTitle("jkdfghspdifj");
startMarker.setAnchor(Marker.ANCHOR_CENTER, 1.0f);
MarkerInfoWindow infoWindow = new MyInfoWindow(R.layout.bonuspack_bubble,
mapView,title);
startMarker.setInfoWindow(infoWindow);
mapView.getOverlays().add(startMarker);
mapView.zoomToBoundingBox(boundingBox);
mapView.invalidate();
everything works fine :)
I click on the marker and displayed popup window. and how to do that when you click on the card itself, the marker disappeared? (at the moment it disappears unless again click on it)
To close all open popups when tapping anywhere on the map, follow the Tutorial 5, Chapter 16. Handling Map events
To have an open popup that closes when you are opening the popup on an other marker, a simple solution is to share the same infoWindow object between all your markers.
Or call "InfoWindow.closeAllInfoWindowsOn(map);" in onOpen.
paste in my class MarkerInfoWindow
in the metod onOpen
public void onOpen(Object arg0) {
Marker current = (Marker) arg0;
for(int i=0; i<mMapView.getOverlays().size(); ++i) {
Overlay o = mMapView.getOverlays().get(i);
if (o instanceof Marker) {
Marker m = (Marker) o;
if(m!=current)
m.hideInfoWindow();
}
}
}

Card not showing; goes straight to home card

I am trying to show a card so I know everything up to that point works. However, when I try to display the card, it just goes straight to the home card. The card I was trying to show was just going to display what was said in the voice recognizer before but that didn't work so I just put plain text and that didn't work either. Application goes - voice trigger --> voice recognizer --> this service:
public class MedMinderService extends Service {
public String MedName;
public String voiceResults;
private static final String TAG = "ShowData";
private static final String LIVE_CARD_ID = "showdata";
public static final String PREFS_NAME = "MyPreferencesFile";
private TimelineManager mTimelineManager;
private LiveCard mLiveCard;
#Override
public void onCreate() {
super.onCreate();
mTimelineManager = TimelineManager.from(this);
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
public int onStartCommand(Intent intent, int flags, int startId) {
String voiceResults = intent.getExtras()
.getString(RecognizerIntent.EXTRA_RESULTS);
String MedName = voiceResults; //MedName declared
SharedPreferences MedInfo = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = MedInfo.edit();
editor.putString("MedName", MedName.toString());
editor.commit();
mLiveCard = mTimelineManager.getLiveCard(LIVE_CARD_ID);
Intent i = new Intent(this, ShowDataActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
return START_STICKY;
}
}
The intent at the bottom goes to this activity:
public class ShowDataActivity extends Activity {
private LiveCard mLiveCard;
public static final String PREFS_NAME = "MyPreferencesFile";
private GestureDetector mGestureDetector;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences MedInfo = getSharedPreferences(PREFS_NAME, 0);
Card ShowDataCard = new Card(this);
ShowDataCard.setText("IT WORKS!");
//ShowDataCard.setText(MedInfo.getString("MedName", "your medication"));
View ShowDataCardView = ShowDataCard.toView();
setContentView(ShowDataCardView);
}
The "ShowDataCard" that has been commented out is what I was origonally trying to do with the voice recognition but it wouldn't even work with the text "IT WORKS!"
Again: I am just trying to show a card with the text "IT WORKS"
thanks
The easiest way to get a live card to appear with just text is using widgets that are compatible with RemoteViews. You can find a list of them in the GDK documentation here:
https://developers.google.com/glass/develop/gdk/ui/live-cards
under the Creating low-frequency live cards section.
Here is some sample code (based on your code above) that can get that working quickly:
final String LIVE_CARD_ID = "showdata";
mLiveCard = mTimelineManager.getLiveCard(LIVE_CARD_ID);
RemoteViews remoteViews =
new RemoteViews(getPackageName(), R.layout.layout_helloglass);
mLiveCard.setViews(remoteViews);
// Make sure Glass navigates to LiveCard immediately
mLiveCard.setNonSilent(true);
mLiveCard.publish();
The layout file can look like this for layout_helloglass.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Hello, Glass!" />
</FrameLayout>
If you still want to navigate to another Activity from your LiveCard, you need to associate the Activity with a PendingIntent and then associate that PendingIntent with the LiveCard's action. This would happen immediately before the LiveCard.publish() method:
Intent i = new Intent(this, ShowCardActivity.class);
mLiveCard.setAction(PendingIntent.getActivity(this, 0, i, 0));
That should get you up and running! Hopefully this will help.
There was a bug in the GDK Sneak Peek that prevented voice prompts from creating Services. If one inserted a Log.d() call in a Service's onStartCommand() override, they would discover that it were never called.
This bug has been fixed in the GDK Preview. This behavior should not appear again.
This question was rewritten after the GDK Preview launch to remove this outdated answer. Thanks to user Falcon for notifying me.

Android menuItem act strange?

I belive my question is kinda simple for most experienced Android developers but not for me!
I am trying to set the name in a menuItem and make the text WHITE colored. First of all the text gets displayed only if I klick on the menuItem, or rather when I click on the menuItem the text turns white and is readable. What I dont know is if the text gets displayed only when I click on the menuItem or if it for some reason changes color to WHITE when clicked so I am able to see it? Any useful help how I can make the text white and visible all the time in the menuItem?
item.setTitle(this.task.getName()); is supposed to be White and visible all the time in the menuItem!. Thx alot!
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate( R.menu.show_task_feedback_menu, menu );
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch ( item.getItemId() )
{
case R.id.show_task_feedback_menu_add_feedback: item.setTitle(this.task.getName()); <------------- Here is the problem!!!!!!!!!!
this.startTaskFeedback();
return true;
default: return true;
}
}