How to move a View from one ViewGroup to another - android

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.

Related

Setting OnClickListener in a Fragment

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;

How to identify which button is being dragged

Am new to Drag and drop in android and I have three buttons and i want to know which button is being dragged because I want to do different tasks on each one of them.
Thank you in advance.
Your activity must be declared like
YourActivity extends Activity implements View.OnTouchListener
You declare buttons and set tags on them in oncreate as
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button button = (Button) findViewById(R.id.button_send);
button.setTag("1");
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do something in response to button click
}
});
}
In the onTouch function you can get the tag of the view which is touched
#Override
public boolean onTouch(final View view, final MotionEvent event) {
Log.d("TAG",view.getTag());
so you will know which button is dragged

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.

Listener(s) for array of buttons

For my own practice I am a creating an array of 3 buttons in the instance field and i would like all of them to have setOnClickListeners,which allow each button to change the BackGround Color of a text View.Can any person please guide me towards the right direction.Here is my code:
public class MainActivity extends Activity {
Button b = {(Button)findViewById(R.id.button1),
(Button)findViewById(R.id.button2),
(Button)findViewById(R.id.button3),};
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView1);
for(int i=0; i < b.length;i++){
b[i].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(butt[0].isPressed()){
tv.setBackgroundColor(Color.BLACK);
}
if(b[1].isPressed()){
tv.setBackgroundColor(Color.BLUE);
}
if(b[2].isPressed()){
tv.setBackgroundColor(Color.RED);
}
}
});
}
}
}
You aren't declaring an Array for your Buttons. I'm not sure what this would do or if it would even compile but I wouldn't think so
Button b = {(Button)findViewById(R.id.button1),
(Button)findViewById(R.id.button2),
(Button)findViewById(R.id.button3),};
Change it to
Button[] b = {(Button)findViewById(R.id.button1),
(Button)findViewById(R.id.button2),
(Button)findViewById(R.id.button3),};
Also, this code has to go after setcontentView() or you will get a NPE since your Buttons exist in your layout and your layout doesn't exist until you inflate it by calling setContentView().
You can declare your Array before onCreate() but you can't initialize them until you inflate your layout
So you can do something like this
public class MainActivity extends Activity {
Button[] b = new Button[3]; //initialize as an array
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = {(Button)findViewById(R.id.button1),
(Button)findViewById(R.id.button2),
(Button)findViewById(R.id.button3),}; //add buttons AFTER calling setContentView()
...
Edit Since #pragnani deleted his answer I will edit with a bit of it that is a good idea
You can simplify your logic by choosing which Button was clicked with a switch by doing something like below in your for loop
b[i].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) { / v is the button that was clicked
switch (v.getId()) //so we get its id here
{
case (R.id.button1):
tv.setBackgroundColor(Color.BLACK);
break;
case (R.id.button2):
tv.setBackgroundColor(Color.BLUE);
break;
case (R.id.button3):
tv.setBackgroundColor(Color.RED);
break;
}

Why to inflate layout into another layout in Android?

I'm new in Android development and I wanted to make application that has header, body and footer and by clicking on one of the buttons in footer some layout will be loaded into body. I used some kind of "MasterPage" as described here.
When the button is pressed neither new_exercise layout nor exercises layout is loaded. Why? Maybe instead of all of this I should use any kind of tabs? Or maybe I can't inflate layout and should create new activity?
Here the code of the BaseActivity and NewExercise activity:
public class BaseActivity extends Activity{
LinearLayout linBaseBody;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.base_layout);
linBaseBody = (LinearLayout)findViewById(R.id.base_body);
initButtons();
}
#Override
public void setContentView(int id) {
LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(id, linBaseBody);
}
private void initButtons()
{
Button btn1 = (Button) findViewById(R.id.newEx);
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
setContentView(R.layout.new_exercise);
}
});
Button btn2 = (Button) findViewById(R.id.showAllEx);
btn2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
setContentView(R.layout.exercises);
}
});
}
public class NewExercise extends BaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.new_exercise);
}
}
public class Exercises extends BaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.exercises);
}
}
How your code is written, it would make more sense to use a new Activity. However, If you wanted to keep all of the view in one Activity, you could walk through all of your layouts calling mLayout.setVisible(View.VISIBLE); or you could use ViewStubs.
As to answer your question, why, what you are doing is adding the view (and their layouts) directly to your already created and inflated content view (the one you created in onCreate). You will need to clear the Activities contentView first to see the changes you are making with the button.

Categories

Resources