Changing views with ViewFlipper Android - android

I need a little help.I'm using a ViewFlipper to change views in TabActivity but not really sure how to do it.Here is my code :
MainActivity.class
public class Collections extends Activity implements OnClickListener {
ViewFlipper vFlip;
Button genre, recommended, soon;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.collections_layout);
vFlip = (ViewFlipper) findViewById(R.id.tags_flipper);
genre = (Button) findViewById(R.id.genre_btn);
genre.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
vFlip.getCurrentView();
}
});
recommended = (Button) findViewById(R.id.recom_btn);
recommended.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
vFlip.getCurrentView();
}
});
soon = (Button) findViewById(R.id.soon_btn);
soon.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
vFlip.getCurrentView();
}
});
}
}
I have three different xml, which I want to load with clicking the buttons.Here is the collection_layout.xml
Collection_layout.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"
android:background="#FFFFFF">
<LinearLayout
android:id="#+id/actionbar"
android:layout_width="fill_parent"
android:layout_height="65px"
android:background="#drawable/pink_bar"
android:tileMode="repeat"
android:orientation="vertical">
<ImageView
android:src="#drawable/stampii_logo"
android:id="#+id/stampii_logo_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingTop="20px"/>
</LinearLayout>
<LinearLayout
android:id="#+id/first_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="1"
android:background="#d229ce">
<Button
android:id="#+id/genre_btn"
android:text="By Genre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp" />
<Button
android:id="#+id/recom_btn"
android:text="Recommended"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp" />
<Button
android:id="#+id/soon_btn"
android:text="Expect Soon!"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/flip"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF" >
<ViewFlipper
android:id="#+id/tags_flipper"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<include android:id="#+id/genre" layout="#layout/by_genre" />
<include android:id="#+id/recom" layout="#layout/recommended" />
<include android:id="#+id/soon" layout="#layout/expect_soon" />
</ViewFlipper>
</LinearLayout>
</LinearLayout>
So how can i do that?Thanks a lot!

Use ViewFlipper.setDisplayedChild

Related

Android click listener on View work in strange manner

I have the following layout.xml containing one ScrollView with some elements(picture and text)
<?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"
tools:context=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/clickable"
android:orientation="vertical">
<LinearLayout
android:id="#+id/layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_weight="1"
android:layout_gravity="center"
android:src="#drawable/crown" />
<TextView
android:id="#+id/n1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp"
android:text="some text" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_weight="1"
android:layout_gravity="center"
android:src="#drawable/medal" />
<TextView
android:id="#+id/n2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp"
android:text="some text" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_weight="1"
android:layout_gravity="center"
android:src="#drawable/star" />
<TextView
android:id="#+id/n3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp"
android:text="some text" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
MainActivity.java
public class MainActivity extends Activity{
LayoutInflater inflater;
RelativeLayout MyLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
inflater = LayoutInflater.from(this);
MyLayout = (RelativeLayout) inflater.inflate(R.id.layout, null, false);
MyLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("TAG"," I'm clicked! ");
}
});
Click Listener above does not work when I click screen!!!
But if I change code to this - everything is working
MyLayout.findViewById(R.id.clickable).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("TAG"," I'm clicked! ");
}
});
So why should I set click listener to LinearLayout inside ScrollView inside parent RelativeLayout?
Why click listener does not work for root xml element RelativeLayout???
The problem is that your ScrollView is intercepting the touch events and not propagating them to parent layouts (in this case your RelativeLayout).
This thread might have some useful information about this topic.
Just add android:clickable="false" in your ScrollView. Then change your MainActivity like below:
public class MainActivity extends Activity {
LinearLayout myLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myLayout = (LinearLayout) findViewById(R.id.layout);
myLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("TAG"," I'm clicked! ");
}
});
}
}
Note: activity_main.xml is xml file which you are loading.
Now your OnClick will work as expected.

ImageView onClick is not working FragmentActivity

May be it is so easy but do not know i have tried so much but it's not working :(
I have layout like this way
And i have following java code
public class MainActivity extends FragmentActivity{
private ImageView ivMainMenu;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ivMainMenu = (ImageView) findViewById(R.id.ivMainMenu);
ivMainMenu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("showSlider clicked");
}
});
}
i have tried
android:clickable="true"
but still not working
below is my xml code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/rlMainHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/header_bg"
android:orientation="vertical" >
<TextView
android:id="#+id/tvHeaderText"
style="#style/header_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<TextView
android:id="#+id/txtScore"
style="#style/black_large"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_margin="#dimen/extra_small"
android:background="#drawable/keyboard_btn"
android:padding="#dimen/extra_small"
android:text="#string/finish"
android:textColor="#color/dark_text_color"
android:textStyle="bold"
android:visibility="gone" />
<ImageView
android:id="#+id/ivMainMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:clickable="true"
android:src="#drawable/menu" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/rlMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/rlMainHeader"
android:background="#color/main_bg"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/flMain"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<LinearLayout
android:id="#+id/llMainSlider"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
android:visibility="visible" >
<include
android:layout_width="wrap_content"
android:layout_height="match_parent"
layout="#layout/slider" />
<LinearLayout
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#color/dark_text_color"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
</RelativeLayout>
in my logcat i am getting following message whenever i click on imageview
04-27 11:09:42.996: V/Provider/Settings(1018): from settings cache , name = sound_effects_enabled , value = 0
also i want to let you know that if i load any fragment in flMain, in that fragment all event is working fine but not on main layout :( also in llMainSlider noting was happening :(
Why don't you try declaring the event on the XML?
It is as simple as this
android:id="#+id/ivMainMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:onClick="myNewMethod" <!--add this extra line!--->
android:src="#drawable/menu" />
and simply on your Main activity add the myNewMethod as am extra function like this
public class MainActivity extends FragmentActivity{
private ImageView ivMainMenu;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void myNewMethod(View v) {
System.out.println("showSlider clicked");
}
Good Luck ;)

Android Sliding Drawer with first row always visible

I've created an Android sliding drawer but when it is dismissed it is completely hidden. The behavior that I'm looking for is to have the first row of the drawer visible at all times. If this is possible what is the best way to attempt this?
This works for me:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Button 1" />
<SlidingDrawer
android:id="#+id/slidingDrawer1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#id/button1"
android:content="#+id/content"
android:handle="#+id/handle" >
<Button
android:id="#+id/handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Handle" />
<ScrollView
android:id="#+id/content"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/content1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:scrollbars="vertical" >
<Button
android:id="#+id/button1a"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 1" />
<Button
android:id="#+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 2" />
</LinearLayout>
</ScrollView>
</SlidingDrawer>
</RelativeLayout>
And in main activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionsSlider = (SlidingDrawer) findViewById(R.id.slidingDrawer1);
actionsSlider.setOnDrawerCloseListener(new OnDrawerCloseListener() {
public void onDrawerClosed() {
firstButton = (Button) findViewById(R.id.button1);
firstButton.setVisibility(View.VISIBLE);
}
});
actionsSlider.setOnDrawerScrollListener(new OnDrawerScrollListener() {
public void onScrollStarted() {
if (!actionsSlider.isOpened()) {
findViewById(R.id.button1).setVisibility(View.GONE);
firstButton = (Button) findViewById(R.id.button1a);
}
}
#Override
public void onScrollEnded() {
}
});
}

SlidingDrawer not working

In my app I have implemented the SlidingDrawer and it is not working. Am I wrong in any place.
Drawer = (SlidingDrawer) findViewById(R.id.drawer);
Drawer.setOnDrawerOpenListener(new OnDrawerOpenListener() {
#Override
public void onDrawerOpened() {
Toast.makeText(getApplicationContext(),"Drawer Opened", Toast.LENGTH_SHORT).show();
}
});
similarly the setOnDrawerCloseListener is also not working and my XML file is:
<SlidingDrawer android:id="#+id/drawer"
android:background="#null" android:layout_height="fill_parent"
android:handle="#+id/handle" android:content="#+id/chat_history_container"
android:layout_width="fill_parent">
<ImageView android:id="#+id/handle" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="#drawable/chat_icon" />
<RelativeLayout android:id="#+id/chat_history_container"
android:layout_width="fill_parent" android:layout_height="wrap_content">
//Some TextViews here
</RelativeLayout>
</SlidingDrawer>
Try this Code :
Xml Code :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:gravity="bottom"
android:background="#drawable/androidpeople">
<SlidingDrawer android:layout_width="wrap_content"
android:id="#+id/SlidingDrawer"
android:handle="#+id/slideHandleButton" android:content="#+id/contentLayout"
android:padding="10dip" android:layout_height="250dip">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="#+id/slideHandleButton" android:background="#drawable/closearrow">
</Button>
<LinearLayout android:layout_width="wrap_content" android:id="#+id/contentLayout" android:orientation="vertical" android:gravity="center|top" android:padding="10dip" android:background="#C0C0C0" android:layout_height="wrap_content">
<Button android:id="#+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Content"></Button>
<Button android:id="#+id/Button02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Content"></Button>
<Button android:id="#+id/Button03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Content"></Button>
Java Code :
public class slidingDrawerExample extends Activity {
Button slideHandleButton;
SlidingDrawer slidingDrawer;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
slideHandleButton = (Button) findViewById(R.id.slideHandleButton);
slidingDrawer = (SlidingDrawer) findViewById(R.id.SlidingDrawer);
slidingDrawer.setOnDrawerOpenListener(new OnDrawerOpenListener() {
#Override
public void onDrawerOpened() {
slideHandleButton.setBackgroundResource(R.drawable.openarrow);
}
});
slidingDrawer.setOnDrawerCloseListener(new OnDrawerCloseListener() {
#Override
public void onDrawerClosed() {
slideHandleButton.setBackgroundResource(R.drawable.closearrow);
}
});
}
}
Check this Link for Reference Sliding Drawer...

Changing background color with Radio Buttons Android

I am attempting to change the background of a tab of my application by selecting a Radio Button from a RadioGroup, however I am not sure how to go about this.
So far I have
Favs.java:
import android.app.Activity;
import android.os.Bundle;
public class Favs extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.favs);
}
}
which points to this .xml file -> favs.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="What is your favorite color?"
android:padding="3dip"/>
<RadioGroup android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton android:id="#+id/radio_red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Red"
android:onClick="onClick" />
<RadioButton android:id="#+id/radio_yellow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yellow" />
</RadioGroup>
</LinearLayout>
I understand that the android:onClick="onClick" needs to be there however I have no idea what to do after this.
hi use below code for changing the background according to radio button selection
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="fill_parent"
android:orientation="vertical"
android:id="#+id/LinearLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="What is your favorite color?"
android:padding="3dip"/>
<RadioGroup android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/Group1"
android:orientation="vertical">
<RadioButton android:id="#+id/radio_red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Red"
/>
<RadioButton android:id="#+id/radio_yellow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yellow" />
</RadioGroup>
</LinearLayout>
Activity
public class Change extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final LinearLayout ll=(LinearLayout) findViewById(R.id.LinearLayout);
final RadioButton radio_red = (RadioButton) findViewById(R.id.radio_red);
final RadioButton radio_yellow = (RadioButton) findViewById(R.id.radio_yellow);
radio_red.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
ll.setBackgroundColor(Color.RED);
}
});
radio_yellow.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
ll.setBackgroundColor(Color.YELLOW);
}
});
}
}

Categories

Resources