second activity crashes only on the phone not on the emulator - java

The first screen upload right and works fine (Toast and all other setting and parameters) until I try to save the result and open the new screen by the same button. Pushing the button crashes the app in my phone without opening the second screen. However in the emulator all work fine.
this is the Main activity:
public class MainActivity extends AppCompatActivity {
LineChart lineChart1;
RadioGroup rg1,rg2;
Button tipBtn;
Button checkAndCont;
int [] mSlop ={-1,3};
int [] b= {-3,3};
boolean userAnswer1 = false;
boolean userAnswer2 = false;
String quesType = "Slop Recognition";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lineChart1 = (LineChart)findViewById(R.id.lineChart1);
rg1 = (RadioGroup)findViewById(R.id.rg1);
rg2 = (RadioGroup)findViewById(R.id.rg2);
tipBtn = (Button)findViewById(R.id.tipBtb);
tipBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,"גרף במערכת צירים נקרא משמאל לימין",Toast.LENGTH_SHORT ).show();
}
});
checkAndCont = (Button)findViewById(R.id.checkAndCont);
ArrayList<String> xAxes = new ArrayList<>();
ArrayList<Entry> yAxesLinear1 = new ArrayList<>();
ArrayList<Entry> yAxesLinear2 = new ArrayList<>();
int numberDataPoint = 21;
for (int i=0 ; i<numberDataPoint;i++ ){
int x=i-10;
float funLinear1= mSlop[0]*x+b[0];
float funLinear2= mSlop[1]*x+b[1];
yAxesLinear1.add(new Entry(x,funLinear1));
yAxesLinear2.add(new Entry(x,funLinear2));
//xAxes.add(i,String.valueOf(x));
}
//String [] xaxes = new String[xAxes.size()];
//for (int i=0; i<xAxes.size(); i++){
// xaxes[i]=xAxes.get(i);
//}
ArrayList<ILineDataSet> lineDataSet= new ArrayList<>();
LineDataSet lineDataSet1 = new LineDataSet(yAxesLinear1,"פונקציה קווית 1");
lineDataSet1.setDrawCircles(false);
lineDataSet1.setColor(Color.CYAN);
lineDataSet1.setDrawValues(false);
lineDataSet1.setLineWidth(3);
LineDataSet lineDataSet2 = new LineDataSet(yAxesLinear2,"פונקציה קווית 2");
lineDataSet2.setDrawCircles(false);
lineDataSet2.setColor(Color.GREEN);
lineDataSet2.setDrawValues(false);
lineDataSet2.setLineWidth(3);
lineDataSet.add(lineDataSet1);
lineDataSet.add(lineDataSet2);
lineChart1.setData(new LineData(lineDataSet));
lineChart1.setDrawBorders(true);
lineChart1.setBorderColor(0xffff00ff);
lineChart1.setBorderWidth(2);
lineChart1.setScaleEnabled(true);
//lineChart1.setDrawGridBackground(true);
//lineChart1.setGridBackgroundColor(0xffff00ff);
//lineChart1.setBackgroundColor(0x00000000);
XAxis xAxis = lineChart1.getXAxis();
//xAxis.setEnabled(true);
xAxis.setDrawAxisLine(true);
xAxis.setAxisLineColor(Color.WHITE);
lineChart1.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
lineChart1.getAxisRight().setEnabled(false);
lineChart1.getXAxis().setAxisMaximum(10);
lineChart1.getXAxis().setAxisMinimum(-10);
lineChart1.getXAxis().setGridColor(0xffff00ff);
//lineChart1.getXAxis().setDrawAxisLine(true);
//lineChart1.getXAxis().setAxisLineColor(Color.WHITE); // The buttom limit line of the chart
//lineChart1.setVisibleXRangeMaximum(50);
lineChart1.getXAxis().setTextColor(Color.WHITE);
lineChart1.getXAxis().setTextSize(15);
LimitLine ll = new LimitLine(0);
ll.setLineColor(Color.WHITE);
ll.setLineWidth(1);
xAxis.addLimitLine(ll);
YAxis yAxis = lineChart1.getAxisLeft();
yAxis.setDrawZeroLine(true);
yAxis.setZeroLineColor(Color.WHITE);// no grid lines
yAxis.setZeroLineWidth(1); //Sets the line-width of the zero line.
yAxis.setAxisMinimum(-10f); // start at zero
yAxis.setAxisMaximum(10f); // the axis maximum is 100
yAxis.setGridColor(0xffff00ff);
yAxis.setTextColor(Color.WHITE);
yAxis.setTextSize(15);
Legend legend = lineChart1.getLegend();
legend.setEnabled(true);
legend.setTextColor(Color.WHITE);
legend.setTextSize(18);
legend.setFormSize(13);
legend.setForm(Legend.LegendForm.CIRCLE);
legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
RadioButton rb1 = (RadioButton) radioGroup.findViewById(i);
//if(R.id.rg1==i ? userAnswer1==true : userAnswer1==false);
if (null != rb1 && i > -1) {
// checkedId is the RadioButton selected
switch (i) {
case R.id.rb1:
userAnswer1=false;
break;
case R.id.rb2:
userAnswer1=true;
break;
}
}
Toast.makeText(MainActivity.this,Boolean.toString(userAnswer1),Toast.LENGTH_SHORT ).show();
}
});
rg2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
RadioButton rb2 = (RadioButton) radioGroup.findViewById(i);
//if(R.id.rg1==i ? userAnswer1==true : userAnswer1==false);
if (null != rb2 && i > -1) {
// checkedId is the RadioButton selected
switch (i) {
case R.id.rb3:
userAnswer2=false;
break;
case R.id.rb4:
userAnswer2=true;
break;
}
}
Toast.makeText(MainActivity.this,Boolean.toString(userAnswer2),Toast.LENGTH_SHORT ).show();
}
});
}
public void result(View view) {
if (isTrue(mSlop[0])==userAnswer1 && isTrue(mSlop[1])==userAnswer2){
Toast.makeText(MainActivity.this,"תשובה נכונה",Toast.LENGTH_SHORT ).show();
}else {
Toast.makeText(MainActivity.this,"לא נכון",Toast.LENGTH_SHORT ).show();
}
startActivity(new Intent(MainActivity.this, Main2Activity.class));
}
boolean mType;
public boolean isTrue (int m){
if (m >0){
mType=true;
}else{
mType=false;
}
return mType;
}
}
this is the related XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:paddingTop="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:background="#drawable/background_color">
<com.github.mikephil.charting.charts.LineChart
android:layout_width="match_parent"
android:layout_height="400dp"
android:id="#+id/lineChart1">
</com.github.mikephil.charting.charts.LineChart>
<TextView
android:id="#+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/lineChart1"
android:layout_marginStart="187dp"
android:paddingRight="15dp"
android:text="פונקציה 1 עולה"
android:textColor="#00FFFF"
android:textSize="25sp" />
<TextView
android:id="#+id/tv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/tv1"
android:layout_marginStart="187dp"
android:paddingRight="15dp"
android:text="פונקציה 2 עולה"
android:textColor="#ff00ff00"
android:textSize="25sp" />
<RadioGroup
android:id="#+id/rg1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_below="#+id/lineChart1"
android:orientation="horizontal">
<RadioButton
android:id="#+id/rb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#00FFFF"
android:text="לא נכון"
android:textColor="#00FFFF"
android:paddingLeft="2dp"
android:textSize="25sp"
/>
<RadioButton
android:id="#+id/rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#00FFFF"
android:text="נכון"
android:textColor="#00FFFF"
android:paddingLeft="2dp"
android:textSize="25sp"
android:layout_marginLeft="5sp"/>
</RadioGroup>
<RadioGroup
android:id="#+id/rg2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_below="#+id/rg1"
android:orientation="horizontal">
<RadioButton
android:id="#+id/rb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#ff00ff00"
android:text="לא נכון"
android:textColor="#ff00ff00"
android:paddingLeft="2dp"
android:textSize="25sp"
/>
<RadioButton
android:id="#+id/rb4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#ff00ff00"
android:text="נכון"
android:textColor="#ff00ff00"
android:paddingLeft="2dp"
android:textSize="25sp"
android:layout_marginLeft="5dp"/>
</RadioGroup>
<Button
android:id="#+id/tipBtb"
android:layout_width="50sp"
android:layout_height="50sp"
android:layout_alignParentStart="true"
android:layout_below="#+id/rg2"
android:layout_marginStart="66dp"
android:layout_marginTop="11dp"
android:scaleType="fitCenter"
android:text="TIP" />
<Button
android:id="#+id/checkAndCont"
android:text="שלח"
android:layout_width="80sp"
android:layout_height="50sp"
android:layout_marginTop="11dp"
android:layout_below="#+id/tv2"
android:layout_centerHorizontal="true"
android:onClick="result"/>
</RelativeLayout>
this is the second activity:
public class Main2Activity extends AppCompatActivity {
LineChart lineChart2;
Button quickTipBtn,sendAndNext;
TextView question1,question2;
EditText answer1,answer2;
final int [] mSlop ={1,3};
final int [] b= {1,3};
private boolean userAnswer1 = false;
private boolean userAnswer2 = false;
final String quesType = "y-intercept Recognition";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
question1 = (TextView)findViewById(R.id.question1);
question2 = (TextView)findViewById(R.id.question2);
answer1 = (EditText)findViewById(R.id.answer1);
answer2 = (EditText)findViewById(R.id.answer2);
quickTipBtn =(Button)findViewById(R.id.quickTipBtn);
quickTipBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(Main2Activity.this,"קואורדינטת החיתוך עם ציר Y ו b - שווים",Toast.LENGTH_SHORT ).show();
}
});
sendAndNext = (Button)findViewById(R.id.sendAndNext);
lineChart2 = (LineChart)findViewById(R.id.lineChart2);
ArrayList<String> xAxes = new ArrayList<>();
ArrayList<Entry> yAxesLinear1 = new ArrayList<>();
ArrayList<Entry> yAxesLinear2 = new ArrayList<>();
int numberDataPoint = 21;
for (int i=0 ; i<numberDataPoint;i++ ){
int x=i-10;
float funLinear1= mSlop[0]*x+b[0];
float funLinear2= mSlop[1]*x+b[1];
yAxesLinear1.add(new Entry(x,funLinear1));
yAxesLinear2.add(new Entry(x,funLinear2));
//xAxes.add(i,String.valueOf(x));
}
//String [] xaxes = new String[xAxes.size()];
//for (int i=0; i<xAxes.size(); i++){
// xaxes[i]=xAxes.get(i);
//}
ArrayList<ILineDataSet> lineDataSet= new ArrayList<>();
LineDataSet lineDataSet1 = new LineDataSet(yAxesLinear1,"פונקציה קווית 1");
lineDataSet1.setDrawCircles(false);
lineDataSet1.setColor(Color.CYAN);
lineDataSet1.setDrawValues(false);
lineDataSet1.setLineWidth(3);
LineDataSet lineDataSet2 = new LineDataSet(yAxesLinear2,"פונקציה קווית 2");
lineDataSet2.setDrawCircles(false);
lineDataSet2.setColor(Color.GREEN);
lineDataSet2.setDrawValues(false);
lineDataSet2.setLineWidth(3);
lineDataSet.add(lineDataSet1);
lineDataSet.add(lineDataSet2);
lineChart2.setData(new LineData(lineDataSet));
lineChart2.setDrawBorders(true);
lineChart2.setBorderColor(0xffff00ff);
lineChart2.setBorderWidth(2);
lineChart2.setScaleEnabled(true);
//lineChart1.setDrawGridBackground(true);
//lineChart1.setGridBackgroundColor(0xffff00ff);
//lineChart1.setBackgroundColor(0x00000000);
XAxis xAxis = lineChart2.getXAxis();
//xAxis.setEnabled(true);
xAxis.setDrawAxisLine(true);
xAxis.setAxisLineColor(Color.WHITE);
lineChart2.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
lineChart2.getAxisRight().setEnabled(false);
lineChart2.getXAxis().setAxisMaximum(10);
lineChart2.getXAxis().setAxisMinimum(-10);
lineChart2.getXAxis().setGridColor(0xffff00ff);
//lineChart1.getXAxis().setDrawAxisLine(true);
//lineChart1.getXAxis().setAxisLineColor(Color.WHITE); // The buttom limit line of the chart
//lineChart1.setVisibleXRangeMaximum(50);
lineChart2.getXAxis().setTextColor(Color.WHITE);
lineChart2.getXAxis().setTextSize(15);
LimitLine ll = new LimitLine(0);
ll.setLineColor(Color.WHITE);
ll.setLineWidth(1);
xAxis.addLimitLine(ll);
YAxis yAxis = lineChart2.getAxisLeft();
yAxis.setDrawZeroLine(true);
yAxis.setZeroLineColor(Color.WHITE);// no grid lines
yAxis.setZeroLineWidth(1); //Sets the line-width of the zero line.
yAxis.setAxisMinimum(-10f); // start at zero
yAxis.setAxisMaximum(10f); // the axis maximum is 100
yAxis.setGridColor(0xffff00ff);
yAxis.setTextColor(Color.WHITE);
yAxis.setTextSize(15);
Legend legend = lineChart2.getLegend();
legend.setEnabled(true);
legend.setTextColor(Color.WHITE);
legend.setTextSize(18);
legend.setFormSize(13);
legend.setForm(Legend.LegendForm.CIRCLE);
legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
}
public void sendAndNext(View view) {
String temValue1= answer1.getText().toString();
//Toast.makeText(secondScreen.this, answer1.getText(),Toast.LENGTH_LONG).show();
String temValue2= answer2.getText().toString();
if (Integer.parseInt(temValue1)==mSlop[0] && Integer.parseInt(temValue2)==mSlop[1]){
Toast.makeText(Main2Activity.this, "תשובה נכונה",Toast.LENGTH_LONG).show();
}else {
Toast.makeText(Main2Activity.this, "תשובה לא נכונה או שלא הוזנו נתונים",Toast.LENGTH_LONG).show();
}
}
}
this is the second XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="5"
android:gravity="top"
android:paddingTop="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:background="#drawable/background_color2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3.5"
android:paddingTop="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp"
android:paddingLeft="5dp">
<com.github.mikephil.charting.charts.LineChart
android:id="#+id/lineChart2"
android:layout_width="match_parent"
android:layout_height="400dp"
>
</com.github.mikephil.charting.charts.LineChart>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:paddingTop="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:orientation="horizontal"
android:weightSum="2"
android:gravity="right">
<EditText
android:id="#+id/answer1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:digits="0123456789."
android:inputType="numberSigned"
android:maxLength="2"
android:hint="הכנס מספר"
android:textColorHint="#78ffd6"
android:textSize="18dp"
android:textColor="#00FFFF"
/>
<TextView
android:id="#+id/question1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.5"
android:paddingRight="5dp"
android:paddingLeft="5dp"
android:text="למה שווה הפרמטר b ?"
android:textColor="#00FFFF"
android:textSize="25sp"
>
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:paddingTop="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:orientation="horizontal"
android:weightSum="2"
android:gravity="right"
>
<EditText
android:id="#+id/answer2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:digits="0123456789."
android:maxLength="2"
android:hint="הכנס מספר"
android:textColorHint="#64f38c"
android:textSize="18dp"
android:inputType="numberSigned"
android:textColor="#ff00ff00"
/>
<TextView
android:id="#+id/question2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.5"
android:text="מהי נק' החיתוך עם ציר Y?"
android:textColor="#ff00ff00"
android:textSize="25sp"
>
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:paddingTop="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:orientation="horizontal"
android:weightSum="2"
android:gravity="center">
<Button
android:layout_marginRight="40dp"
android:id="#+id/quickTipBtn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Quick Tip"
android:background="#android:color/transparent"
android:textColor="#f05053"
/>
<Button
android:id="#+id/sendAndNext"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Next One"
android:background="#android:color/transparent"
android:textColor="#ffafbd"
android:onClick="sendAndNext"/>
</LinearLayout>
</LinearLayout>
this is the Logcat error:
FATAL EXCEPTION: main
Process: com.example.rachmani.mythematix_linears, PID: 18087
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.rachmani.mythematix_linears/com.example.rachmani.mythematix_linears.Main2Activity}:
android.view.InflateException: Binary XML file line #0: Error
inflating class

Thanks for your patience...
After a close review of the error log, I've indicated the next error:
com.example.rachmani.mythematix_linears:drawable/background_color2" (7f060055) is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f060055 a=-1 r=0x7f060055}
The original drawable was:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:startColor="#0f0c29"
android:endColor="#302b63"
android:type="linear"
android:angle="90"/>
</shape>
</item>
</selector>
Just changed it to:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:startColor="#141e30"
android:endColor="#243b55"
android:type="linear"
android:angle="90"/>
</shape>
</item>
</selector>
It's seems that the gradient background I tried to use make the problem...
Thanks all for your support.

Please check your second XML file for a closing </LinearLayout> tag in the end.
From the code snippet you have posted,it is evident that you haven't closed the first LinearLayout tag. Hope it helps!

Related

I am unable to refer to things inside my activity, like buttons, and radiogroups

so I am trying to create an activity, and then add onclick listeners to it, but it wont let me refer. So the moment I open this activity in my app, my app crashes, saying 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference'
I have no idea why this is happening. I have correctly names them in accordance with the xml file as well.
Please help.
public class SubtaskActivity extends AppCompatActivity {
EditText etSubtaskName;
Button btnDone;
RadioGroup radgrpPri, radgrpTime;
RadioButton radbtnPriHigh, radbtnPriMed, radbtnPriLow, radbtnTimeMore, radbtnTimeMed, radbtnTimeLess;
boolean priHigh, priMed, priLow, timeMore, timeMed, timeLess;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
btnDone = findViewById(R.id.btnDone);
radgrpPri = findViewById(R.id.radgrpPri);
radgrpTime = findViewById(R.id.radgrpTime);
radbtnPriHigh = findViewById(R.id.radbtnPriHigh);
radbtnPriMed = findViewById(R.id.radbtnPriMed);
radbtnPriLow = findViewById(R.id.radbtnPriLow);
radbtnTimeMore = findViewById(R.id.radbtnTimeMore);
radbtnTimeMed = findViewById(R.id.radbtnTimeMed);
radbtnTimeLess = findViewById(R.id.radbtnTimeLess);
etSubtaskName = findViewById(R.id.etSubtaskName);
radgrpPri.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (radbtnPriHigh.isChecked())
{
priHigh = true;
priLow = false;
priMed = false;
}
else if (radbtnPriMed.isChecked())
{
priHigh = false;
priLow = false;
priMed = true;
}
else if (radbtnPriLow.isChecked())
{
priHigh = false;
priLow = true;
priMed = false;
}
}
});
radgrpTime.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (radbtnTimeMore.isChecked())
{
timeMore = true;
timeMed = false;
timeLess = false;
}
else if (radbtnTimeMed.isChecked())
{
timeMore = false;
timeMed = true;
timeLess = false;
}
else if (radbtnTimeLess.isChecked())
{
timeMore = false;
timeMed = false;
timeLess = true;
}
}
});
btnDone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String name = etSubtaskName.getText().toString().trim();
Intent intent = new Intent(SubtaskActivity.this, TaskInfo.class);
intent.putExtra("subtaskName", name);
intent.putExtra("priHigh", priHigh);
intent.putExtra("priMed", priMed);
intent.putExtra("priLow", priLow);
intent.putExtra("timeMore", timeMore);
intent.putExtra("timeMed", timeMed);
intent.putExtra("timeLess", timeLess);
startActivity(intent);
}
});
}
}
XML File :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/it"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:background="#color/background"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/background"
android:orientation="vertical">
<TextView
android:id="#+id/tvSubtaskPriorityHeading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:fontFamily="#font/roboto"
android:text="#string/priority_of_subtask"
android:textColor="#B8AEAE"
android:textSize="16sp" />
<RadioGroup
android:id="#+id/radgrpPri"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<RadioButton
android:id="#+id/radbtnPriHigh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:buttonTint="#color/red"
android:text="#string/high"
android:textColor="#color/white" />
<RadioButton
android:id="#+id/radbtnPriMed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:buttonTint="#color/yellow"
android:text="#string/medium"
android:textColor="#color/white" />
<RadioButton
android:id="#+id/radbtnPriLow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:buttonTint="#color/green"
android:text="#string/low"
android:textColor="#color/white" />
</RadioGroup>
<TextView
android:id="#+id/tvTimeWeightHeading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="32dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:fontFamily="#font/roboto"
android:text="#string/time_this_subtask_may_consume"
android:textColor="#B8AEAE"
android:textSize="16sp" />
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/floating_hint_time_minutes"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:hintTextAppearance="#style/FlotatingHintStyle">
<EditText
android:id="#+id/etSubtaskName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:ems="10"
android:fontFamily="#font/roboto"
android:hint="#string/name_your_subtask"
android:inputType="textPersonName"
android:maxLength="20"
android:textColor="#color/white"
android:textColorHint="#B8AEAE"
android:textSize="14sp" />
</com.google.android.material.textfield.TextInputLayout>
<RadioGroup
android:id="#+id/radgrpTime"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<RadioButton
android:id="#+id/radbtnTimeMore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:buttonTint="#color/red"
android:text="#string/more"
android:textColor="#color/white" />
<RadioButton
android:id="#+id/radbtnTimeMed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:buttonTint="#color/yellow"
android:text="#string/medium"
android:textColor="#color/white" />
<RadioButton
android:id="#+id/radbtnTimeLess"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:buttonTint="#color/green"
android:text="#string/less"
android:textColor="#color/white" />
</RadioGroup>
</LinearLayout>
<Button
android:id="#+id/btnDone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="16dp"
android:layout_marginTop="32dp"
android:layout_marginRight="16dp"
android:gravity="center_horizontal"
android:text="#string/done"
app:backgroundTint="#color/orange_accent" />
</LinearLayout>
You didn't call in onCreate method setContentView(R.layout.youractivity). If you didn't, Android doesn't know what to render, so there are no views for you to provide.
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.youractivity);
}
just set your (xml)layout file to setContentView in onCreate()
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
}

How to save the data from multiple Edittexts that are being added dynamically in android?

I have some EditTexts and I'm trying to save their values to an ArrayList, but my EditTexts are being added by using an add button which adds a LinearLayout, and the EditTexts are within the LinearLayouts being created.
Normally having a fixed amount of EditTexts would be easy to save, but how would I save a continuous amount of EditTexts. I'm adding the LinearLayouts in the method createNewLinearLayout() below, and I want to save the input from the EditTexts from those LinearLayouts.
How can I do that?
My layout
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<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:id="#+id/relativeOverall"
android:orientation="vertical"
android:layout_centerHorizontal="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingTop="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:id="#+id/relativeLayout">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/addCounter"
android:text="Add"
android:layout_marginLeft="0dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/saveBtn"
android:layout_toRightOf="#+id/addCounter"
android:text="Save Data"
android:layout_marginLeft="30dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnView"
android:text="View Data"
android:layout_marginLeft="30dp"
android:layout_toRightOf="#+id/saveBtn" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:id="#+id/horizontalLine"
android:background="#B3B3B3"
android:layout_below="#+id/relativeLayout"
android:layout_marginTop="10dp"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/nameEditText"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:layout_marginTop="0dp"
android:hint="Name of Patient"
android:layout_below="#+id/horizontalLine"/>
<View
android:layout_width="match_parent"
android:layout_height="1sp"
android:id="#+id/horizontalLine2"
android:background="#B3B3B3"
android:layout_below="#+id/nameEditText"
android:layout_marginTop="0dp"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/idEditText"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:layout_marginTop="0dp"
android:hint="ID of Patient"
android:layout_below="#+id/horizontalLine2"/>
<View
android:layout_width="match_parent"
android:layout_height="1sp"
android:id="#+id/horizontalLine3"
android:background="#B3B3B3"
android:layout_below="#+id/idEditText"
android:layout_marginTop="0dp"/>
<EditText
android:layout_width="245dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:id="#+id/actionEditText"
android:textAlignment="center"
android:hint="Action To Record"
android:layout_below="#+id/horizontalLine3"/>
<EditText
android:layout_width="245dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:id="#+id/durationEditText"
android:textAlignment="center"
android:hint="Duration of Action"
android:layout_below="#+id/actionEditText"/>
<LinearLayout
android:id="#+id/linearLayout"
android:layout_below="#+id/durationEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:orientation="horizontal"
android:layout_marginTop="0dp">
<Button
android:id="#+id/minusBtn"
android:layout_width="55sp"
android:layout_height="55sp"
android:text="-"
android:textSize="25dp"/>
<TextView
android:id="#+id/counterTxt"
android:layout_width="wrap_content"
android:layout_height="55dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:textSize="45sp"
android:text="25"/>
<Button
android:id="#+id/plusBtn"
android:layout_width="55sp"
android:layout_height="55sp"
android:text="+"
android:textSize="25sp"/>
</LinearLayout>
<Button
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/delete"
android:layout_below="#+id/linearLayout"
android:text="Delete"
android:textAlignment="center"
android:layout_centerHorizontal="true"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:orientation="vertical"
android:id="#+id/linearLayoutText"
android:layout_below="#+id/horizontalLine3">
</LinearLayout>
</RelativeLayout>
</ScrollView>
My method
private LinearLayout createNewLinearLayout(){
final LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lparams.gravity = Gravity.CENTER_HORIZONTAL;
final ViewGroup.LayoutParams btnParam = plusBtn.getLayoutParams();
final ViewGroup.LayoutParams textParam = counterTxt.getLayoutParams();
final LinearLayout.LayoutParams deleteParam = new LinearLayout.LayoutParams(delete.getLayoutParams());
deleteParam.gravity = Gravity.CENTER_HORIZONTAL;
final LinearLayout.LayoutParams editParams = new LinearLayout.LayoutParams(actionEditText.getLayoutParams());
editParams.gravity = Gravity.CENTER_HORIZONTAL;
final LinearLayout.LayoutParams durationParams = new LinearLayout.LayoutParams(durationEditText.getLayoutParams());
durationParams.gravity = Gravity.CENTER_HORIZONTAL;
LinearLayout.LayoutParams line = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,TypedValue.COMPLEX_UNIT_SP, 1);
line.setMargins(0,15,0,0);
final LinearLayout newLinear = new LinearLayout(this);
newLinear.setLayoutParams(lparams);
final LinearLayout combined = new LinearLayout(this);
combined.setLayoutParams(lparams);
final Button plus = new Button(this);
plus.setLayoutParams(btnParam);
final Button minus = new Button(this);
minus.setLayoutParams(btnParam);
final TextView count = new TextView(this);
count.setLayoutParams(textParam);
final EditText editText = new EditText(this);
editText.setLayoutParams(editParams);
editText.setHint("Action To Record");
editText.setGravity(Gravity.CENTER_HORIZONTAL);
final View v = new View(this);
v.setLayoutParams(line);
v.setBackgroundColor(Color.parseColor("#B3B3B3"));
final EditText duration = new EditText(this);
duration.setLayoutParams(durationParams);
duration.setHint("Duration of Action");
duration.setGravity(Gravity.CENTER_HORIZONTAL);
final Button delete = new Button(this);
delete.setLayoutParams(deleteParam);
delete.setText("delete");
delete.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
linearLayoutText.removeView(combined);
}
});
plus.setText("+");
plus.setTextSize(TypedValue.COMPLEX_UNIT_SP, 25);
plus.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
counting[0]++;
count.setText(counting[0] + "");
}
});
minus.setText("-");
minus.setTextSize(TypedValue.COMPLEX_UNIT_SP, 25);
minus.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
if (counting[0] > 0)
counting[0]--;
count.setText(counting[0] + "");
}
});
count.setTextSize(TypedValue.COMPLEX_UNIT_SP, 45);
newLinear.setOrientation(LinearLayout.HORIZONTAL);
newLinear.setGravity(Gravity.CENTER_HORIZONTAL);
combined.setOrientation(LinearLayout.VERTICAL);
combined.setGravity(Gravity.CENTER_HORIZONTAL);
newLinear.addView(minus);
newLinear.addView(count);
newLinear.addView(plus);
combined.addView(editText);
combined.addView(duration);
combined.addView(newLinear);
combined.addView(delete);
combined.addView(v);
counting[0] = 0;
count.setText(counting[0] + "");
return combined;
}
}
I suggest :
for (int i =0;i<LinearLayout.getChildCount();i++)
{
EditText edt= new EditText (this);
edt = LinearLayout.getChildAt(i);
YourAarraylist[i]=edt.getText().toString();
}
If you have multiple views inside your LinearLayout you can repeat the for like this :
for (int i =0;i<LinearLayout.getChildCount();i++)
{
LinearLayout secondLayout= (LinearLayout) LinearLayout.getChildAt(i);
// and then:
EditText edt= new EditText (this);
edt = secondLayout.getChildAt(indexofyoutEdittext);//You should know this because it's the order of the views you add in your layout
YourAarraylist[i]=edt.getText().toString();
}
I hope it helps !

Scroll entire page when particular EditText has focus?

I have registration page,in that four EditText exists,when I click on third EditText one LinearLayout(assume password guidelines) has to be visible and at the same time total layout should be scroll to view all guidelines.When other EditText (1,2 or 4) got focus this LinearLayout(password guidelines) should be closed. I tried lot and I searched lot , no solution worked for me,please help me. For understand I did small project ,please watch it.
public class MainActivity extends AppCompatActivity {
private EditText mEditText1,mEditText2,mEditText3,mEditText4;
private LinearLayout ll,root;
ScrollView mScrollView ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
root = (LinearLayout) findViewById(R.id.ll_linear_layout);
ll = (LinearLayout) findViewById(R.id.ll);
mEditText1 = (EditText) findViewById(R.id.et1);
mEditText2 = (EditText) findViewById(R.id.et2);
mEditText3 = (EditText) findViewById(R.id.et3);
mEditText4 = (EditText) findViewById(R.id.et4);
mEditText1.setOnFocusChangeListener(mListener1);
mEditText2.setOnFocusChangeListener(mListener2);
mEditText3.setOnFocusChangeListener(mLayoutExtract);
mEditText4.setOnFocusChangeListener(mListener4);
mScrollView = new ScrollView(this);
}
View.OnFocusChangeListener mListener1 = new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if (b) {
} else {
}
}
};
View.OnFocusChangeListener mListener2 = new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if (b) {
} else {
}
}
};
View.OnFocusChangeListener mLayoutExtract = new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if (b) {
ll.setVisibility(View.VISIBLE);
} else {
ll.setVisibility(View.GONE);
}
}
};
View.OnFocusChangeListener mListener4 = new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if (b) {
} else {
}
}
}};
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/ll_linear_layout"
xmlns:android="http://schemas.android.com/apk/res/android">
<EditText
android:layout_marginTop="200dp"
android:id="#+id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="first name"/>
<EditText
android:id="#+id/et2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="second name"/>
<EditText
android:id="#+id/et3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="second name"/>
<LinearLayout
android:id="#+id/ll"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="300dp"
android:visibility="gone">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="75dp"
android:textColor="#000000"
android:text="Hidable text"/>
</LinearLayout>
<EditText
android:id="#+id/et4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="second name"/>
Try this
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/ll_linear_layout"
xmlns:android="http://schemas.android.com/apk/res/android">
<EditText
android:layout_marginTop="200dp"
android:id="#+id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="first name"/>
<EditText
android:id="#+id/et2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="second name"/>
<EditText
android:id="#+id/et3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="second name"/>
<LinearLayout
android:id="#+id/ll"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="300dp"
android:visibility="gone">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="75dp"
android:textColor="#000000"
android:text="Hidable text"/>
</LinearLayout>
<EditText
android:id="#+id/et4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="second name"/>
</LinearLayout>
</ScrollView>
Try this use android:windowSoftInputMode="AdjustResize" in Your Manifest where your are decraing YOur Activity like below code
<activity
android:name=".YourActivity"
android:parentActivityName="XXX.XXX.XXXX"
android:windowSoftInputMode="AdjustResize" />

Showing text from string through TextView after pressing RadioButton

I'm trying to show automaticly "This is the correct answer" or "Try again" just right after the radio button is pressed.
My question is:
How to add two strings
<string name="Good_answer">That is the correct answer</string>
<string name="Wrong_answer">Try again</string>
to this textView
<TextView
android:id="#+id/textViewAnswer"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textAlignment="center"
android:layout_below="#id/rg2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
so I can proceed this
for (boolean radioAnswer : answer)
correct = correct && radioAnswer;
if (correct)
tv.setText(R.string.Good_answer);
else
tv.setText(R.string.Wrong_answer);
And here is setOnClick... (at first it was with checkbutton 'mbuton')
mbuton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean check = true;
boolean correct = true;
// To check if all questions have been answered
for (boolean radioChecked : checked)
check = check && radioChecked;
if (check) {
// To check if all questions have been answered correctly
for (boolean radioAnswer : answer)
correct = correct && radioAnswer;
if (correct)
tv.setText(R.string.Good_answer);
else
tv.setText(R.string.Wrong_answer);
}
else
tv.setText("Answer all questions");
}
});
Xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<RadioGroup
android:id="#+id/rg1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textColor="#android:color/black"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Question 1"/>
<RadioButton
android:text="Correct Option"
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:text="Wrong Option"
android:id="#+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:text="Wrong Option"
android:id="#+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
<RadioGroup
android:layout_marginTop="16dp"
android:layout_below="#+id/rg1"
android:id="#+id/rg2"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textColor="#android:color/black"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Question 2"/>
<RadioButton
android:text="Wrong Option"
android:id="#+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:text="Correct Option"
android:id="#+id/radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:text="Wrong Option"
android:id="#+id/radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
<TextView
android:id="#+id/textViewAnswer"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textAlignment="center"
android:layout_below="#id/rg2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
And string
<string name="Good_answer">That is the correct answer</string>
<string name="Wrong_answer">Try again</string>
// i have modified your code, please check it.
// i have display message if user does not select any radio button,
//another wise it display correct answer count on textview.
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import java.util.ArrayList;
public class Main2Activity2 extends Activity {
TextView tv;
Button mbuton;
RadioGroup rg1,rg2;
ArrayList<Integer> arrayListOfRadioGroupId =new ArrayList<Integer>();
int noAnswerCount= 0;
int correctAnswerRadioButtonCount= 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textViewAnswer);
mbuton = (Button) findViewById(R.id.mbuton);
rg1 = (RadioGroup)findViewById(R.id.rg1);
rg2 = (RadioGroup)findViewById(R.id.rg2);
// Store Radio group id to arraylist
arrayListOfRadioGroupId.add(rg1.getId());
arrayListOfRadioGroupId.add(rg2.getId());
mbuton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
noAnswerCount= 0;
correctAnswerRadioButtonCount= 0;
for(int radioGroupId: arrayListOfRadioGroupId)
{
RadioGroup objRadioGroup = (RadioGroup) findViewById(radioGroupId);
int checkedId=objRadioGroup.getCheckedRadioButtonId();
// get Selected Radio button id.
if(checkedId>-1) {
RadioButton rB = (RadioButton) findViewById(checkedId);
String strRadioButtonText = rB.getText().toString();
// get Selected Radio button Text.
if(strRadioButtonText.equals("Correct Option"))
{
correctAnswerRadioButtonCount ++;
noAnswerCount --;
}
}
else
{
noAnswerCount++;
}
}
if(noAnswerCount > 0)
{
tv.setText("Answer all questions");
}
else
{
tv.setText("Correct Answer Count is: " +correctAnswerRadioButtonCount);
}
}
});
rg2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.radioButton5) {
RadioButton rB = (RadioButton) findViewById(checkedId);
String strRadioButtonText = rB.getText().toString();
// get Selected Radio button Text.
if(strRadioButtonText.equals("Correct Option")) {
tv.setText("Correct Answer");
}
else
{
tv.setText("Wrong Answer");
}
}
}
});
}
}
// my Xml code is.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<RadioGroup
android:id="#+id/rg1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textColor="#android:color/black"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Question 1"/>
<RadioButton
android:text="Correct Option"
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:text="Wrong Option"
android:id="#+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:text="Wrong Option"
android:id="#+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
<RadioGroup
android:layout_marginTop="16dp"
android:layout_below="#+id/rg1"
android:id="#+id/rg2"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textColor="#android:color/black"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Question 2"/>
<RadioButton
android:text="Wrong Option"
android:id="#+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:text="Correct Option"
android:id="#+id/radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:text="Wrong Option"
android:id="#+id/radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
<TextView
android:id="#+id/textViewAnswer"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textAlignment="center"
android:layout_below="#id/rg2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/mbuton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MButton"
android:layout_below="#id/textViewAnswer"/>
</RelativeLayout>
If your problem is just about setting those string in the TextView then, try this:
//For correct answer.
String CorrectAnswer = getString(R.string.Good_answer);
tv.setText(CorrectAnswer);
//For wrong answer.
String WrongAnswer = getString(R.string.Wrong_answer);
tv.setText(WrongAnswer);
The Code is still unclear (I can't Figure out what mButton is) .
However can you also share your layout xml I would suggest having a Radio Group comprising of X number of Radio Buttons (options) that you like and put the on checkedChangeListener
<RadioGroup
android:id="#+id/rgroup"
android:layout_width="fill_parent"
android:layout_height="45dp"
android:background="#drawable/background"
android:gravity="center"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/option1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="#drawable/test_image"
android:button="#null" />
<RadioButton
android:id="#+id/option2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="#drawable/test_image"
android:button="#null" />
</RadioGroup>
Your Activity should implement the OnCheckedChangeListener and you can then define your Radio Group RadioGroup Options = (RadioGroup) findViewById(R.id.rgroup);
Then you can implement a method in your activity where if a option inside that Radio Button is clicked you can use the below method.
#Override
public void onCheckedChanged(RadioGroup group,
int checkedId)
{
switch (checkedId)
{
case R.id.option1:
// settextview to correct here
break;
case R.id.option2:
//set textview to wrong here
break;
default:
break;
}
}
Assuming you might have answers at dynamic positions you might want to code some logic in the on checked change method . Hope this helps
Layout file; look at those questions and answers. I've hardcoded the string here. You can set it in string resource file and fetch it here using #string or set it dynamically using yourTextView.setText() method.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<RadioGroup
android:id="#+id/rg1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="What is your name?"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/black" />
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Melisa" />
<RadioButton
android:id="#+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alisha" />
<RadioButton
android:id="#+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lolita" />
</RadioGroup>
<RadioGroup
android:id="#+id/rg2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/rg1"
android:layout_marginTop="16dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="What are you learning?"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/black" />
<RadioButton
android:id="#+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C++" />
<RadioButton
android:id="#+id/radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android" />
<RadioButton
android:id="#+id/radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HTML" />
</RadioGroup>
<TextView
android:id="#+id/textViewAnswer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/rg2"
android:textAlignment="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
And in your activity.
public class MainActivity extends AppCompatActivity {
private RadioGroup radioGroupFirst, radioGroupSecond;
private String[] realAnswers = {"Melisa", "Android"};
private String[] userAnswers;
private TextView tv;
private boolean status = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroupFirst = (RadioGroup) findViewById(R.id.rg1);
radioGroupSecond = (RadioGroup) findViewById(R.id.rg2);
tv = (TextView) findViewById(R.id.textViewAnswer);
userAnswers = new String[2];
radioGroupFirst.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
tv.setText("");
RadioButton nameRadio = (RadioButton) findViewById(checkedId);
Log.e("ID", "" + nameRadio.getText().toString());
userAnswers[0] = nameRadio.getText().toString();
}
});
radioGroupSecond.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton learningRadio = (RadioButton) findViewById(checkedId);
Log.e("ID", "" + learningRadio.getText().toString());
userAnswers[1] = learningRadio.getText().toString();
if (checkStatus(userAnswers))
tv.setText(getString(R.string.Good_answer));
else
tv.setText(getString(R.string.Wrong_answer));
}
});
}
private boolean checkStatus(String[] userAnswers) {
status = true;
for (int i = 0; i < userAnswers.length; i++) {
Log.e(userAnswers[i], realAnswers[i]);
if (!userAnswers[i].equals(realAnswers[i]))
status = false;
}
return status;
}
}
I haven't use the string resources here, if you want to set the text view with the string from resources too, look it in my first answer.
So, I think this will help you.

Focus changes to listview once i clear the Edittext

I'm using broadcast receiver to show a dialog.So the flow of code is something like:
Step1 Getting the requestCode value
Step2 Based on this requestCode the broadCast receiver goes to if or else if or else part
Step3 If the value that i entered using some scanner into the EditText(i.e Scan) doesn't matches it shows a Toast "Item Not Available".
Step 4 Once "Item Not Available" toast comes the focus changes to the Listview which is my problem.
Step5 Again if i pass value to the Scan EditText the Listview get click automatically.
So my question is "How to remove focus from the Listview" and set it to the EditText(i.e Scan).
For Reference I'm attaching the snap with code snippet and the layout.xml.Please have a look and drop your suggestions why the focus is going to the listview.
.java snippet
final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent != null) {
loc = mspinner.getItemAtPosition(mspinner.getSelectedItemPosition())
.toString();
final String ItemNo;
final String Desc;
final String StockUnit;
final String PickSeq;
final String qtyCount;
final String qtyonHand;
final Button mok;
final Button mcancel;
final Button mplus;
final Button mminus;
final EditText medtQtyCount;
final EditText medtItem;
final EditText medtdesc;
final EditText medtuom;
final DatabaseHandler dbHandler;
final String[] UOM = null;
int requestCode;
LayoutInflater li = LayoutInflater.from(InventoryCount.this);
View promptsView = li.inflate(R.layout.quantityupdate, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
InventoryCount.this);
alertDialogBuilder.setView(promptsView);
//requestCode=Integer.parseInt(intent.getStringExtra("idx"));
requestCode=intent.getIntExtra("idx", -1);
// create alert dialog
final AlertDialog alertDialog = alertDialogBuilder.create();
dbHandler = new DatabaseHandler(InventoryCount.this);
medtuom = (EditText) promptsView.findViewById(R.id.edt_mseshipuom_mic);
mok = (Button) promptsView.findViewById(R.id.btn_mseshipOk_mic);
mcancel = (Button) promptsView.findViewById(R.id.btn_mseshipCancel_mic);
mplus = (Button) promptsView.findViewById(R.id.btn_mseshipIncr_mic);
mminus = (Button) promptsView.findViewById(R.id.btn_mseshipDecr_mic);
medtQtyCount = (EditText) promptsView
.findViewById(R.id.edt_shipShiped_mic);
medtdesc = (EditText) promptsView
.findViewById(R.id.edt_mseshipQtyOrd_mic);
medtItem = (EditText) promptsView
.findViewById(R.id.edt_mseshipItemNo_mic);
if (requestCode == 1) {
}
else if (requestCode == 0) {
// ItemNo
/*if (resultCode == RESULT_OK) {
Log.i("Scan resul format: ",
intent.getStringExtra("SCAN_RESULT_FORMAT"));
*/
String itNo = intent.getStringExtra("SCAN_RESULT");
dbhelper.getReadableDatabase();
MIC_Inventory mic_inventory = dbhelper.getMicInventoryDetails(
loc, itNo);
dbhelper.closeDatabase();
if (mic_inventory != null) {
loc = mspinner.getItemAtPosition(
mspinner.getSelectedItemPosition()).toString();
ItemNo = mic_inventory.getItemno();
Desc = mic_inventory.getItemdescription();
PickSeq = mic_inventory.getPickingseq();
StockUnit = mic_inventory.getStockunit();
qtyonHand = mic_inventory.getQoh();// This value gives
// QOHand
qtyCount = mic_inventory.getQc();
medtItem.setText(ItemNo);
medtdesc.setText(Desc);
medtQtyCount.setText(qtyCount);
medtuom.setText(StockUnit);
mplus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String a = medtQtyCount.getText().toString();
int b = Integer.parseInt(a);
b = b + 1;
a = a.valueOf(b);
medtQtyCount.setText(a);
}
});
mminus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int c = Integer.parseInt(medtQtyCount.getText()
.toString());
c = c - 1;
medtQtyCount.setText(new Integer(c).toString());
}
});
mok.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
/*
* UOM[mspinnerUom.getSelectedItemPosition()] =
* medtQtyCount .getText().toString();
*/
MIC_UOMInternal mic_uom = new MIC_UOMInternal();
mic_uom.setLocation(loc);
mic_uom.setItemno(ItemNo);
String updatedqtyCount = medtQtyCount.getText()
.toString();
if (!qtyCount.equals(updatedqtyCount)) {
mic_uom.setQc(Double
.parseDouble(updatedqtyCount));
mic_uom.setUom(StockUnit);
MIC_Inventory mic_Inventory = new MIC_Inventory();
mic_Inventory.setItemdescription(Desc);
mic_Inventory.setItemno(ItemNo);
mic_Inventory.setLocation(loc);
mic_Inventory.setPickingseq(PickSeq);
mic_Inventory.setQc(updatedqtyCount);
mic_Inventory.setQoh(qtyonHand);
mic_Inventory.setStockunit(StockUnit);
dbHandler.getWritableDatabase();
String result = dbHandler
.insertIntoInternal(mic_uom);
if (result.equals("success")) {
result = dbHandler.updateMIC(mic_Inventory);
}
dbHandler.closeDatabase();
}
Intent i = new Intent(InventoryCount.this,
InventoryCount.class);
i.putExtra("et", 1);
i.putExtra("LOCATION", loc);
// i.putExtra("ID", ID);
startActivity(i);
// InventoryCount.this.finish();
}
});
mcancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
alertDialog.cancel();
}
});
// show it
alertDialog.show();
} else {
/*
* Toast.makeText(this, "Item not available",
* Toast.LENGTH_LONG).show();
*/
toastText.setText("Item not available");
Toast toast = new Toast(getBaseContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 410);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(toastLayout);
toast.show();
msearchtext.setText("");
/*msearchtext.setFocusableInTouchMode(true);
msearchtext.requestFocus();*/
/*msearchtext.setSelection(0);
lstView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
*/msearchtext.requestFocus();
}
else if (requestCode == 2) {
}
else
{
toastText.setText("Problem in Scanning");
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 410);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(toastLayout);
toast.show();
}
}
Layout.xml
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/border_green"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin" >
<TextView
android:id="#+id/txt_InvTitle"
style="#style/pageTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:text="#string/invTitle" />
<View
android:id="#+id/txt_InvView"
android:layout_width="match_parent"
android:layout_height="2dip"
android:layout_below="#+id/txt_InvTitle"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#2E9AFE" />
<LinearLayout
android:id="#+id/invLocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/txt_InvView"
android:layout_marginTop="16dp" >
<TextView
android:id="#+id/txtLoc"
style="#style/textRegular"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="left|center"
android:text="#string/location" />
<Spinner
android:id="#+id/sploc"
style="#style/SpinnerItemAppTheme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:editable="false" />
</LinearLayout>
<LinearLayout
android:id="#+id/invScanType"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/invLocation"
android:layout_gravity="center"
android:layout_marginBottom="3dp"
android:layout_marginLeft="3dp"
android:layout_marginTop="18dp"
android:orientation="horizontal" >
<EditText
android:id="#+id/edt_Search_mic"
style="#style/EditTextAppTheme_Scan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_weight=".15"
android:gravity="center"
android:hint="#string/scan" />
<RadioGroup
android:id="#+id/radioScanBasedOn_mic"
style="#style/RadioButtonAppTheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/radioInum_mic"
style="#style/textRegular"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:button="#drawable/radiobutton_selector"
android:checked="true"
android:drawablePadding="50dp"
android:paddingLeft="10dip"
android:text="#string/itemno" />
<RadioButton
android:id="#+id/radioNum_mic"
style="#style/textRegular"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:button="#drawable/radiobutton_selector"
android:checked="false"
android:layout_marginRight="5dp"
android:layout_weight=".25"
android:drawablePadding="50dp"
android:paddingLeft="10dip"
android:text="#string/manfno" />
<RadioButton
android:id="#+id/radioUpc_mic"
style="#style/textRegular"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:button="#drawable/radiobutton_selector"
android:checked="false"
android:layout_marginRight="5dp"
android:layout_weight=".25"
android:drawablePadding="50dp"
android:paddingLeft="10dip"
android:text="#string/upc" />
</RadioGroup>
</LinearLayout>
<HorizontalScrollView
android:id="#+id/scroll_full_mic"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/invScanType" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginTop="25dp"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/lay_fullTitle_mic"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:orientation="horizontal"
android:padding="5dp" >
<TextView
style="#style/textRegular_list"
android:layout_width="105dp"
android:layout_height="wrap_content"
android:text="#string/itemno"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<TextView
style="#style/textRegular_list"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:gravity="center|left"
android:text="#string/description"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<TextView
style="#style/textRegular_list"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:gravity="center|left"
android:text="#string/pick_seq"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<TextView
style="#style/textRegular_list"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:gravity="center|left"
android:text="#string/qoh"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<TextView
style="#style/textRegular_list"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:gravity="center|left"
android:text="#string/qc"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<TextView
style="#style/textRegular_list"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:gravity="center|left"
android:text="#string/uom"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
</LinearLayout>
<ListView
android:id="#+id/lst_msefull_mic"
android:layout_width="match_parent"
android:layout_height="match_parent"
style="#style/ListViewAppTheme.White" >
</ListView>
</LinearLayout>
</HorizontalScrollView>
<LinearLayout
android:id="#+id/lay_PO_mic"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="41dp"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:visibility="gone" >
<Button
android:id="#+id/btn_OrderLstImport_mic"
android:layout_width="100dp"
android:layout_height="100dp"
android:textSize="18dp"
android:textStyle="bold" />
<Button
android:id="#+id/btn_OrderLstExport_mic"
android:layout_width="100dp"
android:layout_height="100dp"
android:textSize="18dp"
android:textStyle="bold" />
<Button
android:id="#+id/btn_OrderLstExit_mic"
android:layout_width="100dp"
android:layout_height="100dp"
android:textSize="18dp"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
Add a textwatcher to edit text and check when text is not blank and it is not equal to expected text then only switch the focus.
/* Set Text Watcher listener */
yourEditText.addTextChangedListener(passwordWatcher);
and check for text once user enter text
private final TextWatcher passwordWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
public void afterTextChanged(Editable s) {
if (s.length() != 0 && passwordEditText.getText().equals("Your expected text")) {
// show your toast and change focus
}
}
}
You should make your listview not focusable by using setFocusable(false) when not required and when you get response correctly from barcode scanner then you can again make your listview focusable.

Categories