0% found this document useful (0 votes)
28 views6 pages

Practical No 14

Uploaded by

tangiro2305
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views6 pages

Practical No 14

Uploaded by

tangiro2305
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Practical No 14

Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id ="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>

MainActivity.java
package com.example.q1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {


ListView lv;
String[] programming_lang =
{"Android","Assembly","Java","PHP","Hadoop","Sap","Python",

"Ajax","C++","Ruby","Rails","C","Android","Assembly","Java","PHP","Hadoop","Sap","P
ython",
"Ajax","C++","Ruby","Rails","C"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = findViewById(R.id.lv);
ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(),
android.R.layout.simple_list_item_1,programming_lang);
lv.setAdapter(adapter);
}
}
Output

2. Write a program to display an image using Image view and a button named “Change Image”

Once you click on the button another image should be displayed.

Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">

<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_launcher_background"
tools:layout_editor_absoluteX="132dp"
tools:layout_editor_absoluteY="245dp" />

<Button
android:id ="@+id/chg"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Button"/>
</LinearLayout>
ActivityMain.java
package com.example.q2;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {


Button chg;
ImageView img;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = findViewById(R.id.imageView);
chg = findViewById(R.id.chg);
chg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Drawable img2 = getDrawable(R.drawable.ic_launcher_foreground);
img.setImageDrawable(img2);
}
});
}
}

Output:-
3. Write a program to display 15 buttons using Grid View.

Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<GridView
android:id="@+id/grid_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:horizontalSpacing="5dp"
android:numColumns="3"
android:verticalSpacing="5dp"
/>

</LinearLayout>

Button_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn"
android:layout_width="100dp"
android:text="Button"
android:layout_height="75dp"/>
</LinearLayout>

ButtonModal.java
package com.example.q3;

public class ButtonModal {


private final String btn_txt;
public ButtonModal(String btn_txt)
{
this.btn_txt = btn_txt;
}
public String get_txt()
{
return btn_txt;
}
}

GridAdapter.java
package com.example.q3;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class GridAdapter extends ArrayAdapter<ButtonModal> {

public GridAdapter(@NonNull Context cxt,ButtonModal[] btn_modal) {

super(cxt,0,btn_modal);
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull
ViewGroup parent) {
View grid_view_item = convertView;
if(grid_view_item == null) {
grid_view_item =
LayoutInflater.from(getContext()).inflate(R.layout.button_layout, parent, false);
}
ButtonModal btn_modal = getItem(position);
Button btn = grid_view_item.findViewById(R.id.btn);
btn.setText(btn_modal.get_txt());
return grid_view_item;
}
}

MainActivity.java
package com.example.q3;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridView;

public class MainActivity extends AppCompatActivity {


GridView gv;
ButtonModal[] arr = new ButtonModal[15];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gv = findViewById(R.id.grid_view);

// Creating and Initializing Buttons


for(int i = 0 ; i<15;i++){
arr[i] = new ButtonModal("Button "+i);
}

GridAdapter adapter = new GridAdapter(getApplicationContext(),arr);


gv.setAdapter(adapter);

}
}
Output

You might also like