Creating a "next" button that switches the content of a TextView - android

I have a popup text view with a next button and onClick I would like to switch the content.
This is what I tried ...cant get it to work. The error im getting is type mismatch cannot convert from void to int
package com.jibushi;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class LessonsShell extends Activity{
private static final int MESSAGE_SHOW_POPUP=7;
private static final long TIME_DELAY=1000;//1 seconds
private View view;
private Button nextButton;
private Button skipButton;
private TextView nextSwitch;
private int tutStrings;
private int tutStrings2;
private int tutStrings3;
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch(msg.what) {
case MESSAGE_SHOW_POPUP:
view();
break;
}
};
};
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.lessons);
//this will send a message to the handler to display the popup after 1 seconds.
handler.sendEmptyMessageDelayed(MESSAGE_SHOW_POPUP,TIME_DELAY);
nextSwitch = (TextView) findViewById(R.id.lessonsDialog);
tutStrings = nextSwitch.setText(R.string.lessons_introduction); //type mismatch cannot convert from void to int
tutStrings2 = nextSwitch.setText(R.string.lessons_introduction2); //type mismatch cannot convert from void to int
tutStrings3 = nextSwitch.setText(R.string.lessons_introduction3); //type mismatch cannot convert from void to int
this.nextButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case tutStrings:
nextSwitch.setText(R.string.lessons_introduction2);
break;
case tutStrings2:
nextSwitch.setText(R.string.lessons_introduction3);
break;
}
}
});
}
private void view() {
// TODO Auto-generated method stub
ViewGroup parent = (ViewGroup) findViewById(R.id.lessons_bg);
view = LayoutInflater.from(getBaseContext()).inflate(R.layout.lessons_dialog, null);
parent.addView(view);
}
public Button getSkipButton() {
return skipButton;
}
public void setSkipButton(Button skipButton) {
this.skipButton = skipButton;
skipButton.findViewById(R.id.skip_button);
}
public Button getNextButton() {
return nextButton;
}
public void setNextButton(Button nextButton) {
this.nextButton = nextButton;
nextButton.findViewById(R.id.next_button);
}
}
Yeah, lack of research. Didn't know about string arrays...finally got it!
package com.jibushi;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class LessonsShell extends Activity{
private static final int MESSAGE_SHOW_POPUP=7;
private static final long TIME_DELAY=1000;//1 seconds
private View view;
private int count = 0;
private TextView lessonsDialog;
private String[] lessonDialogString;
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch(msg.what) {
case MESSAGE_SHOW_POPUP:
view();
break;
}
};
};
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.lessons);
//this will send a message to the handler to display the popup after 1 seconds.
handler.sendEmptyMessageDelayed(MESSAGE_SHOW_POPUP,TIME_DELAY);
}
private void view() {
// TODO Auto-generated method stub
ViewGroup parent = (ViewGroup) findViewById(R.id.lessons_bg);
view = LayoutInflater.from(getBaseContext()).inflate(R.layout.lessons_dialog, null);
parent.addView(view);
lessonsDialog = (TextView) findViewById(R.id.lessonsDialog);
Resources res = getResources();
myString = res.getStringArray(R.array.lessons_dialog_array);
Button nextButton = (Button) findViewById(R.id.next_button);
nextButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (count < lessonDialogString.length) {
lessonsDialog.setText(lessonDialogString[count]);
count++;
}
}
});
}
}

There are a few problems here.
I don't know what you are trying to do here:
nextSwitch = (TextView) findViewById(R.id.lessonsDialog);
tutStrings = nextSwitch.setText(R.string.lessons_introduction); //type mismatch cannot convert from void to int
tutStrings2 = nextSwitch.setText(R.string.lessons_introduction2); //type mismatch cannot convert from void to int
tutStrings3 = nextSwitch.setText(R.string.lessons_introduction3); //type mismatch cannot convert from void to int
the TextView.setText(String) does not return a value so these lines don't make any sense. Are you trying to save the strings themselves?
Also your onClick method has problems as well. The View that is passed to onClick is the actual view that was clicked which in this case will be the nextButton. In your code you are switching on the undefined ints and not the id's of the different views in your activity.
Instead do something like this:
void onClick(View v){
switch(v.getId()){
case R.id.next_button:
setTextViewNextString();
break;
case R.id.prev_button:
setTextViewPrevString();
}
Then you simply define the setTextViewNextString() and setTextViewPrevString() methods

The method setText returns void:
public final void setText (int resid)
(from http://developer.android.com/reference/android/widget/TextView.html#setText%28int%29)
You cannot assign void to int variables. These lines
tutStrings = nextSwitch.setText(R.string.lessons_introduction); //type mismatch cannot convert from void to int
tutStrings2 = nextSwitch.setText(R.string.lessons_introduction2); //type mismatch cannot convert from void to int
tutStrings3 = nextSwitch.setText(R.string.lessons_introduction3); //type mismatch cannot convert from void to int
are wrong.
I think you can solve your problem in this way:
1) Save the IDs in your private variables:
tutStrings = R.string.lessons_introduction;
tutStrings2 = R.string.lessons_introduction2;
tutStrings3 = R.string.lessons_introduction3;
2) Save the strings, getting them from resources:
private String s1, s2, s3;
...
s1 = this.getString(R.string.lessons_introduction);
s2 = this.getString(R.string.lessons_introduction2);
s3 = this.getString(R.string.lessons_introduction3);
3) Now you can use the onClick method:
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case tutStrings:
nextSwitch.setText(s2);
break;
case tutStrings2:
nextSwitch.setText(s3);
break;
}
}

Sorry, lack of research on my part. Used string-array.. works perfectly!

Change your Code like this:
tutStrings = this.getString(R.string.lessons_introduction);
tutStrings2 = this.getString(R.string.lessons_introduction2);
tutStrings3 = this.getString(R.string.lessons_introduction3);
Please Note: In onClick you have to replace this with the name of your class e.g. Myclass.getString or just remove the this.

Related

Passing Boolean value from fragments to activity

I am new to android development and I am trying to pass a Boolean value from dialog fragment to the activity.
the boolean value is supposed to be decided by the user(depends on which button user clicked). However, the boolean immediately turned to false without clicking any button.
I have tried various method recommended I found on internet but none of them works for me(I guess I have some part screwed up...), these method include:
-broadcasting
-implementing interface
-intent.putExtra
and below is the code that I have came up with, could anyone help me take a look? Any help is appreciated.
Stage:
package com.example.fuj.valorsafeworldbytrade;
import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import static com.example.fuj.valorsafeworldbytrade.LosingDialogFragment.LOSING_FRAGMENT;
public class Stage extends AppCompatActivity {
String stage;
FragmentManager fragmentManager = getSupportFragmentManager();
LosingDialogFragment losingDialogFragment = new LosingDialogFragment();
BasicInfo basicInfo = new BasicInfo();
public void setText(){
TextView cpuReputation = (TextView) findViewById(R.id.cpu_reputation);
TextView cpuGold = (TextView) findViewById(R.id.cpu_gold);
TextView pGoldText = (TextView) findViewById(R.id.player_gold);
TextView pRepText = (TextView) findViewById(R.id.player_reputation);
cpuReputation.setText(String.valueOf(basicInfo.cRep));
cpuGold.setText(String.valueOf(basicInfo.cGold));
pGoldText.setText(String.valueOf(basicInfo.pGold));
pRepText.setText(String.valueOf(basicInfo.pRep));
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stage);
setText();
}
protected void onStart() {
super.onStart();
stage = getIntent().getStringExtra(StageChoosingMenu.STAGE);
}
public void playerChoice(View view) {
boolean deceiveEnabled;
switch (view.getId()) {
case R.id.cooperate_button:
deceiveEnabled = false;
break;
case R.id.deceive_button:
deceiveEnabled = true;
break;
default:
throw new RuntimeException("Unknown Button ID");
}
switch (stage){
case "xumo":
xuMo(deceiveEnabled);
break;
}
}
public void xuMo(boolean playerDeceiveEnabled){
boolean cpuDeceiveEnabled;
cpuDeceiveEnabled = (Math.random() - basicInfo.faith > 0);
if (cpuDeceiveEnabled){
if (playerDeceiveEnabled){
basicInfo.playerDvsCpuD();
// faith changes to be amend w/ proper value, need record on the change of status
}
if (!playerDeceiveEnabled){
basicInfo.playerCvsCpuD();
}
}
if (!cpuDeceiveEnabled){
if (playerDeceiveEnabled){
basicInfo.playerDvsCpuC();
}
if (!playerDeceiveEnabled){
basicInfo.playerCvsCpuC();
}
}
if(basicInfo.pGold <= 0 || basicInfo.cGold <= 0 || basicInfo.pRep <= 0 || basicInfo.cRep <= 0){
//to be changed
setText();
losingDialogFragment.show(fragmentManager,LOSING_FRAGMENT);
//trying to show a alert dialog fragment
Log.d("True", String.valueOf(losingDialogFragment.tryAgainEnabled));
if(losingDialogFragment.tryAgainEnabled){//tryAgainEnabled is always false
//This is the part that I wanted to retrieve data from the user(if they want to try again or not)
basicInfo.reset();
losingDialogFragment.dismiss();
}else{
losingDialogFragment.dismiss();
}
}
setText();
// to be changed
}
}
LosingDialogFragment:
package com.example.fuj.valorsafeworldbytrade;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
/**
* Created by fuj on 20/1/2017.
*/
public class LosingDialogFragment extends DialogFragment{
public static final String LOSING_FRAGMENT = "LOSING";
public boolean tryAgainEnabled;
Intent intent = new Intent();
#Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.fragment_lose, container, false);;
//the following code is used to set the boolean value after the user click the button
final Button tryAgainButton = (Button)rootView.findViewById(R.id.try_again_button);
tryAgainButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tryAgainEnabled = true;
}
});
Button giveUpButton =(Button)rootView.findViewById(R.id.give_up_button);
giveUpButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tryAgainEnabled = false;
}
});
getDialog().setTitle(LOSING_FRAGMENT);
return rootView;
}
}
I am also sorry that I am not the best with English, if any things is not clear or not polite because of my poor English, please kindly let me know. I apologize in advance for any mistakes I have made.
It's because you check the value of tryAgainEnabled exactly right after showing the Dialog. Dialog starts Asynchronously, It means that it starts in diffrent Thread and your current thread doesn't wait for dismissing Dialog, So this line of your code if(losingDialogFragment.tryAgainEnabled){ runs exactly after you show dialog, befor you set value to tryAgainEnabled. Because the default value of boolean is always false, you will get false everytime.
I suggest that use listener for this:
Dialog:
public class LosingDialogFragment extends DialogFragment {
public static final String LOSING_FRAGMENT = "LOSING";
public boolean tryAgainEnabled;
Intent intent = new Intent();
private View.OnClickListener onTryAgainButtonClickLisnter;
private View.OnClickListener onGiveUpButtonClickLisnter;
#Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.fragment_lose, container, false);;
final Button tryAgainButton = (Button)rootView.findViewById(R.id.try_again_button);
tryAgainButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(onTryAgainButtonClickLisnter!=null)
onTryAgainButtonClickLisnter.onClick(v);
}
});
Button giveUpButton =(Button)rootView.findViewById(R.id.give_up_button);
giveUpButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(onGiveUpButtonClickLisnter!=null)
onGiveUpButtonClickLisnter.onClick(v);
}
});
getDialog().setTitle(LOSING_FRAGMENT);
return rootView;
}
public void setOnTryAgainButtonClickLisnter(View.OnClickListener onTryAgainButtonClickLisnter) {
this.onTryAgainButtonClickLisnter = onTryAgainButtonClickLisnter;
}
public void setOnGiveUpButtonClickLisnter(View.OnClickListener onGiveUpButtonClickLisnter) {
this.onGiveUpButtonClickLisnter = onGiveUpButtonClickLisnter;
}
}
In your activity call dialog like this:
LosingDialogFragment losingDialogFragment = new LosingDialogFragment();
losingDialogFragment.setOnTryAgainButtonClickLisnter(new View.OnClickListener() {
#Override
public void onClick(View v) {
tryAgain();
losingDialogFragment.dismiss();
}
});
losingDialogFragment.setOnGiveUpButtonClickLisnter(new View.OnClickListener() {
#Override
public void onClick(View v) {
giveUp();
losingDialogFragment.dismiss();
}
});
losingDialogFragment.show(fragmentManager, "");
Declare public boolean tryAgainEnabled; in your Stage Activity and youd can update that variable using ((Stage)context).tryAgainEnabled.
package com.example.fuj.valorsafeworldbytrade;
import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import static com.example.fuj.valorsafeworldbytrade.LosingDialogFragment.LOSING_FRAGMENT;
public class Stage extends AppCompatActivity {
String stage;
FragmentManager fragmentManager = getSupportFragmentManager();
LosingDialogFragment losingDialogFragment = new LosingDialogFragment();
BasicInfo basicInfo = new BasicInfo();
public boolean tryAgainEnabled;
public void setText(){
TextView cpuReputation = (TextView) findViewById(R.id.cpu_reputation);
TextView cpuGold = (TextView) findViewById(R.id.cpu_gold);
TextView pGoldText = (TextView) findViewById(R.id.player_gold);
TextView pRepText = (TextView) findViewById(R.id.player_reputation);
cpuReputation.setText(String.valueOf(basicInfo.cRep));
cpuGold.setText(String.valueOf(basicInfo.cGold));
pGoldText.setText(String.valueOf(basicInfo.pGold));
pRepText.setText(String.valueOf(basicInfo.pRep));
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stage);
setText();
}
protected void onStart() {
super.onStart();
stage = getIntent().getStringExtra(StageChoosingMenu.STAGE);
}
public void playerChoice(View view) {
boolean deceiveEnabled;
switch (view.getId()) {
case R.id.cooperate_button:
deceiveEnabled = false;
break;
case R.id.deceive_button:
deceiveEnabled = true;
break;
default:
throw new RuntimeException("Unknown Button ID");
}
switch (stage){
case "xumo":
xuMo(deceiveEnabled);
break;
}
}
public void xuMo(boolean playerDeceiveEnabled){
boolean cpuDeceiveEnabled;
cpuDeceiveEnabled = (Math.random() - basicInfo.faith > 0);
if (cpuDeceiveEnabled){
if (playerDeceiveEnabled){
basicInfo.playerDvsCpuD();
// faith changes to be amend w/ proper value, need record on the change of status
}
if (!playerDeceiveEnabled){
basicInfo.playerCvsCpuD();
}
}
if (!cpuDeceiveEnabled){
if (playerDeceiveEnabled){
basicInfo.playerDvsCpuC();
}
if (!playerDeceiveEnabled){
basicInfo.playerCvsCpuC();
}
}
if(basicInfo.pGold <= 0 || basicInfo.cGold <= 0 || basicInfo.pRep <= 0 || basicInfo.cRep <= 0){
//to be changed
setText();
losingDialogFragment.show(fragmentManager,LOSING_FRAGMENT);
Log.d("True", String.valueOf(losingDialogFragment.tryAgainEnabled));
if(losingDialogFragment.tryAgainEnabled){//tryAgainEnabled is always false
basicInfo.reset();
losingDialogFragment.dismiss();
}else{
losingDialogFragment.dismiss();
}
}
setText();
// to be changed
}
}
And in Fragment
package com.example.fuj.valorsafeworldbytrade;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
/**
* Created by fuj on 20/1/2017.
*/
public class LosingDialogFragment extends DialogFragment{
public static final String LOSING_FRAGMENT = "LOSING";
Context context;
Intent intent = new Intent();
#Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.fragment_lose, container, false);;
final Button tryAgainButton = (Button)rootView.findViewById(R.id.try_again_button);
tryAgainButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((Stage)context).tryAgainEnabled = true;
}
});
Button giveUpButton =(Button)rootView.findViewById(R.id.give_up_button);
giveUpButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((Stage)context).tryAgainEnabled = false;
}
});
getDialog().setTitle(LOSING_FRAGMENT);
return rootView;
}
}

Closing Android Game After 5 Seconds

I'm tring to build an simple android game.
Users answer the questions, when the answer is correct, it is continue..
I want to add time control for each answer.
I tried to add handler function, but I didn't.
My Code;
import java.util.Collections;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class EasyGameActivity extends Activity {
public int score = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_easygame);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
finishScreen();
}
}, 5000);
startGame();
}
private void startGame() {
// TODO Auto-generated method stub
Button b1 = (Button)findViewById(R.id.answer_one);
Button b2 = (Button)findViewById(R.id.answer_two);
Button b3 = (Button)findViewById(R.id.answer_three);
Button b4 = (Button)findViewById(R.id.answer_four);
Random number = new Random();
int first = number.nextInt(100)+1;
int second = number.nextInt(100)+1;
int answer = first + second;
int rnd1 = answer + 1;
int rnd2 = answer + 2;
int rnd3 = answer - 1;
final String a = Integer.toString(answer);
String b = Integer.toString(rnd1);
String c = Integer.toString(rnd2);
String d = Integer.toString(rnd3);
((TextView) findViewById(R.id.display)).setText(Integer.toString(first) + '+' + Integer.toString(second));
List<Button> buttons = Arrays.asList(b1, b2, b3, b4);
List<String> texts = Arrays.asList(a, b, c, d);
Collections.shuffle(texts);
int i = 0;
OnClickListener onClick = new OnClickListener() {
public void onClick(View view) {
Button button = (Button) view;
String value = (String) button.getText();
if(value == a) {
checkTrue();
} else {
finishScreen();
}
}
};
for(Button button : buttons) {
button.setText(texts.get(i++));
button.setOnClickListener(onClick);
}
}
private void checkTrue() {
score++;
((TextView) findViewById(R.id.score)).setText(Integer.toString(score));
startGame();
}
private void finishScreen() {
score = 0;
startActivity (new Intent("com.bsinternet.mathfast.RESTARTGAMESCREEN"));
finish();
}
}
How can I add time control. Thanks.
This bit of code doesn't look right
if(value == a) {
checkTrue();
} else {
finishScreen();
}
You should be using equals() to check for String equality. At the moment you are checking only object equality, which will evaluate to False, and the code will never call checkTrue().
Do this instead:
if(value.equals(a) {
checkTrue();
} else {
finishScreen();
}

How To Use Method onProgressChanged for RangeSeekbar?

I´ve succesfully implemented the RangeSeekbar from this website ( http://code.google.com/p/range-seek-bar/) into my App.
The Problem alot of people now have tho, is that we can´t find an onProgressChanged Method for the two thumbs in the code for the RangeSeekbar.
Do i have to write my own onProgressChanged?
I implemented the RangeSeekbar into a DialogFragment. For the normal Seekbar everything worked fine.
http://www.directupload.net/file/d/3582/mokh6mkb_jpg.htm <-- How can I get the two TextViews to the left and right of "to" to show the selected Value of the RangeSeekbar?
My Code so far:
package com.example.test2;
import com.example.test2.RangeSeekBar.OnRangeSeekBarChangeListener;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.SeekBar;
import android.widget.TextView;
public class LookingAgeFragment extends DialogFragment implements View.OnClickListener, OnRangeSeekBarChangeListener {
protected static final String TAG = null;
public static String username;
static int lowestAge;
static int highestAge;
static TextView viewTo, viewMaxAge, viewMinAge;
RangeSeekBar<Integer> seekBar;
public void onAttach(Activity activity){
super.onAttach(activity);
}
public void onCeate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view= inflater.inflate(R.layout.fragment_age_looking,null);
getDialog().setTitle(getString(R.string.lookingage_title));
Button button = (Button)view.findViewById(R.id.buttonLeaveFragmentLookingAge);
viewTo = (TextView)view.findViewById(R.id.textView_to);
viewMaxAge = (TextView)view.findViewById(R.id.textView_maxAge);
viewMinAge = (TextView)view.findViewById(R.id.textView_minAge);
button.setOnClickListener(this);
// create RangeSeekBar as Integer range between 20 and 75
seekBar = new RangeSeekBar<Integer>(20, 75, getActivity());
seekBar.setOnRangeSeekBarChangeListener(new OnRangeSeekBarChangeListener<Integer>() {
#Override
public void onRangeSeekBarValuesChanged(RangeSeekBar<?> bar, Integer minValue, Integer maxValue) {
// handle changed range values
Log.i(TAG, "User selected new range values: MIN=" + minValue + ", MAX=" + maxValue);
}
});
ViewGroup layout = (ViewGroup) view.findViewById(R.id.rangeSeekbarContainer);
layout.addView(seekBar);
setUpLayout();
return view;
}
#SuppressLint("NewApi")
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
Bundle bundle = this.getArguments();
if(bundle != null){
username = bundle.getString("username", null);
}
}
private void setUpLayout() {
// TODO Auto-generated method stub
setCancelable(false);
//bar.setOnSeekBarChangeListener(yourSeekBarListener);
}
public void saveDataChange(String s, String data){
String help = "com.example.test2.PREFERENCE_FILE_KEY_"+username;
Context context = getActivity();
SharedPreferences sharedPref = context.getSharedPreferences(help, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString(s, data);
editor.commit();
}
public String getData(String s){
String data;
String help = "com.example.test2.PREFERENCE_FILE_KEY_"+username;
Context context = getActivity();
SharedPreferences sharedPref = context.getSharedPreferences(help, Context.MODE_PRIVATE);
data = sharedPref.getString(s, null);
return data;
}
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
switch(view.getId()){
case R.id.buttonLeaveFragmentLookingAge:
dismiss();
break;
}
}
public void onProgressChanged() {
// TODO Auto-generated method stub
int maxvalue = (Integer) seekBar.getSelectedMaxValue();
viewMaxAge.setText(maxvalue);
int minvalue = (Integer) seekBar.getSelectedMinValue();
viewMinAge.setText(minvalue);
}
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
int maxvalue = (Integer) seekBar.getSelectedMaxValue();
viewMaxAge.setText(maxvalue);
int minvalue = (Integer) seekBar.getSelectedMinValue();
viewMinAge.setText(minvalue);
}
public void onRangeSeekBarValuesChanged(RangeSeekBar bar, Object minValue,
Object maxValue) {
// TODO Auto-generated method stub
viewMaxAge.setText((String)maxValue);
viewMinAge.setText((String)minValue);
}
}
Thank you for your help.
RangeSeekbar is a generic class. if your pass T parameter to it as type of range values, then RangSeekBar pass left thumb Value as minValue and right thumb Value as maxValue in
RangeSeekBar.OnRangeSeekBarChangeListener.(RangeSeekBar bar, T minValue, T maxValue).
change line of your class extends to:
public class LookingAgeFragment extends DialogFragment implements OnRangeSeekBarChangeListener<Integer>
after that you should change Object to Integer in your code at this line
seekBar.setOnRangeSeekBarChangeListener(new RangeSeekBar.OnRangeSeekBarChangeListener<Integer>() {
#Override
public void onRangeSeekBarValuesChanged(RangeSeekBar<?> bar, Integer minValue, Integer maxValue) {
viewMinAge.setText(String.valueOf(minValue));
viewMaxAge.setText(String.valueOf(minValue));
}
});
I think by this changes your problem will be solved.
for more generic solution when your ages range is not continuous and maybe discrete, I suggest this solution
public class LookingAgeFragment extends DialogFragment implements RangeSeekBar.OnRangeSeekBarChangeListener<Integer> {
RangeSeekBar<Integer> seekBar;
int[] ageValues={10, 20, 30, 50, 100};
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
// create RangeSeekBar as Integer range between 20 and 75
seekBar = new RangeSeekBar<Integer>(0, ageValues.length - 1, getActivity());
seekBar.setOnRangeSeekBarChangeListener(new RangeSeekBar.OnRangeSeekBarChangeListener<Integer>() {
#Override
public void onRangeSeekBarValuesChanged(RangeSeekBar<?> bar, Integer minValue, Integer maxValue) {
viewMinAge.setText(String.valueOf(ageValues[minValue]));
viewMaxAge.setText(String.valueOf(ageValues[minValue]));
}
});
}
}
I use next code for getting the values on changing event:
rangeSeekBar.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Log.d("MinValue", rangeSeekBar.getSelectedMinValue().toString());
Log.d("MaxValue", rangeSeekBar.getSelectedMaxValue().toString());
return false;
}
});
In class ReangeSekkBar you have this if in ACTION_MOVE
if (notifyWhileDragging && listener != null) {
listener.onRangeSeekBarValuesChanged(this, getSelectedMinValue(), getSelectedMaxValue());
}
delete this if
//if (notifyWhileDragging && listener != null) {
listener.onRangeSeekBarValuesChanged(this, getSelectedMinValue(), getSelectedMaxValue());
//}

How to set numeric value in EditText?

I want to make a simple calculator My code is below:
package som.dev.android.calc;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class CalcApp extends Activity {
/** Called when the activity is first created. */
Button addValues, subValues, equalsValue;
EditText inputValue;
int inputNum = 0;
public int value1;
public int value2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// initializing Views
inputValue = (EditText) findViewById(R.id.editText1);
addValues = (Button) findViewById(R.id.addBtn);
subValues = (Button) findViewById(R.id.subBtn);
equalsValue = (Button) findViewById(R.id.equalsBtn);
// adding onClick Listeners
addValues.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
pressed = true;
value1 = Integer.parseInt(inputValue.getText().toString());
inputValue.setText("");
}
});
equalsValue.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
value2 = Integer.parseInt(inputValue.getText().toString());
int result = value1+value2;
inputValue.setText(result);// There an exception occurs....
}
});
}
}
But it does not sets result to Edittext in my app and an exception occurs and message comes Unfortunately app is colsed some thing like that so please any one tell me the solution of this problem
use this inputValue.setText(""+result);instead of inputValue.setText(result);// There an exception occurs....
just conversion integer into string
The reason you get an exception is that the EditText thinks you are setting a resource id. You need to convert the integer to a string yourself Integer.toString(result) before using it in the EditText Object. http://developer.android.com/reference/android/widget/TextView.html
change Following Line:-
inputValue.setText(result);
to
inputValue.setText(String.Valueof(result));
then your error is solved.

deflate view and inflate another

on the press of my "next" button I have a speech bubble run through a string array. After all the items finish displaying and the user clicks the "next" button once more I would like to deflate the current child view and inflate a new view. Right now it crashes after the string array finishes displaying on multiple clickings of the "next" button.
How can I get this to work?
package com.jibushi;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class LessonsShell extends Activity{
private static final int MESSAGE_SHOW_POPUP = 1;
private static final int MESSAGE_SHOW_POPUP2 = 1;
private static final long TIME_DELAY = 1000;//1 seconds
private static final long TIME_DELAY2 = 500;
private View view;
private View view2;
private int count = 0;
private TextView lessonsDialog;
private String[] myIntroString;
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch(msg.what) {
case MESSAGE_SHOW_POPUP:
view();
break;
}
};
};
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.lessons);
//this will send a message to the handler to display the popup after 1 seconds.
handler.sendEmptyMessageDelayed(MESSAGE_SHOW_POPUP,TIME_DELAY);
}
private void view() {
// TODO Auto-generated method stub
ViewGroup parent = (ViewGroup) findViewById(R.id.lessons_bg);
view = LayoutInflater.from(getBaseContext()).inflate(R.layout.lessons_dialog, null);
parent.addView(view);
lessonsDialog = (TextView) findViewById(R.id.lessonsDialog);
Resources res = getResources();
myIntroString = res.getStringArray(R.array.lessons_dialog_array);
Button nextButton = (Button) findViewById(R.id.next_button);
nextButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (count < myIntroString.length) {
lessonsDialog.setText(myIntroString[count]);
count++;
} else {
if (myIntroString[-1] != null) {
handler2.sendEmptyMessageDelayed(MESSAGE_SHOW_POPUP2, TIME_DELAY2);
}
}
}
});
}
private Handler handler2 = new Handler() {
public void handleMessage(Message msg) {
switch(msg.what) {
case MESSAGE_SHOW_POPUP2:
view2();
break;
}
}
private void view2() {
// TODO Auto-generated method stub
ViewGroup parent = (ViewGroup) findViewById(R.id.lessons_bg);
view2 = LayoutInflater.from(getBaseContext()).inflate(R.layout.lessons_start, null);
parent.addView(view2);
parent.removeView(view);
};
};
}
Ah, yes, I see it now. You can't do this:
myString[-1]
The index -1 does not exist in arrays. What are you trying to do? Perhaps myString[count - 1]?

Categories

Resources