The app is closed when I click the button - android

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

Related

App showing no error but crashing as soon as I press a button

I encountered this problem while trying to develop an android addition app and I tried to solve it by re-writing my code with making sure I don't make silly mistakes but I have no idea on how to solve it now.
I am trying to develop an android app which takes two numbers from the user and shows the sum of it, I think I am writing the correct code as their is no build errors or any problem highlighting and the app even opens but as soon as I write two numbers in the input numbers field and tap on the 'Calculate' button, my app crashes while I was expecting that the number would pop up in the TextView3.
The code of my XML file:
<?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:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter 1st no:"
android:textSize="22sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.159"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.119" />
<EditText
android:id="#+id/editTextNumber2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/textView1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.12" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter 2nd no:"
android:textSize="22sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.163"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView1"
app:layout_constraintVertical_bias="0.109" />
<EditText
android:id="#+id/editTextNumber3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.36"
app:layout_constraintStart_toEndOf="#+id/textView2"
app:layout_constraintTop_toBottomOf="#+id/editTextNumber2"
app:layout_constraintVertical_bias="0.089" />
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.383" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="34sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button5"
app:layout_constraintVertical_bias="0.087" />
</androidx.constraintlayout.widget.ConstraintLayout>
The code of my Java file is:
package com.example.chapter1_futiletries;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private Button button5;
private TextView textView1;
private TextView textView2;
private TextView textView3;
private EditText editTextNumber2;
private EditText editTextNumber3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button5 = findViewById(R.id.button5);
textView1 = findViewById(R.id.textView1);
textView2 = findViewById(R.id.textView2);
textView3 = findViewById(R.id.textView3);
editTextNumber2 = findViewById(R.id.editTextNumber2);
editTextNumber3 = findViewById(R.id.editTextNumber3);
button5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String editTextNumber2_value = editTextNumber2.getText().toString();
int num1 = Integer.parseInt(editTextNumber2_value);
String editTextNumber3_value = editTextNumber3.getText().toString();
int num2 = Integer.parseInt(editTextNumber3_value);
int finalNum = num1 + num2;
textView3.setText(finalNum);
}
});
}
}
I am using min SDK version 22 and targetSDK as 31 and I am trying to run it in the Pixel 4A and I am still learning android development so I could have made some mistakes and not understood them.
the error in line :
textView3.setText(finalNum);
because setText() should be filled always with string , As for the Integer , it is used to get String Resource , ex :
textView3.setText(R.string.appname);
The solution to your problem is :
textView3.setText(String.valueOf(finalNum));
or :
textView3.setText(""+finalNum);

How to refresh an activity in 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");

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"

TextView onClickListener crashes app when clicked on [duplicate]

This question already has answers here:
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
findViewById returns null
(4 answers)
Null pointer Exception - findViewById()
(12 answers)
Closed 3 years ago.
I am new in android development. I'm trying to make a simple login and registration form. I have a TextView 'Sign up' which basically takes the user to the registerActivity when clicked on, but when everytime when i click on the sign up textView it crashes my app. I have searched for the solutions but i can't seem to figure out whats causing the crash.
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=".LoginActivity"
android:orientation="vertical"
android:gravity="center_horizontal"
android:background="#drawable/bg"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_marginTop="70dp"
app:srcCompat="#drawable/logo"
/>
<EditText
android:id="#+id/edittext_username"
android:layout_width="249dp"
android:layout_height="71dp"
android:layout_marginTop="20dp"
android:drawableLeft="#drawable/user"
android:drawablePadding="10dp"
android:textColorHint="#f8f8f8"
android:paddingLeft="15dp"
android:hint="#string/mobile"
android:textColor="#f8f8f8"
android:background="#drawable/rounded_edittext" />
<EditText
android:id="#+id/edittext_password"
android:layout_width="249dp"
android:layout_height="71dp"
android:layout_marginTop="20dp"
android:drawableLeft="#drawable/password"
android:drawablePadding="10dp"
android:textColorHint="#f8f8f8"
android:paddingLeft="15dp"
android:hint="#string/password"
android:textColor="#f8f8f8"
android:background="#drawable/rounded_edittext"/>
<Button
android:id="#+id/button_login"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginTop="20dp"
android:background="#drawable/rounded_buttons"
android:text="#string/login"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="35dp"
android:text="Not Registered?"
android:textColor="#000000" />
<TextView
android:id="#+id/textview_register"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:paddingLeft="5dp"
android:textStyle="bold"
android:textColor="#e8170c"
android:text="#string/register"/>
</LinearLayout>
JAVA:
package com.example.careerhunts.raw;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class LoginActivity extends AppCompatActivity {
EditText mTextUsername;
EditText mTextPassword;
Button mButtonLogin;
TextView mTextViewRegister;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); //will hide the title
getSupportActionBar().hide(); // hide the title bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN); //enable full
screen
setContentView(R.layout.activity_login);
mTextUsername = findViewById(R.id.edittext_username);
mTextPassword = findViewById(R.id.edittext_password);
mButtonLogin = findViewById(R.id.button_login);
mTextViewRegister = findViewById(R.id.textview_register);
mTextViewRegister.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent registerIntent = new Intent(LoginActivity.this,
RegisterActivity.class);
startActivity(registerIntent);
}
});
}
}
I tested same code, Its's working fine, by guessing i can tell that you may misspelled name of activity_login (may be you assigned different layout which doesnot have text view named textview_register) or may be you dont have activity named RegisterActivity.class.

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