How can I close the inflated layer? - java

I made the close button on the inflated layer. And I want to close this inflated layer when I click that button. How can I solve this problem
I used Rl.setVisibility(View.GONE);
But When I restart this method it's not working properly.
(different layer comes out...all the buttons are not working)
public void layer(){
final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final RelativeLayout Rl = (RelativeLayout) inflater.inflate(R.layout.menu_bookmark, null);
//Rl.setBackgroundColor(Color.parseColor("#99000000"));
final RelativeLayout.LayoutParams paramRl = new RelativeLayout.LayoutParams
(LinearLayout.LayoutParams.MATCH_PARENT, WRAP_CONTENT);
addContentView(Rl, paramRl);
final String current_url = webView.getUrl();
final Button btn_add = (Button) findViewById(R.id.btn_add);
final Button btn_out = (Button) findViewById(R.id.btn_out);
final EditText editText = (EditText) findViewById(R.id.edit_input);
editText.setText(current_url);
final ListView listView = (ListView) findViewById(R.id.lv_bookmark);
final ArrayList<String> arrayList = new ArrayList<String>();
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.menu_bookmark_row, arrayList);
listView.setAdapter(arrayAdapter);
btn_add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (editText.getText().toString().length() > 0) {
String inputStr = editText.getText().toString();
arrayList.add(inputStr);
arrayAdapter.notifyDataSetChanged();
}
}
});
btn_out.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// <--I NEED HELP!!!!!
}
});

parentLinearLayout.removeView((View) v.getParent());
https://www.androidtutorialpoint.com/basics/dynamically-add-and-remove-views-in-android/

Related

Show a list view in a different Activity

Hey I was wondering if this is possible. I'm working on a app that takes user input and creates flashcards. When I click the red button I want it to open a new activity displaying the input as a list. I'm not sure how to get the second activity to read the Edittext and put it into a list display.
When you press the green button, a prompt takes user input and post it to the page. When you click the red button it will take you to a list view of what the user entered. I'm a bit stumped on how to pass the information to the next page to display a list.
enter code here package Main Code ;
TextView QuestionTV;
TextView AnswerTV;
//Buttons of Main Flashcard xml
Button addcardbtn;
Button Answerbtn;
Button nextbtn;
//Input and Buttons from dialog_Flashcard xml
EditText QuestionET;
EditText AnswerET;
Button Submitcardbtn;
Button cancelbtn;
Button deletecard;
//counter for array
int count = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flash_cards);
addcardbtn = findViewById(R.id.addcardbtn);
Answerbtn = findViewById(R.id.Answerbtn);
nextbtn = findViewById(R.id.nextbtn);
deletecard = findViewById(R.id.deletecard);
//new array
QuestionArray = new ArrayList<>();
adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, QuestionArray);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, QuestionArray);
AnswerArray = new ArrayList<>();
adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, AnswerArray);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, AnswerArray);
//Text views
QuestionTV = findViewById(R.id.QuestionTV);
AnswerTV = findViewById(R.id.answerTV);
deletecard.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplication(), FlashCard_ListView.class));
}
});
addcardbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final AlertDialog.Builder flashbuilder = new AlertDialog.Builder(FlashCards.this);
//get layout for the alert box
View fView = getLayoutInflater().inflate(R.layout.dialog_flashcard, null);
//get EditText from the alertbox layout
QuestionET = fView.findViewById(R.id.QuestionET);
AnswerET = fView.findViewById(R.id.AnswerET);
//get buttons from the alertbox layout
Submitcardbtn = (Button) fView.findViewById(R.id.Submitcardbtn);
cancelbtn = fView.findViewById(R.id.cancelbtn);
//make dialog box pop up
flashbuilder.setView(fView);
final AlertDialog dialog = flashbuilder.create();
dialog.show();
//code for the submit button
Submitcardbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!QuestionET.getText().toString().isEmpty() && !AnswerET.getText().toString().isEmpty()){
QuestionArray.add(QuestionET.getText().toString());
AnswerArray.add(AnswerET.getText().toString());
Toast.makeText(FlashCards.this, "Card Created", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
}
});
//code for cancel button
cancelbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
nextbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
QuestionTV.setText(QuestionArray.get(count));
AnswerTV.setText(AnswerArray.get(count));
if (count<QuestionArray.size()-1 || count<AnswerArray.size()-1){
count++;
AnswerTV.setVisibility(View.INVISIBLE);
}else if(count == QuestionArray.size()-1){
count = 0;
AnswerTV.setVisibility(View.INVISIBLE);
}
}
});
Answerbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AnswerTV.setVisibility(View.VISIBLE);
}
});
}
});
}
}
First Activity code
ArrayList<String> QuestionArray = new ArrayList<>();
QuestionArray.add(QuestionET.getText().toString());
ArrayList<String> AnswerArray = new ArrayList<>();
AnswerArray.add(AnswerET.getText().toString());
Intent i = new Intent(FlashCards.this, FlashCard_ListView.class);
i.putExtra("Questions", QuestionArray);
i.putExtra("Answers", AnswerArray);
startActivity(i);
Second Activity Code
Intent i = getIntent();
ArrayList<String> QuestionArray = i.getStringArrayListExtra("Questions");
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, QuestionArray);
QuestionListView.setAdapter(adapter);
ArrayList<String> AnswerArray = i.getStringArrayListExtra("Answers");
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, AnswerArray);
AnswersListView.setAdapter(adapter);
I Figured It out thanks!

in "saved.add(FileName); there is an error on add how to fix this error?

I've been trying to build a page that take the the file name and then input it in a listview and then this problem occurred first I don't know how to build a
layout page and then there is a problem with this line Saved.add(FileName); on add particularly?
I've tried by removing the final infront list view but it ended up with an error on Saved instead
this is the code in question:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final ImageButton Add = (ImageButton)findViewById(R.id.imageButton);
final ListView Saved = (ListView)findViewById(R.id.Saved_list);
final ArrayAdapter<String> Arr1 = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1);
Saved.setAdapter(Arr1);
Add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Save");
View mView = getLayoutInflater().inflate(R.layout.SaveFile_dialog_layout, null);
final EditText SavedText = (EditText) findViewById(R.id.SavedText);
Button Save = (Button)mView.findViewById(R.id.Save);
Button Later = (Button)mView.findViewById(R.id.Later);
Button Cancel = (Button)mView.findViewById(R.id.Cancel);
builder.setView(mView);
final AlertDialog SavingsDialogue =builder.create();
Save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!SavedText.getText().toString().isEmpty()){
String FileName = SavedText.getText().toString();
Saved.add(FileName);
SavedText.getText().clear();
Toast.makeText(MainActivity.this,"file name added",Toast.LENGTH_LONG).show();
Intent Transition = new Intent(MainActivity.this,Main2Activity.class);
startActivity(Transition);
// Here I need to add the file organizayion system to start adding the file
}

How to detect a click on the checkbox within a ListView when the its focusable is set to false

I am working on an application where I need to create a list. The list contains 2 textViews and 1 checkbox. The list has to delete the row which detects a click on it. But the click will only be detected when the checkbox's focusability is set to false. But the aim of the application is to also delete the row when the respective checkbox is checked.
But the onItemClickListener does not detect the click on the checkbox.
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
ArrayList<ListData> ListData = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate: Starting");
final ListDataAdapter adapter = new ListDataAdapter(this ,R.layout.list_layout,ListData );
Button button= (Button) findViewById(R.id.Button);
CheckBox checkBox =(CheckBox)findViewById(R.id.checkbox);
final ListView listView = (ListView)findViewById(R.id.ListView);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TextView textView1 = (TextView) findViewById(R.id.TextView1);
TextView textView2 = (TextView) findViewById(R.id.TextView2);
EditText editText1 = (EditText) findViewById(R.id.EditText1);
EditText editText2 = (EditText) findViewById(R.id.EditText2);
if (editText1.getText() != null && editText2!=null) {
String title = editText1.getText().toString();
String description = editText2.getText().toString();
textView1.setText(title);
textView2.setText(description);
ListData data = new ListData(title, description);
ListData.add(data);
saveArrayList();
Log.i(TAG, "display:" + ListData);
adapter.notifyDataSetChanged();
editText1.setText("");
editText2.setText("");
listView.setAdapter(adapter);
}
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
Log.d(TAG, "onItemClick: tapped");
ListData.remove(i);
saveArrayList();
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);
return true;
}
});
}
}
The XML file is
<CheckBox
android:id="#+id/checkbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="90"
android:focusable="false"
android:focusedByDefault="false" />

Android-Picasso java.lang.IllegalArgumentException: Target must not be null. when the image view is in a custom layout for a alertdialog

I have java.lang.IllegalArgumentException: Target must not be null. when I try to download an image and put it in the imageView By picasso library. let me show you my code:
public class ads_show extends AppCompatActivity {
private Toolbar toolbar;
private ImageView img_view1;
private HashMap<String, Object> hmap;
private ImageView img;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ads_show);
getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
img = (ImageView) findViewById(R.id.ads_show_img);
TextView title = (TextView) findViewById(R.id.title_ads_show);
TextView desc = (TextView) findViewById(R.id.desc_ads_show);
TextView seller = (TextView) findViewById(R.id.seller_ads_show);
TextView phone = (TextView) findViewById(R.id.phone_ads_show);
TextView email = (TextView) findViewById(R.id.mail_ads_show);
TextView date = (TextView) findViewById(R.id.date_ads_show);
Bundle ads = getIntent().getExtras();
hmap = (HashMap<String, Object>) ads.get("ads");
Picasso.with(this).load((String) hmap.get("image_path"))
.resize(300, 150)
.into(img);
title.setText((String) hmap.get("title"));
desc.setText((String) hmap.get("description"));
seller.setText((String) hmap.get("seller"));
phone.setText((String) hmap.get("phone"));
email.setText((String) hmap.get("email"));
date.setText((String) hmap.get("date"));
}
public void onFullScreenClick(View v) {
try {
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(getApplicationContext().LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.fullscreen_image_show,null);
img_view1 = (ImageView) findViewById(R.id.img_fullscreen);
Picasso.with(this).load((String)hmap.get("image_path")).into(img_view1);
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setView(layout);
alert.setCancelable(true);
alert.setNeutralButton("exit", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
alert.show();
} catch (Exception e) {
Log.i("app_error", "Full screen" + e.toString());
}
}
public void onBtnBackClick(View v) {
finish();
}
}
My problem is with onFullScreenClick method. and the image view in the onCreate method works fine.
You are forget to bind ImageView like ...
change in this line ...
img_view1 = (ImageView)layout.findViewById(R.id.img_fullscreen);

how to get textView text in onclicklistener inside listView adapter

i have a list View which contains imageView, TextView and two Buttons
what i want is when i click on the button for example for the third item in the list view i want to get the text that in the textview when i click on the button which is in the same position as the text view.. how to do that in my list view adapter inside getView() in the onclicklistener ?
here is my SimpleArrayAdapter.java
public class MySimpleArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public MySimpleArrayAdapter(Context context, String[] values) {
super(context, R.layout.my_listview, values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.my_listview, parent, false);
Button d=(Button) rowView.findViewById(R.id.d);
Button a=(Button) rowView.findViewById(R.id.a);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
textView.setText(values[position]);
// Change the icon for Windows and iPhone
String s = values[position];
if (s.startsWith("Windows7") || s.startsWith("iPhone")
|| s.startsWith("Solaris")) {
imageView.setImageResource(R.drawable.no);
} else {
imageView.setImageResource(R.drawable.ok);
}
d.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// here where i want to get the current textview text
Log.d(null, "");
/*Intent intent = new Intent(context, MainActivity2.class);
context.startActivity(intent);*/
}
});
a.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d(null, "aaaaaaaaaaaaaaaaa");
}
});
return rowView;
}
}
In button onClick Simply do
String txt = textView.getText().toString();
i.e. rewrite your code as
d.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// here where i want to get the current textview text
String txt = textView.getText().toString(); // add here
Log.d(null, "");
/*Intent intent = new Intent(context, MainActivity2.class);
context.startActivity(intent);*/
}
});
and make textview to final i.e. Change
TextView textView = (TextView) rowView.findViewById(R.id.label);
to
final TextView textView = (TextView) rowView.findViewById(R.id.label);
final TextView textView = (TextView) rowView.findViewById(R.id.label);
d.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String myText = textView.getText().toString();
Log.d(null, "");
/*Intent intent = new Intent(context, MainActivity2.class);
context.startActivity(intent);*/
}
});
Use
String txtStr = textView.getText().toString();
in click event of button
try this:
final TextView textView = (TextView) rowView.findViewById(R.id.label);
d.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textView.getText().toString();
}
});

Categories