Android sliding menu to change content on page can this be done? - android

The main page has a horizontalScrollView across the top and a number of TextView text fields within it. I would like to be able to scroll and click on any on the TextView text and it change the RelativeLayout to match the selection made in the HorizontalScrollView.
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"
>
<HorizontalScrollView android:id="#+id/horizontalScrollView1" android:layout_height="wrap_content" android:scrollbars="none" android:layout_width="wrap_content">
<LinearLayout android:layout_width="match_parent" android:orientation="horizontal" android:layout_height="match_parent" android:id="#+id/linearLayout1">
<TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="#+id/EditTOne" android:text="tOne"></TextView>
<TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="#+id/EditTTwo" android:text="tTwo"></TextView>
<TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="#+id/EditTThree" android:text="tThree"></TextView>
<TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="#+id/EditTFour" android:text="tFour"></TextView>
<TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="#+id/EditTFive" android:text="tFive"></TextView>
<TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="#+id/EditTSix" android:text="tSix"></TextView>
<TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="#+id/EditTSeven" android:text="tSeven"></TextView>
</LinearLayout>
</HorizontalScrollView>
<RelativeLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:id="#+id/relativeLayout1"></RelativeLayout>
</LinearLayout>
I have a second layout page called tone which would be linked to the menu option tOne in the HorizontalScrollView.
tone.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">
<TextView android:id="#+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is tOne"></TextView>
</LinearLayout>
The main activity will flag if one of the first 4 options are selected by writing to the LogCat. Is it possible to display the tone.xml within the relativelayout if the user clicks the tOne option from the HorizontalScrollView? Then if the user selects tTwo display ttwo.xml in the RelativeLayoutView etc?
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class SlideMenu extends Activity implements OnClickListener {
/** Called when the activity is first created. */
TextView tOne, tTwo, tThree, tFour, tFive, tSix, tSeven, tEight, tNine, tTen;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RelativeLayout vSpace = (RelativeLayout)findViewById(R.id.relativeLayout1);
tOne = (TextView)findViewById(R.id.EditTOne);
tTwo = (TextView)findViewById(R.id.EditTTwo);
tThree = (TextView)findViewById(R.id.EditTThree);
tFour = (TextView)findViewById(R.id.EditTFour);
tFive = (TextView)findViewById(R.id.EditTFive);
tSix = (TextView)findViewById(R.id.EditTSix);
tSeven = (TextView)findViewById(R.id.EditTSeven);
tOne.setOnClickListener(this);
tTwo.setOnClickListener(this);
tThree.setOnClickListener(this);
tFour.setOnClickListener(this);
tFive.setOnClickListener(this);
tSix.setOnClickListener(this);
tSeven.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()) {
//--------------
case R.id.EditTOne:
System.out.println("Text One pressed");
break;
case R.id.EditTTwo:
System.out.println("Text Two pressed");
break;
case R.id.EditTThree:
System.out.println("Text Three pressed");
break;
case R.id.EditTFour:
System.out.println("Text Four pressed");
break;
}
}
}

I believe it is fairly simple. I added
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.tone, vSpace);
after you print "Text One Pressed". Be sure to make vSpace a field first.
To do this with all of them, call
vSpace.removeAllViews()
before you inflate each view.

Related

How Whatsapp attachment's window can override keyboard?

I'm working on a similiar problem developing my chat and I was checking on how whatsapp solves it to get an idea to work on it.
On mobile and inside a conversation, when your keyboard is up on the screen and you click on attachments, the attachments window overrides the keboard like the image below. It also look likes the activity under it freezes ( you can look at the slash "|" on the EditText, it seems that is freeze ).
I was wondering how WP could override the keyboard and why it looks like the activity under it is freezing, thanks in advance!
/Use the below code Snipet without using any library/
action_attach.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openDialog();
}
});
private void openDialog() {
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// inflate the custom popup layout
final View inflatedView;
inflatedView = layoutInflater.inflate(R.layout.custom_dialog_options_menu, null, false);
LinearLayout layoutGallery;
layoutGallery = (LinearLayout) inflatedView.findViewById(R.id.layoutGallery);
layoutGallery.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FloatingView.dismissWindow();
}
});
FloatingView.onShowPopup(this, inflatedView);
}
/**Sample Layout for Pop Up window ie: custom_dialog_options_menu
and change layout according to your choice***/
<?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="wrap_content"
android:orientation="vertical"
android:paddingTop="82dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFF"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/layoutGallery"
android:layout_width="0dp"
android:layout_marginTop="18dp"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:src="#drawable/gallery_attach_icon_selector"/>
<TextView
android:id="#+id/tvGallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Gallery"
android:layout_marginTop="10dp"
android:textSize="16sp"
android:textColor="#4d4747"/>
</LinearLayout>
<LinearLayout
android:id="#+id/layoutPhoto"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="18dp"
android:orientation="vertical">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:src="#drawable/camera_attach_icon_selector"/>
<TextView
android:id="#+id/tvPhoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Camera"
android:layout_marginTop="10dp"
android:textSize="16sp"
android:textColor="#4d4747"/>
</LinearLayout>
<LinearLayout
android:id="#+id/layoutVideo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="40dp"
android:layout_weight="1"
android:layout_marginTop="18dp"
android:orientation="vertical">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:src="#drawable/video_attach_icon_selector"/>
<TextView
android:id="#+id/tvVideo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Video"
android:layout_marginTop="10dp"
android:textSize="16sp"
android:textColor="#4d4747"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
/**FloatingView Class**/
public class FloatingView
{
/* Floating view is used to display a custom view for attachments in the chat screen */
import android.app.Activity;
import android.graphics.Point;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
private static PopupWindow popWindow;
private FloatingView() {
}
public static void onShowPopup(Activity activity, View inflatedView) {
// get device size
Display display = activity.getWindowManager().getDefaultDisplay();
final Point size = new Point();
display.getSize(size);
// fill the data to the list items
// set height depends on the device size
popWindow = new PopupWindow(inflatedView, size.x, ViewGroup.LayoutParams.WRAP_CONTENT,
true);
// set a background drawable with rounders corners
popWindow.setBackgroundDrawable(activity.getResources().getDrawable(
R.drawable.comment_popup_bg));
// make it focusable to show the keyboard to enter in `EditText`
popWindow.setFocusable(true);
// make it outside touchable to dismiss the popup window
popWindow.setOutsideTouchable(true);
popWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
// show the popup at bottom of the screen and set some margin at
// bottom ie,
popWindow.showAtLocation(activity.getCurrentFocus(), Gravity.BOTTOM, 0,
0);
}
public static void dismissWindow() {
popWindow.dismiss();
}

Remove and add layout dynamically by click button in Android

I'm working on a big project and I can not solve the problem of a dynamically adding of components.I want to add layout into other layout by click on a button ADD. After this I want to remove it by click a button REMOVE.
Specially for stackoverflow I build a small example of what I want to do.
To ADD it's not a problem but remove it's a problem.When I click a "remove" button this remove not what I need (I want remove parent of "remove" button).
After this I want to ask something more important.I will need save all this data to the DB.So I don't know how to get data from each Text Fields and put it into list (or something else) because all this Text Fields have same ID.
So I see two way of solution:
1)Change there ID dynamically
2)Something else))
Thank you very much!!!
This is
sub_fields.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:nestedScrollingEnabled="false"
android:id="#+id/detailsLayout"
android:focusableInTouchMode="true">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/txtName"
android:hint="name" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="#+id/txtPhone"
android:layout_gravity="center_horizontal"
android:hint="phone" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD"
android:id="#+id/btnAddd"
android:onClick="onClickAddd" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="REMOVE"
android:id="#+id/btnRemove"
android:onClick="onClickAddd" />
</LinearLayout>
</LinearLayout>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:nestedScrollingEnabled="false"
android:id="#+id/generalLayout">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="56dp"
android:gravity="center"
android:background="#7d65258a">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="FILL FIELDS"
android:id="#+id/textView" />
</LinearLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/subLayoutFields">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/txtName"
android:hint="name" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="#+id/txtPhone"
android:layout_gravity="center_horizontal"
android:hint="phone" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD"
android:id="#+id/btnAdd"
android:onClick="onClickAdd" />
</LinearLayout>
</LinearLayout>
MainActivity.java
package andrey.adddinamicallycontrolsapp;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onClickAdd(View view) {
LayoutInflater ltInflater = getLayoutInflater();
final LinearLayout subLayoutFields = (LinearLayout) findViewById(R.id.subLayoutFields);
final View view1 = ltInflater.inflate(R.layout.sub_fields, subLayoutFields, true);
Button buttonRemove = (Button)view1.findViewById(R.id.btnRemove);
buttonRemove.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
subLayoutFields.removeView((LinearLayout)(v.getParent().getParent()));
}});
}
Before adding some view by addView(childView) you should save reference to that. In that case removeView(childView) will work. Getting that view by traveling in view tree by getParent() is not a good idea.
By the way, where is your addView call?
maybe try subLayoutFields.removeView(view1) - view1 is final
if you want to somehow differ your widgets (for storing in db, iterating or whatever you want) you may set for them dynamic ids using View.generateViewId() (API17) or using custom method from most voted answer here
Thank you guys!!!
The answer is :
public void onClickAddPanel(View view) {
LayoutInflater ltInflater = getLayoutInflater();
LinearLayout subLayoutFieldsForBtnAdd = (LinearLayout) findViewById(R.id.productDetails);
View view1 = ltInflater.inflate(R.layout.product_details, subLayoutFieldsForBtnAdd, true);
}
public void onClickRemovePanel(View v) {
View v1 = (View) v.getParent();
LinearLayout subLayoutFieldsForBtnRemove = (LinearLayout) findViewById(R.id.productDetails);
subLayoutFieldsForBtnRemove.removeView(v1);
}

Android TextView doesn't show affected text

Ok so... I've been trying to affect text to TextView (because it's meant to show text, right?)
But for some unknown to me reason it refuses to print whatever I put into it.
My application has two activities, the MainAcivity, or a welcoming screen, per se, and a GameActivity (the project is a Domino game).
I create my previously written java objects at the beginning of the GameActivity class, trying then to show player's name within a TextView.
Player player1 = new Player("Hulk");
creates a player with name Hulk, as you can imagine. I have a method within Player.java to return player's name:
player1.getName()
returns string "Hulk"
I then try to set my TextView's text to hulk, first off by creating a handle to it with:
TextView p1v = (TextView) findViewById(R.textviews.p1view);
then doing the following:
p1v.setText("Player 1 : " + player1.toString());
the p1view is defined as follows within game.xml layout:
<LinearLayout
...
<LinearLayout
...
<TextView
android:id="#+textviews/p1view"
android:text="Board"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
I tried p1view.textAppend, tried using android:editable="true". Nothing seems to work.
Funnily enough though, when I try doing the same thing, within MainActivity, it works, as so:
public class
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
with the TextView defined within main.xml as follows:
<TextView
android:id="#+textviews/mainview_player"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
Following are the full codes of the files, just to avoid answers like: Give us the full code D:
GameActivity.java:
package domino.asd;
import LI260.*; import android.app.Activity; import android.os.Bundle;
import android.widget.TextView;
public class GameActivity extends Activity {
private Player player1 = new Player("HULK");
private Player player2 = new Player("CPU");
private Pioche bag = new Pioche();
private Plateau board = new Plateau();
private Game game1 = new Game(bag, board, player1, player2);
TextView p1name = null;
TextView p2name = null;
/**
* Called when the activity is first created.
*
* #param savedInstanceState
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
TextView tv = (TextView) findViewById(R.textviews.p1view);
tv.setText(player1.getName());
} }
game.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:weightSum="10"
>
<!-- Played layout -->
<LinearLayout
android:id="#+drawable/gameview_Scores"
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_gravity="top"
android:layout_weight="2"
android:layout_marginBottom="10px"
android:orientation="horizontal"
android:background="#ff0000"
android:gravity="center"
>
<Button
android:id="#+buttons/exitButton"
android:text="Exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
/>
<Button
android:id="#+buttons/drawButton"
android:text="Draw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="#+buttons/scoresButton"
android:text="Show scores"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<!-- Board layout -->
<LinearLayout
android:id="#+textviews/gameview_Table"
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_gravity="center"
android:layout_weight="6"
android:layout_marginBottom="10px"
android:background="#ffff00"
>
<TextView
android:id="#+textviews/p1view"
android:text="Board"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<!-- At hand layout -->
<LinearLayout
android:id="#+drawable/gameview_Playable"
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="2"
android:layout_gravity="bottom"
android:background="#ff00ff"
>
<TextView
android:id="#+textviews/gameview_AtHandText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
MainActivity.java:
package domino.asd;
import LI260.*;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
Player p1 = new Player("Hulk");
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView p1view = (TextView) findViewById(R.textviews.mainview_player);
p1view.setText(p1.getName());
}
public void btnClick_Name(View view) {
if (view.getId() == R.buttons.B_EnterName) {
EditText playersname = (EditText) findViewById(R.string.playerNameInput);
Toast.makeText(this, "Your name: " + playersname.getText().toString(), Toast.LENGTH_SHORT).show();
}
setContentView(R.layout.game);
}
}
and lastly 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"
android:gravity="center"
>
<EditText
android:id="#+string/playerNameInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:lines="1"
/>
<TextView
android:id="#+textviews/mainview_player"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="#+buttons/B_EnterName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Save name and start game."
android:onClick="btnClick_Name"
/>
</LinearLayout>
When you are first loading the activity, you are calling this code:
setContentView(R.layout.main);
TextView p1view = (TextView) findViewById(R.textviews.mainview_player);
p1view.setText(p1.getName());
Yet when the user click's the button you are only calling this code:
setContentView(R.layout.game);
Because you have reset the main content view the child view mainview_player has been destroyed and replaced with a new one when inflated from R.layout.game.
You would need to call this code again to find the new view in the game layout and populate it with your player name:
TextView p1view = (TextView) findViewById(R.textviews.mainview_player);
p1view.setText(p1.getName());
You would be better to look into how activities relate to one another and start a new activity for R.layout.game

Change the background image of a button

I am using two buttons, one for next news and one for previous news. In my activity when I am on the first news my button should set the image with the grey image and when I move forward my previous news button should change from the grey image to the colored one. Similarly when I reach at the last news my next button should change to grey image.
This is my xml file.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFFFF"
>
<TextView
android:id="#+id/tv_submitDate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Medium Text"
android:textColor="#000000"
android:padding="5dp"/>
<TextView
android:id="#+id/tv_headline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_submitDate"
android:text="Medium Text"
android:textColor="#000000"
android:textStyle="bold"
android:padding="5dp"/>
<View
android:id="#+id/viewSeperator"
android:layout_width="fill_parent"
android:layout_height="2dip"
android:layout_below="#+id/tv_headline"
android:background="#FF909090" />
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/btn_relative_layout"
android:layout_below="#+id/viewSeperator" android:padding="10dp">
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/tv_detailNews"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textColor="#000000" />
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/btn_relative_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#DCDCDC"
android:padding="10sp" >
<Button
android:id="#+id/btn_prevNews"
android:layout_width="120sp"
android:layout_height="40sp"
android:layout_weight="0.5"
android:layout_marginRight="5sp"
android:background="#drawable/arrow_left"
android:clickable="true" />
<Button
android:id="#+id/btn_nextNews"
android:layout_width="120sp"
android:layout_height="40sp"
android:layout_marginLeft="5sp"
android:layout_weight="0.5"
android:background="#drawable/arrow_right"
android:clickable="true" />
</LinearLayout>
</RelativeLayout>
This is where I am trying to change the image of my button:
public void onClick(View v) {
if (v == btnPrevNews && newsId > 0) {
if (newsId > 0) {
newsId--;
putDetailNewsinView(newsId);
} else if (newsId == 0) {
btnPrevNews
.setBackgroundResource(R.drawable.disable_arrow_left);
btnPrevNews.setEnabled(false);
}
// btnPrevNews.setVisibility(View.INVISIBLE);
} else if (v == btnNextNews && newsId < newsListDetail.size() - 1) {
if (newsId < newsListDetail.size() - 1) {
newsId++;
putDetailNewsinView(newsId);
if(newsId == 0)
btnNextNews.setBackgroundDrawable(getResources().getDrawable(
R.drawable.disable_arrow_right));
} else if (newsId == newsListDetail.size()) {
// btnNextNews.setVisibility(View.INVISIBLE);
btnNextNews.setBackgroundDrawable(getResources().getDrawable(
R.drawable.disable_arrow_right));
// btnNextNews.setBackgroundResource(R.drawable.disable_arrow_right);
btnNextNews.setEnabled(false);
}
}
}
Please help me out in this.
Thanks in advance.
I assume you have Methods which will be executed if the Button is pressed. Just use these and add Logic to change the Background of these Buttons to grey:
Give both Buttons an ID, you can access these Buttons with:
Button mybutton = (Button) findViewById(R.id.yourid);
Now you can do all kinds of things with your button, like changing the background.
edit:
The Problem could lay in the Pictures, but if i were you, i would set some breakpoints and debug step-by-step.
I hacked some code of mine to test this functionality, it works fine for me. Here is it:
package my.namespac;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class SomeTestProjectActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button)findViewById(R.id.btn_prevNews);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Button b = (Button)v;
v.setBackgroundDrawable(getResources().getDrawable(R.drawable.koala));
}
});
}
}
And the Layout-Xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/btn_prevNews"
android:layout_width="120sp"
android:layout_height="40sp"
android:layout_weight="0.5"
android:layout_marginRight="5sp"
android:background="#drawable/ic_launcher"
android:clickable="true" />
</LinearLayout>

How to use view flipper with three layouts?

I am currently using ViewFlipper for my main activity with two different layouts. I want to use a third layout, but I can only find the showNext() and showPrevious() commands. Can someone show me how to implement a third layout using ViewFlipper?
Made an example for you that shows howto display different views in a ViewFlipper.
The layout of the example is made up of the following parts. There are three radio buttons. A ViewFlipper is placed below the radio buttons. This flipper holds three different simple views with different texts.
The radio buttons are then hooked up to a listener in the java code that will change the view displayed by the ViewFlipper depending on which radio button that currently is chosen.
XML
<?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">
<RadioGroup android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton android:layout_height="wrap_content"
android:id="#+id/radio0" android:layout_width="wrap_content"
android:text="Show View 1" android:checked="true"></RadioButton>
<RadioButton android:layout_height="wrap_content"
android:id="#+id/radio1" android:layout_width="wrap_content"
android:text="Show view 2"></RadioButton>
<RadioButton android:layout_height="wrap_content"
android:id="#+id/radio2" android:layout_width="wrap_content"
android:text="Show View 3"></RadioButton>
</RadioGroup>
<ViewFlipper android:id="#+id/ViewFlipper01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!--adding views to ViewFlipper-->
<TextView android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First view is now displayed"></TextView>
<TextView android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second view is now displayed"></TextView>
<TextView android:id="#+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Third view is now displayed"></TextView>
</ViewFlipper>
</LinearLayout>
JAVA
package com.test.threeviews;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
import android.widget.ViewFlipper;
public class ThreeViewsinaFlipperActivity extends Activity {
RadioButton RB0;
RadioButton RB1;
RadioButton RB2;
ViewFlipper VF;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*
* Find the views declared in main.xml.
*/
RB0 = (RadioButton) findViewById(R.id.radio0);
RB1 = (RadioButton) findViewById(R.id.radio1);
RB2 = (RadioButton) findViewById(R.id.radio2);
VF = (ViewFlipper) findViewById(R.id.ViewFlipper01);
/*
* Set a listener that will listen for clicks on the radio buttons and
* perform suitable actions.
*/
RB0.setOnClickListener(radio_listener);
RB1.setOnClickListener(radio_listener);
RB2.setOnClickListener(radio_listener);
}
/*
* Define a OnClickListener that will change which view that is displayed by
* the ViewFlipper
*/
private OnClickListener radio_listener = new OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.radio0:
VF.setDisplayedChild(0);
break;
case R.id.radio1:
VF.setDisplayedChild(1);
break;
case R.id.radio2:
VF.setDisplayedChild(2);
break;
}
}
};
}
See this simple use of android.widget.ViewFlipper. With it you can create different layout from xml and then switch among them with simple method like this:
ViewFlipper viewFlipper = (ViewFlipper) findViewById(R.id.myViewFlipper);
// you can switch between next and previous layout and display it
viewFlipper.showNext();
viewFlipper.showPrevious();
// or you can switch selecting the layout that you want to display
viewFlipper.setDisplayedChild(1);
viewFlipper.setDisplayedChild(viewFlipper.indexOfChild(findViewById(R.id.secondLayout)
Xml example with tree layouts:
<ViewFlipper
android:id="#+id/myViewFlipper"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/firstLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
[...]
</LinearLayout>
<LinearLayout
android:id="#+id/secondLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
[...]
</LinearLayout>
<LinearLayout
android:id="#+id/thirdLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
[...]
</LinearLayout>
</ViewFlipper>
You can use it in this way also. I have attached the java code and xml file where a button is used to change the flipper view.
package com.nikhil.play.add_subtract;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ViewFlipper;
public class Flipper extends Activity implements OnClickListener {
ViewFlipper flippy;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flipper);
flippy = (ViewFlipper) findViewById(R.id.viewFlipper1);
flippy.setOnClickListener(this);
flippy.setFlipInterval(10000);
flippy.startFlipping();
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
flippy.showNext();
}
}
XML CODE-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ViewFlipper
android:id="#+id/viewFlipper1"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Flipper 2" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Flipper 3" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Flipper 4" />
</ViewFlipper>
</LinearLayout>
xml
<ViewFlipper
android:id="#+id/viewflip"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_weight="1"
/>
Java
public class BlankFragment extends Fragment{
ViewFlipper viewFlipper;
FragmentManager fragmentManager;
int gallery_grid_Images[]={drawable.image1, drawable.image2, drawable.image3,
drawable.image1, drawable.image2, drawable.image3, drawable.image1,
drawable.image2, drawable.image3, drawable.image1
};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootView = inflater.inflate(fragment_blank, container, false);
viewFlipper =(ViewFlipper)rootView.findViewById(R.id.viewflip);
for(int i=0;i<gallery_grid_Images.length;i++)
{
// This will create dynamic image view and add them to ViewFlipper
setFlipperImage(gallery_grid_Images[i]);
}
return rootView;
}
private void setFlipperImage(int res) {
Log.i("Set Filpper Called", res+"");
ImageView image = new ImageView(getContext());
image.setBackgroundResource(res);
viewFlipper.addView(image);
viewFlipper.setFlipInterval(1000);
viewFlipper.setAutoStart(true);
}

Categories

Resources