I am trying to change my LinearLayout height to 50% of the parent layout when i click a button. I trired it with LinearLayout.LayoutParams but it doesn't change the height of my layout if i call it when buttonClicked from a method outside onCreate . But if i keep LinearLayout.LayoutParams inside onCreate then it works. How can i do it when i click a button?
This Code Works :
public class ViewReport extends AppCompatActivity {
Button button;
LinearLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_report);
button=(Button) findViewById(R.id.button);
layout=(LinearLayout) findViewById(R.id.fold);
final LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) layout.getLayoutParams();
lp.height = 0;
lp.width=LinearLayout.LayoutParams.MATCH_PARENT;
lp.weight=0.5f;
}
}
This Code Doesn't Work :
public class ViewReport extends AppCompatActivity {
Button button;
LinearLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_report);
button=(Button) findViewById(R.id.button);
layout=(LinearLayout) findViewById(R.id.fold);
}
public void Click(View view)
{
Toast.makeText(getApplicationContext(),"tapped",Toast.LENGTH_LONG).show();
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) layout.getLayoutParams();
lp.height = 0;
lp.width=LinearLayout.LayoutParams.MATCH_PARENT;
lp.weight=0.5f;
}
}
Layout XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="me.imbishal.imageblur.ViewReport"
android:weightSum="1">
<LinearLayout
android:id="#+id/fold"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#drawable/profilepageback"
android:orientation="vertical">
<EditText
android:id="#+id/editText4"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginBottom="3dp"
android:background="#drawable/my_button"
android:ems="10"
android:inputType="textPersonName" />
<EditText
android:id="#+id/editText3"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginBottom="5dp"
android:background="#drawable/my_button"
android:ems="10"
android:inputType="textPersonName" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#drawable/just_border"
android:onClick="Click"
android:text="Button" />
</LinearLayout>
You have changed your LayoutParams but haven't set them. Add this line
layout.setLayoutParams(lp);
Related
I designed two buttons b1,b2 inside "KeyboardService.java" which should update the value of PACK_LIB inside "Stickers.java".
Three files attached:
main_board_layout.xml
Stickers.java
KeyboardService.java
I assigned both buttons with IDs of Button1 = b1 and Button2 = b2.
The built is successful but when I click on the buttons both do not work.
I think it is maybe a problem with this line ->
final Button button1 = (Button) mainBoard.findViewById(R.id.b1);
Because the Buttons are inside a FrameLayout. But when I use ID of that it won't work either.
Any ideas?
-------main_board_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main_board"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/black"
android:orientation="vertical">
<android.inputmethodservice.KeyboardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/keyboard_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/black" />
<!--top bar-->
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#ffffff"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:padding="2dp"
android:id="#+id/buttons"
>
<Button
android:id="#+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button1" />
<Button
android:id="#+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="#string/button2"
/>
<TextView
android:id="#+id/packNameLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:text="#string/app_name"
android:textColor="#000000"
android:textSize="1sp"
android:visibility="gone"
/>
<ImageView
android:id="#+id/btShareLinkGP"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:src="#mipmap/bt_share"
tools:srcCompat="#mipmap/bt_share"
/>
</FrameLayout>
<View
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#000000"/>
<ScrollView
android:id="#+id/gif_view"
android:layout_width="match_parent"
android:layout_height="190dp"
android:background="#color/black"
android:paddingLeft="0dp"
android:paddingRight="15dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/black"
android:gravity="left|center"
android:orientation="horizontal"
android:padding="0dp">
<android.support.v7.widget.RecyclerView
android:id="#+id/pack_recycler_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#color/black"
android:orientation="horizontal"
app:layoutManager="android.support.v7.widget.LinearLayoutManager" />
</LinearLayout>
</LinearLayout>
-------Stickers.java
public static String PACK_LIB ="";
public void setDefaultStickerPack() {
checkVersion(true);
InputStream in = null;
String packList[]=new String[0];
final String PACK_APP="pack_app";
final String PACK_ICON="pack_on.png";
String curAssets="";
-------KeyboardService.java
#Override
public View onCreateInputView() {
mainBoard = (LinearLayout) getLayoutInflater().inflate(R.layout.main_board_layout, null);
packNameLabel = (TextView) mainBoard.findViewById(R.id.packNameLabel);
scrollView = (ScrollView) mainBoard.findViewById(R.id.gif_view);
stickerView = (RecyclerView) getLayoutInflater().inflate(R.layout.recycler_view, null);
stickerView.addItemDecoration(new MarginDecoration(this));
stickerView.setHasFixedSize(true);
stickerView.setLayoutManager(new GridLayoutManager(this, 4));
scrollView.addView(stickerView);
ImageView btShareLinkGP = (ImageView) mainBoard.findViewById(R.id.btShareLinkGP);
btShareLinkGP.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
shareLinkToGP();
}
});
// packs bar
packView = (RecyclerView) mainBoard.findViewById(R.id.pack_recycler_view);
// BUTTONS ACTIONS
final Button button1 = (Button) mainBoard.findViewById(R.id.b1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Stickers.PACK_LIB = "allstickers";
}
});
final Button button2 = (Button) mainBoard.findViewById(R.id.b2);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Stickers.PACK_LIB = "teststickers";
}
});
showStickers();
return mainBoard;
}
I want to have a transition in my android views on view is Button and other is EditText, transitions must be like this animation
I tried in this way
Layout xml is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="app.itc.org.todo.MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:background="#android:color/white"
android:gravity="center_horizontal|center_vertical">
<TextView
android:id="#+id/title_tv"
android:text="#string/to_do"
android:textSize="22sp"
android:textStyle="bold"
android:textColor="#android:color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"/>
<TextView
android:id="#+id/date_tv"
android:textSize="16sp"
android:textColor="#android:color/darker_gray"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/colorGray"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical">
<TextView
android:text="#string/what_do_you_want_to_do_today"
android:textSize="16sp"
android:textColor="#android:color/darker_gray"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"/>
<TextView
android:text="#string/start_adding_items_to_your_to_do_list"
android:textSize="16sp"
android:textColor="#android:color/darker_gray"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
<FrameLayout
android:id="#+id/transitions_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible">
<Button
android:id="#+id/add_btn"
android:layout_width="200dp"
android:layout_height="60dp"
android:text="#string/add_item"
android:textSize="18sp"
android:paddingLeft="20dp"
android:textColor="#android:color/white"
android:drawableLeft="#drawable/ic_add_24dp"
android:background="#drawable/rounded_black_bg"
android:layout_gravity="center"/>
<EditText
android:id="#+id/item_input_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:minHeight="50dp"
android:layout_marginLeft="#dimen/margin_30dp"
android:layout_marginRight="#dimen/margin_30dp"
android:paddingLeft="#dimen/dimen_20dp"
android:paddingRight="#dimen/dimen_50dp"
android:textColor="#android:color/black"
android:inputType="text"
android:background="#drawable/rounded_edit_text"
android:layout_gravity="center"/>
</FrameLayout>
</RelativeLayout>
Preview is
Java code is
public class MainActivity extends AppCompatActivity {
private EditText mItemInputEditText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView dateTextView = (TextView) findViewById(R.id.date_tv);
mItemInputEditText = (EditText) findViewById(R.id.item_input_et);
final Button addButton = (Button) findViewById(R.id.add_btn);
final ViewGroup transitionsContainer = (ViewGroup) findViewById(R.id.transitions_container);
mItemInputEditText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
TransitionSet set = new TransitionSet()
.addTransition(new Fade())
.setInterpolator(new FastOutLinearInInterpolator());
TransitionManager.beginDelayedTransition(transitionsContainer, set);
}
addButton.setVisibility(View.VISIBLE);
mItemInputEditText.setVisibility(View.GONE);
}
});
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
TransitionSet set = new TransitionSet()
.addTransition(new Fade())
.setInterpolator(new FastOutLinearInInterpolator());
TransitionManager.beginDelayedTransition(transitionsContainer, set);
}
addButton.setVisibility(View.GONE);
mItemInputEditText.setVisibility(View.VISIBLE);
}
});
SimpleDateFormat dt = new SimpleDateFormat("EEE d MMM yyyy");
dateTextView.setText(dt.format(new Date()));
}
}
but with this code the resulting transition is
As you can see this is bit weird as compared to expected one, can any one suggest me some changes to get the desired transitions.
Transitions API is surely nice thing, but it cannot solve each and everything for you. You haven't instructed how to perform that animation to the transition framework, how come it would understand what is the final animation that you want to perform?
I'm not sure this animation can be achieved using solely Transitions API. Instead, you can stick to standard animations APIs, e.g. ValueAnimator.
The animation consists of several stages. When a button is clicked, you want it to become slightly wider, also losing its transparency. And after this is done, you want the EditText to come into the scene and be animated to its final value starting from the width, where the button was landed at.
So, inside button click listener:
#Override
public void onClick(View v) {
final int from = addButton.getWidth();
final int to = (int) (from * 1.2f); // increase by 20%
final LinearInterpolator interpolator = new LinearInterpolator();
ValueAnimator firstAnimator = ValueAnimator.ofInt(from, to);
firstAnimator.setTarget(addButton);
firstAnimator.setInterpolator(interpolator);
firstAnimator.setDuration(DURATION);
final ViewGroup.LayoutParams params = addButton.getLayoutParams();
firstAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
params.width = (Integer) animation.getAnimatedValue();
addButton.setAlpha(1 - animation.getAnimatedFraction());
addButton.requestLayout();
}
});
firstAnimator.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
// reset alpha channel
addButton.setAlpha(1.0f);
addButton.setVisibility(View.GONE);
mItemInputEditText.setVisibility(View.VISIBLE);
ValueAnimator secondAnimator = ValueAnimator.ofInt(to, editTextWidth);
secondAnimator.setTarget(mItemInputEditText);
secondAnimator.setInterpolator(interpolator);
secondAnimator.setDuration(DURATION);
final ViewGroup.LayoutParams params = mItemInputEditText.getLayoutParams();
secondAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
params.width = (Integer) animation.getAnimatedValue();
mItemInputEditText.requestLayout();
}
});
secondAnimator.start();
}
});
firstAnimator.start();
}
Similar actions are performed when coming back from EditText to button:
#Override
public void onClick(View view) {
final int from = mItemInputEditText.getWidth();
final int to = (int) (from * 0.8f);
final LinearInterpolator interpolator = new LinearInterpolator();
ValueAnimator firstAnimator = ValueAnimator.ofInt(from, to);
firstAnimator.setTarget(mItemInputEditText);
firstAnimator.setInterpolator(interpolator);
firstAnimator.setDuration(DURATION);
final ViewGroup.LayoutParams params = mItemInputEditText.getLayoutParams();
firstAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
params.width = (Integer) animation.getAnimatedValue();
mItemInputEditText.requestLayout();
}
});
firstAnimator.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
mItemInputEditText.setVisibility(View.GONE);
addButton.setVisibility(View.VISIBLE);
ValueAnimator secondAnimator = ValueAnimator.ofInt(to, buttonWidth);
secondAnimator.setTarget(addButton);
secondAnimator.setInterpolator(interpolator);
secondAnimator.setDuration(DURATION);
final ViewGroup.LayoutParams params = addButton.getLayoutParams();
secondAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
params.width = (Integer) animation.getAnimatedValue();
addButton.setAlpha(animation.getAnimatedFraction());
addButton.requestLayout();
}
});
secondAnimator.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationStart(Animator animation) {
addButton.setAlpha(0.0f);
}
});
secondAnimator.start();
}
});
firstAnimator.start();
}
editTextWidth and buttonWidth are initial sizes of views:
private int editTextWidth, buttonWidth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
// `transitions_container` is the parent of both `EditText` and `Button`
// Thus, posting on it ensures that both of those views are laid out when this runnable is executed
findViewById(R.id.transitions_container).post(new Runnable() {
#Override
public void run() {
editTextWidth = mItemInputEditText.getWidth();
// `mItemInputEditText` should be left visible from XML in order to get measured
// setting to GONE after we have got actual width
mItemInputEditText.setVisibility(View.GONE);
buttonWidth = addButton.getWidth();
}
});
}
Here's the output:
You can have the patch file with changes here.
First, change your Layout XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="app.itc.org.todo.MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:background="#android:color/white"
android:gravity="center_horizontal|center_vertical">
<TextView
android:id="#+id/title_tv"
android:text="#string/to_do"
android:textSize="22sp"
android:textStyle="bold"
android:textColor="#android:color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"/>
<TextView
android:id="#+id/date_tv"
android:textSize="16sp"
android:textColor="#android:color/darker_gray"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/colorGray"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical">
<TextView
android:text="#string/what_do_you_want_to_do_today"
android:textSize="16sp"
android:textColor="#android:color/darker_gray"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"/>
<TextView
android:text="#string/start_adding_items_to_your_to_do_list"
android:textSize="16sp"
android:textColor="#android:color/darker_gray"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
<FrameLayout
android:id="#+id/transitions_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible">
<include layout="#layout/a_scene" />
</FrameLayout>
</RelativeLayout>
Then create your first scene for the button.
The layout for the first scene is defined as follows:
res/layout/a_scene.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scene_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/add_btn"
android:layout_width="200dp"
android:layout_height="60dp"
android:text="#string/add_item"
android:textSize="18sp"
android:paddingLeft="20dp"
android:textColor="#android:color/white"
android:drawableLeft="#drawable/ic_add_24dp"
android:background="#drawable/rounded_black_bg"
android:layout_gravity="center"/>
</RelativeLayout>
The layout for the second scene contains editText (with the same IDs) and is defined as follows:
res/layout/another_scene.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scene_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="#+id/add_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:minHeight="50dp"
android:layout_marginLeft="#dimen/margin_30dp"
android:layout_marginRight="#dimen/margin_30dp"
android:paddingLeft="#dimen/dimen_20dp"
android:paddingRight="#dimen/dimen_50dp"
android:textColor="#android:color/black"
android:inputType="text"
android:background="#drawable/rounded_edit_text"
android:layout_gravity="center"/>
</RelativeLayout>
Java Code:
public class MainActivity extends AppCompatActivity {
private EditText mItemInputEditText;
private Scene mAScene;
private Scene mAnotherScene;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView dateTextView = (TextView) findViewById(R.id.date_tv);
mItemInputEditText = (EditText) findViewById(R.id.item_input_et);
final Button addButton = (Button) findViewById(R.id.add_btn);
final ViewGroup transitionsContainer = (ViewGroup) findViewById(R.id.transitions_container);
// Create the scenes
mAScene = Scene.getSceneForLayout(mSceneRoot, R.layout.a_scene, this);
mAnotherScene = Scene.getSceneForLayout(mSceneRoot, R.layout.another_scene, this);
mItemInputEditText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Transition transition = new ChangeBounds();
TransitionManager.go(mAScene, transition);
}
addButton.setVisibility(View.VISIBLE);
mItemInputEditText.setVisibility(View.GONE);
}
});
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Transition transition = new ChangeBounds();
TransitionManager.go(mAnotherScenetransition);
}
addButton.setVisibility(View.GONE);
mItemInputEditText.setVisibility(View.VISIBLE);
}
});
SimpleDateFormat dt = new SimpleDateFormat("EEE d MMM yyyy");
dateTextView.setText(dt.format(new Date()));
}
}
You can use FABReveal Layout for that. Take a reference from that and change Relative layout to button. and modify it as per requirement.
XML
<com.truizlop.fabreveallayout.FABRevealLayout
android:id="#+id/fab_reveal_layout"
android:layout_width="match_parent"
android:layout_height="#dimen/fab_reveal_height"
>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:backgroundTint="#color/some_color"
android:src="#drawable/some_drawable"
/>
<RelativeLayout
android:id="#+id/main_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
...
</RelativeLayout>
<RelativeLayout
android:id="#+id/secondary_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
...
</RelativeLayout>
</com.truizlop.fabreveallayout.FABRevealLayout>
https://github.com/truizlop/FABRevealLayout
https://github.com/saulmm/Curved-Fab-Reveal-Example
I'm new to Android, and am currently using a ViewFlipper. I'd like to know if it's possible to use only one ImageView? That way I can just create an Arraylist of my images. I think using multiple ImageViews is not a good practice since I have 50+ images.
public void initContent() {
imageArrayList = new ArrayList<Integer>();
imageArrayList.add(R.drawable.pig2);
imageArrayList.add(R.drawable.pig3);
imageArrayList.add(R.drawable.pig4);
imageArrayList.add(R.drawable.pig5);
imageArrayList.add(R.drawable.pig6);
imageArrayList.add(R.drawable.pig7);
imageArrayList.add(R.drawable.pig8);
imageArrayList.add(R.drawable.pig9);
imageArrayList.add(R.drawable.pig10);
imageArrayList.add(R.drawable.pig11);
imageArrayList.add(R.drawable.pig12);
imageArrayList.add(R.drawable.pig13);
imageArrayList.add(R.drawable.pig14);
imageArrayList.add(R.drawable.pig24);
imageArrayList.add(R.drawable.pig25);
imageArrayList.add(R.drawable.theend);
MainActivity.java
public class MainActivity extends Activity {
ViewFlipper viewFlipper;
Button Next;
private Integer images[] = {R.drawable.ic_launcher,
R.drawable.ic_no_image, R.drawable.calendar52};
ImageView imageView1;
private int currImage = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewFlipper = (ViewFlipper) findViewById(R.id.ViewFlipper01);
Next = (Button) findViewById(R.id.Next);
imageView1 = (ImageView) findViewById(R.id.imageView1);
Next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
currImage++;
if (currImage == 3) {
currImage = 0;
}
final ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageResource(images[currImage]);
viewFlipper.showNext();
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/RelativeLayout02"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ViewFlipper
android:id="#+id/ViewFlipper01"
android:layout_width="fill_parent"
android:layout_height="400dp" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#4B0082" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="117dp"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
</ViewFlipper>
</RelativeLayout>
<RelativeLayout
android:id="#+id/RelativeLayout03"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#000000"
android:gravity="center" >
<Button
android:id="#+id/Next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="5dp"
android:layout_marginRight="20dp"
android:text="Next" />
</RelativeLayout>
</LinearLayout>
Yes. Use the frame animation, delete your arraylist. Here is some sample code: Link
I'm a beginner in android and making my first app. Here, I had taken 3 textViews and a button named Add. When I click on this button, I need to show the content of 2 textViews on third textView. I used eventListener and buttonClick event but it doesn't work. Please guide me with the code of Add button.
First create the xml as below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_gravity="center_horizontal"
android:orientation="horizontal"
android:layout_weight="1"
android:id="#+id/singleEmployee"
android:background="#ffffff">
<TextView
android:text="TextView"
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:text="TextView"
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:text="TextView"
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="Button"
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
And then use this Java code to done your desire action:
public class MainActivity extends Activity {
/** Called when the activity is first created. */
TextView tv1,tv2,tv3;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testing);
tv1 = findViewById(R.id.textView1);
tv2 = findViewById(R.id.textView2);
tv3 = findViewById(R.id.textView3);
tv1.setText("Hello");
tv2.setText("World");
Button add = findViewById(R.id.button1);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv3.setText("");
String tvValue1 = tv1.getText().toString();
String tvValue2 = tv2.getText().toString();
tv3.setText("Value of First Text is: "+tvValue1+".And the value of second TextView is: "+tvValue2);
}
});
}
I am wanting to create a set of checkboxes dynamically during run time in my android application. When the application runs nothing shows up except the button. What am i forgetting? Thanks in advance!
public class DataNotificationSurvey extends Activity {
private Date timeStamp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.datanotifylayout);
final Button notifySubmitButton = (Button) findViewById(R.id.notifySubmitButton);
TableLayout t1 = (TableLayout)findViewById(R.id.notificationTableLayout);
for(int i = 0; i < 5; i++) {
TableRow tr = new TableRow(this);
CheckBox chk = new CheckBox(this);
chk.setText(Integer.toString(i));
tr.addView(chk);
t1.addView(tr);
}
notifySubmitButton.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
//My xml layout, will be changing this later to the one posted below to see if it works.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/notificationLinearLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableLayout
android:id="#+id/notificationTableLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:text="Static Button"/>
</TableRow>
</TableLayout>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/notifySubmitButton"
android:text="Submit"></Button>
</LinearLayout>
This works fine for me.
public class DynamicTableRowWithCheckBox extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button notifySubmitButton = (Button) findViewById(R.id.myButton);
TableLayout table = (TableLayout) findViewById(R.id.myTable);
for (int i = 0; i < 3; i++)
{
TableRow row = new TableRow(this);
CheckBox chk = new CheckBox(this);
chk.setText(Integer.toString(i));
row.addView(chk);
table.addView(row);
}
notifySubmitButton.setOnClickListener(
new View.OnClickListener()
{
#Override
public void onClick(View v)
{
finish();
}
});
}
}
<?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">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<Button
android:id="#+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Close" />
<TableLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/myTable" />
</LinearLayout>
Change your XML to:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/notificationLinearLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableLayout
android:id="#+id/notificationTableLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow>
<Button
android:text="Static Button" />
</TableRow>
</TableLayout>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/notifySubmitButton"
android:text="Submit"></Button>
</LinearLayout>
This removes the layout_height of your TableLayout to wrap_content, and it removes the height and width settings of your TableRow, because, "The children of a TableRow do not need to specify the layout_width and layout_height attributes in the XML file. TableRow always enforces those values to be respectively MATCH_PARENT and WRAP_CONTENT."