Show-hide toggle button for Layout in Android - android

I was wondering why a button I made to show/hide my layouts only works once, i.e, initially my layouts are GONE, then when I click a button they're VISIBLE, but later when I click the same button, their View is not being set back to GONE.
/**
* Method to show/hide buttons, on button click.
* #param v
*/
public void hideOrDisplayOptionIconsButton(View v)
{
// Hide layouts if VISIBLE
if(mMapViewsButtonsLinearLayout.getVisibility() == View.VISIBLE
&& mLocationButtonsLinearLayout.getVisibility() == View.VISIBLE)
{
mMapViewsButtonsLinearLayout.setVisibility(View.GONE);
mLocationButtonsLinearLayout.setVisibility(View.GONE);
}
// Show layouts if they're not VISIBLE
else
{
mMapViewsButtonsLinearLayout.setVisibility(View.VISIBLE);
mLocationButtonsLinearLayout.setVisibility(View.VISIBLE);
}
}

Here is a sample which should work for you
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
private LinearLayout mMapViewsButtonsLinearLayout=null;
private LinearLayout mLocationButtonsLinearLayout=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMapViewsButtonsLinearLayout= (LinearLayout) findViewById(R.id.mMapViewsButtonsLinearLayout);
mLocationButtonsLinearLayout= (LinearLayout) findViewById(R.id.mLocationButtonsLinearLayout);
}
public void hideOrDisplayOptionIconsButton(View v)
{
// Hide layouts if VISIBLE
if(mMapViewsButtonsLinearLayout.getVisibility() == View.VISIBLE
&& mLocationButtonsLinearLayout.getVisibility() == View.VISIBLE)
{
mMapViewsButtonsLinearLayout.setVisibility(View.GONE);
mLocationButtonsLinearLayout.setVisibility(View.GONE);
}
// Show layouts if they're not VISIBLE
else
{
mMapViewsButtonsLinearLayout.setVisibility(View.VISIBLE);
mLocationButtonsLinearLayout.setVisibility(View.VISIBLE);
}
}
}
Here is the layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.hideshow.MainActivity">
<LinearLayout
android:id="#+id/mMapViewsButtonsLinearLayout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button2" />
</LinearLayout>
<LinearLayout
android:id="#+id/mLocationButtonsLinearLayout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button4" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hide or Show"
android:id="#+id/hideorshow"
android:onClick="hideOrDisplayOptionIconsButton" />
</LinearLayout>
</LinearLayout>

Related

How to make a radio button control a button

I am making an app where it takes two types of users. "Viewers" and "Contractors". I made two radio buttons for each option. I want to know three things:
How to activate a button when a radio button is selected. How to inactivate a button when there is no radio button selected. Lastly, how to make both of the radio buttons send you to a unique activity depending of the option chosen. For example, I pick "Contractor" then press the button to continue, it'll send me to a unique layout that connects to that radio button.
Here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/b"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient_background"
tools:context="com.devteam.abire.abire.b">
<android.support.v7.widget.CardView
app:cardElevation="15dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_width="300dp"
android:layout_height="345dp">
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
app:cardElevation="20dp"
android:layout_width="320dp"
android:layout_height="320dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:background="#141526"
android:layout_width="match_parent"
android:layout_height="50dp"/>
<ImageView
android:id="#+id/abire_app_icon_v2"
android:layout_marginTop="21dp"
android:elevation="45dp"
android:layout_centerHorizontal="true"
android:background="#drawable/abire_logo_v1"
android:layout_width="55dp"
android:layout_height="55dp" />
<TextView
android:layout_marginStart="20dp"
android:id="#+id/register_as_text"
android:layout_marginTop="10dp"
android:text="Register As A..."
android:textColor="#141526"
android:layout_below="#+id/abire_app_icon_v2"
android:textSize="28sp"
android:textAlignment="textStart"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RadioButton
android:layout_marginStart="20dp"
android:textSize="22sp"
android:textColor="#141526"
android:id="#+id/viewer_radioBtn"
android:text="Viewer"
android:layout_marginTop="18dp"
android:layout_below="#+id/register_as_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RadioButton
android:layout_marginStart="20dp"
android:textSize="22sp"
android:textColor="#141526"
android:id="#+id/contractor_radioBtn"
android:text="Contractor"
android:layout_marginTop="18dp"
android:layout_below="#+id/viewer_radioBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/continueBtn"
android:textSize="18sp"
android:text="CONTINUE"
android:textColor="#fff"
android:layout_marginTop="25dp"
android:layout_centerHorizontal="true"
android:layout_below="#+id/contractor_radioBtn"
android:background="#drawable/ripple_maroon"
android:layout_width="250dp"
android:layout_height="38dp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
Here is my Java:
package com.devteam.abire.abire;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class b extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.b);
}
}
If you want to make only one of the buttons clickable at a time, you should put radiobuttons inside radiogroup
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:layout_marginStart="20dp"
android:textSize="22sp"
android:textColor="#141526"
android:id="#+id/viewer_radioBtn"
android:onCLick="onRadioButtonClicked"
android:text="Viewer"
android:layout_marginTop="18dp"
android:layout_below="#+id/register_as_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RadioButton
android:layout_marginStart="20dp"
android:textSize="22sp"
android:textColor="#141526"
android:id="#+id/contractor_radioBtn"
android:onCLick="onRadioButtonClicked"
android:text="Contractor"
android:layout_marginTop="18dp"
android:layout_below="#+id/viewer_radioBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RadioGroup>
<Button
android:id="#+id/continueBtn"
android:textSize="18sp"
android:text="CONTINUE"
android:textColor="#fff"
android:layout_marginTop="25dp"
android:layout_centerHorizontal="true"
android:layout_below="#+id/contractor_radioBtn"
android:onCLick="onButtonClicked"
android:background="#drawable/ripple_maroon"
android:layout_width="250dp"
android:layout_height="38dp" />
package com.devteam.abire.abire;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class b extends AppCompatActivity {
private String mClickedRadioButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.b);
}
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.viewer_radioBtn":
if (checked)
sClickedRadioButton = "viewer";
break;
case R.id.contractor_radioBtn"
if (checked)
sClickedRadioButton; = "contractor";
break;
}
}
public void onButtonClicked(View v){
if(sClickedRadioButton == null){
return;
}else if(sClickedRadioButton.equals("viewer")){
//Do something when view is checked
}else if(sClickedRadioButton.equals("contractor"){
// Do something when contractor is checked
}
}
Firstly, it is good to use radiogroup instead of radio button if you have multipe buttons.
For "How to activate a button when a radio button is selected. How to inactivate a button when there is no radio button selected.", create references for both buttons and radio buttons in activity. Then, if first radio button is checked, disable button by :
button.setEnabled(false);
For "Lastly, how to make both of the radio buttons send you to a unique activity depending of the option chosen.", while using radiogroup use :
public void onCheckedChanged(RadioGroup arg0, int arg1) {
radioButton = (RadioButton) findViewById(radioGroup.getCheckedRadioButtonId(););
if (radioButton.isChecked()) {
text=radioButton.getText().toString();
if (text.equals("radiobtn1Option")) {
//TODO : start new activity
Intent intent = new Intent(this, YourNextSCreen.class);
startActivity(intent);
} if (text.equals("radiobtn2Option")) {
//TODO
} else {
//TODO
}
}
}
});

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>

Button visibility problems in Android

I'm creating a toolbar that toggles the visibility of buttons when you click a button from a toolbar. So, if the user clicks on the "Draw" button, invisible buttons "Pencil" and Pen" will become visible above the "Draw" button. If you click the "Draw" button again, the buttons "Pencil" and "Pen" will become invisible again.
Within my xml file I have set the visibilty of some buttons to be "invisible" so when I launch the activity they won't be seen. This part is straight forward.
.xml file of btnDrawLine - (Update # 12:21)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
<com.odhranlynch.testSection.UserInterface
android:id="#+id/UserInterface"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true" />
<Button
android:id="#+id/btnDraw"
android:layout_width="80dip"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Draw" />
<Button
android:id="#+id/btnDrawLine"
android:layout_width="80dip"
android:layout_height="wrap_content"
android:layout_above="#+id/btnDraw"
android:layout_alignParentLeft="true"
android:visibility="visible"
android:text="Line" />
<Button
android:id="#+id/btnDrawCurve"
android:layout_width="80dip"
android:layout_height="wrap_content"
android:layout_above="#+id/btnDrawLine"
android:layout_alignParentLeft="true"
android:visibility="visible"
android:text="Curve" />
<Button
android:id="#+id/btnCutout"
android:layout_width="80dip"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/btnDraw"
android:text="Cutout" />
<Button
android:id="#+id/btnCutInner"
android:layout_width="80dip"
android:layout_height="wrap_content"
android:layout_above="#+id/btnDraw"
android:layout_toRightOf="#+id/btnDraw"
android:visibility="visible"
android:text="Inner" />
<Button
android:id="#+id/btnCutOutter"
android:layout_width="80dip"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btnDrawCurve"
android:layout_alignBottom="#+id/btnDrawCurve"
android:layout_toLeftOf="#+id/btnCancel"
android:visibility="visible"
android:text="Outter" />
<Button
android:id="#+id/btnCancel"
android:layout_width="80dip"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/btnFinish"
android:text="Cancel" />
<Button
android:id="#+id/btnFinish"
android:layout_width="80dip"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Finish" />
</RelativeLayout>
Next, when the user clicks a button that is visible, I'd like the invisible buttons to appear.
Here's the thing, they won't reappear!lol I'm confused by it.
I would be very grateful if someone would be kind enough to shed so light onto this for me :)
testActivity.java
package com.odhranlynch.testSection;
import com.odhranlynch.testSection.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class testActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_product);
// Find buttons and give them a name.
final View btnDraw = findViewById(R.id.btnDraw);
final View btnCutOut = findViewById(R.id.btnCutout);
final View btnDrawLine = findViewById(R.id.btnDrawLine);
final View btnDrawCurve = findViewById(R.id.btnDrawCurve);
final View btnCutInner = findViewById(R.id.btnCutInner);
final View btnCutOutter = findViewById(R.id.btnCutOutter);
//Draw Button clicked (UI Toolbar).
btnDraw.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//Treat button as a toggle button
//So if a sub-button (e.g. Draw Line) is visible, then we know the button has
//been toggled to visible so lets now make it invisible.
if (btnDrawLine.getVisibility()== View.VISIBLE) {
//Its visible.
btnDrawLine.setVisibility(View.INVISIBLE);
btnDrawCurve.setVisibility(View.INVISIBLE);
Log.d("TAG", "INVISIBLE");
} else {
// Either gone or invisible
btnDrawLine.setVisibility(View.VISIBLE);
btnDrawCurve.setVisibility(View.VISIBLE);
Log.d("TAG", "VISIBLE");
}
}
});
}
}
As a further point to mention, if I set the visibilty of the buttons to be visible within the .xml file I can toggle the visibilty perfectly fine during runtime!
Again, I would be grateful of some help :)
Try replacing View.INVISIBLE to View.GONE.
Your code is working fine..
The XML file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<Button
android:id="#+id/btnDrawLine"
android:layout_width="80dip"
android:layout_height="wrap_content"
android:layout_above="#+id/btnDraw"
android:layout_alignParentLeft="true"
android:visibility="invisible"
android:text="Line" />
<Button
android:id="#+id/draw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="draw" />
</LinearLayout>
The activity
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class DrawCanvasActivity extends Activity {
private static final String Number1 = "9686801147";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final View btnDrawLine = findViewById(R.id.btnDrawLine);
Button btnDraw = (Button) findViewById(R.id.draw);
btnDraw.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (btnDrawLine.getVisibility()== View.VISIBLE) {
btnDrawLine.setVisibility(View.INVISIBLE);
Log.d("TAG", "INVISIBLE");
} else {
btnDrawLine.setVisibility(View.VISIBLE);
Log.d("TAG", "VISIBLE");
}
}
});
}
}
The button line appears when the draw button is clicked.Guess there may be problem in the view formatting in your code.
If you want ot use gone/visible, just add a LinearLayout around your buttons, which you want to hide. LinearLayout will have a layout_width=wrap_content; and you referer position of other element to this Layout.
After you free to change visibility to gone/visible of your buttons.

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

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.

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