Android Studio Activity Layout Changing - android

I need help in changing layout of Activity in Android Studio when I click on button.
But I don't want to open new activity I want to change layout and design completely on the current activity by clicking button. Is it possible ?

you can inflate another layout during button click:
public class LayoutInflateDemo extends Activity {
private LinearLayout layoutToAdd;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_existed);
layoutToAdd = (LinearLayout) findViewById(R.id.existedlayout);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater inflater = LayoutInflater
.from(getApplicationContext());
View view = inflater.inflate(R.layout.layout_toinfliate, null);
//Add the inflated layout to your existing view
layoutToAdd.addView(view);
}
});
}
}
layout to inflate Xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/mytext"
android:text="Inflated layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
And this is your existing layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/existedlayout"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Clicck to inflate view"
android:id="#+id/button"
/>
</LinearLayout>

Yes it is possible by using setContentView() method. But you have to make sure that you didn't use any view (e.g. view by id) that is not available on current layout.

Best way is Fragments and FragmentActivity https://developer.android.com/guide/components/fragments.html

Related

Load a layout from XML and add views

I'm trying to load an existing layout from XML and then create dynamically a button and a Circle, the reason which I cannot include them onto the XML is because the purpose is to create Circles dynamically, actually the following code is a snippet of what I want to create.
I know the way I am doing this (adding the layout) is wrong, but after reading a lot of Internet content I failed to did it by myself, because that I request help.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_etest);
LayoutInflater inflater;
inflater = this.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout layout = (LinearLayout)
inflater.inflate(R.layout.activity_etest ,
null);
LinearLayout viewGroup = layout;
Button b1 = new Button(this);
b1.setText("test");
viewGroup.addView(b1);
viewGroup.addView(new Circle(this));
}
}
And my class Circle, which extends from View and its method onDraw() consists of:
... onDraw(){
canvas.drawCircle(x, y, radius, paint);
}
The parameters of drawCircle are not rellevant to this question. I have defined them elsewhere.
I also add the XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button12"
android:layout_gravity="center_horizontal" />
</LinearLayout>
create one view group like linearlayout in your xml and using its instance add your dynamic views in that.
activity_etest.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/viewg" xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
activity
setContentView(R.layout.activity_etest);
Linearlayout viewgroup = (LinearLayout)findViewById(R.id.viewg);
Button b1 = new Button(this);
b1.setText("test");
viewGroup.addView(b1);
viewGroup.addView(new Circle(this));

Fragment is not fully covering parent activity

There is Button in Activity when we click on Button fragment's view overlaps Activity's view .
When Button is clicked it is forwarded to Fragment. Problem is it overlaps with Button.
In MainActivity i have created Button and set listener on that when user click on Button it is forwarded to fragment file which contain only textview.
MainActivity.java:
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
fragment fr = new fragment();
getSupportFragmentManager().beginTransaction().add(R.id.parent_frame, fr).commit();
}
});
}
}
fragment.java
public class fragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, container, false);
return view;
}
}
fragment.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/detailsText"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:text="Default Text ggggggg"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="30dip" />
</RelativeLayout>
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press to update"
/>
<fragment
class="com.example.demo3.fragment"
android:id="#+id/parent_frame"
android:layout_alignParentTop="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
Why aren't you using the Activity merely as a container, and use Fragments for ALL ui elements, including the button you are clicking to change between them?
Something like this: Programatically switching between Fragments
this is unique thought and i appreciate it.if you want to do this just follow below steps:
1) in your activity main take framelayout with height width system fit(matchparent) and id is parent_frame
2) take button inside it and when you click on it set button visible to gone
3) then you can add fragment to this framlayout
Make sure in your activity_main must contain framlayout with matchparent

Get element from Sliding Menu

I defined a layout view in a .xml file called menu_list_slide_lateral.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linear_menu_slide"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/btnSliBebe"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/bebe" />
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/foto_diario" />
<Button
android:id="#+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/consejos" />
<Button
android:id="#+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/ajustes" />
</LinearLayout>
Im creating the SlidingMenu from code:
setBehindContentView(R.layout.menu_list_slide_lateral);
setSlidingActionBarEnabled(true);
slideMenu = getSlidingMenu();
slideMenu.setMode(SlidingMenu.LEFT);
slideMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
slideMenu.setShadowWidthRes(R.dimen.shadow_width);
slideMenu.setBehindOffset(100);
slideMenu.setFadeDegree(0.35f);
And mi activity extends from SlidingFragmentActivity:
public class TimelineActivity extends SlidingFragmentActivity
It shows perfectly the menu, but i want to do some actions when the user chooses an option from the menu:
For example, i want to open another activity when i choose the "Bebe" option.
I tried to set a onClick event to that button but it doesn't seem to work, it makes nothing:
inflater = getLayoutInflater();
item = inflater.inflate(R.layout.menu_list_slide_lateral, null);
btnSliBebe = (Button) item.findViewById(R.id.btnSliBebe);
btnSliBebe.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
Log.e(TAG, "boton bebe");
}
});
How can i access that buttons and asign them a event?
Thanks!
What you should to is to create separate fragment for slide menu which will contain your layout R.layout.menu_list_slide_lateral and will handle actions. This is easy to do.
Now when you have this fragment you need to insert it into your slide activity.
setBehindContentView which you used is placeholder for menu. So create simple layout which will hold your menu fragment. E.g. call it R.layout.menu_frame and it should be like this:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/menu_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Then in your sliding activity set R.layout.menu_frame as behindContentView and add your menu fragment into this view.
public class BaseSlidingActivity extends SlidingFragmentActivity {
protected MenuFragment mSlidingMenuFragment;
private SlidingMenu mSlidingMenu;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set the Behind View
setBehindContentView(R.layout.menu_frame);
//if we create new menu - create new MenuFragment and insert it into
//menu_frame
if (savedInstanceState == null) {
FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
mSlidingMenuFragment = new MenuFragment();
t.replace(R.id.menu_frame, mSlidingMenuFragment);
t.commit();
}
//if activity was restored(e.g. on orientation change) find it in fragment
//manager
else {
mSlidingMenuFragment = (MenuFragment) this.getSupportFragmentManager().findFragmentById(R.id.menu_frame);
}
// customize the SlidingMenu
mSlidingMenu = getSlidingMenu();
mSlidingMenu.setShadowWidthRes(R.dimen.shadow_width);
mSlidingMenu.setShadowDrawable(R.drawable.shadow);
mSlidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
mSlidingMenu.setFadeDegree(0.35f);
mSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
}
I hope it helps. You can browse through source code of example at github slidinfmenu example

make a frame with components visible only when needed in android

Well what i wanted to do was create an initial layout which will have a toggle button and on clicking the toggle but it should make a frame visible which will have a few buttons or textviews.
Does anybody know how to do this in Android 2.2??
You can use the visibility attribute on a view to control whether it is visible or not. Here's a small example that should do what you're looking for.
The main Activity:
public class DynamicLayoutTestActivity extends Activity {
private ToggleButton toggleButton;
private View possiblyHiddenView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
possiblyHiddenView = (View) findViewById(R.id.possiblyHiddenView);
toggleButton.setOnCheckedChangeListener(toggleButtonOnCheckedChangeListener);
}
private final CompoundButton.OnCheckedChangeListener toggleButtonOnCheckedChangeListener
= new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
possiblyHiddenView.setVisibility(View.VISIBLE);
} else {
possiblyHiddenView.setVisibility(View.INVISIBLE);
}
}
};
}
The layout file, main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ToggleButton
android:id="#+id/toggleButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textOff="Show"
android:textOn="Hide" />
<LinearLayout
android:id="#+id/possiblyHiddenView"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible"
>
<TextView
android:text="Stuff that could be hidden."
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
If you don't want the hidden view to take up any space, use visibility gone instead of invisible.
Hope this helps!
If you're developing for Android 3.0+, look into fragments.

How to use the xml setting in a view of a activity?

I want to show two views in one activity. If I clicked on button in the first view I want to see the second and other way round.
The views should not have the same size as the screen so I want e.g. to center it, like you see in first.xml.
But if I add the views with
addContentView(mFirstView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
the views are not centered. They are shown at top left.
How can I use the xml settings to e.g. center it?
first.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:background="#drawable/background"
android:layout_gravity="center"
android:minWidth="100dp"
android:minHeight="100dp"
android:paddingBottom="5dp"
>
<LinearLayout android:id="#+id/head"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton android:id="#+id/first_button"
android:src="#drawable/show_second"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null" />
</LinearLayout>
second.xml same as first.xml but with
<ImageButton android:id="#+id/second_button"
android:src="#drawable/show_first"
... />
ShowMe.java
public class ShowMe extends Activity {
View mFirstView = null;
View mSecondView = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initFirstLayout();
initSecondLayout();
showFirst();
}
private void initFirstLayout() {
LayoutInflater inflater = getLayoutInflater();
mFirstView = inflater.inflate(R.layout.first, null);
getWindow().addContentView(mFirstView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
ImageButton firstButton = (ImageButton)mMaxiView.findViewById(R.id.first_button);
firstButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ShowMe.this.showSecond();
}
});
}
private void initSecondLayout() {
// like initMaxiLayout()
}
private void showFirst() {
mSecondView.setVisibility(View.INVISIBLE);
mFirstView.setVisibility(View.VISIBLE);
}
private void showSecond() {
mFirstView.setVisibility(View.INVISIBLE);
mSecondView.setVisibility(View.VISIBLE);
}}
Hope someone can help.
Thanks
Why don't you use setContentView(R.layout.yourlayout)? I believe the new LayoutParams you're passing in addContentView() are overriding those you defined in xml.
Moreover, ViewGroup.LayoutParams lacks the layout gravity setting, so you would have to use the right one for the layout you're going to add the view to (I suspect it's a FrameLayout, you can check with Hierarchy Viewer). This is also a general rule to follow. When using methods that take layout resources as arguments this is automatic (they might ask for the intended parent).
With this consideration in mind, you could set your layout params with:
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(/* wrap wrap */);
lp.setGravity(Gravity.CENTER);
addContentView(mYourView, lp);
But I would recommend setContentView() if you have no particular needs.
EDIT
I mean that you create a layout like:
~~~/res/layout/main.xml~~~
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="....."
android:id="#+id/mainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
then in your onCreate() or init...Layout():
setContentView(R.layout.main);
FrameLayout mainLayout = (FrameLayout)findViewById(R.id.mainLayout);
// this version of inflate() will automatically attach the view to the
// specified viewgroup.
mFirstView = inflater.inflate(R.layout.first, mainLayout, true);
this will keep the layout params from xml, because it knows what kind it needs. See reference.

Categories

Resources