How to change layout android

android: how to change layout on button click?

I have to following code for selecting layout on button click.

When I click the «AppView» button, the layout changes, but when I click the «DownloadView «button, nothing happens.

This link says that I have to start a new activity.

But I don’t know how to use the code there of intent to start new activity, will a new file be added?

EDIT: I have my code on the new activity:

but it does not work, it force closes, the logcat says:

8 Answers 8

Note : and then you should declare all your activities in the manifest .xml file like this :

EDIT : update this part of Code 🙂 :

I would add an android:onClick to the layout and then change the layout in the activity.

So in the layout

Then in the activity add the following:

I think what you’re trying to do should be done with multiple Activities. If you’re learning Android, understanding Activities is something you’re going to have to tackle. Trying to write a whole app with just one Activity will end up being a lot more difficult. Read this article to get yourself started, then you should end up with something more like this:

I know I’m coming to this late, but what the heck.

I’ve got almost the exact same code as Kris, using just one Activity but with 2 different layouts/views, and I want to switch between the layouts at will.

As a test, I added 2 menu options, each one switches the view:

Note, I’ve got one Activity class. This works perfectly. So I have no idea why people are suggesting using different Activities / Intents. Maybe someone can explain why my code works and Kris’s didn’t.

You wanted to change the layout at runtime on button click. But that is not possible and as it has been rightly stated above, you need to restart the activity. You will come across a similar problem when u plan on changing the theme based on user’s selection but it will not reflect in runtime. You will have to restart the activity.

The logcat shows the error, you should call super.onCreate(savedInstanceState) :

Читайте также:  Tele2 личный кабинет android

First I would suggest putting a Log in each case of your switch to be sure that your code is being called.

Then I would check that the layouts are actually different.

Источник

Change Android layout from Relative to Linear

I just started creating android apps, but i have a problem changing the layout , every time when i create new android project , it gives me RelativeLayout instead of LinearLayout , (well, basically i am following the book instruction, but it doesn’t teach me how to change it) I assume there must be a default setting so that i can change the Relativelayout to LinearLayout .

6 Answers 6

Replace the following tags :

In addition, you should remove the following from your TextView attributes :

because they apply for the relative layout and not the linear layout.

To change the Parent of your layout XML file follow these steps:

    Right click your layout folder in res folder from project folder in eclipse. (

)

  • Choose option New -> Other. . Refer picture:
  • Choose Android Layout XML file option from here.
  • Press Next and select LinearLayout as Root Element and give File Name . Press Finish
  • Now your Layout XML has the root element as LinearLayout
  • If you are using Eclipse, just: Rigth click, change layout and select the layout of your choice.

    You just need to change this tag with
    , and don’t forget to close it with same Layout

    In Latest versions of android studio i believe this will be the way to do it.

    Go to res-> layout folder and Right Click on it and you get a sidebar asking for what type of file you want to create

    Go to -> Edit File Templates option.

    Источник

    Android change layout dynamically

    I have situation where I want to change layout of activity after 3 seconds. Ho can I do that? For example application starts with splashscreen that will run for 3 seconds then automatically switch layout to application’s first screen layout. This should happen in the same activity, any ideas?

    4 Answers 4

    I did this using only one xml layout. I just put an extra RelativeLayout in it that represents my intro screen then I use a fadeOut Animation on it and then call .setVisibility(View.GONE).

    This is part of my main.xml layout file

    Then inside my activity I have this:

    You could make this run after 3 seconds by putting the startAnimation(), and the setVisibility inside of a runnable and using postDelayed() as markus mentioned. Do consider doing some work while this intro layout is on the screen though, so its not just a 3 second delay for the user. Perhaps check to see if the running version of the application is the current version or not.

    Читайте также:  Android php editor app

    EDIT: You’ll need to add the fadout.xml file to /res/anim/ (create the anim directory if it doesn’t exist). Here is an example.

    fadeout.xml

    Just use the ViewSwitcher,using this one can switch between any number of layouts within the application without having to do any setContentView.

    maybe you could use a postDelayed()-call to execute a runnable that will load a new xml file by calling setContentView(R.xml.anotherxml).

    This leaves me to add to adnorid’s post, that ViewSwitcher itself is only able to switch between 2 layouts/views. This is simply because it’s hardcoded to only 2 layouts/views without deeper meaning, as far as i know. So i built my own MultipleViewSwitcher aka MVS, base on the original ViewSwitcher.java from android source. The only changes that had to be made are:

    1. Provide the MVS with own
      • boolean mFirstTime and
      • int mWhichChild
    2. Correct hardcoded into dynamic handling of an arbitrary number of views in functions
      • addView(. )
      • getNextView()
      • reset()

    This can all be done in MultipleViewSwitcher.java alone and works like a charm.

    I came accross a bunch of stuff like that where the android sources could be made much more powerful with, many times, near-no-brainer-edits. I’d like to know why that is, but that’s not for this thread here.

    Источник

    How to change layout of spinner in Android

    When O click on this spinner it gives a big dropdown:

    I want a very small view as in the second image. Just like dropdowns in ASP.NET. Like this with width little more reduced.

    I used the following code. Any help to change the view of second image will be appreciated.

    3 Answers 3

    This is a good article : Customizing the Action Bar

    And also you can try this :

    Design you own customized drawable for spinner background and apply to it. For spinnerbackground.xml images you can refer the images from the SDK. recreate the images as per your design requirements

    then for spinner widget apply your custom drawable:

    Edited :

    where popup_background is :

    Design your custom layout for spinner texts as (name : custom_spiner.xml)

    if you want to do this using java code read about PopupWindow

    Follow these steps. I have already implemented this.

    Читайте также:  Обои митсубиси лансер для андроид

    (1) Do not use spinner. Instead use a button with background set to «@android:drawable/btn_dropdown». This button will look exactly same as native spinner. If you want it to look any different use your own resource.

    (2) You need to override dialog class and invoke it on button click. In the constructor of the extended Dialog class, you can use your own layout resource.

    You can also change window look and feel using following code

    (3) You can have a list view in your custom layout and on «OnItemClickListener» of the list, dismiss the dialog and do what you need to do further.

    Источник

    Android: alternate layout xml for landscape mode

    How can I have one layout for landscape and one for portrait? I want to assume extra width and conserve vertical space when the user rotates the phone over sideways.

    6 Answers 6

    By default, the layouts in /res/layout are applied to both portrait and landscape.

    If you have for example

    you can add a new folder /res/layout-land , copy main.xml into it and make the needed adjustments.

    In the current version of Android Studio (v1.0.2) you can simply add a landscape layout by clicking on the button in the visual editor shown in the screenshot below. Select «Create Landscape Variation»

    The layouts in /res/layout are applied to both portrait and landscape, unless you specify otherwise. Let’s assume we have /res/layout/home.xml for our homepage and we want it to look differently in the 2 layout types.

    1. create folder /res/layout-land (here you will keep your landscape adjusted layouts)
    2. copy home.xml there
    3. make necessary changes to it

    Fastest way for Android Studio 3.x.x and Android Studio 4.x.x

    1.Go to the design tab of the activity layout

    2.At the top you should press on the orientation for preview button, there is a option to create a landscape layout (check image), a new folder will be created as your xml layout file for that particular orientation

    I will try to explain it shortly.

    First, you may notice that now you should use ConstraintLayout as requested by google (see androix library).

    In your android studio projet, you can provide screen-specific layouts by creating additional res/layout/ directories. One for each screen configuration that requires a different layout.

    This means you have to use the directory qualifier in both cases :

    • Android device support
    • Android landscape or portrait mode

    Источник

    Оцените статью