I'm working on android app that creat layout with textView & checkbox programmatically for each text user input in EditText, and when the user select one of the checkbox and click on delete button the layout that contain that checkbox remove from the main layout
public void plusBtn(View view)
{
item = actv.getText().toString(); // text from EditText
actv.setText("");
creatt();
}
public void deletBtn(View view)
{
if(chbox.isChecked()){
linear.removeView(linr);
}
}
public void creatt()
{
linr = new LinearLayout(this);
linr.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
linr.setOrientation(LinearLayout.HORIZONTAL);
linr.setGravity(Gravity.RIGHT);
TextView txt = new TextView(this);
txt.setText(item);
linr.addView(txt);
chbox = new CheckBox(this);
linr.addView(chbox);
linear.addView(linr); // main layout
}
But when I click on delete button just the last layout removed, and that not what I want.
though the technique you're using is not fine. but let me give you a shortcut. in your deletBtn method try to get the reference of the desired deleted view (try to handle case of id or boolean you've to make logic) and enclose this line linear.removeView(linr); into that condition.
Summery, only delete the view which meets the condition.
Alternative, do your whole task from scratch with the help of Recycler-view. as it Recycler-view will give you exact location of cell.
Related
I am using an library called CircularFloating to show menu in my homepage in one fragment but when i click and go to the next fragmetn it still appears to be in next fragment also. how can I remove or set visibility off when I move to next fragments.
here is my code.
SubActionButton.Builder itemBuilder = new SubActionButton.Builder(getActivity());
ImageView itemIcon1 = new ImageView(getActivity());
itemIcon1.setImageDrawable(getResources().getDrawable(R.drawable.camera_button));
SubActionButton button1 = itemBuilder.setContentView(itemIcon1).build();
ImageView itemIcon2 = new ImageView(getActivity());
itemIcon2.setImageDrawable(getResources().getDrawable(R.drawable.button_action_dark_touch));
SubActionButton button2 = itemBuilder.setContentView(itemIcon2).build();
final FloatingActionMenu actionMenu = new FloatingActionMenu.Builder(getActivity())
.addSubActionView(button1)
.addSubActionView(button2)
.attachTo(actionButton)
.build();
itemIcon2.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
Fragment abt = new Feedback_Fragment();
((AppCompatActivity)getActivity()).getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, abt).addToBackStack(null).commit();
actionMenu.close(true);
}
});
Library I have used is
implementation 'com.oguzdev:CircularFloatingActionMenu:1.0.2'
You can remove the visibility with
actionMenu.setVisibility(View.INVISIBLE)
or
actionMenu.setVisibility(View.GONE)
And set the visibility to visible again with
actionMenu.setVisibility(View.VISIBLE)
Without knowing exactly how this library is built, you could try playing around with the attribute
app:elevation="0". Try to set this attribute to the layout that is currently appearing on top of all fragments, if this attribute is available for that layout. If not, try setting it for the FloatingActionButtons. Hope this helps.
I am trying to take all the rows from my db and add it to the current layout, also, making each row clickable in the layout to take the user to a new screen with the id...
Here is my current code, but stuck on that part... I understand that I can put an onClickListener, but then does it have to be a button?
For a visual representation refer to a notepad app on any device where each note title appears and clicking on it takes you to that note.
public class MainActivity extends Activity implements OnClickListener {
private Button add_new_dictionary;
// Database helper
private DatabaseHelper db;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// db setup
db = new DatabaseHelper(getApplicationContext());
// get all dictionaries
List<db_dictionary> allDictionaries = db.getAllDictioniaries();
for (db_dictionary dictionary_found : allDictionaries) {
// create new view for each dictionary name include id and make it
// dynamic and include onclick to take to dictionary_view screen
Button dictionary_button = new Button(this);
}
add_new_dictionary = (Button) findViewById(R.id.add_new_dictionary);
}
#Override
public void onClick(View v) {
if (v == add_new_dictionary) {
Intent add_new_dictionary_intent = new Intent(MainActivity.this,
add_new_dictionary.class);
startActivity(add_new_dictionary_intent);
}
}
}
To re-iterate the question: How do I go about dynamically taking rows from my db and adding it to my layout dynamically based on how many results are returned from the query? (However, the rows should be able to point to a new screen with the dictionary id)
All views in android can implement the OnClickListener interface. So no, it doesn't HAVE to be a button.
As you've decided to use the activity to handle this then you need to tell your code to pass the event to your implementation wihin your activity.
// create new view for each dictionary name include id and make it
// dynamic and include onclick to take to dictionary_view screen
Button dictionary_button = new Button(this);
dictionary_button.setOnClickListener(this);
A trick I use to store information is the setTag method which would allow you to retrieve the correct reference during your onClick:
dictionary_button.setTag(some_record_id);
Then retrieve it later:
#Override
public void onClick(View v) {
if (v == add_new_dictionary) {
Intent add_new_dictionary_intent = new Intent(MainActivity.this,
add_new_dictionary.class);
startActivity(add_new_dictionary_intent);
}
else (
Object tag = v.getTag();
//now launch the detail activity using the data from the tag
}
}
You should really look into ListAdapters and cursors to do this properly, but this method should get you going for now
If you need to pick data from a db and show it as a list (getting click events) you should probably look into CursorAdapter and ListView
http://developer.android.com/reference/android/widget/CursorAdapter.html
http://developer.android.com/reference/android/widget/ListView.html
You can fins many examples on the web on how to use a cursoradapter and the listview
I am using cardslib by gabrielemariotti. I am using an expandable ListView Card. I have a custom layout inside it, and on of the views inside the ListView Card is an image view, name remove. I am planning to use the Remove imageview as a trigger to dynamically remove a card from the list.
The problem is, there are two layout involved on a Card, the header and the main content. The Cards by the way are contained inside the ListView. One of the layout, is the remove imageview is located. The code below can remove a Card by clicking it. CardListView
CardHeader header = new CardHeader(getActivity());
//Set the header title
header.setTitle(titleHeader);
//Set visible the expand/collapse button
header.setButtonExpandVisible(true);
header.setOtherButtonClickListener(new CardHeader.OnClickCardHeaderOtherButtonListener() {
#Override
public void onButtonItemClick(Card card, View view) {
Toast.makeText(getActivity(), "Drawable", Toast.LENGTH_SHORT).show();
if(mCardArrayAdapter!=null){
mCardArrayAdapter.remove(card); //It is an example.
}
}
});
The code below will trigger a Toast when the imageview Remove is clicked,
#Override
public void setupInnerViewElements(ViewGroup parent,View view){
//Add simple title to header
if (view!=null){
TextView mTitleView=(TextView) view.findViewById(R.id.card_header_inner_simple_title);
if (mTitleView!=null)
mTitleView.setText(mTitle);
ImageView img = (ImageView)view.findViewById(R.id.cancel);
img.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getContext(), "Clicked", Toast.LENGTH_SHORT).show();
}});
}
}
I wanted to combine the two. The library
Cardslib works like first you add all your cards to one cards array and then you initalize your CardArrayAdapter with your cards array.
It's same as using ListView. So deleting one item is also same as delete from your ListView.
1) On any event remove the card from cards array.
2) Call notifyDataSetChanged(); on your CardArrayAdapter object. It will update the Cards list and remove the deleted card from the view as well.
In my app I have 4 main buttons, plus another 2. These 4 buttons are declared at the beginning of the main activity.
Button button1, button2, button3, button4;
button1 = (Button) findViewById(R.id.button1);
button1.setTag("blue");
(each button has a tag and is set the same way as button1)
The four buttons I want to cycle through different colours when they are pressed. I manage this by;
public void button1(View v) {
if ("blue".equals(button1.getTag())) {
button1.setBackgroundDrawable(getResources().getDrawable(
R.drawable.brown));
button1.setTag("brown");
} else if ("brown".equals(button1.getTag())) {
button1.setBackgroundDrawable(getResources().getDrawable(
R.drawable.red));
button1.setTag("red");
} else if //...etc
This works all well and good until I press any of the two buttons, an example code of one of the buttons
public void back(View v) {
setContentView(R.layout.main);
t = new TextView(this);
t = (TextView) findViewById(R.id.textView1);
t.setText("");
}
Once I press any of the two buttons the colours change back to the original drawable set in the xml file
android:background="#drawable/blue"
Now when I press the 4 main buttons the drawable does not change, but I definitely know that it is getting re-tagged, so why won't it change the drawable after I press the button?
If your 'two buttons' onClick handler makes a call to Activity.setContentView(int), then all of the buttons will reset to how they are specified in the original XML layout. New views will be inflated and these will not have a tag (you do not seem to be re-setting the tags after the call to setContentView). A null tag will not match any of your colour strings and so your buttons will not cycle their background.
If you want to maintain the views how they were, then do not reset the content view of the Activity. In most cases, setContentView is only called once per lifetime of an Activity, although obviously there can be a few exceptions.
I have a button that I have created in code, which has a listener for Click events. Every time that the button is clicked, it should generate another button and add it below the original button. However, no matter how many times I click the first button, it will only add a dynamic button once, and not add any more.
Here is my coding:
public class DynaminControlActivity extends Activity {
private RelativeLayout container;
private int mainIdCnt = 0;
private int mainId = 100;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
createMainButton();
}
public void createMainButton() {
container = (RelativeLayout) findViewById(R.id.workLayout);
Button b = new Button(this);
b.setId(mainIdCnt + mainId);
CharSequence text = "Main +";
b.setText(text);
container.addView(b);
if (mainId > 0) {
mainId++;
}
b.setOnClickListener((new View.OnClickListener() {
public void onClick(View v) {
createDynamicButton();
}
}));
}
public void createDynamicButton() {
container = (RelativeLayout) findViewById(R.id.workLayout);
Button b = new Button(this);
CharSequence text = "Main +";
b.setText(text);
RelativeLayout.LayoutParams relLayout = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relLayout.addRule(RelativeLayout.BELOW, mainIdCnt + mainId);
container.addView(b, relLayout);
if (mainId > 0) {
mainId++;
}
}
A few things...
If your main layout is a LinearLayout, you shouldn't need to add a rule to indicate that the button should appear underneath the existing button - it will automatically be added to the very bottom (vertical alignment) or very right (horizontal alignment) of the layout.
All your buttons have the same text. Are you certain that you're clicking the first button each time? I note that only your first button has a listener on it, so if you're accidentally clicking one of the other buttons then nothing will happen.
If you're intending to add multiple buttons, it will quickly expand to be larger than the screen size, so you should make sure that your main layout is within a ScrollView so that you can see all the buttons you add
The call to setId() might be stuffing around with the internal workings of Android. Rather than setting an ID, you should let Android generate the ID automatically, and just retrieve that value if you need to reference it.