This is kind of beginner/noob question, but I want to use InputActivity.java and activity_input.xml as the one that the user use to input data. The code for the input are as shown below:
InputActivity.java
package com.lukmanyahoo.denny.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class InputActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_input);
}
//Send data to HasilActivity.java
}
activity_input.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.lukmanyahoo.denny.myapplication.InputActivity">
<EditText
android:id="#+id/editText"
android:layout_width="569dp"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="636dp"
android:layout_marginStart="16dp"
android:layout_marginTop="15dp"
android:ems="10"
android:hint="#string/umur"
android:inputType="number"
app:layout_constraintBottom_toTopOf="#+id/editText"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="DuplicateIds,NotSibling,RtlHardcoded"
app:layout_constraintVertical_bias="0.251" />
<EditText
android:id="#+id/editText2"
android:layout_width="569dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="#string/daerah"
android:inputType="textPersonName"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editText"
tools:ignore="MissingConstraints,RtlHardcoded" />
<TextView
android:id="#+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:text="#string/jurusan"
android:textSize="30sp"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:ignore="MissingConstraints"
android:layout_marginTop="32dp"
app:layout_constraintTop_toBottomOf="#+id/editText2"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp" />
<RadioGroup
android:id="#+id/radioGroup"
android:layout_width="569dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView"
tools:ignore="RtlHardcoded">
<RadioButton
android:id="#+id/radioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/informatika" />
<RadioButton
android:id="#+id/radioButton2"
android:layout_width="569dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/sistem_informasi"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="178dp" />
</RadioGroup>
<Button
android:id="#+id/button3"
android:layout_width="569dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="#string/kirim"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#+id/radioGroup"
tools:ignore="RtlHardcoded" />
</android.support.constraint.ConstraintLayout>
The code for the output part of my application are shown below:
HasilActivity.java
package com.lukmanyahoo.denny.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class HasilActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hasil);
}
}
activity_hasil.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.lukmanyahoo.denny.myapplication.HasilActivity">
<TableLayout
android:layout_width="568dp"
android:layout_height="791dp"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/umur"
android:textStyle="bold" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" "
tools:layout_editor_absoluteX="58dp"
tools:layout_editor_absoluteY="169dp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/daerah"
android:textStyle="bold" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" " />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/jurusan"
android:textStyle="bold" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" " />
</TableRow>
</TableLayout>
</android.support.constraint.ConstraintLayout>
Thanks for taking time answering this. I am new in giving question here.
In your InputActivity
public class InputActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_input);
}
//Send data to HasilActivity.java
Intent intent = new Intent(this, HasilActivity.class);
// put your data here
intent.putExtra("value_one", editText.getText().toString().trim());
intent.putExtra("value_two", editText2.getText().toString().trim());
startActivity(intent)
}
and in your HasilActivity
public class HasilActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hasil);
String value = getIntent().getStringExtra("value_one");
String value2 = getIntent().getStringExtra("value_two");
}
}
that is :)
Related
If i click on login button my username and password is same as condition but get login failed in Toast Message. Insert correct username and password compiler going to else part. I don't know what is the problem. I am stuck from 4 hours. If you know please solve i am a newbie.
Here down is my code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView Username=(TextView) findViewById(R.id.Username);
TextView password=(TextView) findViewById(R.id.password);
Button loginbtn=(Button) findViewById(R.id.Button);
loginbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (Username.getText().toString().equals("ADMIN") && password.getText().toString().equals("ADMIN")) {
Toast.makeText(getApplicationContext(), "Login Successfull", Toast.LENGTH_SHORT).show();
} else{
Toast.makeText(getApplicationContext(), "Login Faild!!", Toast.LENGTH_SHORT).show();
}
}
});
}
}
activity.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/backs"
tools:context=".MainActivity">
<TextView
android:id="#+id/Username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="Sign in"
android:textColor="#FFFFFF"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/edittext"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="30dp"
android:layout_marginRight="30dp"
android:background="#FFFFFF"
android:drawableLeft="#drawable/ic_baseline_person_outline_24"
android:drawablePadding="8dp"
android:ems="10"
android:hint="Username"
android:inputType="textPersonName"
android:minHeight="48dp"
android:padding="10dp"
android:textColor="#000000"
android:textColorHint="#000000"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/Username" />
<EditText
android:id="#+id/password"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="40dp"
android:layout_marginEnd="30dp"
android:layout_marginRight="30dp"
android:background="#FFFFFF"
android:drawableLeft="#drawable/ic_baseline_lock_24"
android:drawablePadding="8dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
android:minHeight="48dp"
android:padding="10dp"
android:textColor="#000000"
android:textColorHint="#000000"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.95"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/edittext" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:text="Forgot password"
android:textColor="#000000"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.837"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/password" />
<Button
android:id="#+id/Button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="28dp"
android:layout_marginRight="30dp"
android:background="#drawable/bg"
android:text="#string/login"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView3" />
<TextView
android:id="#+id/other"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="288dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:text="or sign in with"
android:textColor="#BDB9B9"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.495"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/Button" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/other"
app:layout_constraintVertical_bias="1.0">
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginRight="20dp"
android:src="#drawable/google" />
<ImageView
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_marginRight="20dp"
android:src="#drawable/facebook" />
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginRight="20dp"
android:src="#drawable/twitter" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
OUTPUT
You are referring to the R.id.Username component which is the SIGN UP text and you are casting the fields as TextViews instead of EditTexts.
Change your variables to:
EditText Username=(EditText) findViewById(R.id.edittext);
EditText password=(EditText) findViewById(R.id.password);
You are getting the text from the wrong reference, Change the below code to later.
TextView Username=(TextView) findViewById(R.id.Username);
to
TextView Username=(TextView) findViewById(R.id.edittext);
I am creating a fitness application where the user is allowed to create their own list of workouts and they can do so by clicking a "+" button beside the image view. one clicked, the imageview, textview1 and textview2 should be added to the list view on the next activity. basically when the button is clicked, data should be added to the UserCustomiseWorkout which has a listview
AllExercise Java
public class AllExercises extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_all_exercises);
ImageButton imagebutton7 = (ImageButton) findViewById(R.id.imageButton7);
imagebutton7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), UserCustomiseWorkout.class);
startActivity(intent);
}
});
}
public void clickNextExPage(View v)
{
Intent nextpage = new Intent(this, AllExercises2.class);
startActivity(nextpage);
}
AllExercise XML
'''
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AllExercises">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageView11"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="80dp"
android:layout_marginLeft="80dp"
android:layout_marginTop="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/bicyclecrunch" />
<ImageView
android:id="#+id/imageView12"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="51dp"
android:layout_marginLeft="51dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="80dp"
android:layout_marginRight="80dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="#+id/imageView11"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/catpose" />
<ImageView
android:id="#+id/imageView13"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="80dp"
android:layout_marginLeft="80dp"
android:layout_marginTop="30dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView11"
app:srcCompat="#drawable/chairpose" />
<ImageView
android:id="#+id/imageView14"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="51dp"
android:layout_marginLeft="51dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="80dp"
android:layout_marginRight="80dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/imageView13"
app:layout_constraintTop_toBottomOf="#+id/imageView12"
app:srcCompat="#drawable/childspose" />
<ImageView
android:id="#+id/imageView15"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="80dp"
android:layout_marginLeft="80dp"
android:layout_marginTop="30dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView13"
app:srcCompat="#drawable/cobblerpose" />
<ImageView
android:id="#+id/imageView16"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="51dp"
android:layout_marginLeft="51dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="80dp"
android:layout_marginRight="80dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/imageView15"
app:layout_constraintTop_toBottomOf="#+id/imageView14"
app:srcCompat="#drawable/cobrapushup" />
<ImageView
android:id="#+id/imageView17"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="80dp"
android:layout_marginLeft="80dp"
android:layout_marginTop="30dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView15"
app:srcCompat="#drawable/crunches" />
<ImageView
android:id="#+id/imageView18"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="51dp"
android:layout_marginLeft="51dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="80dp"
android:layout_marginRight="80dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/imageView17"
app:layout_constraintTop_toBottomOf="#+id/imageView16"
app:srcCompat="#drawable/doubleleglifts" />
<ImageView
android:id="#+id/imageView19"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="80dp"
android:layout_marginLeft="80dp"
android:layout_marginTop="30dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView17"
app:srcCompat="#drawable/downwarddogpose" />
<ImageView
android:id="#+id/imageView20"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="51dp"
android:layout_marginLeft="51dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="80dp"
android:layout_marginRight="80dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/imageView19"
app:layout_constraintTop_toBottomOf="#+id/imageView18"
app:srcCompat="#drawable/glutebridge" />
<ImageButton
android:id="#+id/imageButton7"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="152dp"
android:layout_marginLeft="152dp"
android:layout_marginBottom="12dp"
android:scaleType="fitCenter"
app:layout_constraintBottom_toTopOf="#+id/imageView13"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="#drawable/more" />
<ImageButton
android:id="#+id/imageButton8"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="152dp"
android:layout_marginLeft="152dp"
android:layout_marginBottom="16dp"
android:scaleType="fitCenter"
app:layout_constraintBottom_toTopOf="#+id/imageView15"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="#drawable/more" />
<ImageButton
android:id="#+id/imageButton9"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="152dp"
android:layout_marginLeft="152dp"
android:layout_marginBottom="16dp"
android:scaleType="fitCenter"
app:layout_constraintBottom_toTopOf="#+id/imageView17"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="#drawable/more" />
<ImageButton
android:id="#+id/imageButton10"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="152dp"
android:layout_marginLeft="152dp"
android:layout_marginBottom="16dp"
android:scaleType="fitCenter"
app:layout_constraintBottom_toTopOf="#+id/imageView19"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="#drawable/more" />
<ImageButton
android:id="#+id/imageButton11"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="152dp"
android:layout_marginLeft="152dp"
android:layout_marginBottom="64dp"
android:scaleType="fitCenter"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="#drawable/more" />
<ImageButton
android:id="#+id/imageButton21"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginEnd="58dp"
android:layout_marginRight="58dp"
android:layout_marginBottom="16dp"
android:scaleType="fitCenter"
app:layout_constraintBottom_toTopOf="#+id/imageView14"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="#drawable/more" />
<ImageButton
android:id="#+id/imageButton22"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginEnd="58dp"
android:layout_marginRight="58dp"
android:layout_marginBottom="16dp"
android:scaleType="fitCenter"
app:layout_constraintBottom_toTopOf="#+id/imageView16"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="#drawable/more" />
<ImageButton
android:id="#+id/imageButton23"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginEnd="58dp"
android:layout_marginRight="58dp"
android:layout_marginBottom="16dp"
android:scaleType="fitCenter"
app:layout_constraintBottom_toTopOf="#+id/imageView18"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="#drawable/more" />
<ImageButton
android:id="#+id/imageButton24"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginEnd="58dp"
android:layout_marginRight="58dp"
android:layout_marginBottom="16dp"
android:scaleType="fitCenter"
app:layout_constraintBottom_toTopOf="#+id/imageView20"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="#drawable/more" />
<ImageButton
android:id="#+id/imageButton25"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginEnd="56dp"
android:layout_marginRight="56dp"
android:layout_marginBottom="68dp"
android:scaleType="fitCenter"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="#drawable/more" />
<Button
android:id="#+id/next_ex_page"
android:layout_width="150dp"
android:layout_height="45dp"
android:layout_marginTop="40dp"
android:onClick="clickNextExPage"
android:text="Next Page"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView19" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView> '''
UserCustomiseWorkout Java
public class UserCustomiseWorkout extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_customise_workout);
}
UserCustomiseWorkout XML
''' <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageView31"
android:layout_width="100dp"
android:layout_height="100dp"
android:padding="10dp"
app:layout_constraintBottom_toBottomOf="#+id/linearLayout7"
app:layout_constraintEnd_toStartOf="#+id/linearLayout7"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/linearLayout7"
app:layout_constraintVertical_bias="0.0"
tools:srcCompat="#tools:sample/avatars" />
<LinearLayout
android:id="#+id/linearLayout7"
android:layout_width="313dp"
android:layout_height="100dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/imageView31"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/textView39"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_marginLeft="10dp"
android:layout_marginTop="25dp"
android:textSize="20sp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView40"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="TextView" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout> '''
get the text From the text views, create a list , add them to the list, pass the list via intent to the next activity, get the list from the intent. this is a simple way for your problem to work. but maybe for a better approach you would like to add this item to a database and fetch them when need it.
Here In Source Activity from you want to send data add this ,
Intent intent = new Intent(this, NextActivity.class);
//attach the key value pair using putExtra to this intent
String user_name = "Jhon Doe";
intent.putExtra("USER_NAME", user_name);
//starting the activity
startActivity(intent);
And in destination activity where you want to get Data
Intent intent = getIntent();
//get the attached extras from the intent
//we should use the same key as we used to attach the data.
String user_name = intent.getStringExtra("USER_NAME");
Log.d("User Name",user_name);
As like this you can add this part in Adapter by using context reference or with getApplicationContext() for Activity data transmission.
I am using Android Studio to create 2 CardViews that expand and collapse on a mouseclick. However, when I expand a card, the card underneath does not get pushed down and still visible even if the card above is expanded and is an overlap problem.
How do i fix this so either the card below the one being expanded gets pushed down or that the card that is being expanded information is at the front?
Screenshot of how cards look before clicked:Before Clicked
screenshot of when when clicked: when clicked (as you can see the 'Steven Smith card is still being shown)
Note: I did look into using a Recycler View + adapter etc. but in each card I would want different layouts/images (vs all being consistent info like a contact in someones phone)
Here is my java code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_engineering_page);
expandableView = findViewById(R.id.expandableView);
arrowBtn = findViewById(R.id.arrowBtn);
cardView = findViewById(R.id.cardView);
arrowBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (expandableView.getVisibility()==View.GONE){
TransitionManager.beginDelayedTransition(cardView, new AutoTransition());
expandableView.setVisibility(View.VISIBLE);
arrowBtn.setBackgroundResource(R.drawable.down_arrow);
} else {
TransitionManager.beginDelayedTransition(cardView, new AutoTransition());
expandableView.setVisibility(View.GONE);
arrowBtn.setBackgroundResource(R.drawable.android_icon);
}
}
});
expandableView2 = findViewById(R.id.expandableView2);
arrowBtn2 = findViewById(R.id.arrowBtn2);
cardView2 = findViewById(R.id.cardView2);
arrowBtn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (expandableView2.getVisibility()==View.GONE){
TransitionManager.beginDelayedTransition(cardView2, new AutoTransition());
expandableView2.setVisibility(View.VISIBLE);
arrowBtn2.setBackgroundResource(R.drawable.down_arrow);
} else {
TransitionManager.beginDelayedTransition(cardView2, new AutoTransition());
expandableView2.setVisibility(View.GONE);
arrowBtn2.setBackgroundResource(R.drawable.android_icon);
}
}
});
}
}
Here is my XML code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".EngineeringPage"
android:background="#22325A">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/toolbar"
android:background="#C4C4C4"
app:logo="#drawable/image1"
app:title="">
</android.support.v7.widget.Toolbar>
<TextView
android:id="#+id/engineeringTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="409dp"
android:layout_marginEnd="111dp"
android:layout_marginLeft="111dp"
android:layout_marginRight="111dp"
android:layout_marginStart="111dp"
android:layout_marginTop="61dp"
android:text="Engineering"
android:textColor="#android:color/white"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.v7.widget.CardView
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/CardView.Light"
android:layout_marginTop="98dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="12dp">
<TextView
android:id="#+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Section 1"
style="#style/TextAppearance.AppCompat.Title"
android:layout_marginStart="12dp" />
<Button
android:id="#+id/arrowBtn"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginEnd="12dp"
android:background="#drawable/down_arrow"
app:layout_constraintEnd_toEndOf="parent" />
<android.support.constraint.ConstraintLayout
android:id="#+id/expandableView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="12dp"
android:visibility="gone"
android:layout_marginTop="12dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<ImageView
android:id="#+id/phoneIcon"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="52dp"
android:layout_marginStart="12dp"
android:src="#drawable/android_icon"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:id="#+id/phoneNumber"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="(999) 345 32 45"
android:layout_marginStart="32dp"
android:textColor="#000"
style="#style/TextAppearance.AppCompat.Medium"
app:layout_constraintTop_toTopOf="#+id/phoneIcon"
app:layout_constraintStart_toEndOf="#id/phoneIcon"
app:layout_constraintBottom_toTopOf="#+id/phoneDesc"/>
<TextView
android:id="#+id/phoneDesc"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Mobile"
android:layout_marginStart="32dp"
android:textColor="#8A000000"
style="#style/TextAppearance.AppCompat.Body1"
app:layout_constraintTop_toBottomOf="#+id/phoneNumber"
app:layout_constraintStart_toEndOf="#id/phoneIcon"
app:layout_constraintBottom_toBottomOf="#+id/phoneIcon"/>
<ImageView
android:id="#+id/mailIcon"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="22dp"
android:layout_marginStart="12dp"
android:src="#drawable/android_icon"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/phoneIcon"/>
<TextView
android:id="#+id/mailNumber"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="workemail#gmail.com"
android:layout_marginStart="32dp"
android:textColor="#000"
style="#style/TextAppearance.AppCompat.Medium"
app:layout_constraintTop_toTopOf="#+id/mailIcon"
app:layout_constraintStart_toEndOf="#id/mailIcon"
app:layout_constraintBottom_toTopOf="#+id/mailDesc"/>
<TextView
android:id="#+id/mailDesc"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Work"
android:layout_marginStart="32dp"
android:textColor="#8A000000"
style="#style/TextAppearance.AppCompat.Body1"
app:layout_constraintTop_toBottomOf="#+id/mailNumber"
app:layout_constraintStart_toEndOf="#id/mailIcon"
app:layout_constraintBottom_toBottomOf="#+id/mailIcon"/>
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="#+id/cardView2"
style="#style/CardView.Light"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="184dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="12dp">
<TextView
android:id="#+id/name2"
style="#style/TextAppearance.AppCompat.Title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:text="Steven Smith" />
<Button
android:id="#+id/arrowBtn2"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginEnd="12dp"
android:background="#drawable/down_arrow"
app:layout_constraintEnd_toEndOf="parent" />
<android.support.constraint.ConstraintLayout
android:id="#+id/expandableView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:paddingBottom="12dp"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<ImageView
android:id="#+id/phoneIcon2"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginStart="12dp"
android:layout_marginTop="52dp"
android:src="#drawable/android_icon"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/phoneNumber2"
style="#style/TextAppearance.AppCompat.Medium"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:text="(999) 345 32 45"
android:textColor="#000"
app:layout_constraintBottom_toTopOf="#+id/phoneDesc2"
app:layout_constraintStart_toEndOf="#id/phoneIcon2"
app:layout_constraintTop_toTopOf="#+id/phoneIcon2" />
<TextView
android:id="#+id/phoneDesc2"
style="#style/TextAppearance.AppCompat.Body1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:text="Mobile"
android:textColor="#8A000000"
app:layout_constraintBottom_toBottomOf="#+id/phoneIcon2"
app:layout_constraintStart_toEndOf="#id/phoneIcon2"
app:layout_constraintTop_toBottomOf="#+id/phoneNumber2" />
<ImageView
android:id="#+id/mailIcon2"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginStart="12dp"
android:layout_marginTop="22dp"
android:src="#drawable/android_icon"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/phoneIcon2" />
<TextView
android:id="#+id/mailNumber2"
style="#style/TextAppearance.AppCompat.Medium"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:text="workemail#gmail.com"
android:textColor="#000"
app:layout_constraintBottom_toTopOf="#+id/mailDesc2"
app:layout_constraintStart_toEndOf="#id/mailIcon2"
app:layout_constraintTop_toTopOf="#+id/mailIcon2" />
<TextView
android:id="#+id/mailDesc2"
style="#style/TextAppearance.AppCompat.Body1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:text="Work"
android:textColor="#8A000000"
app:layout_constraintBottom_toBottomOf="#+id/mailIcon2"
app:layout_constraintStart_toEndOf="#id/mailIcon2"
app:layout_constraintTop_toBottomOf="#+id/mailNumber2" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>
I have modified your sample to make it work correctly, you need to animated the parent layout for the two cardViews to make the animation smoth
java
final ConstraintLayout content = findViewById(R.id.content_layout);
final ConstraintLayout expandableView = findViewById(R.id.expandableView);
final Button arrowBtn = findViewById(R.id.arrowBtn);
arrowBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (expandableView.getVisibility() == View.GONE) {
TransitionManager.beginDelayedTransition(content, new AutoTransition());
expandableView.setVisibility(View.VISIBLE);
arrowBtn.setBackgroundResource(R.drawable.down_arrow);
} else {
TransitionManager.beginDelayedTransition(content, new AutoTransition());
expandableView.setVisibility(View.GONE);
arrowBtn.setBackgroundResource(R.drawable.android_icon);
}
}
});
final ConstraintLayout expandableView2 = findViewById(R.id.expandableView2);
final Button arrowBtn2 = findViewById(R.id.arrowBtn2);
arrowBtn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (expandableView2.getVisibility() == View.GONE) {
TransitionManager.beginDelayedTransition(content, new AutoTransition());
expandableView2.setVisibility(View.VISIBLE);
arrowBtn2.setBackgroundResource(R.drawable.down_arrow);
} else {
TransitionManager.beginDelayedTransition(content, new AutoTransition());
expandableView2.setVisibility(View.GONE);
arrowBtn2.setBackgroundResource(R.drawable.android_icon);
}
}
});
xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/content_layout"
android:background="#22325A">
<android.support.v7.widget.Toolbar
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:id="#+id/toolbar"
android:background="#C4C4C4"
app:logo="#drawable/logo"
app:title="">
</android.support.v7.widget.Toolbar>
<android.support.v7.widget.CardView
android:id="#+id/cardView"
android:layout_width="0dp"
android:layout_height="wrap_content"
style="#style/CardView.Light"
android:layout_marginTop="98dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#id/toolbar">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="12dp">
<TextView
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:id="#+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Section 1"
style="#style/TextAppearance.AppCompat.Title"
android:layout_marginStart="12dp" />
<Button
android:id="#+id/arrowBtn"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginEnd="12dp"
android:background="#drawable/down_arrow"
app:layout_constraintTop_toTopOf="#id/name"
app:layout_constraintBottom_toBottomOf="#id/name"
app:layout_constraintEnd_toEndOf="parent" />
<android.support.constraint.ConstraintLayout
android:id="#+id/expandableView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingBottom="12dp"
android:visibility="gone"
app:layout_constraintTop_toBottomOf="#id/arrowBtn"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<ImageView
android:id="#+id/phoneIcon"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginStart="12dp"
android:src="#drawable/logo"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:id="#+id/phoneNumber"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="(999) 345 32 45"
android:layout_marginStart="32dp"
android:textColor="#000"
style="#style/TextAppearance.AppCompat.Medium"
app:layout_constraintTop_toTopOf="#+id/phoneIcon"
app:layout_constraintStart_toEndOf="#id/phoneIcon"
app:layout_constraintBottom_toTopOf="#+id/phoneDesc"/>
<TextView
android:id="#+id/phoneDesc"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Mobile"
app:layout_constraintStart_toStartOf="#id/phoneNumber"
android:textColor="#8A000000"
style="#style/TextAppearance.AppCompat.Body1"
app:layout_constraintTop_toBottomOf="#+id/phoneNumber"
app:layout_constraintStart_toEndOf="#id/phoneIcon"
app:layout_constraintBottom_toBottomOf="#+id/phoneIcon"/>
<ImageView
android:id="#+id/mailIcon"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="22dp"
android:layout_marginStart="12dp"
android:src="#drawable/android_icon"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/phoneIcon"/>
<TextView
android:id="#+id/mailNumber"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="workemail#gmail.com"
app:layout_constraintStart_toStartOf="#id/phoneNumber"
android:textColor="#000"
style="#style/TextAppearance.AppCompat.Medium"
app:layout_constraintTop_toTopOf="#+id/mailIcon"
app:layout_constraintStart_toEndOf="#id/mailIcon"
app:layout_constraintBottom_toTopOf="#+id/mailDesc"/>
<TextView
android:id="#+id/mailDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Work"
android:textColor="#8A000000"
style="#style/TextAppearance.AppCompat.Body1"
app:layout_constraintStart_toStartOf="#id/phoneNumber"
app:layout_constraintTop_toBottomOf="#+id/mailNumber"
app:layout_constraintBottom_toBottomOf="#+id/mailIcon"/>
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="#+id/cardView2"
style="#style/CardView.Light"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="48dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/cardView">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="12dp">
<TextView
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/name2"
style="#style/TextAppearance.AppCompat.Title"
android:layout_marginStart="12dp"
android:text="Steven Smith" />
<Button
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginEnd="12dp"
android:background="#drawable/down_arrow"
app:layout_constraintTop_toTopOf="#id/name2"
app:layout_constraintBottom_toBottomOf="#id/name2"
app:layout_constraintEnd_toEndOf="parent"
android:id="#+id/arrowBtn2"
/>
<android.support.constraint.ConstraintLayout
android:id="#+id/expandableView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingBottom="12dp"
app:layout_constraintTop_toBottomOf="#id/arrowBtn2"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<ImageView
android:id="#+id/phoneIcon2"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginStart="12dp"
android:src="#drawable/logo"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<TextView
android:id="#+id/phoneNumber2"
style="#style/TextAppearance.AppCompat.Medium"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:text="(999) 345 32 45"
android:textColor="#000"
app:layout_constraintBottom_toTopOf="#+id/phoneDesc2"
app:layout_constraintStart_toEndOf="#id/phoneIcon2"
app:layout_constraintTop_toTopOf="#+id/phoneIcon2" />
<TextView
android:id="#+id/phoneDesc2"
style="#style/TextAppearance.AppCompat.Body1"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="#id/phoneNumber2"
android:text="Mobile"
android:textColor="#8A000000"
app:layout_constraintBottom_toBottomOf="#+id/phoneIcon2"
app:layout_constraintStart_toEndOf="#id/phoneIcon2"
app:layout_constraintTop_toBottomOf="#+id/phoneNumber2" />
<ImageView
android:id="#+id/mailIcon2"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginStart="12dp"
android:layout_marginTop="22dp"
android:src="#drawable/android_icon"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/phoneIcon2" />
<TextView
android:id="#+id/mailNumber2"
style="#style/TextAppearance.AppCompat.Medium"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="#id/phoneNumber2"
android:text="workemail#gmail.com"
android:textColor="#000"
app:layout_constraintBottom_toTopOf="#+id/mailDesc2"
app:layout_constraintStart_toEndOf="#id/mailIcon2"
app:layout_constraintTop_toTopOf="#+id/mailIcon2" />
<TextView
android:id="#+id/mailDesc2"
style="#style/TextAppearance.AppCompat.Body1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Work"
android:textColor="#8A000000"
app:layout_constraintStart_toStartOf="#id/phoneNumber2"
app:layout_constraintBottom_toBottomOf="#+id/mailIcon2"
app:layout_constraintStart_toEndOf="#id/mailIcon2"
app:layout_constraintTop_toBottomOf="#+id/mailNumber2" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>
Just put everything inside a Linear Layout orientation vertical or Inside other layouts align your bottom and tops correctly with android:layout_below="#+id/YOUR ID"
I have to program a TicTacToe app:
This is the code of the activity_main.xml:
(It's a simple 3x3 field of TextViews on a black background)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:screenOrientation="portrait"
tools:context=".MainActivity">
<androidx.gridlayout.widget.GridLayout
android:layout_width="294dp"
android:layout_height="286dp"
android:background="#000000"
app:layout_constraintBottom_toTopOf="#+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView">
<TextView
android:id="#+id/tictactoe_center_left"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:editable="false"
android:ems="10"
android:enabled="false"
android:onClick="click_center_left"
android:text="#string/game_playersymbol_x"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="70sp"
android:textStyle="bold"
app:layout_column="0"
app:layout_row="1" />
<TextView
android:id="#+id/tictactoe_center_right"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:editable="false"
android:ems="10"
android:enabled="false"
android:onClick="click_center_right"
android:text="#string/game_playersymbol_x"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="70sp"
android:textStyle="bold"
app:layout_column="2"
app:layout_row="1" />
<TextView
android:id="#+id/tictactoe_center_center"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:editable="false"
android:ems="10"
android:enabled="false"
android:onClick="click_center_center"
android:text="#string/game_playersymbol_x"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="70sp"
android:textStyle="bold"
app:layout_column="1"
app:layout_row="1" />
<TextView
android:id="#+id/tictactoe_top_left"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:clickable="true"
android:ems="10"
android:enabled="false"
android:focusable="true"
android:onClick="click_top_left"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="70sp"
android:textStyle="bold"
app:layout_column="0"
app:layout_row="0"
tools:text="#string/game_playersymbol_x" />
<TextView
android:id="#+id/tictactoe_top_center"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:editable="false"
android:ems="10"
android:enabled="false"
android:onClick="click_top_center"
android:text="#string/game_playersymbol_x"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="70sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tictactoe_top_right"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:editable="false"
android:ems="10"
android:enabled="false"
android:onClick="click_top_right"
android:text="#string/game_playersymbol_x"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="70sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tictactoe_bottom_left"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:editable="false"
android:ems="10"
android:enabled="false"
android:onClick="click_bottom_left"
android:text="#string/game_playersymbol_x"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="70sp"
android:textStyle="bold"
app:layout_column="0"
app:layout_row="2" />
<TextView
android:id="#+id/tictactoe_bottom_right"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:editable="false"
android:ems="10"
android:enabled="false"
android:onClick="click_bottom_right"
android:text="#string/game_playersymbol_x"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="70sp"
android:textStyle="bold"
app:layout_column="2"
app:layout_row="2" />
<TextView
android:id="#+id/tictactoe_bottom_center"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:editable="false"
android:ems="10"
android:enabled="false"
android:onClick="click_bottom_center"
android:text="#string/game_playersymbol_x"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="70sp"
android:textStyle="bold"
app:layout_column="1"
app:layout_row="2" />
</androidx.gridlayout.widget.GridLayout>
<Button
android:id="#+id/button"
android:layout_width="213dp"
android:layout_height="61dp"
android:layout_marginBottom="56dp"
android:background="#drawable/roundbutton"
android:text="#string/menu_play_singleplayer"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name"
android:textColor="#979797"
android:textSize="50sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.066" />
</androidx.constraintlayout.widget.ConstraintLayout>
This is the code of the MainActivity.java:
package com.example.tictactoe;
import androidx.appcompat.app.AppCompatActivity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private int counter = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
public void click_top_left(View v) { click_action((TextView)findViewById(R.id.tictactoe_top_left)); }
public void click_top_center(View v) { click_action((TextView)findViewById(R.id.tictactoe_top_center)); }
public void click_top_right(View v) { click_action((TextView)findViewById(R.id.tictactoe_top_right)); }
public void click_center_left(View v) { click_action((TextView)findViewById(R.id.tictactoe_center_left)); }
public void click_center_center(View v) { click_action((TextView)findViewById(R.id.tictactoe_center_center)); }
public void click_center_right(View v) { click_action((TextView)findViewById(R.id.tictactoe_center_right)); }
public void click_bottom_left(View v) { click_action((TextView)findViewById(R.id.tictactoe_bottom_left)); }
public void click_bottom_center(View v) { click_action((TextView)findViewById(R.id.tictactoe_bottom_center)); }
public void click_bottom_right(View v) { click_action((TextView)findViewById(R.id.tictactoe_bottom_right)); }
public void click_action(TextView tv) {
if ((counter%2)==0)
tv.setText("O");
else
tv.setText("X");
counter++;
}
}
I've set the android:clickable to true.
I've set a onclick listener.
I simply cannot find my mistake...
I would appreciate any kind of help!
You have set to all the TextViews:
android:enabled="false"
Why?
Change it to:
android:enabled="true"
first give an id to your textview then Define a Sample like Textview textview find it in java and use setOnClickListener() in onCreate method just like below.It will definitely works.
textveiw.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) { }});
This question already has answers here:
Resource not found TextView
(2 answers)
Closed 7 years ago.
Hi i am new to android development and i couldn't execute method when a button is clicked.I retyped the code as in the tutorial but it ended up lot of errors. Check the code below,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Basketball Score Game"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="25sp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:paddingLeft="70dp"
android:id="#+id/header"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Team A"
android:layout_marginLeft="50dp"
android:layout_marginTop="80dp"
android:textSize="30sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="50sp"
android:id="#+id/score_a"
android:layout_marginLeft="90dp"
android:layout_marginTop="10dp"
android:onClick="display_score_a"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff6000"
android:text="Outside The Ring"
android:onClick="three_pts_a"
android:padding="10dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="40dp"
/>
<Button
android:layout_width="wrap_content"
android:background="#ff6000"
android:layout_height="wrap_content"
android:text="Inside the Ring"
android:layout_marginLeft="40dp"
android:padding="10dp"
android:layout_marginTop="10dp"
/>
<Button
android:layout_width="wrap_content"
android:background="#ff6000"
android:text="Free Throw"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="10dp"
android:padding="10dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Team B"
android:layout_marginLeft="250dp"
android:layout_marginTop="80dp"
android:textSize="30sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="50sp"
android:layout_marginLeft="290dp"
android:layout_marginTop="10dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff6000"
android:text="Outside The Ring"
android:layout_marginLeft="230dp"
android:padding="10dp"
android:layout_marginTop="40dp"
/>
<Button
android:layout_width="wrap_content"
android:background="#ff6000"
android:text="Inside the Ring"
android:layout_height="wrap_content"
android:layout_marginLeft="230dp"
android:layout_marginTop="10dp"
android:padding="10dp"
/>
<Button
android:layout_width="wrap_content"
android:background="#ff6000"
android:layout_height="wrap_content"
android:layout_marginLeft="230dp"
android:layout_marginTop="10dp"
android:text="Free Throw"
android:padding="10dp"
/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
android:background="#ff6000"
android:layout_gravity="center_horizontal"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
****Mainactivity.java****
package android.mytest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int total_pts1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void three_pts_a(View view)
{
total_pts1 = total_pts1 + 3;
display_score_a(total_pts1);
}
private void display_score_a(int number) {
TextView num = (TextView) findViewById(
R.id.score_a);
num.setText(number);
}
}
its because of the integer variable.you have to parse it to a string or change that line to num.setText(String.valueOf(number));