I'm having a problem with my code. Whenever I run it, it shows the error error application stopped unexpectedly. I think the issue is caused by some error in the Java class. What exactly is causing this issue?
Here is my XML file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:padding="10dp"
android:overScrollMode="always"
android:layout_height="match_parent"
android:background="#A52A2A"
tools:context="com.safeermalik.gpa.Start">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18sp"
android:text="Subject :"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter Subject Name"
android:id="#+id/subject"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18sp"
android:text="Marks :"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter Subject Marks"
android:id="#+id/marks"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18sp"
android:text="Credits :"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter Subject Credits"
android:id="#+id/credits"/>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18sp"
android:text="Criteria :"/>
<RadioButton android:id="#+id/radio_old"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Old Criteria"
android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="#+id/radio_new"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Criteria"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>
<Button
android:padding="5dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Add course"
android:background="#696969"
android:id="#+id/add"
android:onClick="sendMessage"/>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:id="#+id/add_table"
>
<TableRow
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/border"
android:textStyle="bold"
android:textSize="18sp"
android:text="Subject "/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/border"
android:textStyle="bold"
android:textSize="18sp"
android:text="Marks "/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/border"
android:textStyle="bold"
android:textSize="18sp"
android:text="Credits "/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/border"
android:textStyle="bold"
android:textSize="18sp"
android:text="Percentage "/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/border"
android:textStyle="bold"
android:textSize="18sp"
android:text="GPA "/>
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18sp"
android:text="Total Marks : "/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/total_marks"
android:hint="Total Marks "/>
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18sp"
android:text="S-GPA : "/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/s_gpa"
android:hint="GPA "/>
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18sp"
android:text="PERCENTAGE : "/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/final_percentage"
android:hint="Percentage "/>
</TableRow>
</TableLayout>
</LinearLayout>
Here is the Java class:
public class Start extends ActionBarActivity {
EditText sub;
EditText mark;
EditText credit;
Button Adds;
int a=0,b=0,hour=0,x=0,counter=0;
String grade="";
String subject="";
EditText SUM,percent,GPA;
double gpa,gpa_multiple;
TableLayout table;
CalcGPA cal=new CalcGPA();
public void onRadioButtonClicked(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch(view.getId()) {
case R.id.radio_new:
if (checked)
x=1;
break;
case R.id.radio_old:
if (checked)
x=0;
break;
} }
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start);
sub=(EditText)findViewById(R.id.subject);
subject=sub.getText().toString();
mark=(EditText)findViewById(R.id.marks);
a=Integer.parseInt(String.valueOf(mark));
credit=(EditText)findViewById(R.id.credits);
b=Integer.parseInt(String.valueOf(credit));
Adds=(Button)findViewById(R.id.add);
table=(TableLayout)findViewById(R.id.add_table);
SUM=(EditText)findViewById(R.id.total_marks);
GPA=(EditText)findViewById(R.id.s_gpa);
percent=(EditText)findViewById(R.id.final_percentage);
Adds.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
sub.setText("");
mark.setText("");
credit.setText("");
counter++;
hour +=b;
if(x==0){
gpa=cal.subgpa(a);
grade=cal.Grade(a);
}
else if(x==1){
gpa=cal.ChangedGpa(a);
grade=cal.ChangedGrade(a);
}
add_row(subject,a,b,gpa,grade);
gpa_multiple+=(gpa*b);
SUM.setText(""+(counter*a));
GPA.setText(""+(gpa_multiple/hour));
percent.setText(""+((counter*a)/(counter*100)*100));
}
} );
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.start, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void add_row(String sub, int crdt, int marks, double sgpa, String subgrade ){
TableLayout tb = (TableLayout) findViewById(R.id.add_table);
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
TextView subject_name=new TextView(this);
subject_name.setText(counter+""+sub);
subject_name.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
subject_name.setBackgroundResource(R.drawable.border);
TextView subject_marks=new TextView(this);
subject_marks.setText(""+marks);
subject_marks.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
subject_marks.setBackgroundResource(R.drawable.border);
TextView subject_credits=new TextView(this);
subject_credits.setText(""+crdt);
subject_credits.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
subject_credits.setBackgroundResource(R.drawable.border);
TextView subject_gpa=new TextView(this);
subject_gpa.setText(""+sgpa);
subject_gpa.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.WRAP_CONTENT));
subject_gpa.setBackgroundResource(R.drawable.border);
TextView subject_grade=new TextView(this);
subject_grade.setText(""+subgrade);
subject_grade.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
subject_grade.setBackgroundResource(R.drawable.border);
}
}
You have a logical error and a runtime error here.
Why are you parsing your content of your EditText in the onCreate() method?
just remove those two lines :
//a = Integer.parseInt(String.valueOf(mark));
//b = Integer.parseInt(String.valueOf(credit));
First you don't need to parse them in the onCreate method. You parse the content in the onClick() method of your Button.
that was the logical error.
Second:
Your runtime error is being cause by the fact that you are trying to get the valueOf an EditText and not of a String. so mark should be replace by mark.getText().toString() and credit by credit.getText().toString()
Related
I have a problem with adding multiple popup menus to different buttons. However, I configured to add one popup menu to one setting button as you can see in the screenshot below but when I apply the same code to other setting buttons app crashes.
please tell me how to add multiple pop up menu for all different settings button.
https://i.stack.imgur.com/u5wjf.jpg
here's my java code
package com.techjapreet.shivshankarkiringtone;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.Toast;
public class ringtone_tab extends AppCompatActivity {
Button ringtonepopup;
Button ringtonepopup2;
Button ringtonepopup3;
Button ringtonepopup4;
Button ringtonepopup5;
Button ringtonepopup6;
Button clk1;
Button clk2;
Button clk3;
Button clk4;
Button clk5;
Button clk6;
MediaPlayer mediaPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ringtone_tab);
final Button ringtonepopup = (Button) findViewById(R.id.btn_setting1);
ringtonepopup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
PopupMenu popupMenu = new PopupMenu(ringtone_tab.this, ringtonepopup);
popupMenu.getMenuInflater().inflate(R.menu.popup_menu, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(ringtone_tab.this, "" + item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
});
popupMenu.show();
}
});
clk6 = (Button) findViewById(R.id.btn_play6);
clk5 = (Button) findViewById(R.id.btn_play5);
clk4 = (Button) findViewById(R.id.btn_play4);
clk3 = (Button) findViewById(R.id.btn_play3);
clk2 = (Button) findViewById(R.id.btn_play2);
clk1 = (Button) findViewById(R.id.btn_play1);
mediaPlayer = new MediaPlayer();
}
public void setBtn_play6(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_vandana);
mediaPlayer.start();
}
public void setBtn_play5(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_tandav_mantra);
mediaPlayer.start();
}
public void setBtn_play4(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shiv_om);
mediaPlayer.start();
}
public void setBtn_play3(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shiv);
mediaPlayer.start();
}
public void setBtn_play2(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_aaradhna);
mediaPlayer.start();
}
public void setBtn_play1(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shankar);
mediaPlayer.start();
}
private void stopPlayer(){
if(mediaPlayer != null && mediaPlayer.isPlaying())
{mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
}
}
}
layout code
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:padding="5dp">
<Button
android:id="#+id/btn_play1"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="#drawable/ic_play"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="setBtn_play1" />
<Button
android:id="#+id/btn_setting1"
android:onClick="setsetting1"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_toStartOf="#+id/btn_play1"
android:layout_toLeftOf="#+id/btn_play1"
android:background="#drawable/ic_setting"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="#drawable/ic_setting" />
<ImageView
android:id="#+id/img_grid_item1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/txt_grid_item1"
android:layout_alignBottom="#+id/txt_grid_item1"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:contentDescription="#string/todo"
android:gravity="center_vertical"
android:src="#drawable/ic_logo"
android:visibility="visible" />
<TextView
android:id="#+id/txt_grid_item1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="2dp"
android:layout_marginLeft="2dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="4dp"
android:layout_marginRight="4dp"
android:layout_toStartOf="#+id/btn_setting1"
android:layout_toLeftOf="#+id/btn_setting1"
android:layout_toEndOf="#+id/img_grid_item1"
android:layout_toRightOf="#+id/img_grid_item1"
android:gravity="center_vertical"
android:text="#string/textview"
android:textColor="#color/colorPrimaryDark"
android:textSize="30sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/RelativeLayout2"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:padding="5dp">
<Button
android:id="#+id/btn_play2"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="#drawable/ic_play"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="setBtn_play2"/>
<Button
android:id="#+id/btn_setting2"
android:onClick="setsetting2"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_toStartOf="#+id/btn_play2"
android:layout_toLeftOf="#+id/btn_play2"
android:background="#drawable/ic_setting"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="#drawable/ic_setting" />
<ImageView
android:id="#+id/img_grid_item2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/txt_grid_item2"
android:layout_alignBottom="#+id/txt_grid_item2"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:contentDescription="#string/todo1"
android:gravity="center_vertical"
android:src="#drawable/ic_logo"
android:visibility="visible" />
<TextView
android:id="#+id/txt_grid_item2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toStartOf="#+id/btn_setting2"
android:layout_toLeftOf="#+id/btn_setting2"
android:layout_toEndOf="#+id/img_grid_item2"
android:layout_toRightOf="#+id/img_grid_item2"
android:gravity="center_vertical"
android:text="#string/textview1"
android:textColor="#color/colorPrimaryDark"
android:textSize="30sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/RelativeLayout3"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:padding="5dp">
<Button
android:id="#+id/btn_play3"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="#drawable/ic_play"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="setBtn_play3"/>
<Button
android:id="#+id/btn_setting3"
android:onClick="setsetting3"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_toStartOf="#+id/btn_play3"
android:layout_toLeftOf="#+id/btn_play3"
android:background="#drawable/ic_setting"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="#drawable/ic_setting" />
<ImageView
android:id="#+id/img_grid_item3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/txt_grid_item3"
android:layout_alignBottom="#+id/txt_grid_item3"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:contentDescription="#string/todo2"
android:gravity="center_vertical"
android:src="#drawable/ic_logo"
android:visibility="visible" />
<TextView
android:id="#+id/txt_grid_item3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toStartOf="#+id/btn_setting3"
android:layout_toLeftOf="#+id/btn_setting3"
android:layout_toEndOf="#+id/img_grid_item3"
android:layout_toRightOf="#+id/img_grid_item3"
android:gravity="center_vertical"
android:text="#string/textview2"
android:textColor="#color/colorPrimaryDark"
android:textSize="30sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/RelativeLayout4"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:padding="5dp">
<Button
android:id="#+id/btn_play4"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="#drawable/ic_play"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="setBtn_play4"/>
<Button
android:id="#+id/btn_setting4"
android:onClick="setsetting4"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:layout_toStartOf="#+id/btn_play4"
android:layout_toLeftOf="#+id/btn_play4"
android:background="#drawable/ic_setting"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="#drawable/ic_setting" />
<ImageView
android:id="#+id/img_grid_item4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/txt_grid_item4"
android:layout_alignBottom="#+id/txt_grid_item4"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:contentDescription="#string/todo3"
android:gravity="center_vertical"
android:src="#drawable/ic_logo"
android:visibility="visible" />
<TextView
android:id="#+id/txt_grid_item4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toStartOf="#+id/btn_setting4"
android:layout_toLeftOf="#+id/btn_setting4"
android:layout_toEndOf="#+id/img_grid_item4"
android:layout_toRightOf="#+id/img_grid_item4"
android:gravity="center_vertical"
android:text="#string/textview3"
android:textColor="#color/colorPrimaryDark"
android:textSize="30sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/RelativeLayout5"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:padding="5dp">
<Button
android:id="#+id/btn_play5"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="#drawable/ic_play"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="setBtn_play5"/>
<Button
android:id="#+id/btn_setting5"
android:onClick="setsetting5"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:layout_toStartOf="#+id/btn_play5"
android:layout_toLeftOf="#+id/btn_play5"
android:background="#drawable/ic_setting"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="#drawable/ic_setting" />
<ImageView
android:id="#+id/img_grid_item5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/txt_grid_item5"
android:layout_alignBottom="#+id/txt_grid_item5"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:contentDescription="#string/todo4"
android:gravity="center_vertical"
android:src="#drawable/ic_logo"
android:visibility="visible" />
<TextView
android:id="#+id/txt_grid_item5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toStartOf="#+id/btn_setting5"
android:layout_toLeftOf="#+id/btn_setting5"
android:layout_toEndOf="#+id/img_grid_item5"
android:layout_toRightOf="#+id/img_grid_item5"
android:gravity="center_vertical"
android:text="#string/textview4"
android:textColor="#color/colorPrimaryDark"
android:textSize="30sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/RelativeLayout6"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:padding="5dp">
<Button
android:id="#+id/btn_play6"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="#drawable/ic_play"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="setBtn_play6"/>
<Button
android:id="#+id/btn_setting6"
android:onClick="setsetting6"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:layout_toStartOf="#+id/btn_play6"
android:layout_toLeftOf="#+id/btn_play6"
android:background="#drawable/ic_setting"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="#drawable/ic_setting" />
<ImageView
android:id="#+id/img_grid_item6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/txt_grid_item6"
android:layout_alignBottom="#+id/txt_grid_item6"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:contentDescription="#string/todo5"
android:gravity="center_vertical"
android:src="#drawable/ic_logo"
android:visibility="visible" />
<TextView
android:id="#+id/txt_grid_item6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toStartOf="#+id/btn_setting6"
android:layout_toLeftOf="#+id/btn_setting6"
android:layout_toEndOf="#+id/img_grid_item6"
android:layout_toRightOf="#+id/img_grid_item6"
android:gravity="center_vertical"
android:text="#string/textview5"
android:textColor="#color/colorPrimaryDark"
android:textSize="30sp"
android:textStyle="bold" />
</RelativeLayout>
</LinearLayout>
Define one listener:
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
// place your code here
switch (v.getId()) {
case R.id.btn_play1:
PopupMenu popupMenu = new PopupMenu(ringtone_tab.this, v);
popupMenu.getMenuInflater().inflate(R.menu.popup_menu, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(ringtone_tab.this, "" + item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
});
popupMenu.show();
break;
case R.id.btn_play2:
// place your code here
break;
}
}
};
and apply this listener to all the buttons:
clk1.setOnClickListener(listener);
clk2.setOnClickListener(listener);
.................................
My project won't open on my device, it say's "Unfortunately,myApp has stopped".
this is my manifest:
this is my activity java :
package bismillah.project;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.content.Intent;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class Project2Activity extends Activity {
public static int a,b,c;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//coding start here!!
//input data
EditText pek=(EditText)findViewById(R.id.editText1);
String value=pek.getText().toString();
final int pekerja=Integer.parseInt(value);
EditText lam=(EditText)findViewById(R.id.editText2);
String value1=lam.getText().toString();
final int lama=Integer.parseInt(value1);
EditText up=(EditText)findViewById(R.id.editText3);
String value2=up.getText().toString();
final int upah=Integer.parseInt(value2);
EditText jum=(EditText)findViewById(R.id.editText4);
String value3=jum.getText().toString();
final int jumlah=Integer.parseInt(value3);
//button proses
Button button1=(Button)findViewById(R.id.Button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Spinner bibit = (Spinner)findViewById(R.id.spinner1);
TextView biaya = (TextView)findViewById(R.id.textView6);
if(bibit.getSelectedItem().toString().equals("Cabai Rp.100")){
a=pekerja*(lama*upah)+(jumlah*100);
biaya.setText("Biaya Rp." + a);
if(bibit.getSelectedItem().toString().equals("Tomat Rp.150")){
a=pekerja*(lama*upah)+(jumlah*150);
biaya.setText("Biaya Rp." + a);
if(bibit.getSelectedItem().toString().equals("Timun Rp.200")){
a=pekerja*(lama*upah)+(jumlah*200);
biaya.setText("Biaya Rp." + a);
}
}
}
}
});
//button reset
Button button2=(Button)findViewById(R.id.Button2);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
TextView biaya = (TextView)findViewById(R.id.textView6);
EditText pek=(EditText)findViewById(R.id.editText1);
EditText lam=(EditText)findViewById(R.id.editText2);
EditText up=(EditText)findViewById(R.id.editText3);
EditText jum=(EditText)findViewById(R.id.editText4);
pek.setText("");
lam.setText("");
up.setText("");
jum.setText("");
biaya.setText("");
}
});
//button pindah activity
Button button3=(Button)findViewById(R.id.Button3);
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i =new Intent(getApplicationContext(),activity2.class);
startActivity(i);
}
});
}
}
and this is my main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/bg"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="#string/hello"
android:textColor="#color/warna"
android:textSize="20dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:text="Jumlah Pekerja :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Lama Kerja :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Upah :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Jenis Bibit :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:entries="#array/list" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Jumlah Bibit :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="Large Text"
android:textColor="#color/warna2"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:text="Proses" />
<Button
android:id="#+id/Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Reset" />
<Button
android:id="#+id/Button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Hasil Panen" />
</LinearLayout>
</LinearLayout>
I tried your code and I had to delete some stuff from your XML file because it was creating some bugs. It might be related to me but here's your XML I was able to use:
But please do not change your XML file before trying the solution below. If it still doesn't work, you should consider the elements I removed.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="hello"
android:textSize="20dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:text="Jumlah Pekerja :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Lama Kerja :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Upah :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Jenis Bibit :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Jumlah Bibit :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:text="Proses" />
<Button
android:id="#+id/Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Reset" />
<Button
android:id="#+id/Button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Hasil Panen" />
</LinearLayout>
</LinearLayout>
Next I created your Project2Activity java class and activity2 (pretty much empty since you gave no info about it).
So when trying to run your application you get the following error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.application.so/com.example.application.so.Project2Activity}: java.lang.NumberFormatException: For input string: ""
and it is at :
at com.example.application.so.Project2Activity.onCreate(Project2Activity.java:24)
now if we consider this lines 23 and 24:
String value=pek.getText().toString(); // line 23
final int pekerja=Integer.parseInt(value); // line 24
The error is that you are trying to read (line 23) your editText Value at the creation of the activity, at that time, your editText is still blank which means value will be equal to "" (empty string).
What you want to do instead is to put your lines from 22 to 33 (the ones below) in a button click event, I tried putting them in your button 1 click event and your app doesn't crash any more:
EditText pek=(EditText)findViewById(R.id.editText1);
String value=pek.getText().toString();
final int pekerja=Integer.parseInt(value);
EditText lam=(EditText)findViewById(R.id.editText2);
String value1=lam.getText().toString();
final int lama=Integer.parseInt(value1);
EditText up=(EditText)findViewById(R.id.editText3);
String value2=up.getText().toString();
final int upah=Integer.parseInt(value2);
EditText jum=(EditText)findViewById(R.id.editText4);
String value3=jum.getText().toString();
final int jumlah=Integer.parseInt(value3);
So to summarize, the problem is that you are extracting empty strings from your editTexts and trying to make integers out of them. Instead try making that all reading and integer extracting process on the click of a button where your really need these inputs. just like get the inputs and the final moment.
You should pay attention to your logcat (error messages), it is quite helpful.
What you can also do is to verify that your editTexts are not empty while trying to extract strings from them and raise exception or Toast if they are empty. Nothing too complicated just something like:
if (value.equals("")):
// editText is empty so do something
You can refer to this link for further understanding of how to know when an editText is empty.
So I would like to have a calculator in my app. User will first click on RadioButton which would be for Imperial and Metric units, and depending on which one does user click, one of the layouts would appear.
As you know imperial units has feets and inches, for which I need two EditTexts, but metric unit needs only one.
So that's what I want, switching between layouts in same activity to avoid using fragments.
Any ideas?
EDIT
xml file
<?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">
<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:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="hr.app.liftme.liftmehr.KalkulatoriBFMan"
tools:showIn="#layout/activity_kalkulatori_bfman"
android:orientation="vertical">
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="false"
android:id="#+id/Osnove5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="2dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_weight="1"
android:elevation="50dp"
card_view:cardBackgroundColor="#ffffff"
android:layout_marginBottom="10dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linearLayout28"
android:layout_marginBottom="20dp"
android:layout_marginTop="55dp"
android:padding="7dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_uvodno"
android:id="#+id/textView140"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/bf_tekst_upute"
android:id="#+id/textView141"
android:textColor="#424242" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="false"
android:id="#+id/Drugo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="2dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_weight="1"
android:layout_marginBottom="10dp"
android:elevation="50dp"
card_view:cardBackgroundColor="#ffffff">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linearLayout27"
android:layout_below="#+id/linearLayout28"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:padding="7dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_radio_naslov"
android:id="#+id/textView3"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioButtonBFZeneCmKg"
android:text="#string/bf_radio2"
android:onClick="radioZeneCmKgClicked"
android:checked="false" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioButtonBFZeneFtLb"
android:text="#string/bf_radio1"
android:onClick="radioZeneBFFtLbClicked"
android:checked="false" />
</RadioGroup>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_edit_tezina"
android:id="#+id/textView133"
android:textStyle="bold" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editTezinaBF"
android:hint="#string/bf_edit_tezina_hint_z"
android:layout_marginBottom="5dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/LayoutVisinaMetric">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_edit_visina"
android:id="#+id/textViewVisinaMetric"
android:textStyle="bold" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editVisinaBF"
android:hint="#string/bf_edit_visina_hint_z"
android:layout_marginBottom="5dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/LayoutVisinaImperial">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_feetinch_naslov"
android:id="#+id/textViewVisinaImperial"
android:textStyle="bold" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editTextVisinaFT"
android:layout_weight="1"
android:hint="#string/bf_radio1_hint1" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editTextVisinaINC"
android:layout_weight="1"
android:hint="#string/bf_radio1_hint2" />
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_edit_struk"
android:id="#+id/textView135"
android:textStyle="bold" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editStrukBF"
android:hint="#string/bf_edit_struk_hint_z"
android:layout_marginBottom="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_edit_bokovi"
android:id="#+id/textView136"
android:textStyle="bold" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editBokoviBF"
android:hint="#string/bf_edit_bokovi_hint_z"
android:layout_marginBottom="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_edit_vrat"
android:id="#+id/textView137"
android:textStyle="bold" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editVratBF"
android:hint="#string/bf_edit_vrat_hint_z"
android:layout_marginBottom="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_edit_godine"
android:id="#+id/textView138"
android:textStyle="bold" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editGodineBF"
android:hint="#string/bf_edit_godine_hint_z"
android:layout_marginBottom="15dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linearLayout27"
android:layout_marginBottom="20dp"
android:id="#+id/linearLayout29">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/bf_btn_izracunaj"
android:id="#+id/btnIzracunajBF"
android:layout_gravity="center_horizontal"
android:background="#424242"
android:textColor="#ffffff"
android:layout_marginBottom="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_tekst_rezultat"
android:id="#+id/textView139"
android:layout_gravity="center_horizontal" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/textRezultatBF"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/bf_rezultat"
android:id="#+id/textView152" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="false"
android:id="#+id/Osnove3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="2dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_weight="1"
android:elevation="50dp"
card_view:cardBackgroundColor="#ffffff">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_below="#+id/linearLayout27"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:padding="7dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_tekst_tumacenje"
android:id="#+id/textView142"
android:gravity="center_horizontal"
android:layout_marginBottom="5dp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/bf_tumacenje_rezultat_z"
android:id="#+id/textView143"
android:gravity="center_horizontal"
android:textColor="#424242" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</ScrollView>
java
public class KalkulatoriBFMan extends AppCompatActivity {
EditText editTezina, editVisina, editStruk, editBokovi, editVrat, editGodine, editVisinaFT, editVisinaINC;
Button btnIzracunajBF;
TextView textRezultatBF, textVisinaMetric, textVisinaImperial;
LinearLayout imperial, metric;
public void radioZeneCmKgClicked(View view){
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()){
case R.id.radioButtonBFZeneCmKg:
if (checked)
editVisina.setVisibility(View.VISIBLE);
imperial.setVisibility(View.GONE);
btnIzracunajBF.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
// IMPERIAL FORMULA
//Body Fat (women) = 495 / (1.29579 - .35004 * log10(Waist + Hip - Neck) + 0.22100 * log10(Height)) - 450
double prvoPolje = Double.parseDouble(editTezina.getText().toString());
double drugoPolje = Double.parseDouble(editVisina.getText().toString());
double trecePolje = Double.parseDouble(editStruk.getText().toString());
double cetvrtoPolje = Double.parseDouble(editBokovi.getText().toString());
double petoPolje = Double.parseDouble(editVrat.getText().toString());
double sestoPolje = Double.parseDouble(editGodine.getText().toString());
double rezultat = 0;
rezultat = 495 / (1.0324 - 0.19077 * Math.log10(trecePolje - petoPolje) + 0.15456 * Math.log10(drugoPolje)) - 450;
rezultat = Math.round(rezultat * 100.0) / 100.0;
textRezultatBF.setText(Double.toString(rezultat) + " %");
}
});
break;
}
}
public void radioZeneBFFtLbClicked(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.radioButtonBFZeneFtLb:
if (checked)
editVisinaFT.setVisibility(View.VISIBLE);
metric.setVisibility(View.GONE);
btnIzracunajBF.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
// IMPERIAL FORMULA
//Body Fat (men) = 495 / (1.29579 - .35004 * log10(Waist - Neck) + 0.22100 * log10(Height)) - 450
double prvoPolje = Double.parseDouble(editTezina.getText().toString());
double drugoPolje = Double.parseDouble(editVisinaFT.getText().toString());
double trecePolje = Double.parseDouble(editStruk.getText().toString());
double cetvrtoPolje = Double.parseDouble(editBokovi.getText().toString());
double petoPolje = Double.parseDouble(editVrat.getText().toString());
double sestoPolje = Double.parseDouble(editGodine.getText().toString());
double sedmoPolje = Double.parseDouble(editVisinaINC.getText().toString());
double rezultat = 0;
rezultat = 495 / (1.29579 - 0.35004 * Math.log10(trecePolje - petoPolje) + 0.22100 * Math.log10(((drugoPolje * 12) + sedmoPolje))) - 450;
rezultat = Math.round(rezultat * 100.0) / 100.0;
textRezultatBF.setText(Double.toString(rezultat) + " %");
}
});
break;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_kalkulatori_bfwoman);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Intent intent = getIntent();
editTezina = (EditText) findViewById(R.id.editTezinaBF);
editVisina = (EditText) findViewById(R.id.editVisinaBF);
editStruk = (EditText) findViewById(R.id.editStrukBF);
editBokovi = (EditText) findViewById(R.id.editBokoviBF);
editVrat = (EditText) findViewById(R.id.editVratBF);
editGodine = (EditText) findViewById(R.id.editGodineBF);
editVisinaFT = (EditText) findViewById(R.id.editTextVisinaFT);
editVisinaINC = (EditText) findViewById(R.id.editTextVisinaINC);
textVisinaImperial = (TextView) findViewById(R.id.textViewVisinaImperial);
textVisinaMetric = (TextView) findViewById(R.id.textViewVisinaMetric);
imperial = (LinearLayout) findViewById(R.id.LayoutVisinaImperial);
metric = (LinearLayout) findViewById(R.id.LayoutVisinaMetric);
btnIzracunajBF = (Button) findViewById(R.id.btnIzracunajBF);
textRezultatBF = (TextView) findViewById(R.id.textRezultatBF);
}
}
Note that I solved blank space problem with setting visibility to GONE, but when I open activity both EditTexts appear, imperial and metric. I want them both to be invisible until user selects one RadioButton
To define the click event handler for a button, add the android:onClick attribute to the <RadioButton> element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method.
For example, here are a couple RadioButton objects:
<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton android:id="#+id/radio_pirates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/pirates"
android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="#+id/radio_ninjas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ninjas"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>
Within the Activity that hosts this layout, the following method handles the click event for both radio buttons:
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.imperial:
if (checked)
EditBox1.setVisibility(Visible)
break;
case R.id.metrics:
if (checked){
EditBox1.setVisibility(Visible)
EditBox2.setVisibility(Visible)
}
break;
}
}
Source: http://developer.android.com/guide/topics/ui/controls/radiobutton.html
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:paddingLeft="24dp"
android:text="1. Do you have an anti-virus installed on your computer :"
android:textAllCaps="false" />
<RadioGroup
android:id="#+id/radio_virus"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="48dp">
<RadioButton
android:id="#+id/radio_virusyes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:onClick="assessUserInput"
android:text="Yes" />
<RadioButton
android:id="#+id/radio_virusno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="assessUserInput"
android:text="No" />
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="48dp"
android:text="If No, skip question 2"
android:textAllCaps="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:paddingLeft="24dp"
android:text="2. How many anti-viruses do you use on your laptop :"
android:textAllCaps="false" />
<RadioGroup
android:id="#+id/radio_avnumber"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="48dp">
<RadioButton
android:id="#+id/radio_avnumberone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="assessUserInput"
android:text="1" />
<RadioButton
android:id="#+id/radio_avnumbermore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="assessUserInput"
android:text="More than 1" />
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:paddingLeft="24dp"
android:text="3. Do you use pop-up/advert blockers:"
android:textAllCaps="false" />
<RadioGroup
android:id="#+id/radio_adblock"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="48dp">
<RadioButton
android:id="#+id/radio_adblockyes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:onClick="assessUserInput"
android:text="Yes" />
<RadioButton
android:id="#+id/radio_adblockno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="assessUserInput"
android:text="No" />
</RadioGroup>
Instead of writing text to guide a user to skip question 2 after he
selects "No" in question 1, i want to be able to find a way to disable question two using java or xml codes when the user selects a "No". or automatically take question two away.
Activity:
(Note:i dont know why you want the strings so i separated them from the disabling method but with this it will disable the radio buttons if the user pressed no and they will enable when the user presses yes)
public class main extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainz);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radio_virus);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioGroup rg1 = (RadioGroup) findViewById(R.id.radio_avnumber);
Log.w("","click!");
if (checkedId == R.id.radio_virusno) {
Log.w("","asdasda");
for (int i = 0; i < rg1.getChildCount(); i++) {
rg1.getChildAt(i).setEnabled(false);
}
}else
{
Log.w("","else");
for (int i = 0; i < rg1.getChildCount(); i++) {
rg1.getChildAt(i).setEnabled(true);
}
}
}
});
}
public void assessUserInput(View view){
//// constructor method for Radio Group : Radio_virus
RadioGroup rgVirus=(RadioGroup)findViewById(R.id.radio_virus);
String radioVirus=((RadioButton)findViewById(rgVirus.getCheckedRadioButtonId())).getText().toString();
//// constructor method for Radio Group : Radio_avnumber
RadioGroup rgAvnNumber=(RadioGroup)findViewById(R.id.radio_avnumber);
String radioAvNumber=((RadioButton)findViewById(rgAvnNumber.getCheckedRadioButtonId())).getText().toString();
/// constructor method for Radio Group : Radio_adblock
RadioGroup rgAdBlock=(RadioGroup)findViewById(R.id.radio_adblock);
String radioAdBlock=((RadioButton)findViewById(rgAdBlock.getCheckedRadioButtonId())).getText().toString();
// constructor method for Input field
//EditText nameTextField=(EditText)findViewById(R.id.edit_name);
//String hasName=nameTextField.getText().toString();
}
}
Used xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:paddingLeft="24dp"
android:text="1. Do you have an anti-virus installed on your computer :"
android:textAllCaps="false" />
<RadioGroup
android:id="#+id/radio_virus"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="48dp">
<RadioButton
android:id="#+id/radio_virusyes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Yes" />
<RadioButton
android:id="#+id/radio_virusno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No" />
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="48dp"
android:text="If No, skip question 2"
android:textAllCaps="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:paddingLeft="24dp"
android:text="2. How many anti-viruses do you use on your laptop :"
android:textAllCaps="false" />
<RadioGroup
android:id="#+id/radio_avnumber"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="48dp">
<RadioButton
android:id="#+id/radio_avnumberone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1" />
<RadioButton
android:id="#+id/radio_avnumbermore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="More than 1" />
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:paddingLeft="24dp"
android:text="3. Do you use pop-up/advert blockers:"
android:textAllCaps="false" />
<RadioGroup
android:id="#+id/radio_adblock"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="48dp">
<RadioButton
android:id="#+id/radio_adblockyes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Yes" />
<RadioButton
android:id="#+id/radio_adblockno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No" />
</RadioGroup>
</LinearLayout>
You have to handle onClick of your radiobutton :
public void assessUserInput(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radio_virusyes:
if (checked){
for(int i = 0; i < radio_avnumber.getChildCount(); i++){
((RadioButton)radio_avnumber.getChildAt(i)).setEnabled(true);}}
break;
case R.id.radio_virusno:
if (checked){
for(int i = 0; i < radio_avnumber.getChildCount(); i++){
((RadioButton)radio_avnumber.getChildAt(i)).setEnabled(false);}
}
break;
}
}
public void assessUserInput(View view) {
//// constructor method for Radio Group : Radio_virus
RadioGroup rgVirus = (RadioGroup) findViewById(R.id.radio_virus);
String radioVirus = ((RadioButton) findViewById(rgVirus.getCheckedRadioButtonId())).getText().toString();
//// constructor method for Radio Group : Radio_avnumber
RadioGroup rgAvnNumber = (RadioGroup) findViewById(R.id.radio_avnumber);
String radioAvNumber = ((RadioButton) findViewById(rgAvnNumber.getCheckedRadioButtonId())).getText().toString();
/// constructor method for Radio Group : Radio_adblock
RadioGroup rgAdBlock = (RadioGroup) findViewById(R.id.radio_adblock);
String radioAdBlock = ((RadioButton) findViewById(rgAdBlock.getCheckedRadioButtonId())).getText().toString();
// constructor method for Input field
EditText nameTextField = (EditText) findViewById(R.id.edit_name);
String hasName = nameTextField.getText().toString();
// Is the button now checked
radioVirus.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId = radio_virusno) {
//set the style of the things you want to disable here
}
}
});
here is the picture. i want to click signs & Symptoms from the table of contents and by clicking i want it to jumps directly to the Signs and Symptoms paragraph. Please solve my this problem. here is my code.
<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:background="#ffffff"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".Atherosclerosis"
tools:ignore="UselessParent,ScrollViewCount" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/tablecontentlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#EFECCB" >
<TextView
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17sp"
android:textStyle="bold"
android:textColor="#000000"
android:text="#string/table" />
<TextView
android:id="#+id/definitionid"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#4682B4"
android:text="#string/Definition" />
<TextView
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#4682B4"
android:text="#string/RiskFactors" />
<TextView
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#4682B4"
android:text="#string/Pathophysiology" />
<TextView
android:id="#+id/Signandsymptompsid"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#4682B4"
android:text="#string/SignsandSymptoms" />
<TextView
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#4682B4"
android:text="#string/Diagnosis" />
<TextView
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#4682B4"
android:text="#string/Treatment" />
<TextView
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#4682B4"
android:text="#string/Prevention" />
<TextView
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#4682B4"
android:text="#string/Complications" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tablecontentlayout"
android:layout_marginTop="15dp"
android:orientation="vertical" >
<TextView
android:id="#+id/atherodefinition"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/Definition"
android:background="#F5F5F5"
android:textColor="#264177"
android:textSize="17sp"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/atherosclerosisdefinition"
android:textSize="15sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/RiskFactors"
android:background="#F5F5F5"
android:textColor="#264177"
android:textSize="17sp"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/Modifiable"
android:textStyle="bold"
android:textSize="15sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/modifiableatherosclerosis"
android:textSize="15sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/Nonmodifiable"
android:textStyle="bold"
android:textSize="15sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/nonmodifiableatherosclerosis"
android:textSize="15sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/Pathophysiology"
android:background="#F5F5F5"
android:textColor="#264177"
android:textSize="17sp"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/pathophysiologyatherosclerosis"
android:textSize="15sp"/>
<TextView
android:id="#+id/Signandsymptomsparagraph"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/SignsandSymptoms"
android:background="#F5F5F5"
android:textColor="#264177"
android:textSize="17sp"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/atherosclerosissignsandsymptoms"
android:textSize="15sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/Diagnosis"
android:background="#F5F5F5"
android:textColor="#264177"
android:textSize="17sp"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/Diagnosisatherosclerosis"
android:textSize="15sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/Treatment"
android:background="#F5F5F5"
android:textColor="#264177"
android:textSize="17sp"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/lifestylemodification"
android:textSize="15sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/treatmentatherosclerosis"
android:textSize="15sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/Prevention"
android:background="#F5F5F5"
android:textColor="#264177"
android:textSize="17sp"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/Preventionatherosclerosis"
android:textSize="15sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/Complications"
android:background="#F5F5F5"
android:textColor="#264177"
android:textSize="17sp"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/complicationatherosclerosis"
android:textSize="15sp"/>
</LinearLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
my java code for this layout:
public class Atherosclerosis extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.atherosclerosis);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.atherosclerosis, menu);
return true;
}
}
Use android:onClick="" and android:Clickable="true"
to make the Textview as a button
You can use OnClickListener on TextView and then use the following method on the OnClick, where textViewAbove is the TextView that is above the one you need to jump:
scrollView.post(
new Runnable() {
#Override
public void run() {
new CountDownTimer(1500, 20) {
#Override
public void onTick(long millisUntilFinished) {
scrollView.scrollTo(0, (int) (textViewAbove.getBottom()-millisUntilFinished));
}
#Override
public void onFinish() {
}
}.start();
}
}
);
Hope it helps!
You can have OnclickListener on TextView
Here is the code but still give me error till me where I am wrong?
public class Atherosclerosis extends Activity{
ScrollView scrollView;
TextView tvabove=(TextView)findViewById(R.id.pathophysiology_id);
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.atherosclerosis);
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#6B8E23")));
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
TextView tv=(TextView)findViewById(R.id.Signandsymptompsid);
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
scrollView.post(
new Runnable() {
#Override
public void run() {
new CountDownTimer(1500, 20) {
#Override
public void onTick(long millisUntilFinished) {
scrollView.scrollTo(0, (int) (tvabove.getBottom()-millisUntilFinished));
}
#Override
public void onFinish() {
}
}.start();
}
}
);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.atherosclerosis, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case R.id.id_search:
Intent newActivity0 = new Intent(this,Search.class);
startActivity(newActivity0);
return true;
case R.id.id_favorit:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}