How to refresh an activity in Android? - android

I'm learning android app development by creating a Wordle like app. I'm currently testing if I can change the custom view Letter's properties by using a button named Set which calls the testSet method in MainActivity. The problem is the properties of the views do get changed but they don't get reflected in main_activity. I can't find a way to refresh the activity. I've tried the solutions to these questions
Android - How to refresh an activity
Programmatically relaunch/recreate an activity?
But none of them worked for me.
MainActivity.java:
package com.example.wordlepromax;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import android.content.Intent;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
public class MainActivity extends AppCompatActivity {
private String letter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ConstraintLayout layout = findViewById(R.id.attempt1);
Letter currView = (Letter) layout.getChildAt(0);
if (letter != null) {
currView.setLetter(letter);
}
}
public void testSet(View view) {
letter = "N";
recreate();
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
letter = savedInstanceState.getString("view_Letter");
}
// invoked when the activity may be temporarily destroyed, save the instance state here
#Override
public void onSaveInstanceState(Bundle outState) {
// call superclass to save any view hierarchy
outState.putString("view_Letter", letter);
super.onSaveInstanceState(outState);
}
}
main_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:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/attempt1"
android:layout_width="380dp"
android:layout_height="94dp"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.483"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.example.wordlepromax.Letter
android:id="#+id/letter10"
android:layout_width="65dp"
android:layout_height="65dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toStartOf="#+id/letter11"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.example.wordlepromax.Letter
android:id="#+id/letter11"
android:layout_width="65dp"
android:layout_height="65dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toStartOf="#+id/letter12"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/letter10"
app:layout_constraintTop_toTopOf="parent" />
<com.example.wordlepromax.Letter
android:id="#+id/letter12"
android:layout_width="65dp"
android:layout_height="65dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toStartOf="#+id/letter13"
app:layout_constraintTop_toTopOf="parent" />
<com.example.wordlepromax.Letter
android:id="#+id/letter13"
android:layout_width="65dp"
android:layout_height="65dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toStartOf="#+id/letter14"
app:layout_constraintTop_toTopOf="parent" />
<com.example.wordlepromax.Letter
android:id="#+id/letter14"
android:layout_width="65dp"
android:layout_height="65dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:hint="#string/set_letter_test"
android:onClick="testSet"
android:textColorHint="#000000"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/attempt1" />
</androidx.constraintlayout.widget.ConstraintLayout>
UPDATE:
I was going through my old questions here and found this silly question I asked when I first started android app development and just wanted to update that both Vanilil and a_local_nobody are right all you have to do is change the attributes of a view and it'll change the appearance itself.

From the javadoc for onRestoreInstanceState :
This method is called after onStart when the activity is being
re-initialized from a previously saved state, given here in
savedInstanceState. Most implementations will simply use onCreate to
restore their state, but it is sometimes convenient to do it here
after all of the initialization has been done or to allow subclasses
to decide whether to use your default implementation. The default
implementation of this method performs a restore of any view state
that had previously been frozen by onSaveInstanceState.
In your case it does not work, because you're setting letter variable to View inside of onCreate method, at this point onRestoreInstanceState just was not called and letter variable was not updated(restored).
Move this line to the begining of onCreate method. In this case you can remove onRestoreInstanceState.
letter = savedInstanceState.getString("view_Letter");

Related

how is it possible that the Toast no longer works?

I'm a beginner, but I want to learn and I'm developing my first app!!I would also like to use the "toaster" function in the app. Unfortunately no longer works !!
Not only in my app, but no matter where I want to use it. Should I reinstall android studio?
Thank you, Stefan
Hi, no error message! Just not work!!
MainActivity:
package de.havadinagy.toaster_test;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
public void info(View view){
Toast.makeText(this,"Only Toastertest!!" ,Toast.LENGTH_LONG).show();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main :
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="243dp"
android:layout_height="172dp"
android:onClick="info"
android:text="ToasterTest"
android:textColor="#color/black"
android:textSize="24sp"
android:textStyle="italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
try this
Toast.makeText(requireContext(), "message", Toast.LENGTH_SHORT).show()
do try this
try
public void info(View view){
Toast.makeText(MainActivity.this,"Only Toastertest!!" ,Toast.LENGTH_LONG).show();
}
and change "info"

How to animate images in android in a loop?

I have this xml file where there are 4 image views, with one logo at the centre and the other three are just circles with radius greater than the previous one surrounding the logo. I want to display this as an animation until my page gets loaded. In want that the three circles appear one after the other in loop in a sequence. How can I do that ?
<?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">
<ImageView
android:id="#+id/imageView"
android:layout_width="150dp"
android:layout_height="150dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/center" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="150dp"
android:layout_height="150dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/anim1" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="150dp"
android:layout_height="150dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/anim2" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="150dp"
android:layout_height="150dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/anim3" />
</androidx.constraintlayout.widget.ConstraintLayout>
I have also attached a picture for a better visualization if needed :
enter image description here
#Srijan,
There are more ways to achieve this.
Please refer to objectAnimator or animation-list drawable for more details.
As a quick answer to your problem, please see the below sample code.
package com.jrh.testanim;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
int count= 0;
int imgArr[] = new int[]{
R.drawable.circle1,
R.drawable.circle2,
R.drawable.circle3,
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView imageView = (ImageView) findViewById(R.id.animation_imageview);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
imageView.setImageResource(imgArr[count]);
handler.postDelayed(this, 500);
count++;
if (count > 2) {
count = 0;
}
}
}, 500);
}
}
Please check this and mark accepted, if it solves your problem.
Thanks
JRH

accessing different children in different pages of ViewPager

The layout of my StartupPreference is defined with only one ViewPager as:
<?xml version="1.0" encoding="utf-8"?>
<androidx.viewpager.widget.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/startPref_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
and the associated activity associates this as:
public class StartupPreference extends AppCompatActivity implements StartupPrefFrag_interfaces{
private final static int no_of_prefs = 2;
private LinearLayout dot_animation_holder;
private static int temp_count = 0;
public void ViewUpdater(View updatedView){
dot_animation_holder = (LinearLayout) updatedView;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_startup_preference);
//we're not using the layout natively, but using Fragment's layout
//but setContentView is required -> it is accessed by : R.id.startPref_pager
dot_animation_holder= findViewById(R.id.dot_animation_holder);
StartPrefPagerAdapter prefPagerAdapter =
new StartPrefPagerAdapter(getSupportFragmentManager());
ViewPager StartPref_Viewpager = findViewById(R.id.startPref_pager);
StartPref_Viewpager.setAdapter(prefPagerAdapter);
StartPref_Viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
((ImageView)(findViewById(R.id.dot_animation_holder).findViewById(R.id.page1))).setImageResource(R.drawable.active_dot);
((ImageView)(findViewById(R.id.dot_animation_holder).findViewById(R.id.page2))).setImageResource(R.drawable.inactive_dot);
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
}
private class StartPrefPagerAdapter extends FragmentPagerAdapter {
public StartPrefPagerAdapter(FragmentManager fm){
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
}
#Override
public int getCount(){
return StartupPreference.no_of_prefs;//no. of preference pages
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new Frag_StartPref_Layout();
case 1:
return new Frag_StartPref_Theme();
}
return null;
}
}
}
The problem I am having is that I'm not getting it how to access different Views associated with the ViewPager. Since both the layout of the fragments include a common layout called dot_animation.xml using the <include...> tag, but the code in the onPageSelected method above updates only the first page, and if I use different ids in the <include...> like:
fragment_startpref_layout.xml:
...
<include
layout="#layout/dot_animation"
android="#+id/dot_animation_holder1"
/>
...
fragment_startpref_theme.xml
...
<include
layout="#layout/dot_animation"
android="#+id/dot_animation_holder2"
/>
...
and I use these ids to update the ImageView then I get a NullPointer Exception.(I use the code in the activity)
So, what can I do to access the different Views in the respective pages of the ViewPager?
The layout of the fragments are given below:
fragment_startpref_layout.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/C_startPref_layout"
android:layout_height="match_parent"
android:layout_width="match_parent"
>
<ImageView
android:id="#+id/startPref_Layout"
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="centerCrop"
app:layout_constraintBottom_toTopOf="#+id/startPref_layout_info"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#mipmap/ic_launcher" />
<TextView
android:id="#+id/startPref_layout_info"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:gravity="center"
android:text="#string/Frag_startPref_layout_info"
android:textSize="25sp"
app:layout_constraintBottom_toTopOf="#+id/startPref_layout_select1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent=".125"
app:layout_constraintStart_toStartOf="parent" />
<RadioGroup
android:id="#+id/startPref_layout_select1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:orientation="vertical"
app:layout_constraintBottom_toTopOf="#+id/dot_animation_holder"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent=".125"
app:layout_constraintStart_toStartOf="parent">
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:checked="true"
android:text="#string/Frag_startPref_Radio1"
android:textSize="15sp" />
<RadioButton
android:id="#+id/radioButton2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="#string/Frag_startPref_Radio2"
android:textSize="15sp" />
</RadioGroup>
<include
layout="#layout/dot_animation"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
fragment_theme_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/C_startPref_theme"
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_height="match_parent"
android:layout_width="match_parent"
>
<ImageView
android:id="#+id/startPref_Theme"
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="centerCrop"
app:layout_constraintBottom_toTopOf="#+id/startPref_layout_info"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#mipmap/ic_launcher" />
<TextView
android:id="#+id/startPref_layout_info"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:gravity="center"
android:text="#string/Frag_startPref_layout_info"
android:textSize="25sp"
app:layout_constraintBottom_toTopOf="#+id/startPref_layout_select2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent=".125"
app:layout_constraintStart_toStartOf="parent" />
<RadioGroup
android:id="#+id/startPref_layout_select2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:orientation="vertical"
app:layout_constraintBottom_toTopOf="#+id/dot_animation_holder"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent=".125"
app:layout_constraintStart_toStartOf="parent">
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:checked="true"
android:text="#string/Frag_startPref_Radio1"
android:textSize="15sp" />
<RadioButton
android:id="#+id/radioButton2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="#string/Frag_startPref_Radio2"
android:textSize="15sp" />
</RadioGroup>
<include
layout="#layout/dot_animation"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
First of all you need to update the view in fragmnet on onViewCreated.
the reason that you cant update other views is the viewpager only shows the current fragment.and the reason for nullpointexception also is that the other view are not loaded into the activity by viewpager.(You may also want to check if the correct layout is being called in the onCreateView method.) If you want to load all of them even if they are out of the screen you can use viewPager.setOffscreenPageLimit(); but this lose the viewpager purpose. so what i suggest is to create call back to communicate with your fragment and send data through callbacks and update your view inside of there fragment. also this make your activity less messy.
Update :
example of using ViewPager with multiple layouts
Document About setOffscreenPageLimit
Set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state. Pages beyond
this limit will be recreated from the adapter when needed.
This is offered as an optimization. If you know in advance the number
of pages you will need to support or have lazy-loading mechanisms in
place on your pages, tweaking this setting can have benefits in
perceived smoothness of paging animations and interaction. If you have
a small number of pages (3-4) that you can keep active all at once,
less time will be spent in layout for newly created view subtrees as
the user pages back and forth.
You should keep this limit low, especially if your pages have complex
layouts. This setting defaults to 1.

The app is closed when I click the button

Hi this is my first question here.
MainActivity.java
package com.example.kerry.chellenge04;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText name;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name=(EditText)findViewById(R.id.text);
}
public void button(View view){
String string=name.getText().toString();
Toast.makeText(getApplicationContext(), string, Toast.LENGTH_LONG).show();
}
}
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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:layout_width="288dp"
android:layout_height="346dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="8dp" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="button"
android:text="Button"
tools:layout_editor_absoluteX="58dp"
tools:layout_editor_absoluteY="401dp" />
</LinearLayout>
It's very simple code. When I press the button, the app is supposed to show you what you typed in edittext, but it stops. Can you guys please tell me what the problem is?
Try this one
You need to assign an id to your EditText:
<EditText
android:id="#+id/text"
android:layout_width="288dp"
android:layout_height="346dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="8dp" />
You need to assign an id to your EditText:
android:id="#+id/text"
otherwise this line:
name=(EditText)findViewById(R.id.text);
will assign null to name.
So when you try to access any property or method of the object name
a Null Pointer Exception will be thrown and your app will crash.
you should you the activity context rather than the application context.
try to replace
Toast.makeText(getApplicationContext(), string, Toast.LENGTH_LONG).show();
with
Toast.makeText(view.getContext(), string, Toast.LENGTH_LONG).show();
then, as already said, you should add the id, but I believe you did, as you can access the generated R.id.text

Custom ListView one item for a specific screen height

I have coded an app to learn about Custom List View. While the app has no errors, I have one issue. I have shown it in the images as follows.
IMAGE 1
IMAGE 2
The problem is I have to scroll a long way before i can find the next list item.The list items do not appear one below the other.How to resolve this issue?
Here is my code:
package com.example.hp.customlistview;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int IMAGES[]={R.drawable.adsense, R.drawable.allo, R.drawable.chrome, R.drawable.firebase, R.drawable.youtube};
String[] NAMES={"AdSense","Allo","Chrome","Firebase","YouTube"};
String [] DESCRIPTIONS={"Money through ads","Video calling","Web Browser","Cloud Database","Video Streaming"};
private ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView)findViewById(R.id.listView);
CustomAdapter customAdapter=new CustomAdapter();
listView.setAdapter(customAdapter);
}
class CustomAdapter extends BaseAdapter{
#Override
public int getCount() {
Log.d("Length of Array ","The length is "+IMAGES.length);
return IMAGES.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater layoutInflater=(LayoutInflater)getApplicationContext().getSystemService(getApplicationContext().LAYOUT_INFLATER_SERVICE);
view=layoutInflater.inflate(R.layout.customlayout, null);
Log.d("Image ID","The id is "+R.id.imageView);
ImageView imageView=(ImageView)view.findViewById(R.id.imageView);
TextView textView_name=(TextView)view.findViewById(R.id.textView);
TextView textView_desc=(TextView)view.findViewById(R.id.textView2);
if(imageView==null)
Log.d("NULL?","YES IT IS NULL");
else
Log.d("NULL?","NO IT IS NOT NULL");
imageView.setImageResource(IMAGES[i]);
textView_name.setText(NAMES[i]);
textView_desc.setText(DESCRIPTIONS[i]);
Log.d("Hello","Hello there "+textView_name.getText().toString());
return view;
}
}
}
customlayout.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="120dp">
<ImageView
android:id="#+id/imageView"
android:layout_width="86dp"
android:layout_height="91dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="32dp"
android:layout_marginTop="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="#mipmap/ic_launcher" />
<TextView
android:id="#+id/textView"
android:layout_width="161dp"
android:layout_height="39dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="28dp"
android:layout_marginTop="24dp"
app:layout_constraintBottom_toTopOf="#+id/textView2"
app:layout_constraintStart_toEndOf="#+id/imageView"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="#+id/textView2"
android:layout_width="237dp"
android:layout_height="57dp"
android:layout_marginBottom="380dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="16dp"
android:layout_marginStart="44dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toEndOf="#+id/imageView" />
<CheckBox
android:id="#+id/checkBox"
android:layout_width="39dp"
android:layout_height="39dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toEndOf="#+id/textView"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
activity_main.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.example.hp.customlistview.MainActivity">
<ListView
android:id="#+id/listView"
android:layout_width="386dp"
android:layout_height="409dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Ok"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/listView" />
</android.support.constraint.ConstraintLayout>
To resolve this, i tried the following
1)android:layout_height="match_parent" to a specific height of 120 dp.
(It is in customlayout.xml)
But it did not work
2)Used some other layout, such as Absolute and Relative Layout, but image fills up the list content much beyond expected size
EDIT:
I have the list items displayed in a compact manner, as follows,
after suggestions by Ben P. in the comments below.
IMAGE 3
But the description is not appearing,although clearly i have set it programatically in MainActivity.java
I observed that setting android:layout_height to some custom value makes the description text view disappear in the preview.
If someone is downvoting,please give the reason for doing so. This will help me improve the quality of my questions.
Ok, I figured it out. Posting it for anyone who may have similar doubts in future.
Set layout height as 120dp or some finite value even before dragging and dropping items.
Now start drag and drop operation on this reduced layout.
Pretty simple i guess. This helped me resolve it.
Here it is:)
Image 5

Categories

Resources