I am building an application that has a GridView that contains a dynamic amount of CardViews. I am having issues accessing the TextView elements in the individual items layout from the custom adapter that I am creating.
tvCastleName and tvShieldTime are unresolved references, and the application will not compile.
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">
<TextView
android:id="#+id/tvNickname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:text="Nickname"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
android:textSize="24sp"
app:layout_constraintTop_toTopOf="parent" />
<GridView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/tvNickname"
android:horizontalSpacing="6dp"
android:verticalSpacing="6dp"
android:numColumns="3"
android:id="#+id/gvCastles" />
items.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="120dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_gravity="center"
android:layout_margin="5dp"
app:cardElevation="5dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tvCastleName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:fontFamily="sans-serif-medium"
android:text="Castle Name"
android:gravity="center"
android:textAppearance="#style/TextAppearance.AppCompat.Large" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#+id/tvCastleName"
android:text="15:00"
android:id="#+id/tvShieldTime"
android:gravity="center" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
GridAdapter.kt
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
class GridAdapter(var nameList: ArrayList<Castle>, var context: Context?) : BaseAdapter() {
override fun getCount(): Int = nameList.size
override fun getItem(position: Int): Castle = nameList[position]
override fun getItemId(position: Int): Long {
TODO("Not yet implemented")
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val nameList = this.nameList[position]
var inflater = context!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val castleView = inflater.inflate(R.layout.items, null)
castleView.tvCastleName.text = nameList.name!!
castleView.tvShieldTime.text = "15:00"
return castleView
}
}
You must be getting Unresolved Reference as the method (Kotlin Synthetics) you are using to access view is now deprecated. You can use ViewBinding to access your views.
You've to make a few changes to your getItem method, to make it work with ViewBinding
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val nameList = this.nameList[position]
val itemsBinding = ItemsBinding.inflate(LayoutInflater.from(parent.context), parent, false)
itemsBinding.tvCastleName.text = nameList.name!!
itemsBinding.tvShieldTime.text = "15:00"
return itemsBinding.root
}
Related
i am having trouble with the Snackbar. I recently moved to Kotlin, Electric Eel and Material design 3. So it is a problem to pinpoint the source of the problem.
So in my main activity i have this 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:id="#+id/rootContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/barLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<com.google.android.material.appbar.MaterialToolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.fragment.app.FragmentContainerView
android:id="#+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="#+id/barLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
tools:layout="#layout/fragment_dummy" />
</androidx.constraintlayout.widget.ConstraintLayout>
Then in my Kotlin MainActivity.kt i do this:
fun deleteItem(position: Int) {
val lDeletedItem = mAdapter.removeItem(position) ?: return
val message = Snackbar.make(binding.rootContainer, R.string.DialogDeleteTripMessage, Snackbar.LENGTH_LONG)
message.setAction(R.string.DialogDeleteTripButton) { view ->
mAdapter.addItem(position,
lDeletedItem
)
}
message.setActionTextColor(Convertor.ResourceToColorInt(this, R.attr.colorSnackBarAction))
message.addCallback(object : Snackbar.Callback() {
override fun onDismissed(transientBottomBar: Snackbar, event: Int) {
if (event == DISMISS_EVENT_TIMEOUT) {
mViewModel.deleteItem(lDeletedItem.id)
}
}
})
message.show()
}
Everything works just fine but the Snackbar is display outside the visible screen:
Anybody an idea? did the same thing before and then is was working but changed so many things that i am lost
I have problem with changing status in my progress bar on my app.
I have while loop which is changing status from 1-100 every 100ms.
Loop works cause when I'm making Log checker which shows how much is actually equals "I" it's from 1-100.
When i'm putting manualy progress to for example 50 it shows but nothing change.
class StartActivity : AppCompatActivity() {
private var progressBar: ProgressBar? = null
private val handler = Handler()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_start)
progressBar = findViewById<ProgressBar>(R.id.progressBar_activityStart)
startProgressBar()
}
private fun startProgressBar() {
progressBar!!.visibility = View.VISIBLE
var i: Int = progressBar!!.progress
progressBar!!.progress = 0
Thread {
while (i < 100) {
i += 1
handler.post {
progressBar!!.progress = i
Log.v("progress", i.toString())
}
try {
progressBar!!.progress = i
Thread.sleep(100)
} catch (e: InterruptedException) {
e.printStackTrace()
}
}
progressBar!!.visibility = View.INVISIBLE
}.start()
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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="#color/white"
tools:context=".StartActivity">
<ImageView
android:id="#+id/imageView_activityStart_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/white"
android:layout_centerInParent="true"
android:src="#drawable/logohorizontal" />
<ProgressBar
android:id="#+id/progressBar_activityStart"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView_activityStart_logo"
android:layout_alignBottom="#+id/imageView_activityStart_logo"
android:layout_marginStart="30dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="30dp"
android:layout_marginBottom="30dp"
android:indeterminate="false"
android:visibility="visible" />
</RelativeLayout>
i had the same problem, then i changed
<ProgressBar
android:id="#+id/progressBar_activityStart"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible" />
to this:
<android.widget.ProgressBar
android:id="#+id/progressBar_activityStart"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible" />
then its worked. dont know what the problem is.
You can try to use androidx.core.widget.ContentLoadingProgressBar.
I'm adding view dynamically in fragment which works fine but when I'm navigating to another fragment and return back to first fragment all layout is set as initial one (dynamically added view is not present). So here I have to save UI state untill my fragment's parent activity is not closed.
Initial Fragment Class Code
package com.hp.billingapp.ui.billfragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.textfield.TextInputEditText;
import com.hp.billingapp.R;
import com.hp.billingapp.ui.BillingActivity;
public class OrderFragment extends Fragment {
Button pay, buttonAdd;
LinearLayout itemLayout;
Integer item = 1, emptyChk;
Float total;
ConstraintLayout constraintLayout;
Integer layoutId;
public OrderFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_order, container, false);
pay = root.findViewById(R.id.pay);
pay.setOnClickListener(v -> {
datafetch(root);
});
itemLayout = root.findViewById(R.id.itemLayout);
constraintLayout = root.findViewById(R.id.constraintLayout);
buttonAdd = root.findViewById(R.id.button);
buttonAdd.setOnClickListener(v -> addView());
return root;
}
private void datafetch(View root) {
Log.i("heetcount", String.valueOf(itemLayout.getChildCount()));
emptyChk = 0;
total = (float) 0;
for (int i = 0; i < itemLayout.getChildCount(); i++) {
View itemView = itemLayout.getChildAt(i);
TextView number = itemView.findViewById(R.id.number);
AutoCompleteTextView name = itemView.findViewById(R.id.namet);
TextInputEditText price = itemView.findViewById(R.id.pricet);
AutoCompleteTextView type = itemView.findViewById(R.id.typet);
if (name.getText().toString().isEmpty() || price.getText().toString().isEmpty() ||
type.getText().toString().isEmpty()) {
Snackbar.make(constraintLayout, "Please fill the required fields",
Snackbar.LENGTH_SHORT).show();
emptyChk += 1;
} else {
total = total + Float.parseFloat(price.getText().toString().trim());
}
}
if (emptyChk == 0) {
Log.i("heettotal", total.toString());
OrderFragmentDirections.ActionOrderFragmentToBillingFragment action =
OrderFragmentDirections.actionOrderFragmentToBillingFragment(total);
Navigation.findNavController(root).navigate(action);
((BillingActivity) getActivity()).heading("b");
}
}
private void addView() {
item = item + 1;
String text = "Item " + item;
View itemView = getLayoutInflater().inflate(R.layout.item_layout, null, false);
TextView number = itemView.findViewById(R.id.number);
ImageButton remove = itemView.findViewById(R.id.remove);
AutoCompleteTextView nametiet = itemView.findViewById(R.id.namet);
AutoCompleteTextView typetiet = itemView.findViewById(R.id.typet);
TextInputEditText pricetiet = itemView.findViewById(R.id.pricet);
number.setText(text);
remove.setOnClickListener(v -> removeView(itemView));
itemLayout.addView(itemView);
}
private void removeView(View itemView) {
Log.i("heetview", String.valueOf(itemView));
itemLayout.removeView(itemView);
}
}
Initial Fragment Layout Code
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/backdropcenter"
android:theme="#style/Theme.MaterialComponents.NoActionBar"
tools:context=".ui.billfragment.OrderFragment">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="80dp"
android:overScrollMode="never">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/itemLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="#layout/item_layout" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/button"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:background="#drawable/allcornerround"
android:fontFamily="#font/montserrat"
android:text="Add Item"
android:textAllCaps="false"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/pay"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:background="#drawable/allcornerround"
android:fontFamily="#font/montserrat"
android:text="Pay"
android:textAllCaps="false"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
Dynamically Added Layout Code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:theme="#style/Theme.MaterialComponents.NoActionBar">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:orientation="horizontal">
<TextView
android:id="#+id/number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:text="Item 1"
android:textColor="#191b20"
android:textSize="18sp"
android:textStyle="bold" />
<ImageButton
android:id="#+id/remove"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"
android:background="#00000000"
android:src="#drawable/ic_close"
app:tint="#191b20" />
</LinearLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/nametil"
style="#style/InputDropDown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:elevation="5dp"
android:hint="Item Name"
app:boxBackgroundMode="filled"
app:endIconMode="dropdown_menu"
app:endIconTint="#AA191b20"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/nametil">
<AutoCompleteTextView
android:id="#+id/namet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/montserrat"
android:gravity="center_vertical"
android:inputType="none"
android:textColor="#191b20"
android:textCursorDrawable="#drawable/cursor"
android:textSize="18sp" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/typetil"
style="#style/InputDropDown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:elevation="5dp"
android:hint="Type"
app:boxBackgroundMode="filled"
app:endIconMode="dropdown_menu"
app:endIconTint="#AA191b20"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/nametil">
<AutoCompleteTextView
android:id="#+id/typet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/montserrat"
android:gravity="center_vertical"
android:inputType="none"
android:textColor="#191b20"
android:textCursorDrawable="#drawable/cursor"
android:textSize="18sp" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/pricetil"
style="#style/InputText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:elevation="5dp"
android:hint="Price"
app:boxBackgroundMode="filled"
app:endIconMode="clear_text"
app:endIconTint="#AA191b20">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/pricet"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="#font/montserrat"
android:gravity="center_vertical"
android:inputType="numberDecimal"
android:textColor="#191b20"
android:textCursorDrawable="#drawable/cursor"
android:textSize="18sp" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
Snaps of issue
Initial state when first fragment starts
Dynamically view added and information filled
First fragment after returning from another fragment
I seem to be stuck at the MainActivity.kt file and my app won't debug as it shows the mentioned errors.
This is for a music player app with a Navigation Drawer.
Edit : I have added the layout file after the comment as requested.
class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
fab.setOnClickListener { view->
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
}
val toggle = ActionBarDrawerToggle(
this, drawer_layout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
drawer_layout.addDrawerListener(toggle)
toggle.syncState()
nav_view.setNavigationItemSelectedListener(this)
}
//as someone asked for the layout file as well here it is
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="200dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg_gradient" />
<ImageView
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:background="#drawable/echo_logo" />
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/navigation_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/header"/>
</RelativeLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
There is no component called fab in your xml layout file. You still have the line of code that access a component called fab from your class. Remove
fab.setOnClickListener { view->
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
}
It should get it to work.
I have a question regarding android layouts. I cannot achieve what I am looking for and I need some direction.
I am looking for a layout that will be inside a Scroll View layout. its hard to explain but a picture is worth thousand words.enter image description here
I want this inside a scroll view, can any help please?
If you do not want to add images dynamically than this is the layout you want Try this.
activity_main.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:id="#+id/sv"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/linMain"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<RelativeLayout
android:id="#+id/lin1"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_margin="10dp">
<ImageView
android:id="#+id/imgView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#android:color/holo_purple" />
<ImageView
android:id="#+id/imgView2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="#id/imgView1"
android:layout_marginTop="20dp"
android:background="#android:color/holo_red_light" />
<ImageView
android:id="#+id/imgView3"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="#id/imgView2"
android:layout_marginTop="20dp"
android:background="#android:color/holo_green_light" />
<ImageView
android:id="#+id/imgView4"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="#id/imgView3"
android:layout_marginTop="20dp"
android:background="#android:color/holo_blue_light" />
</RelativeLayout>
<LinearLayout
android:id="#+id/lin2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="vertical">
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="#id/linImages"></WebView>
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
Here i am attaching screenshot of this layout UI.
Try this,I have made two layouts ..
1) activity_main that contains gridview and webview.
2)activity_gridview which contains imageview which will inflate in gridview.
I have added images dynamically to Gridview using BaseAdapter.
In MainActivity i have set that Adapter to GridView. Try this code once.
activity_main.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="#+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:fillViewport="false"
android:orientation="vertical">
<LinearLayout
android:id="#+id/linImages"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/grid"
android:layout_width="100dp"
android:layout_height="match_parent"
android:numColumns="1"></GridView>
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="#id/linImages"></WebView>
</LinearLayout>
</ScrollView>
</RelativeLayout>
activity_gridview.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/imgView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/star2" />
<ImageView
android:id="#+id/imgView2"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/star1" />
<ImageView
android:id="#+id/imgView3"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/star3" />
<ImageView
android:id="#+id/imgView4"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/star1" />
</LinearLayout>
CustomAdapter.java :
public class CustomAdapter extends BaseAdapter {
Context context;
int flags[];
LayoutInflater inflter;
public CustomAdapter(Context applicationContext, int[] flags) {
this.context = applicationContext;
this.flags = flags;
inflter = (LayoutInflater.from(applicationContext));
}
#Override
public int getCount() {
return flags.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.activity_gridview, null);
ImageView imgView1 = (ImageView) view.findViewById(R.id.imgView1);
ImageView imgView2 = (ImageView) view.findViewById(R.id.imgView2);
ImageView imgView3 = (ImageView) view.findViewById(R.id.imgView3);
ImageView imgView4 = (ImageView) view.findViewById(R.id.imgView4);
imgView1.setImageResource(flags[0]);
imgView2.setImageResource(flags[1]);
imgView3.setImageResource(flags[2]);
imgView4.setImageResource(flags[3]);
return view;
}
}
MainActivity.java :
public class MainActivity extends AppCompatActivity {
int images[] = {R.drawable.star2, R.drawable.star1, R.drawable.star3, R.drawable.star1};
GridView grid;
CustomAdapter customAdapter;
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView= (WebView) findViewById(R.id.webView);
grid = (GridView) findViewById(R.id.grid);
customAdapter = new CustomAdapter(getApplicationContext(), images);
grid.setAdapter(customAdapter);
}
}