Create an application that will pass some number to the next screen, and on the next screen that number of items should be display in the list.
MainActivity.java
MainActivity.java
package com.example.assignment5;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity implements OnClickListener {
EditText editnum;
Button btshow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editnum=(EditText) findViewById(R.id.editTextNumber);
btshow=(Button) findViewById(R.id.buttonshow);
btshow.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() == btshow.getId()) {
Intent intent = new Intent(MainActivity.this, listActivity.class);
startActivity(intent);
intent.putExtra("num", Integer.parseInt(editnum.getText().toString()));
this.startActivity(intent);
}
}
}
activity_main.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"
tools:context=".MainActivity">
<EditText
android:id="@+id/editTextNumber"
android:layout_width="199dp"
android:layout_height="46dp"
android:layout_marginEnd="104dp"
android:layout_marginRight="104dp"
android:layout_marginBottom="40dp"
android:ems="10"
android:hint="Enter the number of item"
android:inputType="textPersonName"
app:layout_constraintBottom_toTopOf="@+id/buttonshow"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/buttonshow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="238dp"
android:text="ENTER"
android:textColor="#000000"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
listActivity.java
package com.example.assignment5;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class listActivity extends AppCompatActivity implements OnItemClickListener {
ListView lv;
String items[];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
lv=(ListView) findViewById(R.id.listViewItems);
int num=getIntent().getIntExtra("num", 0);
items=new String[num];
for(int i=0;i<num;i++)
{
items[i]="item "+(i+1);
}
ArrayAdapter<String> arr=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,items);
lv.setAdapter(arr);
lv.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
Toast.makeText(this,items[pos] + " selected", Toast.LENGTH_SHORT).show();
}
}
activity_list.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"
tools:context=".listActivity">
<ListView
android:id="@+id/listViewItems"
android:layout_width="312dp"
android:layout_height="595dp"
android:layout_marginStart="58dp"
android:layout_marginLeft="58dp"
android:layout_marginEnd="58dp"
android:layout_marginRight="58dp"
android:layout_marginBottom="36dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.529"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
To download follow this link : Program 5
No comments:
Post a Comment