'startActivityForResult(android.content.Intent, int)' is deprecated <<Java>> - java

how to solve this problem
'startActivityForResult(android.content.Intent, int)' is deprecated
here is for my MainActivity.java
`
package com.example.praktikump10;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btnMoveActivity;
private Button btnMoveWithDataActivity;
private Button btnDialPhone;
private Button btnMoveWithObject;
private Button btnMoveForResult;
private TextView tvResult;
private int REQUEST_CODE = 100;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnMoveActivity = (Button) findViewById(R.id.btn_move_activity);
btnMoveActivity.setOnClickListener(this);
btnMoveWithDataActivity = (Button) findViewById(R.id.btn_move_activity_data);
btnMoveWithDataActivity.setOnClickListener(this);
btnDialPhone = (Button) findViewById(R.id.btn_dial_number);
btnDialPhone.setOnClickListener(this);
btnMoveWithObject = (Button) findViewById(R.id.btn_move_activity_object);
btnMoveWithObject.setOnClickListener(this);
btnMoveForResult = (Button) findViewById(R.id.btn_move_for_result);
btnMoveForResult.setOnClickListener(this);
tvResult = (TextView) findViewById(R.id.tv_result);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_move_activity:
Intent moveIntent = new Intent(MainActivity.this, MoveActivity.class);
startActivity(moveIntent);
break;
case R.id.btn_move_activity_data:
Intent moveWithDataIntent = new Intent(MainActivity.this, MoveWithDataActivity.class);
moveWithDataIntent.putExtra(MoveWithDataActivity.EXTRA_NAME, "Hafidz Izdihar Muslim\n");
moveWithDataIntent.putExtra(MoveWithDataActivity.EXTRA_AGE, 20);
startActivity(moveWithDataIntent);
break;
case R.id.btn_dial_number:
String phoneNumber = "087878336770";
Intent dialPhoneIntent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber));
startActivity(dialPhoneIntent);
break;
case R.id.btn_move_activity_object:
Person mPerson = new Person();
mPerson.setName("DicodingAcademy");
mPerson.setAge(5);
mPerson.setEmail("academy#dicoding.com");
mPerson.setCity("Bandung");
Intent moveWithObjectIntent = new Intent(MainActivity.this, MoveWithObjectActivity.class);
moveWithObjectIntent.putExtra(MoveWithObjectActivity.EXTRA_PERSON, mPerson);
startActivity(moveWithObjectIntent);
break;
case R.id.btn_move_for_result:
Intent moveForResultIntent = new Intent(MainActivity.this, MoveForResultActivity.class);
startActivityForResult(moveForResultIntent, REQUEST_CODE);
break;
}
}
protected void onActivityResult ( int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE) {
if (resultCode == MoveForResultActivity.RESULT_CODE) {
int selectedValue = data.getIntExtra(MoveForResultActivity.EXTRA_SELECTED_VALUE, 0);
tvResult.setText("Hasil : " + selectedValue);
}
}
}
}
`
and this is my activity_main.xml
`
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.praktikump10.MainActivity">
<Button
android:id="#+id/btn_move_activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:text="Pindah Activity" />
<Button
android:id="#+id/btn_move_activity_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:text="Pindah Activity Dengan Data" />
<Button
android:id="#+id/btn_dial_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:text="Dial a Number" />
<Button
android:id="#+id/btn_move_activity_object"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:text="Pindah Activity dengan Object" />
<Button
android:id="#+id/btn_move_for_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:text="Pindah Activity untuk Result" />
<TextView
android:id="#+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Hasil"
android:textSize="24sp" />
</LinearLayout>
`
this is for my MoveForResultActivity.java
`
package com.example.praktikump10;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import androidx.appcompat.app.AppCompatActivity;
import com.example.praktikump10.R;
public class MoveForResultActivity extends AppCompatActivity
implements View.OnClickListener {
private Button btnChoose;
private RadioGroup rgNumber;
public static String EXTRA_SELECTED_VALUE = "extra_selected_value";
public static int RESULT_CODE = 110;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_move_for_result);
btnChoose = (Button) findViewById(R.id.btn_choose);
btnChoose.setOnClickListener(this);
rgNumber = (RadioGroup) findViewById(R.id.rg_number);
}
#Override
public void onClick(View v) {
if (v.getId() == R.id.btn_choose) {
if (rgNumber.getCheckedRadioButtonId() != 0) {
int value = 0;
switch (rgNumber.getCheckedRadioButtonId()) {
case R.id.rb_50:
value = 50;
break;
case R.id.rb_100:
value = 100;
break;
case R.id.rb_150:
value = 150;
break;
case R.id.rb_200:
value = 200;
break;
}
Intent resultIntent = new Intent();
resultIntent.putExtra(EXTRA_SELECTED_VALUE, value);
setResult(RESULT_CODE, resultIntent);
finish();
}
}
}
}
`
activity_move_for_result.xml
`
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.praktikump10.MoveForResultActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:text="Pilih angka yang kamu suka" />
<RadioGroup
android:id="#+id/rg_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="#+id/rb_50"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:text="50" />
<RadioButton
android:id="#+id/rb_100"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:text="100" />
<RadioButton
android:id="#+id/rb_150"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:text="150" />
<RadioButton
android:id="#+id/rb_200"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:text="200" />
</RadioGroup>
<Button
android:id="#+id/btn_choose"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Pilih" />
</LinearLayout>
`
why the
'startActivityForResult(android.content.Intent, int)' is deprecated
on MainActivity.java
any answer i really appreciated
thank you for the valueable answer...

Related

How can my buttons be visible after adding a new code?

my code works well, with 2 buttons ("home" and "chiudi") at the bottom, but I wanted to add more code to my webview app, and this new code hides my buttons.
This is the new code I want to add:
...
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView wView, String url)
{
if (url.startsWith("mailto:") || url.startsWith("tel:") || url.startsWith("geo:") ||
url.startsWith("http:") || url.startsWith("https:")) {
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse(url));
startActivity(intent);
return true;
} else if (url.startsWith("whatsapp:")) {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
sendIntent.setPackage("com.whatsapp");
startActivity(sendIntent);
return true;
} else if (url.startsWith("spotify:")) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(
"spotify:album:0sNOF9WDwhWunNAHPD3Baj"));
intent.putExtra(Intent.EXTRA_REFERRER,
Uri.parse("android-app://" + context.getPackageName()));
startActivity(intent);
return true;
}
return false;
}
});
...
And this is my code:
for activity.main.xml:
...
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<Button
android:id="#+id/button"
android:layout_width="183dp"
android:layout_height="41dp"
android:layout_marginStart="191dp"
android:layout_marginLeft="171dp"
android:layout_x="207dp"
android:layout_y="629dp"
android:background="#0000ff"
android:onClick="clickexit"
android:text="Chiudi"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="#+id/swipe">
</Button>
<Button
android:id="#+id/button2"
android:layout_width="183dp"
android:layout_height="41dp"
android:layout_x="207dp"
android:layout_y="629dp"
android:background="#0000ff"
android:onClick="gohome"
android:text="Home"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/button"
app:layout_constraintStart_toStartOf="parent">
</Button>
<TextView
android:id="#+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="#0000ff"
android:textSize="40sp"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
</TextView>
</androidx.constraintlayout.widget.ConstraintLayout>
...
For MainActivity.java:
...
package it...........;
import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.webkit.URLUtil;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import com.onesignal.OneSignal;
public class MainActivity extends AppCompatActivity {
private static final String ONESIGNAL_APP_ID = "a..............";
private SwipeRefreshLayout swipeRefreshLayout;
private WebView webView;
private TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.textview);
textView.postDelayed(new Runnable() {
#Override
public void run() {
textView.setText("sto caricando");
}
}, 500); // delay of 2 seconds before setting a text to textView
textView.postDelayed(new Runnable() {
public void run() {
textView.setVisibility(View.GONE);
}
}, 2000);
// Enable verbose OneSignal logging to debug issues if needed.
OneSignal.setLogLevel(OneSignal.LOG_LEVEL.VERBOSE, OneSignal.LOG_LEVEL.NONE);
// OneSignal Initialization
OneSignal.initWithContext(this);
OneSignal.setAppId(ONESIGNAL_APP_ID);
webView = findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://d...app");
//per fare il refresh
swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swipe);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
swipeRefreshLayout.setRefreshing(true);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
swipeRefreshLayout.setRefreshing(false);
webView.reload();
}
}, 2000);
}
});
swipeRefreshLayout.setColorSchemeColors(
getResources().getColor(android.R.color.holo_blue_dark),
getResources().getColor(android.R.color.holo_orange_dark),
getResources().getColor(android.R.color.holo_green_dark),
getResources().getColor(android.R.color.holo_red_dark));
}
public void gohome(View v)
{
webView.loadUrl("https://...app");
}
public void clickexit(View v)
{
moveTaskToBack(true);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
}
public void onBackPressed() {
if(webView.canGoBack()) {
webView.goBack();
}
else
super.onBackPressed();
}
}
...
When I add the first code, my buttons disappear.
How can I fix this?
Thank you
Roberto
Your alignment of views is not correct and In ConstraintLayout you have arranged your views by giving reference to other views. It would be good to check this link for a better understanding of how to use ConstraintLayout.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#0000ff"
android:onClick="gohome"
android:text="Home"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/button"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#0000ff"
android:onClick="clickexit"
android:text="Chiudi"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#id/textview"
app:layout_constraintStart_toEndOf="#+id/button2" />
<TextView
android:id="#+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text"
android:textAlignment="center"
android:textColor="#0000ff"
android:textSize="40sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/button" />
</androidx.constraintlayout.widget.ConstraintLayout>

How to avoid the softkeyboard overlapping the EditText in fragment?

I try both methods addling line android:windowSoftInputMode="adjustPan" in manifest or adding getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);on Fragment onCreateView but still facing the same issue. That when i type in the my custom EditText the screen does not auto resize.
here is my fragment:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:fillViewport="true"
tools:context=".Fragments.HelpersFrag.UseAvailablePackage.AddListBasicFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="#layout/toolbar" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:orientation="vertical">
<include layout="#layout/firstpage_indicator_layout" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="24dp"
android:orientation="horizontal">
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:contentDescription="#string/pencil_icon"
android:src="#drawable/pencil_icon" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
style="#style/TextViewTitles"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/basic" />
<include
android:id="#+id/edt_jobTitle"
layout="#layout/custom_edittext" />
<include
android:id="#+id/cbAceptCreditCard"
layout="#layout/checkbox_layout" />
<include
android:id="#+id/cbCoupons"
layout="#layout/checkbox_layout" />
<include
android:id="#+id/cbSos"
layout="#layout/checkbox_layout" />
<include
android:id="#+id/cbNonSmoker"
layout="#layout/checkbox_layout" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
My Custom EditText:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:are="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_marginTop="15dp"
android:background="#drawable/boundaries">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottombar"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:padding="5dp">
<com.chinalwb.are.AREditText
android:id="#+id/arEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top|left"
android:hint="#string/job_description" />
</ScrollView>
<LinearLayout
android:id="#+id/bottombar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:gravity="center"
android:padding="5dp"
android:orientation="horizontal"
android:paddingEnd="8dp"
android:paddingRight="8dp"
android:weightSum="1000"
tools:ignore="RtlSymmetry">
<com.chinalwb.are.styles.toolbar.ARE_ToolbarDefault
android:id="#+id/areToolbar"
android:layout_width="0dp"
android:layout_height="20dp"
android:layout_weight="1000"
android:gravity="center_vertical"/>
<ImageButton
android:id="#+id/btnUnlink"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginStart="11dp"
android:layout_marginLeft="11dp"
android:background="#color/transparent_color"
android:contentDescription="#string/text_unlink"
android:scaleType="fitCenter"
android:src="#drawable/unlink_icon" />
<ImageButton
android:id="#+id/btnUndo"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginStart="11dp"
android:layout_marginLeft="11dp"
android:background="#color/transparent_color"
android:contentDescription="#string/text_undo"
android:scaleType="fitCenter"
android:src="#drawable/undo_icon" />
<ImageButton
android:id="#+id/btnRedo"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginStart="11dp"
android:layout_marginLeft="11dp"
android:background="#color/transparent_color"
android:contentDescription="#string/text_redo"
android:scaleType="fitCenter"
android:src="#drawable/redo_icon" />
</LinearLayout>
</RelativeLayout>
checkbox
<?xml version="1.0" encoding="utf-8"?>
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/checkbox_selector"
android:fontFamily="#font/raleway_bold"
android:text="#string/financial_planning"
android:textColor="#color/texthint_color"
android:textSize="13sp"
android:padding="10dp"
android:theme="#style/CheckBox" />
AddListFragment.java
package com.blendev.niffty.Fragments.HelpersFrag.UseAvailablePackage;
import android.annotation.SuppressLint;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import com.blendev.niffty.Constants.Constants;
import com.blendev.niffty.Fragments.BaseFragment;
import com.blendev.niffty.R;
import com.chinalwb.are.AREditText;
import com.chinalwb.are.styles.toolbar.IARE_Toolbar;
import com.chinalwb.are.styles.toolitems.ARE_ToolItem_Bold;
import com.chinalwb.are.styles.toolitems.ARE_ToolItem_Italic;
import com.chinalwb.are.styles.toolitems.ARE_ToolItem_Link;
import com.chinalwb.are.styles.toolitems.ARE_ToolItem_ListBullet;
import com.chinalwb.are.styles.toolitems.ARE_ToolItem_ListNumber;
import com.chinalwb.are.styles.toolitems.IARE_ToolItem;
import com.google.android.material.snackbar.Snackbar;
import java.util.ArrayList;
import java.util.List;
import ren.qinc.edit.PerformEdit;
public class AddListBasicFragment extends BaseFragment {
Button btnNext;
CheckBox cbCreditCard, cbCoupons, cbSos, cbNonSmoker;
View view;
private IARE_Toolbar mToolbar;
private AREditText mEditText;
PerformEdit mPerformEdit;
EditText edtTitle;
ImageButton btnUndo, btnRedo, btnUnlink;
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
String selectedText;
private String mParam1;
private String mParam2;
List arrayList;
public AddListBasicFragment() {
}
public static AddListBasicFragment newInstance(String param1, String param2) {
AddListBasicFragment fragment = new AddListBasicFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_add_list_basic, container, false);
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
initView();
initToolbar();
return view;
}
void initView() {
Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
toolbar.setTitle("");
arrayList = new ArrayList<String>();
ImageButton btnClose = (ImageButton) view.findViewById(R.id.btnClose);
edtTitle = view.findViewById(R.id.edt_jobTitle);
btnNext = view.findViewById(R.id.btnNext_id);
cbCreditCard = view.findViewById(R.id.cbAceptCreditCard);
cbCoupons = view.findViewById(R.id.cbCoupons);
cbSos = view.findViewById(R.id.cbSos);
cbNonSmoker = view.findViewById(R.id.cbNonSmoker);
btnUndo = view.findViewById(R.id.btnUndo);
btnRedo = view.findViewById(R.id.btnRedo);
btnUnlink = view.findViewById(R.id.btnUnlink);
cbCreditCard.setText(R.string.cbAcceptCreditCard);
cbCoupons.setText(R.string.cbCoupons);
cbSos.setText(R.string.cbSosHelper);
cbNonSmoker.setText(R.string.cbNonSmoker);
edtTitle.setHint(R.string.job_title);
btnClose.setOnClickListener(this);
btnNext.setOnClickListener(this);
btnUndo.setOnClickListener(this);
btnRedo.setOnClickListener(this);
btnUnlink.setOnClickListener(this);
}
private void initToolbar() {
mToolbar = view.findViewById(R.id.areToolbar);
IARE_ToolItem bold = new ARE_ToolItem_Bold();
IARE_ToolItem italic = new ARE_ToolItem_Italic();
IARE_ToolItem listNumber = new ARE_ToolItem_ListNumber();
IARE_ToolItem listBullet = new ARE_ToolItem_ListBullet();
IARE_ToolItem link = new ARE_ToolItem_Link();
mToolbar.addToolbarItem(bold);
mToolbar.addToolbarItem(italic);
mToolbar.addToolbarItem(listNumber);
mToolbar.addToolbarItem(listBullet);
mToolbar.addToolbarItem(link);
mEditText = view.findViewById(R.id.arEditText);
mEditText.setTextSize(13);
mEditText.setToolbar(mToolbar);
mPerformEdit = new PerformEdit(mEditText);
bold.getStyle().getImageView().setImageResource(R.drawable.bold_icon);
bold.getStyle().getImageView().setScaleType(ImageView.ScaleType.CENTER_INSIDE);
italic.getStyle().getImageView().setImageResource(R.drawable.italic_icon);
italic.getStyle().getImageView().setScaleType(ImageView.ScaleType.CENTER_INSIDE);
try {
listNumber.getStyle().getImageView().setImageResource(R.drawable.numbering_icon);
listBullet.getStyle().getImageView().setImageResource(R.drawable.bullet_icon);
link.getStyle().getImageView().setImageResource(R.drawable.link_icon);
} catch (Exception e) {
Log.e("ERROR: ", e.toString());
}
}
#SuppressLint("NonConstantResourceId")
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnNext_id:
String jobTitle = edtTitle.getText().toString();
String jobDescription = mEditText.getText().toString();
validateInputs(jobTitle, jobDescription);
break;
case R.id.btnClose:
getActivity().onBackPressed();
break;
case R.id.btnUndo:
mPerformEdit.undo();
break;
case R.id.btnRedo:
mPerformEdit.redo();
break;
case R.id.btnUnlink:
break;
}
}
void validateInputs(String jobTitle, String jobDescription) {
if (jobTitle.isEmpty() || jobDescription.isEmpty()) {
Log.d("TAG", "String field empty");
Snackbar snackBar = Snackbar.make(getActivity().findViewById(android.R.id.content),
"Please fill the requirements....", Snackbar.LENGTH_LONG);
snackBar.show();
} else {
Bundle bundle = new Bundle();
bundle.putString(Constants.JOB_TITLE, jobTitle);
bundle.putString(Constants.JOB_DESCRIPTION, jobDescription);
arrayList.clear();
if (cbCreditCard.isChecked()) {
arrayList.add(cbCreditCard.getText());
}
if (cbCoupons.isChecked()) {
arrayList.add(cbCoupons.getText());
}
if (cbSos.isChecked()) {
arrayList.add(cbSos.getText());
}
if (cbNonSmoker.isChecked()) {
arrayList.add(cbNonSmoker.getText());
}
Log.d("TAG", "List size: " + arrayList.size());
Fragment imgFileFragment = new ImagesFilesFragment();
imgFileFragment.setArguments(bundle);
this.changeFragment(imgFileFragment, Constants.ADD_LIST_BASIC_FRAGMENT);
}
}
}
Manifest
<activity
android:name=".Activities.MainScreenActivity"
android:theme="#style/AppTheme.NoActionBar"
android:windowSoftInputMode="adjustPan"/>

Can't pass data from second Activity to main

I am having a trouble in passing data between Activity2 and ActivityMain.
I have ActivityMain with a "Add Person" button. When I press this button, I go to Activity2, after that I fill field Name and press button "Add", but can't to add Name on my ActivityMain.
ActivityMain.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add Person"
android:onClick="addPerson"
/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
ActivityMain.java
package com.example.test.zadanie01;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
Intent intent = getIntent();
String message = intent.getStringExtra(PersonActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
}
public void addPerson(View view){
Intent intent = new Intent(this, PersonActivity.class);
startActivityForResult(intent, 1);
}
}
Activity2.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Name"
android:id="#+id/send_name"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add Person"
android:onClick="addPersonToMain"
/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
Activity2.java
package com.example.test.zadanie01;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import static android.provider.AlarmClock.EXTRA_MESSAGE;
public class PersonActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE = "EXTRA_MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_person);
}
public void addPersonToMain(View view){
Intent returnintent = new Intent(this, MainActivity.class);
EditText SendName = (EditText) findViewById(R.id.send_name);
String message = SendName.getText().toString();
returnintent.putExtra(EXTRA_MESSAGE, message);
setResult(RESULT_OK, returnintent);
finish();
}
}
try to replace
String message = intent.getStringExtra(PersonActivity.EXTRA_MESSAGE);
with
String message = data.getStringExtra(PersonActivity.EXTRA_MESSAGE);
Change this part of your code.
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (requestCode == 1){
if (resultCode == RESULT_OK){
String message = data.getStringExtra(PersonActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
}
}
}
You don't need to create another Intent in onActivityResult, the Intent is already delivered by the method: Intent data. Then use data as your intent. And before all, check if it is the requestCode you asked and if the result is OK.
EDIT
Answering another part of the question from the comments.
1 - Add a TextView to your activity_main.xml and set its visibility to gone by default.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/text_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
android:visibility="gone" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add Person"
android:onClick="addPerson"
/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
2 - When retrieveng the data message, show the TextView and display the content.
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (requestCode == 1){
if (resultCode == RESULT_OK){
String message = data.getStringExtra(PersonActivity.EXTRA_MESSAGE);
TextView textView = findViewById(R.id.text_message);
textView.setVisibility(View.VISIBLE);
textView.setText(message);
}
}
}

Dynamically Remove EditText Field in LinearLayout

My problem is that once my editText fields are created, I don't know how to remove them one by one with the other button in my app. I don't really understand the android:id element as it relates to setID() in my java code. I guess my main question is, how do I figure out how to target dynamically created editText views with their id's if I don't know their id's. Here is my code, any help is really appreciated:
Java:
package com.zdowning.decisionmaker;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ScrollView;
import android.widget.Toast;
import java.lang.*;
import static com.zdowning.decisionmaker.R.layout.activity_main;
public class MainActivity extends AppCompatActivity {
public int numberOfLines = 3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(activity_main);
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getAnswer();
}
});
final Button add_button = (Button) findViewById(R.id.add_button);
add_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Add_Line();
}
});
final Button remove_button = (Button) findViewById(R.id.remove_button);
remove_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Remove_Line();
}
});
}
public void Add_Line() {
LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayoutDecisions);
// add edittext
EditText et = new EditText(this);
LinearLayout.LayoutParams p = new
LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
et.setLayoutParams(p);
et.setId(numberOfLines++);
et.setText("Enter Answer #" + numberOfLines++);
ll.addView(et);
numberOfLines++;
Toast.makeText(MainActivity.this, "1", Toast.LENGTH_LONG).show();
}
public void Remove_Line() {
}
public void getAnswer() {
String[] options = new String[numberOfLines];
EditText text = (EditText)findViewById(R.id.editText2);
options[0] = text.getText().toString();
text = (EditText)findViewById(R.id.editText3);
options[1] = text.getText().toString();
text = (EditText)findViewById(R.id.editText4);
options[2] = text.getText().toString();
int number = (int)(Math.random() * 3);
String answer = options[number];
TextView answerBox = (TextView)findViewById(R.id.textView7);
answerBox.setText(answer);
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#d4cfcd">
tools:context="com.zdowning.decisionmaker.MainActivity">
<LinearLayout
android:id="#+id/linearLayoutMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical" >
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center"
android:hint="Enter Your Question Here"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"/>
<LinearLayout
android:id="#+id/linearLayoutDecisions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/editText3"
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center"
android:hint="Enter Answer #2" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center"
android:hint="Enter Answer #1" />
<EditText
android:id="#+id/editText4"
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center"
android:hint="Enter Answer #3" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<Button
android:id="#+id/add_button"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:text="+"
android:textColor="#android:color/background_light"
android:textSize="18sp"/>
<View
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" />
<Button
android:id="#+id/remove_button"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:text="-"
android:textColor="#android:color/background_light"
android:textSize="18sp"
android:layout_margin="10dp"/>
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/button"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:text="DECIDE!"
android:textColor="#android:color/background_light"
android:textSize="18sp"
android:layout_centerInParent="true" />
</RelativeLayout>
<TextView
android:id="#+id/textView7"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_margin="20dp"
android:textColor="#android:color/black"
android:textSize="36sp" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
There is no relationship between android:id and your setID.
Check this answer to unsderstand more about that.
Anyway, one approach to achieve to remove a generated line could be based on the children index of your LinearLayout like this:
private LinearLayout mEditTextContainer;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
mEditTextContainer = (LinearLayout)findViewById(R.id.linearLayoutDecisions);
...
}
public void addLine() {
// add edittext
EditText et = new EditText(this);
LinearLayout.LayoutParams p = new
LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
et.setLayoutParams(p);
et.setText("Enter Answer #" + (mEditTextContainer.getChildCount()+1));
mEditTextContainer.addView(et);
Toast.makeText(MainActivity.this, "1", Toast.LENGTH_LONG).show();
}
public void removeLine() {
mEditTextContainer.removeViewAt(mEditTextContainer.getChildCount()-1);
}

What should i do to move to another activity when only special conditions are met

I am making a quiz app. I want the exit page to be displayed when the next button is pressed after last question has been answered. I have a activity_questions.xml and a activity_exit.xml. The xml for activity_questions.xml is :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.crystal.questions">
<TextView
android:id="#+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="29dp"
android:fontFamily="sans-serif-thin"
android:padding="10dp"
android:text=""
android:textColor="#440027"
android:textSize="30dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:id="#+id/edit2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/text3"
android:layout_margin="30dp"
android:background="#9775AA"
android:hint="Answer"
android:inputType="text"
android:padding="10dp"
android:textSize="20dip" />
<LinearLayout
android:id="#+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/edit2"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="#+id/answer"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:onClick="onAnswerClick"
android:padding="2dp"
android:text="Okay" />
<Button
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:onClick="onHintClick"
android:padding="2dp"
android:text="Hint" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingBottom="5dp"
android:orientation="vertical"
android:layout_marginTop="26dp"
android:layout_below="#+id/ll"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<ImageView
android:id="#+id/tickcross"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_below="#+id/ll"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:src="#drawable/wierdtick" />
<TextView
android:id="#+id/correctornot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tickcross"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:text="Correct!"
android:textColor="#440027"
android:textSize="30dp" />
<Button
android:id="#+id/nextbutton"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Next"
android:onClick="onNextClick"
/>
</LinearLayout>
</RelativeLayout>
And the java is:
package com.example.android.crystal;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
public class questions extends AppCompatActivity {
Random r = new Random();
private boolean done;
private int QuestionNo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_questions);
findViewById(R.id.tickcross).setVisibility(View.INVISIBLE);
findViewById(R.id.correctornot).setVisibility(View.INVISIBLE);
findViewById(R.id.nextbutton).setVisibility(View.INVISIBLE);
String[] questions = getResources().getStringArray(R.array.Questions);
TextView t = (TextView) findViewById(R.id.text3);
t.setText(questions[QuestionNo]);
}
public void onFinishClick(View view){
Intent intent = new Intent(this, exit.class);
startActivity(intent);
}
public static void hideKeyboardFrom(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
public void answersubmitted() {
findViewById(R.id.tickcross).setVisibility(View.VISIBLE);
TranslateAnimation animation = new TranslateAnimation(0, 0, 2000, 0);
animation.setDuration(1000);
animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
findViewById(R.id.tickcross).startAnimation(animation);
findViewById(R.id.correctornot).setVisibility(View.VISIBLE);
findViewById(R.id.nextbutton).setVisibility(View.VISIBLE);
}
public void onHintClick(View view) {
String[] hints = getResources().getStringArray(R.array.Hints);
Toast toasty = Toast.makeText(getApplicationContext(), hints[QuestionNo], Toast.LENGTH_SHORT);
toasty.show();
}
public void onAnswerClick(View view) {
if (done == false) {
hideKeyboardFrom(this);
String answer = ((EditText) findViewById(R.id.edit2)).getText().toString();
String[] answers = getResources().getStringArray(R.array.Answers);
String correctAnswer = answers[QuestionNo];
correctAnswer = correctAnswer.toUpperCase();
answer = answer.toUpperCase();
if (answer.equals(correctAnswer)) {
TextView t = (TextView) findViewById(R.id.correctornot);
t.setText("CORRECT!");
ImageView i = (ImageView) findViewById(R.id.tickcross);
i.setImageDrawable(getDrawable(R.drawable.wierdtick));
answersubmitted();
} else {
TextView t = (TextView) findViewById(R.id.correctornot);
t.setText("Correct Answer: " + correctAnswer);
ImageView i = (ImageView) findViewById(R.id.tickcross);
i.setImageDrawable(getDrawable(R.drawable.weirdcross));
answersubmitted();
}
done = true;
}
}
public void onNextClick(View view){
if (done){
String[] questions = getResources().getStringArray(R.array.Questions);
if (QuestionNo < (questions.length - 1)) {
QuestionNo += 1;
TextView t = (TextView) findViewById(R.id.text3);
t.setText(questions[QuestionNo]);
findViewById(R.id.tickcross).setVisibility(View.INVISIBLE);
findViewById(R.id.correctornot).setVisibility(View.INVISIBLE);
findViewById(R.id.nextbutton).setVisibility(View.INVISIBLE);
EditText et = (EditText) findViewById(R.id.edit2);
et.setText("");
done = false;
}
else {
onFinishClick();
}
}
}
}
But the onFinishClick() gives an error
Thanks in advance for your help
What should i do to move to another activity when only special conditions are met
Basically, an if-statement and an Intent.
Raw code with no variables added:
if(condition(s)){
Intent i = new Intent(this, TargetActivity.class);
//Put arguments into the intent if you need them...
startActivity(i);
}
You can put it in a thread, in an onClickListener, anywhere and any way you want to execute the code.
As to the error you are receiving, I cannot help you with that until you supply the stacktrace from Logcat
You can do like this:
mNextBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (mQuestionCount < questionList.size()){
// set next question
} else {
// call finish
}
}
});

Categories

Resources