MainActivity.java (First Class)
package com.example.we.reportcardjavaclass;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
public EditText one, two, three, four, five, six;
public String bang1, bang2, bang3, bang4, bang5, bang6;
public Button results;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find the View that shows the numbers category
}
public void getResult(View view){
results = (Button) findViewById(R.id.result);
Intent numbersIntent = new Intent(MainActivity.this, result.class);
startActivity(numbersIntent);
one = (EditText) findViewById(R.id.name);
bang1 = one.getText().toString();
two = (EditText) findViewById(R.id.Class);
bang2 = two.getText().toString();
three = (EditText) findViewById(R.id.English);
bang3 = three.getText().toString();
four = (EditText) findViewById(R.id.Hindi);
bang4 = four.getText().toString();
five = (EditText) findViewById(R.id.Maths);
bang5 = five.getText().toString();
six = (EditText) findViewById(R.id.Science);
bang6 = six.getText().toString();
}
}
activity_main.xml (First Layout File)
<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.we.reportcardjavaclass.MainActivity"
android:padding="16dp">
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Student's Name"
android:id="#+id/name"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Class"
android:id="#+id/Class"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="64dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Grade in English"
android:id="#+id/English"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="128dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Grade in Hindi"
android:id="#+id/Hindi"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="192dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Grade in Maths"
android:id="#+id/Maths"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="256dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Grade in Science"
android:id="#+id/Science"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="320dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<Button
android:text="Get result"
android:id="#+id/result"
android:onClick="getResult"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="384dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"/>
</android.support.constraint.ConstraintLayout>
</ScrollView>
## result.xml (Second Layout File) ##
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="32dp">
<android.support.v7.widget.CardView
android:id="#+id/resultCard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="2dp"
android:layout_gravity="center_vertical"
card_view:cardUseCompatPadding="true">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="20dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:text="Student's Name"
android:textAllCaps="true"
android:textSize="20sp"
android:textStyle="bold"/>
<TextView
android:id="#+id/one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:textAllCaps="false"
android:textSize="15sp"
android:textStyle="normal"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:text="Class"
android:textAllCaps="true"
android:textSize="20sp"
android:textStyle="bold"/>
<TextView
android:id="#+id/two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="40dp"
android:textAllCaps="false"
android:textSize="15sp"
android:textStyle="normal"/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="160dp"
android:background="#color/cardview_dark_background"
android:padding="20dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="English"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textStyle="bold"
/>
<TextView
android:id="#+id/three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textStyle="normal"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="40dp"
android:text="Hindi"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textStyle="bold"
/>
<TextView
android:id="#+id/four"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="40dp"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textStyle="normal"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="80dp"
android:text="Maths"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textStyle="bold"
/>
<TextView
android:id="#+id/five"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="80dp"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textStyle="normal"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="120dp"
android:text="Science"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textStyle="bold"
/>
<TextView
android:id="#+id/six"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="120dp"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textStyle="normal"/>
</RelativeLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="350dp"
android:text="BACK TO HOME"
/>
</FrameLayout>
</android.support.v7.widget.CardView>
</LinearLayout></ScrollView>
Second Class
package com.example.we.reportcardjavaclass;
import android.os.Bundle;
import android.widget.TextView;
/**
* Created by we on 08-07-2017.
*/
public class result extends MainActivity{
public TextView one, two, three, four, five, six;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
setTitle(bang1+"s Result");
one = (TextView) findViewById(R.id.one);
one.setText(bang1);
two = (TextView) findViewById(R.id.two);
two.setText(bang2);
three = (TextView) findViewById(R.id.three);
three.setText(bang3);
four = (TextView) findViewById(R.id.four);
four.setText(bang4);
five = (TextView) findViewById(R.id.five);
five.setText(bang5);
six = (TextView) findViewById(R.id.six);
six.setText(bang6);
}
}
EditText values are getting stored MainActivity.java in variables bang - 123456. I added public modifier while declaring them so I can access these in second class too. In second class I want to use these variable values to set in into Textviews but while running it is not showing the value.
I have also attached the screenshots of both layouts after running in my emulator.
Screenshot 1
Screenshot 2
In second layout it is supposed to show name and other values.
I would suggest that you take a few steps back and get a better understanding of Android's fundamentals, including Activities in general. Perhaps a good place to start, would be understanding the Activity Lifecycle.
An answer to your question isn't very simple, because the way in which Android works is likely very different from your understanding.
In short, your second activity is a completely different instance of a class than the first. Extending the second from the first does not mean that the assigned variables are available to the second, it merely means that the class inherits the structure of its parent in the form of methods and fields.
You should instead be designing your activities in such a way that they relay those strings. When launching your second activity, include those strings in the intent when starting it.
For instance:
public class MainActivity extends AppCompatActivity {
// ...
/** Your method where the second activity is launched */
public void onButtonClicked() {
Intent intent = new Intent(this, SecondActivity.class);
intent.putString(SecondActivity.EXTRA_BANG1, bang1);
// ...
startActivity(intent);
}
}
public class SecondActivity extends AppCompatActivity {
public static final String EXTRA_BANG1 = "bang1";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
// ...
TextView tvOne = (TextView) findViewById(R.id.one);
// ...
String bang1 = getIntent().getString(EXTRA_BANG1);
tvOne.setText(bang1);
}
}
I thoroughly recommend reading up in the Android documentation (it's excellently written!) to better understand how (and why) it works through this mechanism. In particular, this article on Sending data between activities is very relevant to you.
If you want to use these values in another activity then simply pass these values along with the intent that is used to start a new activity.
MainActivity.java
Intent i = new Intent(MainActivity.this,SecondActivity.class);
i.putExtra("Bang1",bang1);
i.putExtra("Bang2",bang2);
i.putExtra("Bang3",bang3);
...
And you can get them in the SecondActivity.java file as:
Intent i =getIntent();
int bang1 = i.getIntExtra("Bang1");
...
But instead so choosing the string names as shown i would suggest to create a new class to store the the string names as final string constants in order to avoid any kind of mistake.
Or you can directly declare these variables as static and access them in the SecondActivity by referring to their names as :
int bang1 = MainActivity.bang1;
Related
I've made a checklist app and when the user add's the check to the app I get the below error and a crash. Can someone provide some advice to how to fix this? As i'm using fragments, i've created a second Java file and refernced that to the XML as you cannot have more then one class in a single Java, i've tried merging them but get lots of errors and cannot use the public class 'extends' more then once.
Here's the error
Heres the XML
<TextView
android:id="#+id/text_checklist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
android:visibility="invisible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Checklist"
android:textAlignment="center"
android:textAllCaps="false"
android:textSize="24sp"
android:textStyle="bold"
android:typeface="normal"
app:fontFamily="#font/basic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.491"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/text_checklist"
app:layout_constraintVertical_bias="0.0" />
<EditText
android:id="#+id/inputText"
android:layout_width="369dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="13dp"
android:ems="10"
android:hint="#string/placeholder"
android:inputType="textPersonName" />
<Button
android:id="#+id/check_list_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="57dp"
android:onClick="addButton2"
android:text="#string/buttonText" />
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginLeft="0dp"
android:layout_marginTop="107dp" />
Heres the 1st Java (Fragment)
checklistViewModel =
ViewModelProviders.of(this).get(com.example.csd301_project_trackday_timing_dh.ui.checklist.ChecklistViewModel.class);
View root = inflater.inflate(R.layout.fragment_checklist, container, false);
final TextView textView = root.findViewById(R.id.text_checklist);
checklistViewModel.getText().observe(this, new Observer<String>() {
#Override
public void onChanged(#Nullable String s) {
textView.setText(s);
}
});
return root;
}
}
Heres the 2nd Java (CheckList)
Button btn_checklist;
EditText inputText;
ListView listView;
ArrayList<String> list;
public void addButton2(View view){
String text = inputText.getText().toString();
list.add(text);
ArrayAdapter adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,list);
listView.setAdapter(adapter);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_checklist);
btn_checklist= findViewById(R.id.check_list_button);
inputText=findViewById(R.id.inputText);
listView=findViewById(R.id.listView);
list = new ArrayList<>();
}
}
I have a problem with Text View and Button in Android Studio. I would like to update my text view when i clik a button but it doesn't work.
This is my code:
activity_distance_to_run.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=".MainActivity"
tools:layout_editor_absoluteY="25dp">
<TextView
android:id="#+id/Distancerun2"
android:layout_width="126dp"
android:layout_height="50dp"
android:layout_marginTop="50dp"
android:text="Distance to ....."
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/distancetorun"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:text="run"
app:layout_constraintBaseline_toBaselineOf="#+id/distancetoride"
app:layout_constraintEnd_toStartOf="#+id/distancetoride"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/distancetoride"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="9dp"
android:layout_marginRight="9dp"
android:layout_marginTop="32dp"
android:text="ride a bicycle"
app:layout_constraintEnd_toStartOf="#+id/distancetoswim"
app:layout_constraintStart_toEndOf="#+id/distancetorun"
app:layout_constraintTop_toBottomOf="#+id/Distancerun2" />
<Button
android:id="#+id/distancetoswim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="swim"
app:layout_constraintBaseline_toBaselineOf="#+id/distancetoride"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/distancetoride" />
<Button
android:id="#+id/yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="32dp"
android:layout_marginStart="32dp"
android:text="I did it !!"
app:layout_constraintBaseline_toBaselineOf="#+id/no"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="116dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:text="I don't want to do it !!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView1" />
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="23dp"
android:layout_marginEnd="25dp"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_marginStart="25dp"
android:layout_marginTop="221dp"
android:autoText="true"
android:clickable="true"
android:enabled="false"
android:freezesText="false"
android:text="Elo ziomek"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="#+id/no"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
distancetorun.java
package pl.agcode.sqliteexample;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class distance_to_run extends Activity {
Button distancetorun;
TextView txtView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_distance_to_run);
distancetorun = (Button) findViewById(R.id.distancetorun);
txtView = (TextView) findViewById(R.id.textView1);
distancetorun.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtView.setText("NewText");
}
});
}
}
Thanks for help I will try to repair it but don't know how.
This is very important to repair this for me. :)
Looks all good except textview1 color.
Add below line for the textview1 in layout xml
<TextView
android:id="#+id/textView1"
android:textColor="#android:color/black"
After launching the app
After clicking RUN button
I have a ScrollView defined by the xml. Inside of it I want to have two textView like in this photo :
Doing that in xml is fine. But how to add them programmatically? Here's what the code look like without the two TextView example:
.xml file :
<?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:id="#+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"
tools:context=".InventoryActivity">
<ImageView
android:id="#+id/imageView9"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
app:srcCompat="#drawable/inventorylayoutbg" />
<ImageView
android:id="#+id/imageView33"
android:layout_width="61dp"
android:layout_height="59dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:onClick="closeDialog"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.254"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.927"
app:srcCompat="#drawable/blank" />
<ScrollView
android:id="#+id/sv"
android:layout_width="326dp"
android:layout_height="374dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/imageView9">
<LinearLayout
android:id="#+id/ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"></LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
.java file
package com.example.jsciampi.tapper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
public class InventoryActivity extends AppCompatActivity {
//Form
ScrollView sv;
LinearLayout ll;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inventory);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = (int)(dm.widthPixels*.9);
int height = (int)(dm.heightPixels*.8);
getWindow().setLayout(width,height);
sv = findViewById(R.id.sv);
ll = findViewById(R.id.ll);
for(int i = 0; i < 5; i++){
Button b = new Button(InventoryActivity.this);
//Add Constraint there
ll.addView(b);
}
}
public void closeDialog(View view) {
finish();
}
}
The second TextView had this xml config:
<TextView
android:id="#+id/textView2"
android:layout_width="324dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="#+id/sv"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/firstText"
app:layout_constraintVertical_bias="0.0" />
and I really just need to converto from xml to java this part here :
app:layout_constraintBottom_toBottomOf="#+id/sv"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/aads"
app:layout_constraintVertical_bias="0.0"
Can anyone help me out?
for (int i = 0; i < 5; i++) {
View row = LayoutInflater.from(this).inflate(R.layout.image_text_row,null,false);
ll.addView(row);
}
add the below layout to file with name image_text_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_dark"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:src="#android:drawable/ic_dialog_email" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Text"
android:textColor="#android:color/white"
android:textSize="20sp" />
</LinearLayout>
I find that there is a quick way to create animation with ConstraintSet for ConstrainLayout Activity. Faster than use TransitionManager for RelativeLayout
ConstraintSet use two xml file for an Activity. One for the first position and next one for the destination.
I want to create something like this:
https://media.giphy.com/media/2UwXdWEoLWe9iQMFIY/giphy.gif
But there is no clearly instruction show how to use it in Java. Anyone had done this can show me the source code or link to some post like that.
Thanks for reading the post.
This is possible with ConstraintSet. They key is two have two layouts one layout has ui elements of the screen and the other has elements on the screen. Now you can use TransitionManager with interpolator and duration of your choice and animate the layout changes.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
android:id="#+id/constraint"
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="#181818"
tools:context=".MainActivity">
<ImageView
android:id="#+id/backgroundImage"
android:layout_width="0dp"
android:layout_height="0dp"
android:src="#drawable/mugello"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="JUNE 3, 2018"
app:layout_constraintRight_toRightOf="#+id/title"
app:layout_constraintBottom_toBottomOf="#+id/title"
android:textSize="12sp"
android:background="#d3d3d3"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:paddingTop="3dp"
android:paddingBottom="3dp"/>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:background="#F44336"
android:paddingBottom="8dp"
android:paddingEnd="24dp"
android:paddingStart="24dp"
android:paddingTop="8dp"
android:text="Mugello Circuit"
android:textColor="#FFFF"
android:textSize="45sp"
app:layout_constraintRight_toLeftOf="#+id/backgroundImage"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="#+id/fadeBackgroudView"
android:layout_width="wrap_content"
android:layout_height="90dp"
android:foreground="#drawable/gradient_variant"
app:layout_constraintBottom_toTopOf="#+id/description" />
<TextView
android:id="#+id/tap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:text="Tap for info"
android:textSize="15sp"
android:textColor="#ffffff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="The Mugello is a historic region and valley in northern Tuscany, in Italy. It is located to the north of the city of Florence and consists of the northernmost portion of the Metropolitan City of Florence. It is connected to the separate Santerno river valley by the Futa Pass."
android:textSize="22sp"
android:textColor="#FFFF"
android:background="#181818"
android:gravity="center"
android:paddingStart="8dp"
android:paddingEnd="8dp"
android:paddingBottom="8dp"
app:layout_constraintTop_toBottomOf="#+id/backgroundImage"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
</android.support.constraint.ConstraintLayout>
activity_main_detail.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=".MainActivity">
<ImageView
android:id="#+id/backgroundImage"
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="centerCrop"
android:src="#drawable/mugello"
app:layout_constraintBottom_toTopOf="#+id/description"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:background="#F44336"
android:paddingBottom="8dp"
android:paddingEnd="24dp"
android:paddingStart="24dp"
android:paddingTop="8dp"
android:text="Mugello Circuit"
android:textColor="#FFFF"
android:textSize="45sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="JUNE 3, 2018"
app:layout_constraintRight_toRightOf="#+id/title"
app:layout_constraintTop_toBottomOf="#+id/title"
android:textSize="12sp"
android:background="#d3d3d3"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:paddingTop="3dp"
android:paddingBottom="3dp"/>
<View
android:id="#+id/fadeBackgroudView"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:foreground="#drawable/gradient"
app:layout_constraintBottom_toTopOf="#+id/description" />
<TextView
android:id="#+id/tap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:text="Tap for info"
android:textSize="15sp"
android:textColor="#ffffff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="The Mugello is a historic region and valley in northern Tuscany, in Italy. It is located to the north of the city of Florence and consists of the northernmost portion of the Metropolitan City of Florence. It is connected to the separate Santerno river valley by the Futa Pass."
android:textSize="22sp"
android:textColor="#FFFF"
android:gravity="center"
android:background="#181818"
android:paddingStart="8dp"
android:paddingEnd="8dp"
android:paddingBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
</android.support.constraint.ConstraintLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private boolean show = false;
private ImageView backgroundImage;
private ConstraintLayout constraint;
private ConstraintSet constraintSet = new ConstraintSet();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
constraint = findViewById(R.id.constraint);
backgroundImage = findViewById(R.id.backgroundImage);
backgroundImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(show)
hideComponents(); // if the animation is shown, we hide back the views
else
showComponents() ;// if the animation is NOT shown, we animate the views
}
});
}
private void showComponents(){
show = true;
constraintSet.clone(this, R.layout.activity_main_detail);
Transition transition = new ChangeBounds();
transition.setInterpolator(new AnticipateOvershootInterpolator(1.0f));
transition.setDuration(1000);
TransitionManager.beginDelayedTransition(constraint, transition);
constraintSet.applyTo(constraint);
}
private void hideComponents(){
show = false;
constraintSet.clone(this, R.layout.activity_main);
Transition transition = new ChangeBounds();
transition.setInterpolator(new AnticipateOvershootInterpolator(1.0f));
transition.setDuration(1000);
TransitionManager.beginDelayedTransition(constraint, transition);
constraintSet.applyTo(constraint);
}
}
Here's a slide share on ConstraintLayout https://speakerdeck.com/camaelon/advanced-animations-and-constraintlayout
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 :)