Setting OnClickListener in a Fragment - android

I've looked at other questions on StackOverflow with setting OnClickListener on Fragment and I tried different solutions and it didn't work. All stated I'm supposed to set getActivity(), a fragment activity but it doesn't work. How do I set an OnClickListener in a fragment then?

/*declare variable for the button...
Declare it like this before the onCreateView method;
*/
private Button commandButton;
//Inside the onCreateView method use the below code...
commandButton = (Button) rootView.findViewById(R.id.NEXTPG);
commandButton.setOnCickListener(new OnClickListener(){
#override
public void onClick(View view){
//your commands here
}
}

you can return also layout in OnCreateView method, so use it my code are given below.
final RelativeLayout mRelativeLayout = (RelativeLayout) inflater.inflate(R.layout.activity_live_tabber,container, false);
Button mButton = (Button) mRelativeLayout.findViewById(R.id.NEXTPG);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// here you set what you want to do when user clicks your button,
}
});
return mRelativeLayout;

Related

How to move a View from one ViewGroup to another

I have LinearLayout1, LinearLayout2 and a Button in MainActivity. When I click the Button, I want it to jump from LinearLayout1 to LinearLayout2. How can I do that?
You can do like this :
LinearLayout mLinearLayout1 = (LinearLayout)findViewById(R.id.linearlayout_1);
LinearLayout mLinearLayout2 = (LinearLayout)findViewById(R.id.linearlayout_1);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mLinearLayout1.setVisibility(View.GONE);
mLinearLayout2.setVisibility(View.VISIBLE);
}
});
Thanks to all of you specially Mike M
i get my answer with:
public class MainActivity extends AppCompatActivity {
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
final LinearLayout mLinearLayout1 = (LinearLayout)findViewById(R.id.liner1);
final LinearLayout mLinearLayout2 = (LinearLayout)findViewById(R.id.liner2);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mLinearLayout1.removeView(button);
mLinearLayout2.addView(button);
}
});
}
}
If you want one of the many different views (not LinearLayouts) to be displayed right on the main activity (may depend on a condition or a state engine or time based interval), you probably can use ViewFlipper.
You can hide and show one or the other, for example:
LinearLayout mLinearLayout1 = (LinearLayout) findViewById(R.id.linearlayout_1);
LinearLayout mLinearLayout2 = (LinearLayout) findViewById(R.id.linearlayout_1);
mLinearLayout1.setVisibility(View.GONE);
mLinearLayout2.setVisibility(View.VISIBLE);
But If you want to show an hide some views with diifferent behavior I recommend you using Fragments.

Use a button to add other buttons, editText etc.

I need to be able to add buttons to a layout using an "add" button. The problem is that I need each button to have an OnClickListener()/onClick method. I was thinking every time the "add" button is pressed then i would add a new button to an array but im not sure add the listener and implement an onClick method for each button I create.
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SmartChronometer chrono = (SmartChronometer) findViewById(R.id.chrono);
final Button start = (Button) findViewById(R.id.button2);
start.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (chrono.isRunning())
chrono.pause();
else {
chrono.chronoStart();
}
}
});
}
I need to add chronomoter,button and listeners every time I click an "Add" button.
set all the clicklisteners as you wich!
call findViewById(R.id.btnSecond).setVisibility(View.GONE); on creat, then when clickin the first button
Button btnSecond;
...
public void onClick(View v) {
findViewById(R.id.btnSecond).setVisibility(View.VISIBLE);
if (btnSecond.getVisibility() == View.VISIBLE); {
findViewById(R.id.btnThird).setVisibility(View.VISIBLE);}
}
this way you can put all your information in the java file and all buttons in xml, but they will be hidden until click.
This is one way, other answer my come. Good Luck :)
implements OnClickListener
Button add = (Button) findViewById (R.id.addButton);
add.setOnClickListener (this);
List<Button> buttons = new ArrayList <Button>();
for (int i = 0; i < buttons.size (); i++){
buttons.get (i).setOnClickListener (this);
}
#Override
public void onClick (View v){
for (int i = 0; i < buttons.size (); i++){
if (v.getId () == buttons.get (i).getId ()){
// do stuff you want
}else if (v.getId() == R.id.addButton){
//add button
}
}
}
Hope this will work, didnt test it.

performClick() doesn't activate selector

I created a layout with a radiogroup cotaining two buttons. Then i got a drawable selector.
I applied the selector with android:background and withandroid:button to remove the little circles.
In the onCreate method i press one button via button.performClick() but the layout of the button doesn't change. Even if i call button.setChecked(true) it won't change.
Actually it works when the fragment is first created. But if i press the second button, load another fragment and come back with the backbutton, the second button is still pressed.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View f = inflater.inflate(R.layout.fragment_season, container,
false);
final RadioButton bt = (RadioButton) f.findViewById(R.id.bu_season_tabelle);
final RadioButton bs = (RadioButton) f.findViewById(R.id.bu_season_spielplan);
bt.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setTabelle(f);
v.setClickable(false);
bs.setClickable(true);
}
});
bs.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setSpielplan(f);
v.setClickable(false);
bt.setClickable(true);
}
});
bt.performClick();
return f;
}
Would be nice if someone can help me.
I found the solution:
Instead of calling button.performClick() in onCreateView() i have to call it in onStart()

PopupWindow not triggering sytem context dialog on EditText long-press

Sorry if the title was a bit vague.
I'm developing an app on Freelancer and I almost have it finished except for a complaint from the customer after some testing.
I use a PopupWindow in place of a dialog to edit contextual settings, if that makes any sense. I don't want to be too specific and risk giving the app concept away, which I'm sure the customer wouldn't be too pleased about.
The PopupWindow is given a Content View of a layout inflated from XML. In that layout are several EditText widgets. The issue is that those EditTexts will not trigger the default contextual dialog on long press that presents options for text/IME selection, and cut/copy/paste.
I saw a similar question trying to get the TouchTrigger or something and it not working without setBackgroundDrawable(), which I've tried with a simple new ColorDrawable(). It still doesn't work.
Is there any easy way to trigger the system-default long-press dialog in an OnLongPressListener, or will I have to move Heaven and Earth to implement it myself? Because if that's the case, I'll just write a Fragment for it and swap it out in a transaction. I know that'll work.
The relevant code:
Inside the initiating fragment:
RulesDialog dialog;
PopupWindow window;
public void showAddRuleDialog(){
dialog = new RulesDialog();
View view = getView();
window = new PopupWindow(dialog.initViews(this, null), view.getWidth(), view.getHeight(), true);
window.setBackgroundDrawable(new ColorDrawable());
dialog.setRulesDialogListener(new rulesDialogListener(){
#Override
public void onSave(ViewHolder holder) {
addRule(holder);
window.dismiss();
}
#Override
public void onCancel() {
window.dismiss();
}});
int[] location = {0,0};
view.getLocationOnScreen(location);
window.showAtLocation(view, 0, location[0], location[1]);
In RulesDialog:
public class ViewHolder{
public ViewHolder(View dialogView){
name = (TextView) dialogView.findViewById(R.id.name);
response = (TextView) dialogView.findViewById(R.id.response);
senders = (TextView) dialogView.findViewById(R.id.senders);
sendersAdd = (Button) dialogView.findViewById(R.id.sendersAdd);
sendersEdit = (Button) dialogView.findViewById(R.id.sendersEdit);
timeFrom = (TextView) dialogView.findViewById(R.id.from);
timeFromEdit = (Button) dialogView.findViewById(R.id.timeBeforeEdit);
timeTo = (TextView) dialogView.findViewById(R.id.to);
timeToEdit = (Button) dialogView.findViewById(R.id.timeAfterEdit);
keywords = (TextView) dialogView.findViewById(R.id.keywords);
matchCase = (CheckBox) dialogView.findViewById(R.id.matchCase);
matchAlone = (CheckBox) dialogView.findViewById(R.id.matchAlone);
matchPlural = (CheckBox) dialogView.findViewById(R.id.matchPlural);
cancel = (Button) dialogView.findViewById(R.id.cancel);
save = (Button) dialogView.findViewById(R.id.save);
}
TextView name;
TextView response;
TextView senders;
Button sendersAdd;
Button sendersEdit;
TextView timeFrom;
Button timeFromEdit;
TextView timeTo;
Button timeToEdit;
TextView keywords;
CheckBox matchCase;
CheckBox matchAlone;
CheckBox matchPlural;
Button cancel;
Button save;
}
Activity activity;
ViewHolder holder;
Fragment fragment;
public View initViews(Fragment mFragment, Rule rule){
fragment = mFragment;
activity = fragment.getActivity();
View dialogView = LayoutInflater.from(activity).inflate(R.layout.rules_dialog, null);
holder = new ViewHolder(dialogView);
final TextView senders = holder.senders;
holder.sendersAdd.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
showContacts();
}});
holder.sendersEdit.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
editSenders(senders);
}
});
final TextView timeFrom = holder.timeFrom;
holder.timeFromEdit.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
showTimePickerDialog(timeFrom);
}
});
final TextView timeTo = holder.timeTo;
holder.timeToEdit.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
showTimePickerDialog(timeTo);
}
});
holder.cancel.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
mListener.onCancel();
}});
holder.save.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
mListener.onSave(holder);
}});
if(rule == null)
rule = new Rule();
holder.name.setText(rule.name);
holder.response.setText(rule.response);
holder.senders.setText(rule.senders.toString());
holder.senders.setTag(rule.senders);
holder.keywords.setText(rule.keywords);
holder.matchCase.setChecked(rule.matchCase);
holder.matchAlone.setChecked(rule.matchAlone);
holder.matchPlural.setChecked(rule.matchPlural);
holder.timeFrom.setTag(rule.timeFrom);
holder.timeFrom.setText(Rules.formatTime(rule.timeFrom));
holder.timeTo.setTag(rule.timeTo);
holder.timeTo.setText(Rules.formatTime(rule.timeTo));
return dialogView;
}
So I tried rewriting RulesDialog as a fragment, and it didn't work out too well. Had issues with making Fragment Transactions work right when called from the Fragments they're operating on.
(I know this isn't the point to fragments. I'm not really aiming to write a completely modular app right now. I just want to come out with a product the customer will be happy with.)
I ended up rewriting RulesDialog as an Activity instead, and using startActivityForResult() from the calling fragment. Then passing the edited data back with setResult(). It all works nicely in concert.

multiple setOnClickListeners for a single process

In my App I have a line showing an (ImageButton)icon and a (textView)title of an audio file to play and I use the setOnClickListener for an ImageButton to initiate a process that plays the file. I would also like to use the (textView)title as a clickable item to begin the same process.
I could simply duplicate all of the functionality in both setOnClickListeners but that does not seem to be the most efficient way to do it.
Now I am new so is there such a thing as
ImageButton.setOnClickListener() || textView.setOnClickListener() {
.
.
.
}
Basically if the ImageButton is clicked or the textView is clicked do this procedure.
I know the above syntax is not correct but it gives you an idea of what I want to do
Have the class that has both these elements ImageButton and textView implement the OnClickListener. OnClickListener is an interface that has the method onClick(View v) which will have the click implementation for both these elements. Then you can use imageButton.setOnClickListener(this) and textView.setOnClickListener(this).
Example Code:
public MyClass extends Activity implements OnClickListener {
ImageButton imageButton;
TextView textView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
imageButton = (ImageButton) findViewById(R.id.btn);
textView = (TextView) findViewById(R.id.txt);
imageButton.setOnClickListener(this);
textView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int viewId = v.getId() ;
if(viewId == R.id.btn || viewId == R.id.txt){
//common implementation of click event
}
}
}
Hope this explanation helps.
ImageButton iv=(ImageButton)findViewById(R.id.imagebutton1);
TextView tv=(TextView)findViewById(R.id.textview1);
iv.setOnClickListener(OnClick);
tv.setOnClickListener(OnClick);
add this in OnCreate() method and after that add this method as shown below
private OnClickListener OnClick=new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.imagebutton1:{
// do here code what u want on imagebutton click
break;}
case R.id.textview1:{
// do here code what u want on textview click
break;}
}
}
}
};
Put both the imagebutton and textview in a layout and give it an ID. Then implement the onClickListener for the layout, by this way you need to write the code only once.

Categories

Resources