How to use view flipper with three layouts? - android

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);
}

Related

How Can I Add Two Buttons In A Row Of ListView Without Overlapping

I want to make a app like that custom layout ScreenShot: .This is one row and I add like that row 100 more.So the people can clik number or its information(i).
But it is shown like that : two buttons overlapping .But I want two button next to each other it is like that in custom_layout.xml(you can see screen shot).
This is my custom_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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">
<Button
android:id="#+id/button"
android:layout_width="298dp"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="87dp" />
<Button
android:id="#+id/button2"
android:layout_width="51dp"
android:layout_height="49dp"
android:text="Button"
tools:layout_editor_absoluteX="317dp"
tools:layout_editor_absoluteY="87dp" />
</android.support.constraint.ConstraintLayout>
This is my activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/listView1"
android:layout_width="162dp"
android:layout_height="424dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="87dp" />
</RelativeLayout>
</ScrollView>
And this is my MainActivity.java:
package com.hey.task.attendancesystem;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
Button[] button=new Button[15];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listView1);
CustomAdapter customAdapter = new CustomAdapter();
//listeyi customlayouttaki şekilde oluşturdum hepsini
listView.setAdapter(customAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
});
}
class CustomAdapter extends BaseAdapter {
#Override
public int getCount() {
return button.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = getLayoutInflater().inflate(R.layout.custom_layout,null);
Button button1 = (Button) view.findViewById(R.id.button);
Button button2 = (Button) view.findViewById(R.id.button2);
for (int j = 0;j<15;j++)
button1.setText(i + "");
for (int j = 0;j<15;j++)
button2.setText("i");
return view;
}
}
}
I don't know if i get your question but you can simply use LinearLayout with weight. eg:-
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal"
android:weightSum="1">
<Button
android:id="#+id/button_1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".5" // or whatever you want
android:background="?android:attr/selectableItemBackground"
android:gravity="center"
android:padding="10dp"
android:text="Title 1" />
<Button
android:id="#+id/button_2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".5" // or whatever you want
android:background="?android:attr/selectableItemBackground"
android:gravity="center"
android:padding="10dp"
android:text="Title 2" />
</LinearLayout>
You can use linear layout with a android:weightSum="" attribute in place of constraint layout in your custom_layout.xml.
<?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="match_parent"
android:weightSum="1.0"
android:orientation="horizontal"
android:gravity="center">
<Button
android:layout_height="50dp"
android:layout_width="match_parent"
android:text="Button"
android:layout_weight="0.2"/>
<Button
android:layout_height="50dp"
android:layout_width="match_parent"
android:text="Button"
android:layout_weight="0.8"/>
</LinearLayout>
weightSum attribute is useful for having the layout rendered correctly for any device, which will not happen if you set width and height directly.

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);
}

Change a particuar view in Fragment

I am new to android developing and I am currently stumped at this situation.
Is there a way to change a particular child view of a fragment?
For example, I have a TextView and Button in a Fragment. Now after inflating this fragment and committing it, I would like to only change the text of the TextView based on the user input.
EDIT: The same layout is used to create multiple fragments. I would like to just change the TextView of a particular Fragment.
MainActivity Code:
public class MainActivity extends Activity {
Button bt;
EditText et;
LinearLayout ly;
String m;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt=(Button) findViewById(R.id.badd);
ly=(LinearLayout) findViewById(R.id.lay_ver);
et=(EditText) findViewById(R.id.etadd);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// multiple fragments are created
MyFragment f=new MyFragment();
FragmentManager fm=getFragmentManager();
FragmentTransaction t=fm.beginTransaction();
t.add(R.id.sv1, f, "hi");
t.commit();
et.setText("");
}
});
//would like to change the text of the textview of the fragment
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Fragment Code:
public class MyFragment extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v=inflater.inflate(R.layout.frag_lay, null);
return v;
}
}
activity_main XML :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/lay_ver"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:id="#+id/lay_hor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="#+id/etadd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="80"
android:hint="Enter Subject"
android:paddingTop="20dp" />
<Button
android:id="#+id/badd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="20"
android:text="Add" />
</LinearLayout>
<ScrollView
android:id="#+id/sv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/sv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
Fragment XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layfrag"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.53"
android:text="HiHow"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.20"
android:text="Hello"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
You can add a list of Fragments in your activity and each fragment to the list when you create them. Then you can get the fragment you want to change and do as cdblades said :
View parent = fragment.getView();
View childTextView = (TextView)parent.findViewById(R.id.myChildTextView);
But why are you adding the fragments in the same container?
The base View class has the findById() method that you can use to get a reference to child views like so:
View parent = fragment.getView();
View childTextView = (TextView)parent.findViewById(R.id.myChildTextView);

Android - HorizontalScrollView will not scroll all the way

This is bugging me for a while now, Im using HorizontalScrollView.fullScroll(HorizontalScrollView.FOCUS_RIGHT) which scrolls my ScrollView to the right, but not all the way, it's takes the scrollbar 99% percent to the right, I can manually scroll the rest of the way but for some reason it does not do it when i call the fullScroll() API.
Here's a sample code.... If you push the button a few times, the TextView will expand and you'll see the problem.
Activity:
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView tv;
HorizontalScrollView hsv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView1);
hsv = (HorizontalScrollView)findViewById(R.id.horizontalScrollView1);
}
public void btnOnClick(View v) {
tv.append("a");
hsv.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
}
}
Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="push the button" />
</LinearLayout>
</HorizontalScrollView>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btnOnClick"
android:text="Button" />
</LinearLayout>
I updated your code. Replace your code with below one. Which perfectly moves HorizontalScrollView to right of the screen.
Replace Your MainActivity.java with below one:
public class AndroidHTMLActivity extends Activity{
TextView tv;
HorizontalScrollView hsv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.textView1);
}
public void btnOnClick(View v){
tv.append("a");
System.out.println("tv.getWidth()="+tv.getWidth());
hsv = (HorizontalScrollView)findViewById(R.id.horizontalScrollView1);
ViewTreeObserver vto = hsv.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
#Override
public void onGlobalLayout() {
hsv.getViewTreeObserver().removeGlobalOnLayoutListener(this);
hsv.scrollTo(tv.getWidth(), 0);
}
});
}
}
And replace youe Xml file with below one:
<?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="wrap_content"
android:orientation="vertical"
android:id="#+id/ll_parent_layout">
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:text="push the button" />
</LinearLayout>
</HorizontalScrollView>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btnOnClick"
android:text="Button" />
</LinearLayout>
This code fulfile your condition which you wants.

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.

Categories

Resources