0

Hello I am having a problem. I am having three buttons in a view and whenever the view is loaded then all the three buttons are displayed but none of the button is shown in clicked as default. What I want to do is that when that view is loaded I want the first of the three buttons to be shown in clicked form and its data shown in the listview below it.. Can someone please help...

This is my Layout where I have three Buttons and a listview each button has a list to be displayed in listview. This Page is loaded after Splash Screen and what I want is the first button to show the list associated with it in listview when the page loads the each time.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:id="@+id/calender_layout"
        android:layout_width="fill_parent"
        android:layout_height="@dimen/dimention_of_background_of_button"
        android:background="#06960F"
        android:orientation="horizontal"
        android:weightSum="3" >

        <Button
            android:id="@+id/nearby_btn"
            android:layout_width="0dp"
            android:layout_height="@dimen/dimention_of_button"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="@drawable/btn_nearby_selected"/>

        <Button
            android:id="@+id/myevent_btn"
            android:layout_width="0dp"
            android:layout_height="@dimen/dimention_of_button"
            android:layout_gravity="center"
            android:layout_weight="1" 
            android:background="@drawable/btn_myevents"/>

        <Button
            android:id="@+id/fav_btn"
            android:layout_width="0dp"
            android:layout_height="@dimen/dimention_of_button"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="@drawable/btn_favorite"/>
    </LinearLayout>

    <ListView
        android:id="@+id/Eventlist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/calender_layout" 
        android:layout_above="@+id/cal_footer_layout">
    </ListView>

    <LinearLayout
        android:id="@+id/cal_footer_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#06960F"
        android:orientation="horizontal"
        android:weightSum="100" >

        <Spinner
            android:id="@+id/Searchspinner"
            android:layout_width="0dp"
            android:layout_height="@dimen/spinner_selection_height"
            android:layout_gravity="center"
            android:layout_weight="40"
            android:background="#06960F"
            android:spinnerMode="dropdown" />

        <EditText
            android:id="@+id/EnterDataToSearch"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="60"
            android:drawableLeft="@drawable/search_icon"
            android:inputType="text" />
    </LinearLayout>

</RelativeLayout>
Gaurav A Dubey
  • 641
  • 1
  • 6
  • 19

1 Answers1

0

Here is a short example of what I mean from comments.

<RadioGroup android:id="@+id/typeGroup"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:weightSum="3">
            <ToggleButton 
                android:id="@+id/hotBtn"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:checked="true"
                style="@style/HotButtonSmall"/> 
            <ToggleButton 
                android:id="@+id/coldBtn"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                style="@style/ColdButtonSmall"/>
            <ToggleButton 
                android:id="@+id/frozenBtn"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                style="@style/FrozenButtonSmall"/>
   </RadioGroup>    

I have them in a RadioGroup so only one will be checked at a time. My styles just set certain icons for the state they are in. android:checked="true" on the first button sets it to checked when the layout loads.

Remove text (if you want)

In my styles.xml I have

<item name="android:textOff"></item>
<item name="android:textOn"></item>

for each corresponding style so that it doesn't show "Off"/"On" when the states change.

Click event

Since these are in a RadioGroup, I use

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)

to trigger necessary code when a button is pressed instead of an onClick()

CompoundButton Docs

Edit

To simply use a normal button and have it "clicked" when you first load the Activity, you can simply do

firstBtn.performClick();  //assuming firstBtn is the name of the button you want clicked

inside onCreate() after you have initialized the buttons. You will have to set up a state selector to change how the buttons looked when they are pressed/clicked or not.

codeMagic
  • 44,549
  • 13
  • 77
  • 93