Android studio dialogFragment screen dims but doesn't show - android-studio

I am making an app in android studio using java. I have made a dialogfragment for the setting option int the overflow menu. It was working just fine before but now when I click settings, the screen dims and nothing happens. I can click on the phone screen and the screen will undim and the app will continue working like normal and it throws no error in log cat. I set a log cat message after in inflater runs and it seems to be executing correctly. I hav no idea why it was working before but now it is not. Here is my code:
dialogFragment class:
package com.dicegame1;
import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.Switch;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;
public class SettingsMenu extends DialogFragment {
Switch doubles;
Switch triples;
#Override
public Dialog onCreateDialog(Bundle savedInstanceState){
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialogView = inflater.inflate(R.layout.settings_menu,null);
doubles = dialogView.findViewById(R.id.switchDoublesBonus);
triples = dialogView.findViewById(R.id.switchTriplesBonus);
Button save = dialogView.findViewById(R.id.btnSave);
Button cancel = dialogView.findViewById(R.id.btnCancel);
Log.d("load", "dialog load");
cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dismiss();
Log.d("dis", "dismiss");
}
});
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
MainActivity callingActivity = (MainActivity) getActivity();
Dice player = callingActivity.sendPlayer();
player.setDoubles(doubles.isChecked());
player.setTriples(triples.isChecked());
Log.d("s", "save");
dismiss();
}
});
return builder.create();
}
}
here is calling methods in main activity:
#Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.overflow_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
int id = item.getItemId();
if (id == R.id.settings) {
SettingsMenu d = new SettingsMenu();
Log.d("1", "Dialog created");
d.show(getSupportFragmentManager(),"");
Log.d("2", "Show called");
return true;
}
return super.onOptionsItemSelected(item);
}
here is xml for settings menu:
<?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">
<Switch
android:id="#+id/switchDoublesBonus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="48dp"
android:text="Doubles"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.205"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.213"
/>
<Switch
android:id="#+id/switchTriplesBonus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="226dp"
android:minHeight="48dp"
android:text="Triples"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.201"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
/>
<Button
android:id="#+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.124"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.876" />
<Button
android:id="#+id/btnCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.78"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.876" />
</androidx.constraintlayout.widget.ConstraintLayout>
I want to reiterate that things were working correctly before and I have reverted all changes made and its still not working. Any ideas??

Related

Android Studio Recycler View have gaps between the rows. Help me fix

I followed this tutorial on youtube for my school project. The app is able to display selected images from gallery but the recycler view have gaps in every row. What do I code to fix this app? help. enter image description here
here's my 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:scrollbarStyle="insideInset"
android:scrollbars="vertical"
tools:context=".MainActivity">
<Button
android:id="#+id/pick"
android:layout_width="53dp"
android:layout_height="64dp"
android:layout_margin="12dp"
android:layout_weight="0"
android:backgroundTint="#FF5722"
android:text="+"
android:textSize="24sp"
app:cornerRadius="64dp" />
<TextView
android:id="#+id/totalPhotos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="center"
android:textColor="#FFFFFF" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView_Gallery_Images"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_weight="1"
</LinearLayout>
RecyclerAdapter.java
package com.example.mygram;
import android.media.Image;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
private ArrayList<Uri> uriArrayList;
public RecyclerAdapter(ArrayList<Uri> uriArrayList) {
this.uriArrayList = uriArrayList;
}
#NonNull
#Override
public RecyclerAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.custom_single_image,parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull RecyclerAdapter.ViewHolder holder, int position) {
holder.imageView.setImageURI(uriArrayList.get(position));
}
#Override
public int getItemCount() {
return uriArrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
public ViewHolder(#NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.image);
}
}
}
Custom single images.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="0dp"
android:scaleType="centerCrop"
app:layout_constraintDimensionRatio="1"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
And here's the MainActivity.java
package com.example.mygram;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
TextView textView;
Button pick;
ArrayList <Uri> uri = new ArrayList<>();
RecyclerAdapter adapter ;
private static final int Read_Permission = 101;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.totalPhotos);
recyclerView = findViewById(R.id.recyclerView_Gallery_Images);
pick = findViewById(R.id.pick);
adapter = new RecyclerAdapter(uri);
recyclerView.setLayoutManager(new GridLayoutManager(MainActivity.this,3));
recyclerView.setAdapter(adapter);
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, Read_Permission);
}
pick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("image/*");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2){
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
}
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1 && resultCode == Activity.RESULT_OK){
if(data.getClipData() !=null) {
int x = data.getClipData().getItemCount();
for (int i = 0; i < x; i++) {
uri.add(data.getClipData().getItemAt(i).getUri());
}
adapter.notifyDataSetChanged();
textView.setText("Photos ("+uri.size()+")");
}else if (data.getData()!=null){
String imageURL=data.getData().getPath();
uri.add(Uri.parse(imageURL));
}
}
}
}
The concept of the app is instagram feed tester. I want to be able to display images like the grid in instagram but the gap is appearing preventing it to happen. help me please thankyou
In your Custom single images.xml replace the height of the parent layout to wrap_content. Replace your code with the following
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="0dp"
android:scaleType="centerCrop"
app:layout_constraintDimensionRatio="1"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
Everything is right : Just need to Change this to this: (images.xml)
android:layout_height="match_parent"
to
android:layout_height="wrap_content"
in your model xml file

Display ImageButton on last slide of viewpager

I'm trying to have a button appear only on the last page of a ViewPager. I have the working button but it is displayed on all the pages instead of only the last page. I want to have it visible only on the last page. Here is my code.
activity_welcome2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
tools:context=".Welcome_Activity"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.viewpager.widget.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!--- for dots -->
<View
android:id="#+id/view1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:alpha=".5"
android:layout_above="#+id/dotsLayout"
android:background="#color/white"
android:layout_marginBottom="15dp" />
<LinearLayout
android:id="#+id/dotsLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:gravity="center"
android:layout_marginBottom="55sp" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:id="#+id/next3"
android:layout_width="99dp"
android:layout_height="45dp"
android:background="#android:color/transparent"
android:scaleType="fitCenter"
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.822"
app:srcCompat="#drawable/next" />
</androidx.constraintlayout.widget.ConstraintLayout>
</RelativeLayout>
Welcome_Activity.java
package com.group7.salitongue;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.viewpager.widget.ViewPager;
public class Welcome_Activity extends AppCompatActivity {
private ViewPager mPager;
private int[] layouts = { R.layout.activity_main, R.layout.activity_splashscreen2, R.layout.activity_welcome };
private MpagerAdapter mpagerAdapter;
private LinearLayout Dots_Layout;
private ImageView[] dots;
private ImageButton nextBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome2);
mPager = (ViewPager) findViewById(R.id.viewPager);
mpagerAdapter = new MpagerAdapter(layouts, this);
mPager.setAdapter(mpagerAdapter);
Dots_Layout = (LinearLayout) findViewById(R.id.dotsLayout);
createDots(0);
mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
#Override
public void onPageSelected(int position) {
createDots(position);
}
#Override
public void onPageScrollStateChanged(int state) {}
});
// transition button to Signup page
nextBtn = (ImageButton) findViewById(R.id.next3);
nextBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(Welcome_Activity.this,MainActivity.class);
startActivity(intent);
}
});
}
private void createDots(int current_position) {
if(Dots_Layout != null)
Dots_Layout.removeAllViews();
dots = new ImageView[layouts.length];
for (int i = 0; i < layouts.length; i++) {
dots[i] = new ImageView(this);
if (i == current_position) {
dots[i].setImageDrawable(ContextCompat.getDrawable(this, R.drawable.active_dots));
}
else {
dots[i].setImageDrawable(ContextCompat.getDrawable(this, R.drawable.default_dots));
}
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
params.setMargins(4, 0, 4, 0);
Dots_Layout.addView(dots[i],params);
}
}
}
How can I display the button only when the user has reached the last page of swiping?
Just listen in the onPageSelected method of your OnPageChangeListener when the last page is reached and set the button to visible. In all other cases, just hide it.
#Override
public void onPageSelected(int position) {
createDots(position);
// only show the button when the last page is reached
if (position == mPagerAdapter.getCount() - 1)
nextBtn.setVisibility(View.VISIBLE);
else
nextBtn.setVisibility(View.GONE);
}

Clickable URL in Android-Studio TextView does not work

My Link looks like a link (itÅ› color is blue) but when I click, nothing happens. Do you have any ideas? I use an external string where i want to put in the clickable URL.
Here is my xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/anthrazit">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/textinfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:textColor="#color/lightwhite"
android:text="#string/info"
android:linksClickable="true"/>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
My java:
public class info extends MainActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info);
final TextView infoTextView = (TextView) findViewById(R.id.textinfo);
infoTextView.setMovementMethod(LinkMovementMethod.getInstance());
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode ==
KeyEvent.KEYCODE_BACK) {
intent = new Intent(info.this, MainActivity.class);
startActivity(intent);
finish();
}
return false;
}
}
And my string. I found the code online, but in my Version it does not work :-( :
<string name="info">
<i>https://stackoverflow.com/questions/ask</i>
</string>

App Crash: FATAL EXCEPTION: main

App Crash on button click. Here is the log.
Dealing specifically with trying to store a user into Parse.com as my backend. I have a java class called RegisterActivity with corresponding xml file, leading me to think it could be a null pointer exception, but I tested, and that doesnt seem to be the problem. Here are the 3 classes in which I believe are involved with the problem.
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.parse.ParseException;
import com.parse.ParseUser;
import com.parse.SignUpCallback;
public class RegisterActivity extends Activity {
protected EditText mUsername;
protected EditText mUserEmail;
protected EditText mUserPassword;
protected Button mRegisterButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
//initialize
mUsername = (EditText)findViewById(R.id.usernameRegisterEditText);
mUserEmail= (EditText)findViewById(R.id.emailRegisterEditText);
mUserPassword= (EditText)findViewById(R.id.passwordRegisterEditText);
mRegisterButton = (Button)findViewById(registerButton);
//listen to register button click
mRegisterButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//get the username,password, and email, then convert to string
String username = mUsername.getText().toString().trim();
String email = mUserEmail.getText().toString().trim();
String password = mUserPassword.getText().toString().trim();
//store user and parse
ParseUser user = new ParseUser();
user.setUsername(username);
user.setPassword(email);
user.setEmail(password);
user.signUpInBackground(new SignUpCallback() {
#Override
public void done(ParseException e) {
if(e == null){
//user signed up successfully
Toast.makeText(RegisterActivity.this, "Welcome!", Toast.LENGTH_LONG).show();
//take user to homepage
}else{
//error signing up user
}
}
});
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_register, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Here is the corresponding activity_register.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.parse.inclassmode.RegisterActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:hint="username"
android:ems="10"
android:id="#+id/usernameRegisterEditText"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="142dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="email"
android:ems="10"
android:id="#+id/emailRegisterEditText"
android:layout_below="#+id/usernameRegisterEditText"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="password"
android:ems="10"
android:id="#+id/passwordRegisterEditText"
android:layout_below="#+id/emailRegisterEditText"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="sign up"
android:id="#+id/registerButton"
android:layout_marginTop="42dp"
android:layout_below="#+id/passwordRegisterEditText"
android:layout_alignParentStart="true" />
</RelativeLayout>
MainActivity.java class which includes my keys for Parse.com
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import com.parse.Parse;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Enable Local Datastore, ID's hidden
Parse.initialize(this, "id", "id");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
and finally my android manifest, which launches the register activity then takes the user to the home screen.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.parse.inclassmode" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".RegisterActivity"
android:label="#string/title_activity_register" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="InClassMode" >
</activity>
</application>
</manifest>
Figured it out!
For those of you who may come across this problem, create a new class that is not an activity, from there, initialize the Parse.

Strange ListView behavior [faster performance]

I find this very strange behavior of listview and it acts differently on Android 2.1 and 4.1
Here are the 2 classes I wrote which google advises to use for faster performance of ListView.
How to reproduce: Add an entry, check the first checkbox, then add more entries than one page. The checked box floats through the elements!
Here is a small project I wrote: http://tinyurl.com/strange-listview
Anyone can explain the issue or a workaround?
public class AlarmManagerActivity extends Activity {
private PerformanceArrayAdapter mPerformaceArrayAdapter;
private ListView mListView;
private ArrayList<String> mListItems = new ArrayList<String>();
private int mCounter = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mListView = (ListView) findViewById(R.id.listViewCondition);
mPerformaceArrayAdapter = new PerformanceArrayAdapter(this, mListItems);
mListView.setAdapter(mPerformaceArrayAdapter);
}
public void onClickAdd(View view){
mListItems.add("" + mCounter++);
mPerformaceArrayAdapter.notifyDataSetChanged();
}
}
And here is the main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="#+id/buttonAdd" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Add New Alarm"
android:onClick="onClickAdd" />
<ListView android:id="#+id/listViewCondition"
android:layout_height="wrap_content" android:layout_width="match_parent"></ListView>
</LinearLayout>
And the performance adapter class.
public class PerformanceArrayAdapter extends ArrayAdapter<String> {
private final Activity context;
private final ArrayList<String> mListItems;
static class ViewHolder {
public TextView textView1;
public TextView textView2;
public CheckBox checkBox;
}
public PerformanceArrayAdapter(Activity context, ArrayList<String> listItems) {
super(context, R.layout.row_layout, listItems);
this.context = context;
this.mListItems = listItems;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.row_layout, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.textView1 = (TextView) rowView.findViewById(R.id.textView1);
viewHolder.textView2 = (TextView) rowView.findViewById(R.id.textView2);
viewHolder.checkBox = (CheckBox) rowView.findViewById(R.id.checkBox1);
rowView.setTag(viewHolder);
}
ViewHolder holder = (ViewHolder) rowView.getTag();
holder.textView1.setText(mListItems.get(position));
return rowView;
}
}
Here is the row_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView android:id="#+id/textView1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" android:text="TextView1"></TextView>
<TextView android:id="#+id/textView2" android:layout_below="#+id/textView1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="TextView2"></TextView>
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/checkBox1"
android:layout_alignParentRight="true" android:focusable="false"
android:focusableInTouchMode="false"></CheckBox>
</RelativeLayout>
I found the solution, thanks to Vogella :)
I had to use the Domain Model mentioned here to track both the text view and check box.
http://www.vogella.com/articles/AndroidListView/article.html#listadvanced_interactive

Resources