I would like to put the function run_voice() of my MainActivity on my class TypingDialogFragment but it do not work:
Non static method 'run_voice' cannot be reference from a static context
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
Home1=(ImageView)findViewById(R.id.home);
Voice1=(ImageView)findViewById(R.id.voice);
Scan1=(ImageView)findViewById(R.id.scan);
frequence_text=(TextView)findViewById(R.id.frequence_text);
command_text=(TextView)findViewById(R.id.command_text);
gain_text=(TextView) findViewById(R.id.gain_text);
body = (TextView)findViewById(R.id.body);
fab = (FloatingActionButton) findViewById(R.id.fab);
fab_frequence = (FloatingActionButton) findViewById(R.id.freq_value);
fab_gain = (FloatingActionButton) findViewById(R.id.gain_value);
send_voice = (ImageButton) findViewById(R.id.send_voice1);
send_voice2 = (ImageView) findViewById(R.id.send_voice3);
myChrono=(Chronometer) findViewById(R.id.chronometer);
progressBar=(ProgressBar) findViewById(R.id.progressBar);
body.setMovementMethod(new ScrollingMovementMethod());
setSupportActionBar(toolbar);
OUTPUT_FILE= Environment.getExternalStorageDirectory()+ "/audiorecord.wav";
outFile=new File (OUTPUT_FILE);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
type = 1;
showCmdLineDialog(type,run_voice);
}
});
fab_frequence.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
type = 2;
showCmdLineDialog(type,run_voice);
}
});
fab_gain.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
type = 3;
showCmdLineDialog(type,run_voice);
}
});
display_mediaplayer_false();
display_floatbutton_false();
success1=0;
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)){
Toast.makeText(this, "FEATURE_BLUETOOTH NOT support", Toast.LENGTH_LONG).show();
finish();
return;
}
//using the well-known SPP UUID
myUUID = UUID.fromString(UUID_STRING_WELL_KNOWN_SPP);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
Toast.makeText(this, "Bluetooth is not supported on this hardware platform", Toast.LENGTH_LONG).show();
finish();
return;
}
Home1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(success1==1)
{
display_mediaplayer_false();
display_floatbutton_true();
}
}
});
Voice1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(success1==1) {
display_floatbutton_false();
display_mediaplayer_true();
}
}
});
Scan1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(success1==1) {
display_floatbutton_false();
display_mediaplayer_false();
}
}
});
send_voice.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
run_voice(); // FUNCTION RUN VOICE
}
});
}
My function run_voice :
public void run_voice()
{
try {
if (color==0)
{
send_voice.setColorFilter(Color.RED);
beginRecording();
progressBar.setVisibility(View.VISIBLE);
myChrono.setBase(SystemClock.elapsedRealtime());
myChrono.start();
color1=1;
run_voice=1;
}
if (color==1)
{
send_voice.setColorFilter(Color.BLACK);
progressBar.setVisibility(View.INVISIBLE);
stopRecording();
myChrono.stop();
color1=0;
run_voice=0;
}
color=color1;
}
catch (IOException e) {
e.printStackTrace();
}
}
I would like to put this function in this class :
public static class TypingDialogFragment extends DialogFragment {
EditText cmdLine;
static MainActivity parentActivity;
static ThreadConnected cmdThreadConnected;
static int type1;
static int run_voice1;
static TypingDialogFragment newInstance(MainActivity parent, ThreadConnected thread, int type, int run_voice){
type1=type;
run_voice1=run_voice;
parentActivity = parent;
cmdThreadConnected = thread;
TypingDialogFragment f = new TypingDialogFragment();
return f;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
getDialog().setTitle("Cmd Line");
getDialog().setCanceledOnTouchOutside(false);
View typingDialogView = inflater.inflate(R.layout.typing_layout, container, false);
cmdLine = (EditText)typingDialogView.findViewById(R.id.cmdline);
ImageView imgCleaarCmd = (ImageView)typingDialogView.findViewById(R.id.clearcmd);
imgCleaarCmd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cmdLine.setText("");
}
});
ImageButton send_voice=(ImageButton)getActivity().findViewById(R.id.send_voice1);
send_voice.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
byte[] NewLine = "\n".getBytes();
byte[] bytesToSend="pkill -f receiverfm.py".getBytes();
byte[] bytesToSend2="./emetteur.py".getBytes();
byte[] bytesToSend3="pkill -f emetteur.py".getBytes();
byte[] bytesToSend4="./receiverfm.py".getBytes();
if (run_voice1==0)
{
cmdThreadConnected.write(bytesToSend);
cmdThreadConnected.write(NewLine);
cmdThreadConnected.write(bytesToSend2);
}
if (run_voice1==1)
{
cmdThreadConnected.write(bytesToSend3);
cmdThreadConnected.write(NewLine);
cmdThreadConnected.write(bytesToSend4);
}
cmdThreadConnected.write(NewLine);
}
});
Button btnEnter = (Button)typingDialogView.findViewById(R.id.enter);
btnEnter.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(cmdThreadConnected!=null){
byte[] memory_freq="100.0".getBytes();
if (type1==1)
{
byte[] bytesToSend = cmdLine.getText().toString().getBytes();
cmdThreadConnected.write(bytesToSend);
byte[] NewLine = "\n".getBytes();
cmdThreadConnected.write(NewLine);
}
if (type1==2)
{
byte[] byteToSend_freq = "./receiverfm.py --freq ".getBytes();
byte[] bytesToSend = cmdLine.getText().toString().getBytes();
memory_freq=bytesToSend;
cmdThreadConnected.write(byteToSend_freq);
cmdThreadConnected.write(bytesToSend);
byte[] NewLine = "\n".getBytes();
cmdThreadConnected.write(NewLine);
}
if (type1==3)
{
byte[] byteToSend_gain="./receiverfm.py --freq ".getBytes();
cmdThreadConnected.write(byteToSend_gain);
cmdThreadConnected.write(memory_freq);
byte[] byteToSend_gain2=" --".getBytes();
byte[] bytesToSend = cmdLine.getText().toString().getBytes();
cmdThreadConnected.write(byteToSend_gain2);
cmdThreadConnected.write(bytesToSend);
byte[] NewLine = "\n".getBytes();
cmdThreadConnected.write(NewLine);
}
}
}
});
Button btnDismiss = (Button)typingDialogView.findViewById(R.id.dismiss);
btnDismiss.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dismiss();
}
});
Button btnClear = (Button)typingDialogView.findViewById(R.id.clear);
btnClear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
body.setText("");
}
});
return typingDialogView;
}
}
Related
I try to google play subscription billing. Code is working doing payment. But I want to try when a payment successfull ı want to catch payment cost,time(montly,yearly) and I send my php API for example with an ID or Token. How can i do this.
I tried overriding onPurchasesUpdated
public void onPurchasesUpdated(BillingResult billingResult, #Nullable List<Purchase> purchases) {
}
but it didn't work . I tried show a toast. But method didn't work.
Can I do this with this method If I can how can I run this method ?
public void openPayment(final Context mContext){
final List<String> skuList = new ArrayList<>();
//valueof1MonthMoney,valueof3MonthsMoney,valueof6MonthsMoney,valueof1YearMoney;
skuList.add("com.yeniasya.enewspaper.subscription.onemonth");
skuList.add("com.yeniasya.enewspaper.subscription.threemonth");
skuList.add("com.yeniasya.enewspaper.subscription.sixmonth");
skuList.add("com.yeniasya.enewspaper.subscription.oneyear");
SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
final AlertDialog.Builder mBuilder = new AlertDialog.Builder(mContext,android.R.style.Theme_DeviceDefault_Light_NoActionBar_Fullscreen);
LayoutInflater li = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View mView = li.inflate(R.layout.popup_payment, null);
ImageView imvClose=(ImageView)mView.findViewById(R.id.imvClose);
final TextView tv1MonthMoney = (TextView) mView.findViewById(R.id.tv1MonthMoney);
final TextView tv3MonthsMoney = (TextView) mView.findViewById(R.id.tv3MonthsMoney);
final TextView tv6MonthsMoney = (TextView) mView.findViewById(R.id.tv6MonthsMoney);
final TextView tv1YearMoney = (TextView) mView.findViewById(R.id.tv1YearMoney);
final TextView tvOpenPrivacy = (TextView) mView.findViewById(R.id.tvOpenPrivacy);
final LinearLayout linLay1Year = (LinearLayout) mView.findViewById(R.id.linLay1Year);
final LinearLayout linLay6Months = (LinearLayout) mView.findViewById(R.id.linLay6Months);
final LinearLayout linLay3Months = (LinearLayout) mView.findViewById(R.id.linLay3Months);
final LinearLayout linLay1Month = (LinearLayout) mView.findViewById(R.id.linLay1Month);
mBuilder.setView(mView);
dialog = mBuilder.create();
dialog.setCanceledOnTouchOutside(false);
final SkuDetails[] s = new SkuDetails[4];
params.setSkusList(skuList).setType(BillingClient.SkuType.SUBS);
billingClient.querySkuDetailsAsync(params.build(),
new SkuDetailsResponseListener() {
#Override
public void onSkuDetailsResponse(BillingResult billingResult,
List<SkuDetails> skuDetailsList) {
// Process the result.
s[0] =skuDetailsList.get(0);
s[1] =skuDetailsList.get(1);
s[2] =skuDetailsList.get(2);
s[3] =skuDetailsList.get(3);
// Toast.makeText(mContext, "listeye girdi", Toast.LENGTH_SHORT).show();
try {
tv1MonthMoney.setText(skuDetailsList.get(0).getPrice());
tv1YearMoney.setText(skuDetailsList.get(1).getPrice());
tv6MonthsMoney.setText(skuDetailsList.get(2).getPrice());
tv3MonthsMoney.setText(skuDetailsList.get(3).getPrice());
/* if(skuDetailsList.get(i).getSku().equals("com.yeniasya.enewspaper.subscription.onemonth"));
tv1MonthMoney.setText(skuDetailsList.get(i).getPrice()+"");
if(skuDetailsList.get(i).getSku().equals("com.yeniasya.enewspaper.subscription.threemonth"));
tv3MonthsMoney.setText(skuDetailsList.get(i).getPrice()+"");
if(skuDetailsList.get(i).getSku().equals("com.yeniasya.enewspaper.subscription.sixmonth"));
tv6MonthsMoney.setText(skuDetailsList.get(i).getPrice()+"");
if(skuDetailsList.get(i).getSku().equals("com.yeniasya.enewspaper.subscription.oneyear"));
tv1YearMoney.setText(skuDetailsList.get(i).getPrice()+""); */
// Toast.makeText(mContext, skuDetailsList.get(i).getSku()+"", Toast.LENGTH_SHORT).show();
}catch (Exception e){
}
}
});
dialog.show();
imvClose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
linLay1Year.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
takeMoney(s[1]);
}
});
linLay1Month.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
takeMoney(s[0]);
}
});
linLay3Months.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
takeMoney(s[3]);
}
});
linLay6Months.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
takeMoney(s[2]);
}
});
tvOpenPrivacy.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try{
Intent i = new Intent(Intent.ACTION_VIEW);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setData(Uri.parse(mcontext.getString(R.string.PrivacyPolicyUrl)));
mcontext.startActivity(i);}catch (Exception e){}
}
});
}
private void takeMoney(SkuDetails skuDetails){
BillingFlowParams flowParams = BillingFlowParams.newBuilder()
.setSkuDetails(skuDetails)
.build();
billingClient.launchBillingFlow(MainActivity.mActivity,flowParams);
// Toast.makeText(mContext, "bastın3", Toast.LENGTH_SHORT).show();
}
#Override
public void onPurchasesUpdated(BillingResult billingResult, #Nullable List<Purchase> purchases) {
}
I have <> and <> buttons in my quiz app, but if the user is in the first question since there is no previous question there should be toast which says "this is the first question" and the same with <>button.
Here is my code for buttons (which is incorrect):
mNextButton = (Button)findViewById(R.id.next_button);
mNextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCurreentIndex=0;
if (mCurreentIndex>mQuestionBank.length){
Toast.makeText(QuizActivity.this,R.string.last_question,Toast.LENGTH_LONG).show();
}
updateQuestion();
}
});
mPreviousButton = (Button)findViewById(R.id.previous_button);
mPreviousButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCurreentIndex--;
if (mCurreentIndex<mQuestionBank.length){
Toast.makeText(QuizActivity.this, R.string.first_question,Toast.LENGTH_LONG).show();
}
updateQuestion();
}
});
mNextButton = (Button)findViewById(R.id.next_button);
mNextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//mCurreentIndex=0;// not valid
if (mCurreentIndex>=mQuestionBank.length){
Toast.makeText(QuizActivity.this,getString(R.string.last_question),Toast.LENGTH_LONG).show();
return;
}
mCurreentIndex++;
updateQuestion();
}
});
mPreviousButton = (Button)findViewById(R.id.previous_button);
mPreviousButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mCurreentIndex<=0){
Toast.makeText(QuizActivity.this, getString(R.string.first_question),Toast.LENGTH_LONG).show();
return;
}
mCurreentIndex--;
updateQuestion();
}
});
Try this
mPreviousButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mCurreentIndex<=0){
Toast.makeText(QuizActivity.this, R.string.first_question,Toast.LENGTH_LONG).show();
}else{
mCurreentIndex--;
updateQuestion();
}
}
});
mNextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mCurreentIndex>=mQuestionBank.length){
Toast.makeText(QuizActivity.this,getString(R.string.last_question),Toast.LENGTH_LONG).show();
}else{
mCurreentIndex++;
updateQuestion();
}
}
});
You have just display toast message but next statement also execute after displaying toast. For your solution you need to use "return" or if..else block.
mNextButton = (Button)findViewById(R.id.next_button);
mNextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCurreentIndex=0;
if (mCurreentIndex>mQuestionBank.length){
Toast.makeText(QuizActivity.this,R.string.last_question,Toast.LENGTH_LONG).show();
return;
}
updateQuestion();
}
});
mPreviousButton = (Button)findViewById(R.id.previous_button);
mPreviousButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCurreentIndex--;
if (mCurreentIndex<mQuestionBank.length){
Toast.makeText(QuizActivity.this, R.string.first_question,Toast.LENGTH_LONG).show();
return;
}
updateQuestion();
}
});
//Previous Button Condition;
mCurreentIndex--;
if (mCurreentIndex<mQuestionBank.length && mCurreentIndex>=0){
Toast.makeText(QuizActivity.this, R.string.first_question,Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(QuizActivity.this, "This is first question",Toast.LENGTH_LONG).show();
mCurreentIndex++;
}
//Next Button Condition;
mCurreentIndex++;
if (mCurreentIndex<mQuestionBank.length){
Toast.makeText(QuizActivity.this,R.string.last_question,Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(QuizActivity.this, "This is last Question",Toast.LENGTH_LONG).show();
mCurreentIndex--;
}
public class QuizActivity extends AppCompatActivity {
private static final String TAG = "QuizActivity";
private static final String KEY_INDEX = "index";
private Button mTrueButton;
private Button mFalseButton;
private Button mNextButton;
private Button mPreviousButton;
private TextView mQuestionTextView;
private Question[] mQuestionBank = new Question[]{
new Question(R.string.question_australia, true),
new Question(R.string.question_oceans, true),
new Question(R.string.question_mideast, false),
new Question(R.string.question_africa, false),
new Question(R.string.question_americas, true),
new Question(R.string.question_asia, true),
};
private int mCurreentIndex = 0;
private boolean[] QuestionsAnswered = new boolean[mQuestionBank.length];
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG,"onCreate(Bundle) called");
setContentView(R.layout.activity_quiz);
if (savedInstanceState != null) {
mCurreentIndex =savedInstanceState.getInt(KEY_INDEX, 0);
QuestionsAnswered = savedInstanceState.getBooleanArray(KEY_INDEX);
}
mQuestionTextView = (TextView)findViewById(R.id.question_text_view);
mTrueButton = (Button)findViewById(R.id.true_button);
mTrueButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
checkAnswer(true);
}
});
mFalseButton = (Button)findViewById(R.id.false_button);
mFalseButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
checkAnswer(false);
}
});
mNextButton = (Button)findViewById(R.id.next_button);
mNextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCurreentIndex = (mCurreentIndex+mQuestionBank.length +1) % mQuestionBank.length;
updateQuestion();
}
});
mPreviousButton = (Button)findViewById(R.id.previous_button);
mPreviousButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCurreentIndex= (mCurreentIndex+ mQuestionBank.length - 1) % mQuestionBank.length;
updateQuestion();
}
});
updateQuestion();
}
#Override
protected void onStart() {
super.onStart();
Log.d(TAG, "onStart() called");
}
#Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume() called");
}
#Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause() called");
}
#Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putInt(KEY_INDEX,mCurreentIndex);
savedInstanceState.putBooleanArray(KEY_INDEX,QuestionsAnswered);
}
#Override
protected void onStop() {
super.onStop();
Log.d(TAG, "onStop() called");
}
#Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy() called");
}
private void updateQuestion(){
int question = mQuestionBank[mCurreentIndex].getTextResId();
mQuestionTextView.setText(question);
mTrueButton.setEnabled(!QuestionsAnswered[mCurreentIndex]);
mFalseButton.setEnabled(!QuestionsAnswered[mCurreentIndex]);
}
private void checkAnswer(boolean userPressedTrue) {
boolean answerIsTrue = mQuestionBank[mCurreentIndex].isAnswerTrue();
int messageResId = 0;
if (userPressedTrue == answerIsTrue) {
messageResId = R.string.correct_toast;
} else {
messageResId = R.string.incorrect_toast;
}
Toast.makeText(this, messageResId, Toast.LENGTH_LONG).show();
QuestionsAnswered[mCurreentIndex] = true;
mTrueButton.setEnabled(false);
mFalseButton.setEnabled(false);
}
}
in if(saveInstanceState != null) I am trying to save the changes of the text in Text Views that happened by whoWon() method to save it in while user rotate the phone and get it back as it was before rotation but it shows me an error that says getText() in TextView cannot be applied to (java.lang.string) , I tried to add .toString() but it didn't work.
public class Multiplayer extends AppCompatActivity {
private TextView mTicTacTextViews [] = new TextView[9];
private Button mButtonX;
private Button mButtonO;
private Button mResult;
private int mcheckAllButtonsClicked =0;
private String TEXT_VIEW_VALUES_KEY = "VALUES OF TEXT VIEW'S";
private String mTextviewValues [] = new String[9];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState != null)
{
String values[]=savedInstanceState.getStringArray(TEXT_VIEW_VALUES_KEY);
for (int i=0;i<values.length;i++ ) {
mTextviewValues[i]=values[i];
mTicTacTextViews[i].getText(mTextviewValues[i]);
}
}
setContentView(R.layout.activity_tic_tac);
mTicTacTextViews[0] = (TextView) findViewById(R.id.tic_tac_one);
mTicTacTextViews[0].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ifPressed_XButton(mTicTacTextViews[0]);
ifPressed_OButton(mTicTacTextViews[0]);
mcheckAllButtonsClicked+=1;
}
});
mTicTacTextViews[1] = (TextView) findViewById(R.id.tic_tac_two);
mTicTacTextViews[1].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ifPressed_XButton(mTicTacTextViews[1]);
ifPressed_OButton(mTicTacTextViews[1]);
mcheckAllButtonsClicked+=1;
}
});
mTicTacTextViews[2] = (TextView) findViewById(R.id.tic_tac_three);
mTicTacTextViews[2].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ifPressed_XButton(mTicTacTextViews[2]);
ifPressed_OButton(mTicTacTextViews[2]);
mcheckAllButtonsClicked+=1;
}
});
mTicTacTextViews[3] = (TextView) findViewById(R.id.tic_tac_four);
mTicTacTextViews[3].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ifPressed_XButton(mTicTacTextViews[3]);
ifPressed_OButton(mTicTacTextViews[3]);
mcheckAllButtonsClicked+=1;
}
});
mTicTacTextViews[4] = (TextView) findViewById(R.id.tic_tac_five);
mTicTacTextViews[4].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ifPressed_XButton(mTicTacTextViews[4]);
ifPressed_OButton(mTicTacTextViews[4]);
mcheckAllButtonsClicked+=1;
}
});
mTicTacTextViews[5] = (TextView) findViewById(R.id.tic_tac_six);
mTicTacTextViews[5].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ifPressed_XButton(mTicTacTextViews[5]);
ifPressed_OButton(mTicTacTextViews[5]);
mcheckAllButtonsClicked+=1;
}
});
mTicTacTextViews[6] = (TextView) findViewById(R.id.tic_tac_seven);
mTicTacTextViews[6].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ifPressed_XButton(mTicTacTextViews[6]);
ifPressed_OButton(mTicTacTextViews[6]);
mcheckAllButtonsClicked+=1;
}
});
mTicTacTextViews[7] = (TextView) findViewById(R.id.tic_tac_eight);
mTicTacTextViews[7].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ifPressed_XButton(mTicTacTextViews[7]);
ifPressed_OButton(mTicTacTextViews[7]);
mcheckAllButtonsClicked+=1;
}
});
mTicTacTextViews[8] = (TextView) findViewById(R.id.tic_tac_nine);
mTicTacTextViews[8].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ifPressed_XButton(mTicTacTextViews[8]);
ifPressed_OButton(mTicTacTextViews[8]);
mcheckAllButtonsClicked+=1;
}
});
mButtonX = (Button) findViewById(R.id.x_button);
mButtonX.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mUserPressed_XButton =true;
}
});
mButtonO = (Button) findViewById(R.id.o_button);
mButtonO.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mUserPressed_OButton =true;
}
});
mTextviewValues [0]=mTicTacTextViews[0].getText().toString();
mTextviewValues [1]=mTicTacTextViews[1].getText().toString();
mTextviewValues [2]=mTicTacTextViews[2].getText().toString();
mTextviewValues [3]=mTicTacTextViews[3].getText().toString();
mTextviewValues [4]=mTicTacTextViews[4].getText().toString();
mTextviewValues [5]=mTicTacTextViews[5].getText().toString();
mTextviewValues [6]=mTicTacTextViews[6].getText().toString();
mTextviewValues [7]=mTicTacTextViews[7].getText().toString();
mTextviewValues [8]=mTicTacTextViews[8].getText().toString();
mResult = (Button) findViewById(R.id.result_button);
mResult.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
whoWon();
}
});
}
private void whoWon ()
{
mTextviewValues [0]=mTicTacTextViews[0].getText().toString();
mTextviewValues [1]=mTicTacTextViews[1].getText().toString();
mTextviewValues [2]=mTicTacTextViews[2].getText().toString();
mTextviewValues [3]=mTicTacTextViews[3].getText().toString();
mTextviewValues [4]=mTicTacTextViews[4].getText().toString();
mTextviewValues [5]=mTicTacTextViews[5].getText().toString();
mTextviewValues [6]=mTicTacTextViews[6].getText().toString();
mTextviewValues [7]=mTicTacTextViews[7].getText().toString();
mTextviewValues [8]=mTicTacTextViews[8].getText().toString();
if(mTextviewValues[0].equals(mTextviewValues[1]) & mTextviewValues[1].equals(mTextviewValues[2]))
{
Toast.makeText(this,"Someone won",Toast.LENGTH_SHORT).show();
}
else if (mTextviewValues[1].equals(mTextviewValues[4]) & mTextviewValues[4].equals(mTextviewValues[8]))
{
Toast.makeText(this,"Someone won",Toast.LENGTH_SHORT).show();
}
else if (mTextviewValues[0].equals(mTextviewValues[3]) & mTextviewValues[3].equals(mTextviewValues[6]))
{
Toast.makeText(this,"Someone won",Toast.LENGTH_SHORT).show();
}
else if (mTextviewValues[1].equals(mTextviewValues[4]) & mTextviewValues[4].equals(mTextviewValues[7]))
{
Toast.makeText(this,"Someone won",Toast.LENGTH_SHORT).show();
}
else if (mTextviewValues[2].equals(mTextviewValues[5]) & mTextviewValues[5].equals(mTextviewValues[8]))
{
Toast.makeText(this,"Someone won",Toast.LENGTH_SHORT).show();
}
else if (mTextviewValues[2].equals(mTextviewValues[4]) & mTextviewValues[4].equals(mTextviewValues[6]))
{
Toast.makeText(this,"Someone won",Toast.LENGTH_SHORT).show();
}
else if (mTextviewValues[2].equals(mTextviewValues[4]) & mTextviewValues[4].equals(mTextviewValues[6]))
{
Toast.makeText(this,"Someone won",Toast.LENGTH_SHORT).show();
}
else if (mTextviewValues[6].equals(mTextviewValues[7]) & mTextviewValues[7].equals(mTextviewValues[8]))
{
Toast.makeText(this,"Someone won",Toast.LENGTH_SHORT).show();
}
else Log.i("error" , "whowon didn't work");
}
#Override
protected void onSaveInstanceState(Bundle bundle)
{
super.onSaveInstanceState(bundle);
bundle.putStringArray(TEXT_VIEW_VALUES_KEY,mTextviewValues);
}
}
getText() doesn't take a parameter. mTicTacTextViews[i].getText() returns the text in the view. Are you trying to set the text in the view? If so, you want setText(CharSequence), not CharSequence getText().
Hello need help with a class trying to figure out how to create a first and last button in a fragment that will jump to the first item in a ViewPager and the last item in a ViewPager I'm a bit lost with how to implement. Here is the fragment.
public class CrimeFragment extends Fragment {
private static final String ARG_CRIME_ID = "crime_id";
private static final String DIALOG_DATE = "DialogDate";
private static final int REQUEST_DATE = 0;
private Crime mCrime;
private EditText mTitleField;
private Button mDateButton;
private CheckBox mSolvedCheckBox;
private Spinner mSpinner;
private Button mFirstButton;
private Button mLastButton;
public static CrimeFragment newInstance(UUID crimeId) {
Bundle args = new Bundle();
args.putSerializable(ARG_CRIME_ID, crimeId);
CrimeFragment fragment = new CrimeFragment();
fragment.setArguments(args);
return fragment;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_OK) {
return;
}
if (requestCode == REQUEST_DATE) {
Date date =
(Date) data.getSerializableExtra(DatePickerFragment.EXTRA_DATE);
mCrime.setDate(date);
updateDate();
}
}
private void updateDate() {
mDateButton.setText(mCrime.getDate().toString());
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
UUID crimeId = (UUID) getArguments().getSerializable(ARG_CRIME_ID);
mCrime = CrimeLab.get(getActivity()).getCrime(crimeId);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_crime, container, false);
mTitleField = (EditText)v.findViewById(R.id.crime_title);
mTitleField.setText(mCrime.getTitle());
mTitleField.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(
CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(
CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
mDateButton = (Button) v.findViewById(R.id.crime_date);
updateDate();
mDateButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager manager = getFragmentManager();
DatePickerFragment dialog = DatePickerFragment.newInstance(mCrime.getDate());
dialog.setTargetFragment(CrimeFragment.this, REQUEST_DATE);
dialog.show(manager, DIALOG_DATE);
}
});
mSpinner = (Spinner) v.findViewById(R.id.crime_severity);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this.getActivity(),
R.array.severity_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setAdapter(adapter);
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
mCrime.setSeverity((String) mSpinner.getSelectedItem());
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
}
});
mSolvedCheckBox = (CheckBox) v.findViewById(R.id.crime_solved);
mSolvedCheckBox.setChecked(mCrime.isSolved());
mSolvedCheckBox.setOnCheckedChangeListener(
new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
mCrime.setSolved(isChecked);
}
});
mFirstButton = (Button) v.findViewById(R.id.first_crime_button);
mFirstButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
mLastButton = (Button) v.findViewById(R.id.last_crime_button);
mLastButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
return v;
}
}
Here is the Activity with the ViewPager.
public class CrimePagerActivity extends FragmentActivity{
private static final String EXTRA_CRIME_ID = "com.martin.android.criminalintent.crime_id";
private ViewPager mViewPager;
private List<Crime> mCrimes;
public static Intent newIntent(Context packageContext, UUID crimeId) {
Intent intent = new Intent(packageContext, CrimePagerActivity.class);
intent.putExtra(EXTRA_CRIME_ID, crimeId);
return intent;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crime_pager);
UUID crimeId = (UUID) getIntent().getSerializableExtra(EXTRA_CRIME_ID);
mViewPager = (ViewPager) findViewById(R.id.activity_crime_pager_view_pager);
mCrimes = CrimeLab.get(this).getCrimes();
FragmentManager fragmentManager = getSupportFragmentManager();
mViewPager.setAdapter(new FragmentStatePagerAdapter(fragmentManager) {
#Override
public Fragment getItem(int position) {
Crime crime = mCrimes.get(position);
return CrimeFragment.newInstance(crime.getId());
}
#Override
public int getCount() {
return mCrimes.size();
}
});
for (int i = 0; i < mCrimes.size(); i++) {
if (mCrimes.get(i).getId().equals(crimeId)) {
mViewPager.setCurrentItem(i);
break;
}
}
}
}
For the First button Im pretty sure I just need to set item to 0? For last button I need to set item to size - 1 just not sure how to do that.
In your CrimePagerActivity, write two method's to jump to the ViewPager first page and last page.
CrimePagerActivity.java:
public void jumpToFirstPage() {
mViewPager.setCurrentItem(0);
}
public void jumpToLastPage() {
mViewPager.setCurrentItem(mCrimes.size() - 1);
}
Call those method's from mFirstButton and mLastButton click listener method:
CrimeFragment.java:
mFirstButton = (Button) v.findViewById(R.id.first_crime_button);
mFirstButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((CrimePagerActivity)getActivity()).jumpToFirstPage();
}
});
mLastButton = (Button) v.findViewById(R.id.last_crime_button);
mLastButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((CrimePagerActivity)getActivity()).jumpToLastPage();
}
});
Hope this will work perfectly.
Ok this was a answer that I was given by the teacher incase anyone is interested.
mFirstButton = (Button) v.findViewById(R.id.first_crime_button);
mFirstButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ViewPager pager = (ViewPager) getActivity().findViewById(R.id.activity_crime_pager_view_pager);
pager.setCurrentItem(0);
}
});
mLastButton = (Button) v.findViewById(R.id.last_crime_button);
mLastButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ViewPager pager = (ViewPager) getActivity().findViewById(R.id.activity_crime_pager_view_pager);
pager.setCurrentItem(CrimeLab.get(getActivity()).getCrimes().size()-1);
}
});
Create two button and on click set
viewPager.setCurrentItem(0); // for top
and
viewPager.setCurrentItem(mCrimes.size()); // for last
I want to build a simple wallpaper app but just can't set the wallpaper. I have tried many methods but just can't understand how to implement wallpapermanager.
final LinearLayout L1 = (LinearLayout) findViewById(R.id.l1);
final ImageView I1 = (ImageView) findViewById(R.id.imageView);
ImageView I2 = (ImageView) findViewById(R.id.imageView2);
ImageView I3 = (ImageView) findViewById(R.id.imageView3);
ImageView I4 = (ImageView) findViewById(R.id.imageView4);
ImageView I5 = (ImageView) findViewById(R.id.imageView5);
ImageView I6 = (ImageView) findViewById(R.id.imageView6);
ImageView I7 = (ImageView) findViewById(R.id.imageView7);
ImageView I8 = (ImageView) findViewById(R.id.imageView8);
ImageView I9 = (ImageView) findViewById(R.id.imageView9);
ImageView I10 = (ImageView) findViewById(R.id.imageView10);
ImageView I11 = (ImageView) findViewById(R.id.imageView11);
ImageView I12 = (ImageView) findViewById(R.id.imageView12);
ImageView I13 = (ImageView) findViewById(R.id.imageView13);
ImageView I14 = (ImageView) findViewById(R.id.imageView14);
Button b1 = (Button) findViewById(R.id.button);
final WallpaperManager my = WallpaperManager.getInstance(getApplicationContext());
I1.setOnClickListener(new ImageView.OnClickListener() {
#Override
public void onClick(View v) {
{
/*abc = 1;
if(abc==1) {
L1.setVisibility(View.GONE);
abc = 0;
}*/
}
}
}
);
I2.setOnClickListener(new ImageView.OnClickListener() {
#Override
public void onClick(View v) {
I1.setBackground(getDrawable(p));
}
}
);
I3.setOnClickListener(new ImageView.OnClickListener() {
#Override
public void onClick(View v) {
I1.setBackground(getDrawable(r));
}
}
);
I4.setOnClickListener(new ImageView.OnClickListener() {
#Override
public void onClick(View v) {
I1.setBackground(getDrawable(s));
}
}
);
I5.setOnClickListener(new ImageView.OnClickListener() {
#Override
public void onClick(View v) {
I1.setBackground(getDrawable(t));
}
}
);
I6.setOnClickListener(new ImageView.OnClickListener() {
#Override
public void onClick(View v) {
I1.setBackground(getDrawable(u));
}
}
);
I7.setOnClickListener(new ImageView.OnClickListener() {
#Override
public void onClick(View v) {
I1.setBackground(getDrawable(t));
}
}
);
I8.setOnClickListener(new ImageView.OnClickListener() {
#Override
public void onClick(View v) {
I1.setBackground(getDrawable(s));
}
}
);
I9.setOnClickListener(new ImageView.OnClickListener() {
#Override
public void onClick(View v) {
I1.setBackground(getDrawable(w));
}
}
);
I10.setOnClickListener(new ImageView.OnClickListener() {
#Override
public void onClick(View v) {
I1.setBackground(getDrawable(x));
}
}
);
I11.setOnClickListener(new ImageView.OnClickListener() {
#Override
public void onClick(View v) {
I1.setBackground(getDrawable(y));
}
}
);
I12.setOnClickListener(new ImageView.OnClickListener() {
#Override
public void onClick(View v) {
I1.setBackground(getDrawable(z));
}
}
);
I13.setOnClickListener(new ImageView.OnClickListener() {
#Override
public void onClick(View v) {
I1.setBackground(getDrawable(zz));
}
}
);
I14.setOnClickListener(new ImageView.OnClickListener() {
#Override
public void onClick(View v) {
I1.setBackground(getDrawable(xx));
}
}
);
b1.setOnClickListener(new ImageView.OnClickListener() {
#Override
public void onClick(View v) {
my.setResource(R.drawable.);
}
}
);