I am trying to implement image collage preview like Facebook while posting images,
so far i have created 5 layouts according to multiple image count and then i am inflating the layouts according to selected image list and setting image in forloop, i also need to show document icon if any selected.
Here is the code which i have implemented.
private void displayCollage() {
rlLayout.setVisibility(View.VISIBLE);
rlLayout.removeAllViews();
imagelayout5Binding.tvCount.setVisibility(View.GONE);
if (attachList.size() > 0) {
int count = attachList.size();
if (count > 5) {
count = 6;
}
switch (count) {
case 1:
DisplayCollageImage(imagelayout1Binding.ivImage, 0);
rlLayout.addView(imagelayout1Binding.getRoot());
break;
case 2:
for (int i = 0; i <= attachList.size(); i++) {
switch (i) {
case 0:
DisplayCollageImage(imagelayout2Binding.ivImage1, i);
break;
case 1:
DisplayCollageImage(imagelayout2Binding.ivImage2, i);
break;
}
}
rlLayout.addView(imagelayout2Binding.getRoot());
break;
case 3:
for (int i = 0; i <= attachList.size(); i++) {
switch (i) {
case 0:
DisplayCollageImage(imagelayout3Binding.ivImage1, i);
break;
case 1:
DisplayCollageImage(imagelayout3Binding.ivImage2, i);
break;
case 2:
DisplayCollageImage(imagelayout3Binding.ivImage3, i);
break;
}
}
rlLayout.addView(imagelayout3Binding.getRoot());
break;
case 4:
for (int i = 0; i <= attachList.size(); i++) {
switch (i) {
case 0:
DisplayCollageImage(imagelayout4Binding.ivImage1, i);
break;
case 1:
DisplayCollageImage(imagelayout4Binding.ivImage2, i);
break;
case 2:
DisplayCollageImage(imagelayout4Binding.ivImage3, i);
break;
case 3:
DisplayCollageImage(imagelayout4Binding.ivImage4, i);
break;
}
}
rlLayout.addView(imagelayout4Binding.getRoot());
break;
case 5:
for (int i = 0; i <= attachList.size(); i++) {
switch (i) {
case 0:
DisplayCollageImage(imagelayout5Binding.ivImage1, i);
break;
case 1:
DisplayCollageImage(imagelayout5Binding.ivImage2, i);
break;
case 2:
DisplayCollageImage(imagelayout5Binding.ivImage3, i);
break;
case 3:
DisplayCollageImage(imagelayout5Binding.ivImage4, i);
break;
case 4:
DisplayCollageImage(imagelayout5Binding.ivImage5, i);
break;
}
}
rlLayout.addView(imagelayout5Binding.getRoot());
break;
case 6:
for (int i = 0; i <= attachList.size(); i++) {
if (i >= attachList.size()) {
i = 5;
}
switch (i) {
case 0:
DisplayCollageImage(imagelayout5Binding.ivImage1, i);
break;
case 1:
DisplayCollageImage(imagelayout5Binding.ivImage2, i);
break;
case 2:
DisplayCollageImage(imagelayout5Binding.ivImage3, i);
break;
case 3:
DisplayCollageImage(imagelayout5Binding.ivImage4, i);
break;
case 4:
DisplayCollageImage(imagelayout5Binding.ivImage5, i);
break;
case 5:
DisplayCollageImage(imagelayout5Binding.tvCount);
break;
}
if (i == 5) {
break;
}
}
rlLayout.addView(imagelayout5Binding.getRoot());
break;
}
} else {
rlLayout.removeAllViews();
}
}
but doing this is taking too much processing time, please help me how can i reduce this processing time.
If you are trying to achieve the above output then i would suggest using a Recyclerview with StaggeredGrid Layout
Related
I am new to android studio.
I am making an app that lists names of teachers and the subjects thought and then every person has a different class. The teacher names and subjects though are stored in a TreeMap.
I am having difficulty as I want my app to feature sorts, so when outputted the positions of teachers are changed according to the sort. At the moment the items are sorted via hard code alphabetically and a switch case is used and the onclick integer position is obtained. However, I want to make the switch case make a decision according to the string of the itemClicked so whenever a sort is executed when the user clicks the correct class is displayed. Kindly see my code below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView resultsListView = (ListView) findViewById(R.id.results_listview);
TreeMap<String, String> nameSubject = new TreeMap<>();
nameSubject.put("Richard Bonnici", "Maths"); //1
nameSubject.put("Natasha Mifsud", "English"); //2
nameSubject.put("Victor Valdes", "Computing"); //3
nameSubject.put("Jonathan Galea", "Computing"); //4
nameSubject.put("Jerome Micallef", "History"); //5
nameSubject.put("Tarah Galea", "Marketing"); //6
nameSubject.put("Micheal Gonzalez", "Maths"); //7
nameSubject.put("Jason Bartolo", "Chemistry"); //8
nameSubject.put("Jack Grech", "Chemistry"); //9
nameSubject.put("Anna Carbonaro", "Maltese"); //10
nameSubject.put("Kay Azzopardi", "Biology"); //11
nameSubject.put("George Micallef", "Physics"); //12
nameSubject.put("Gabriella Bonnici", "Maths"); //13
Map sortedMap = valueSort(nameSubject);
// Get a set of the entries on the sorted map
Set set = sortedMap.entrySet();
// Get an iterator
Iterator i = set.iterator();
List<HashMap<String, String>> listItems = new ArrayList<>();
SimpleAdapter adapter = new SimpleAdapter(this, listItems, R.layout.list_item,
new String[]{"First Line", "Second Line"},
new int[]{R.id.text1, R.id.text2});
Iterator it = nameSubject.entrySet().iterator();
while (it.hasNext()) {
HashMap<String, String> resultsMap = new HashMap<>();
Map.Entry pair = (Map.Entry) it.next();
resultsMap.put("First Line", pair.getKey().toString());
resultsMap.put("Second Line", pair.getValue().toString());
listItems.add(resultsMap);
}
resultsListView.setAdapter(adapter);
resultsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(android.widget.AdapterView<?> adapterView, View view, int i, long l) {
//
switch (i) {
case 0:
startActivity(new Intent(TeacherList.this, AnnaCarbonaro.class));
break;
case 1:
startActivity(new Intent(TeacherList.this, GabriellaBonnici.class));
break;
case 2:
startActivity(new Intent(TeacherList.this, GeorgeMicallef.class));
break;
case 3:
startActivity(new Intent(TeacherList.this, JackGrech.class));
break;
case 4:
startActivity(new Intent(TeacherList.this, JasonBartolo.class));
break;
case 5:
startActivity(new Intent(TeacherList.this, JeromeMicallef.class));
break;
case 6:
startActivity(new Intent(TeacherList.this, JonathanGalea.class));
break;
case 7:
startActivity(new Intent(TeacherList.this, KayAzzopardi.class));
break;
case 8:
startActivity(new Intent(TeacherList.this, MichealGonzalez.class));
break;
case 9:
startActivity(new Intent(TeacherList.this, NatashaMifsud.class));
break;
case 10:
startActivity(new Intent(TeacherList.this, RichardBonnici.class));
break;
case 11:
startActivity(new Intent(TeacherList.this, TarahGalea.class));
break;
case 12:
startActivity(new Intent(TeacherList.this, VictorValdes.class));
break;
}
}
});
}
}
I have a addAllToGroup click function and I use the DialogWithRadioButtonM in many different places, is it possible to place DialogWithRadioButtonM() and redioGroup1() functions in a separate file(class)? Here is my code:
i tried to put it in a class but i get error for onclicklistener in it and also i dont know how to use context in a nonActivity java file. sorry, I am new in android and java thanks for help.
public void addAllToGroup(View view) {
csM = new String[6];
//dialogTitle="Add This to Group";
csM[0] = "Add to Group 1";
csM[1] = "Add to Group 2";
csM[2] = "Add to Group 3";
csM[3] = "Add to Group 4";
csM[4] = "Add to Group 5";
csM[5] = "Remove Grouping";
DialogWithRadioButtonM("Add Selected to Group");
}
public void DialogWithRadioButtonM(String str){
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle(str);
builder.setSingleChoiceItems(csM, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Log.i("TAG2", "onCreate: "+item);
switch(item)
{
case 0: backFromDialogM=0; break;
case 1: backFromDialogM=1; break;
case 2: backFromDialogM=2; break;
case 3: backFromDialogM=3; break;
case 4: backFromDialogM=4; break;
case 5: backFromDialogM=5; break;
case 6: backFromDialogM=6; break;
case 7: backFromDialogM=7; break;
}
redioGroup1(backFromDialogM);
alertDialogM.dismiss();
}
});
alertDialogM = builder.create();
alertDialogM.show();
}
public void redioGroup1(int group) {
int iCount = myRecycler.getAdapter().getItemCount();
MyDateBase mydbM = new MyDateBase(MainActivity.this);
final SQLiteDatabase databaseM = mydbM.getWritableDatabase();
String strCL = "cl" + "0";
String rawQueryM="";
for (int i = 0; i < iCount; i++) {
if (mAdapter.itemList.get(i).isChecked()) {
mAdapter.itemList.get(i).getuSentId();
rawQueryM ="UPDATE wp_words SET " + strCL + " =" + group + " WHERE wid= " + mAdapter.itemList.get(i).getuSentId();
databaseM.execSQL(rawQueryM);
Log.i("tog", "" + mAdapter.itemList.get(i).getuSentId());
}
}
Log.i("togf", "" + rawQueryM);
}
thanks
Your "DialogWithRadioButtonM" Class depends of the outer class in which it is.
You have to make "DialogWithRadioButtonM" not dependant of other variables/objects outside it, or you cannot move it in a stand alone Class file.
For example: "backFromDialogM" and "alertDialogM" are not declared INSIDE DialogWithRadioButtonM but somewhere else in the same file.
When a video is being downloaded there is a cancel option to cancel the download.but when i cancel the download i don't get any callback from DownloadManager to update the app UI.
This cancle option is only available in Android 7 .
Or else is there any option to hide or remove the cancel option from there.
After registering your broadcast receiver for DownloadManager.ACTION_DOWNLOAD_COMPLETE, you can check for DownloadManager status in the onReceive() method of the DownloadManager broadcast as follows -
private BroadcastReceiver downloadReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
checkDownloadStatus();
}
};
private void checkDownloadStatus(){
// TODO Auto-generated method stub
DownloadManager.Query query = new DownloadManager.Query();
long id = preferenceManager.getLong(strPref_Download_ID, 0);
query.setFilterById(id);
Cursor cursor = downloadManager.query(query);
if(cursor.moveToFirst()){
int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
int status = cursor.getInt(columnIndex);
int columnReason = cursor.getColumnIndex(DownloadManager.COLUMN_REASON);
int reason = cursor.getInt(columnReason);
switch(status){
case DownloadManager.STATUS_FAILED:
String failedReason = "";
switch(reason){
case DownloadManager.ERROR_CANNOT_RESUME:
failedReason = "ERROR_CANNOT_RESUME";
break;
case DownloadManager.ERROR_DEVICE_NOT_FOUND:
failedReason = "ERROR_DEVICE_NOT_FOUND";
break;
case DownloadManager.ERROR_FILE_ALREADY_EXISTS:
failedReason = "ERROR_FILE_ALREADY_EXISTS";
break;
case DownloadManager.ERROR_FILE_ERROR:
failedReason = "ERROR_FILE_ERROR";
break;
case DownloadManager.ERROR_HTTP_DATA_ERROR:
failedReason = "ERROR_HTTP_DATA_ERROR";
break;
case DownloadManager.ERROR_INSUFFICIENT_SPACE:
failedReason = "ERROR_INSUFFICIENT_SPACE";
break;
case DownloadManager.ERROR_TOO_MANY_REDIRECTS:
failedReason = "ERROR_TOO_MANY_REDIRECTS";
break;
case DownloadManager.ERROR_UNHANDLED_HTTP_CODE:
failedReason = "ERROR_UNHANDLED_HTTP_CODE";
break;
case DownloadManager.ERROR_UNKNOWN:
failedReason = "ERROR_UNKNOWN";
break;
}
Toast.makeText(AndroidDownloadManagerActivity.this,
"FAILED: " + failedReason,
Toast.LENGTH_LONG).show();
break;
case DownloadManager.STATUS_PAUSED:
String pausedReason = "";
switch(reason){
case DownloadManager.PAUSED_QUEUED_FOR_WIFI:
pausedReason = "PAUSED_QUEUED_FOR_WIFI";
break;
case DownloadManager.PAUSED_UNKNOWN:
pausedReason = "PAUSED_UNKNOWN";
break;
case DownloadManager.PAUSED_WAITING_FOR_NETWORK:
pausedReason = "PAUSED_WAITING_FOR_NETWORK";
break;
case DownloadManager.PAUSED_WAITING_TO_RETRY:
pausedReason = "PAUSED_WAITING_TO_RETRY";
break;
}
Toast.makeText(AndroidDownloadManagerActivity.this,
"PAUSED: " + pausedReason,
Toast.LENGTH_LONG).show();
break;
case DownloadManager.STATUS_PENDING:
Toast.makeText(AndroidDownloadManagerActivity.this,
"PENDING",
Toast.LENGTH_LONG).show();
break;
case DownloadManager.STATUS_RUNNING:
Toast.makeText(AndroidDownloadManagerActivity.this,
"RUNNING",
Toast.LENGTH_LONG).show();
break;
case DownloadManager.STATUS_SUCCESSFUL:
Toast.makeText(AndroidDownloadManagerActivity.this,
"SUCCESSFUL",
Toast.LENGTH_LONG).show();
downloadManager.remove(id);
break;
}
}
}
When I delete text inside EditText with clrFunc() I lost cursor (I see it flashing, but nothing happens if I type). I can't type back in. I have to click back onEditText and then I can type in.
/* WORKS PERFECT */
private void delFunc(){
String str = display.getText().toString();
if(str.length() > 0){
String strStart = str.substring(0, SELECTOR_POSITION-1);
String strEnd = str.substring(SELECTOR_POSITION);
display.setText(strStart + strEnd);
display.requestFocus();
display.setSelection(--SELECTOR_POSITION);
}
}
/* NOT FULLY WORKING */
private void clrFunc(){
display.setText(""); //text is set to ""
display.requestFocus(); //not working
display.setSelection(display.getText().length()); //not working
}
EDIT: Added more code, I'm building simple calculator.
private EditText display;
private Button b0, b1, b2, b3, b4, b5, b6, b7, b8, b9;
private Button bDec, bEquals, bAdd, bSub, bMultiply, bDivide, bClear, bBracket, bBackBracket, bDel, bClrH;
private Button bSin, bAsin, bCos, bAcos, bTan, bAtan, bLn, bLog, bPow, bPow2, bSqrt, bPi, bE, bToRad, bToDeg;
private TextView history;
private int SELECTOR_POSITION;
private void implementGUI(){
/* EditText */
display = (EditText) findViewById(R.id.etDisplay);
display.setOnTouchListener(this);
/* ... */
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.bZero: insert("0"); break;
case R.id.bOne: insert("1"); break;
case R.id.bTwo: insert("2"); break;
case R.id.bThree: insert("3"); break;
case R.id.bFour: insert("4"); break;
case R.id.bFive: insert("5"); break;
case R.id.bSix: insert("6"); break;
case R.id.bSeven: insert("7"); break;
case R.id.bEight: insert("8"); break;
case R.id.bNine: insert("9"); break;
case R.id.bDecPoint: insert("."); break;
case R.id.bAdd: insert("+"); break;
case R.id.bSub: insert("-"); break;
case R.id.bMultiply: insert("*"); break;
case R.id.bDivide: insert("/"); break;
case R.id.bBracket: insert("("); break;
case R.id.bBackBracket: insert(")"); break;
case R.id.bDel: delFunc(); break;
case R.id.bC: clrFunc(); break;
case R.id.bEquals: calcFunc(); break;
case R.id.bClrH: clrHistory(); break;
case R.id.bSin: insert("sin("); break;
case R.id.bAsin: insert("asin("); break;
case R.id.bCos: insert("cos("); break;
case R.id.bAcos: insert("acos("); break;
case R.id.bTan: insert("tan("); break;
case R.id.bAtan: insert("atan("); break;
case R.id.bLn: insert("ln("); break;
case R.id.bLog: insert("log("); break;
case R.id.bPow: insert("^"); break;
case R.id.bPow2: insert("^2"); break;
case R.id.bSqrt: insert("sqrt("); break;
case R.id.bPi: insert("(PI)"); break;
case R.id.bE: insert("(E)"); break;
case R.id.bToRad: insert("toRadians("); break;
case R.id.bToDeg: insert("toDegrees("); break;
}
}
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
v.onTouchEvent(event);
InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
SELECTOR_POSITION = display.getSelectionStart();
return true;
}
private void delFunc(){
String str = display.getText().toString();
if(str.length() > 0){
String strStart = str.substring(0, SELECTOR_POSITION-1);
String strEnd = str.substring(SELECTOR_POSITION);
display.setText(strStart + strEnd);
display.requestFocus();
display.setSelection(--SELECTOR_POSITION);
}
}
/* NOT FULLY WORKING */
private void clrFunc(){
display.setText("");
display.requestFocus();
display.setSelection(display.getText().length());
}
private void clrHistory(){
history.setText("");
}
private void calcFunc(){
try{
MathEval math = new MathEval();
String input = display.getText().toString();
history.setText(String.format("%s = %s%n%s", input, math.evaluate(input), history.getText()));
}catch(Exception e){
e.printStackTrace();
history.setText(String.format("%s%n%s", "ERROR", history.getText()));
}
clrFunc();
}
private void insert(String midStr){
try{
String input = display.getText().toString();
String startStr = input.substring(0,SELECTOR_POSITION);
String endStr = input.substring(SELECTOR_POSITION);
String retStr = startStr + midStr + endStr;
SELECTOR_POSITION += midStr.length();
display.setText(retStr);
display.setSelection(SELECTOR_POSITION);
}catch(Exception e){
e.printStackTrace();
}
}
P.S.: Sorry for my bad language.
If you want to directly type into the EditText you are missing the following in your method:
display.requestFocus();
EDIT
This is the method I am using to show the keyboard with focus, the different focus call could do the trick.
protected void showKeyboard(EditText edit) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(edit, 0);
edit.requestFocusFromTouch();
}
How can I make this code trimmed down?
prefsDisplay = getSharedPreferences("spinnerSelection",
Context.MODE_PRIVATE);
prefsPlan = getSharedPreferences("spinnerSelection1",
Context.MODE_PRIVATE);
if (prefsDisplay.getInt("spinnerSelection", 0) == 0) {
s1 = 0;
} else if (prefsDisplay.getInt("spinnerSelection", 0) == 1) {
s1 = 1;
} else if (prefsDisplay.getInt("spinnerSelection", 0) == 2) {
s1 = 2;
} else if (prefsDisplay.getInt("spinnerSelection", 0) == 3) {
s1 = 3;
} else {
s1 = 0;
DP.BreakdownMonths = 0;
}
if (prefsPlan.getInt("spinnerSelection1", 0) == 0) {
s2 = 0;
} else if (prefsPlan.getInt("spinnerSelection1", 0) == 1) {
s2 = 1;
} else if (prefsPlan.getInt("spinnerSelection1", 0) == 2) {
s2 = 2;
} else {
s2 = 0;
DP.PlanType = "highint";
}
Basically, what I am doing, when the app logs in, I want it to check SharedPreferences. If it finds a value, it assigns it, otherwise, it defaults to a value.
The following does exactly what your code does. Use the value assigned to shared prefs, and if empty, assign 0 AND also assign DP.
prefsDisplay = getSharedPreferences("spinnerSelection",
Context.MODE_PRIVATE);
prefsPlan = getSharedPreferences("spinnerSelection1",
Context.MODE_PRIVATE);
s1 = prefsDisplay.getInt("spinnerSelection", -1 );
if( s1 < 0 ) {
s1 = 0;
DP.BreakdownMonths = 0;
}
s2 = prefsPlan.getInt("spinnerSelection1", -1 );
if( s2 < 0 ) {
s2 = 0;
DP.PlanType = "highint";
}
Use switch and case to define for respective if statement and use default to assign if it doesn't match any of the if statement.
example:
int month = 8;
String monthString;
switch (month) {
case 1: monthString = "January";
break;
case 2: monthString = "February";
break;
case 3: monthString = "March";
break;
case 4: monthString = "April";
break;
case 5: monthString = "May";
break;
case 6: monthString = "June";
break;
case 7: monthString = "July";
break;
case 8: monthString = "August";
break;
case 9: monthString = "September";
break;
case 10: monthString = "October";
break;
case 11: monthString = "November";
break;
case 12: monthString = "December";
break;
default: monthString = "Invalid month";
break;
}
System.out.println(monthString);