I have a toggleButton that should run code when I press it and more when I let go. However it's not working like that at all, just like a toggle button normally does, I press it and it changes state, it doesn't call the touchlistener code at all.
In my in_call_card.xml I have:
<ToggleButton
android:id="#+id/PTT_button5"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:text="#string/ptt5"
android:layout_weight="50"
android:textOn="Push To Talk On"
android:textOff="Push To Talk Off"
android:background="#drawable/btn_lightblue_glossy"
android:textColor="#android:color/white"
android:textSize="15sp"
/>
in onCreate I have setContent to in_card_main.xml so I have to inflate the layout to use the button in in_call_card.xml. Here is my code, but it never runs when i am pressing the button:
LayoutInflater inflater = getLayoutInflater();
View otherLayout = inflater.inflate(R.layout.in_call_card, null);
final ToggleButton toggle = (ToggleButton) otherLayout.findViewById(R.id.PTT_button5);
toggle.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Toast.makeText(getApplicationContext(), "woot7", Toast.LENGTH_SHORT).show();
//may need to change to loop and send all ids.
int callId = 0;
for (SipCallSession callInfo : callsInfo) {
callId = callInfo.getCallId();
Log.e(TAG, ""+callInfo.getCallId());
}
final int id= callId;
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
toggle.setBackgroundResource(R.drawable.btn_blue_glossy);
toggle.setChecked(true);
OnDtmf(0, 17, 10);
OnDtmf(0, 16, 9);
OnDtmf(id, 17, 10);
OnDtmf(id, 16, 9);
break;
case MotionEvent.ACTION_UP:
// Toast.makeText(getApplicationContext(), "woot", Toast.LENGTH_LONG).show();
//release
toggle.setBackgroundResource(R.drawable.btn_lightblue_glossy);
toggle.setChecked(false);
OnDtmf(0, 18, 11);
OnDtmf(0, 18, 11);
OnDtmf(id, 18, 11);
OnDtmf(id, 18, 11);
break;
}
return false;
}
});
EDIT:
in_call_main:
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2010 Regis Montoya (aka r3gis - www.r3gis.fr)
This file is part of CSipSimple.
CSipSimple is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
If you own a pjsip commercial license you can also redistribute it
and/or modify it under the terms of the GNU Lesser General Public License
as an android library.
CSipSimple is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CSipSimple. If not, see <http://www.gnu.org/licenses/>.
-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/mainFrame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ViewStub
android:id="#+id/dropZones"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout="#layout/in_call_drop_zones" />
<LinearLayout
android:id="#+id/inCallContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="3"
android:orientation="horizontal" >
<!--
Can we do better than using two linear layouts here?
The problem is that RelativeLayout doesn't allow to use weight and
plays bad with hidden childs and above/below only support one id
-->
<!-- Grid for active calls -->
<com.csipsimple.ui.incall.InCallInfoGrid
android:id="#+id/activeCallsGrid"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="2"
android:paddingBottom="4dip"
android:paddingTop="4dip"
tools:ignore="NestedWeights" />
<!-- Grid for held calls -->
<com.csipsimple.ui.incall.InCallInfoGrid
android:id="#+id/heldCallsGrid"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/in_call_held_background"
android:padding="4dip"
android:visibility="gone" />
</LinearLayout>
<!-- Answer slider/buttons -->
<com.csipsimple.ui.incall.locker.InCallAnswerControls
android:id="#+id/inCallAnswerControls"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="2"
android:visibility="gone" />
<!-- Wraps split action bar manually managed -->
<com.csipsimple.ui.incall.InCallControls
android:id="#+id/inCallControls"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/incall_bottom_bar_bg" />
</LinearLayout>
<com.csipsimple.ui.incall.locker.ScreenLocker
android:id="#+id/lockerOverlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#8000"
android:visibility="gone" >
<!--
<TextView android:id="#+id/lockerIcon"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:gravity="center" android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" android:layout_marginBottom="70dip"
android:text="#string/touch_lock" android:textAppearance="?android:attr/textAppearanceLarge"
android:background="#drawable/dialpad_lock" />
-->
</com.csipsimple.ui.incall.locker.ScreenLocker>
<!-- <ToggleButton
android:id="#+id/PTT_button3"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:text="#string/ptt3"
android:layout_weight="50"
android:textOn="Push To Talk On"
android:textOff="Push To Talk Off"
android:background="#drawable/btn_lightblue_glossy"
android:textColor="#android:color/white"
android:textSize="15sp"
/> -->
</RelativeLayout>
in_call_card:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/call_card_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:padding="2dip"
tools:ignore="Overdraw" >
<com.csipsimple.widgets.MaxScaleImageView
android:id="#+id/contact_photo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/call_action_bar"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:contentDescription="#string/empty_description"
android:scaleType="centerCrop"
android:src="#drawable/ic_contact_picture_180_holo_light" />
<RelativeLayout
android:id="#+id/contact_name_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#9e808080"
android:gravity="center_vertical" >
<!--
<EditText
android:id="#+id/editText1"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
-->
<com.csipsimple.widgets.MarqueeTextView
android:id="#+id/contact_name_display_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:padding="5dip"
android:singleLine="true"
android:textColor="#android:color/white"
android:textSize="25sp" />
<com.csipsimple.widgets.MarqueeTextView
android:id="#+id/contact_name_sip_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/contact_name_display_name"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:padding="2dip"
android:singleLine="true"
android:textColor="#android:color/white"
android:textSize="15sp" />
<Chronometer
android:id="#+id/elapsedTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:textSize="20sp" />
</RelativeLayout>
<TextView
android:id="#+id/call_status_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/contact_name_bar"
android:layout_marginTop="1dip"
android:background="#c818617b"
android:gravity="center_vertical|right"
android:padding="3dip"
android:text="#string/call_state_calling"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textSize="15sp" />
<RelativeLayout
android:id="#+id/call_secure_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/call_status_text"
android:layout_marginTop="1dip"
android:background="#c8d4aa00" >
<ImageView
android:id="#+id/call_secure_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_margin="2dip"
android:contentDescription="#string/empty_description"
android:src="#drawable/stat_sys_vp_phone_call" />
<TextView
android:id="#+id/call_secure_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="#+id/call_secure_icon"
android:padding="2dip"
android:textColor="#android:color/white"
android:textSize="15sp" />
</RelativeLayout>
<FrameLayout
android:id="#+id/call_action_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/end_call_bar"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignWithParentIfMissing="true"
android:layout_marginTop="2dip"
android:background="#000000" />
<LinearLayout
android:id="#+id/end_call_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginTop="2dip"
android:background="#drawable/end_call_background"
android:orientation="horizontal" >
<!-- android:onClick="pushtotalk3" -->
<ToggleButton
android:id="#+id/PTT_button5"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:text="#string/ptt5"
android:layout_weight="50"
android:textOn="Push To Talk On"
android:textOff="Push To Talk Off"
android:background="#drawable/btn_lightblue_glossy"
android:textColor="#android:color/white"
android:textSize="15sp"
/>
<ImageButton
android:id="#+id/endButton"
style="?attr/actionButtonStyle"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="50"
android:contentDescription="#string/done"
android:minHeight="64dip"
android:src="#drawable/ic_end_call" />
</LinearLayout>
</RelativeLayout>
It's not your listener. Your listener works fine, with ToggleButton or any other kind of view. With the return false at the end, your onTouchListener should get called and an onClickListener- if one exists - gets called.
The reason why you get nothing when you press the button is, that the button is not on your screen. You inflate it in your code, but without a parent and I cant see any code, that actually adds the inflated ToggleButton to any layout. So, whatever you have on your screen that you press, it's not that button your onTouchListener listens.
I recommend to make the ToggleButtonpart of you layout or - depending on your App Design - create a new Layout and Activity for this.
EDIT According to the posted layouts.
What I understand is, that you have two full layouts: in_card_main.xml and in_call_card.xml but only one Activity. Is that right? If yes, the solution is to have one Activity for each layout and call the Activities with startActivity().
If you use setContentView(int) you have to use findViewById(int) because the activity inflates the XML for you.
For the moment you are putting a listener on a button which is not displayed, because you get it from the layout you have inflated, but not from the one the activity hold.
2 solutions :
Add the missing ending setContentView(otherLayout) call to tell your activity to display the layout you have inflated
Use setContentView(R.layout.in_call_card) and then retrieve the view by using only findViewById(R.id.PTT_button5); to tell you activity to display the layout 'in_call_card' before asking it to search for the view with the id 'R.id.PTT_button5' in its hierarchy
Related
I'm developing a simple BMI calculator for android. I have input fields for lbs, feet, and inches. I want to give users the ability to switch from imperial units to metric, so I added a spinner widget with imperial and metric as options. If the user changes the spinner to metric, I want to be able to change the XML layout so that the input fields are kg and cm. What is the best way to change the XML layout when the user switches between metric and imperial?
I tried creating a new activity that loads the metric layout XML when the user switches to it, but it seems like a poor design to create a new activity just to switch units. I also tried simply using setContentLayout to set the layout to metric.xml if spinner has metric selected and imperial.xml if imperial is selected. This sort of works but the ads and spinner widgets are no longer configured after switching between the layouts. I am still new to android development so I am wondering what the best approach to this would be? I am simply trying to make a slight layout change when the user switches between the different unit options. Any tips appreciated.
Normally, I would put everything inside activity layout xml file and toggle based on the condition like this
<TextView
android:id="#+id/feet"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="gone"/>
<TextView
android:id="#+id/inch"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="gone"/>
And in your spinner adapter selection listener, you can toggle the view you want to show:
feet.setVisibility(View.VISIBLE)
You can use FrameLayout. It allow you to make to different layouts on each other (different views can be located on another views). For example:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#FFFFFF">
<RelativeLayout
android:id="#+id/digital_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginLeft="294dp"
android:layout_marginTop="64dp">
<Spinner
android:id="#+id/spinner101"
android:layout_width="135dp"
android:layout_height="wrap_content"
android:layout_below="#+id/checkBox100"
android:layout_toEndOf="#+id/textView100"
android:visibility="visible"/>
<TextView
android:id="#+id/textView102"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/spinner101"
android:text="Value pulse"
android:textSize="20dp"
android:visibility="visible"/>
<TextView
android:id="#+id/textView107"
android:layout_width="wrap_content"
android:layout_height="22dp"
android:layout_alignStart="#+id/textView106"
android:layout_below="#+id/textView103"
android:gravity="center"
android:text="11%"
android:visibility="visible"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/analog_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<EditText
android:id="#+id/editText3"
android:layout_width="136dp"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/spinner2"
android:layout_below="#+id/checkBox3"
android:ems="10"
android:hint="Constant"
android:inputType="textPersonName"
android:textSize="15dp"
android:visibility="invisible" />
<Button
android:id="#+id/button6"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentStart="true"
android:layout_below="#+id/checkBox9"
android:layout_marginLeft="20dp"
android:onClick="onResetClick"
android:text="Reset" />
<View
android:id="#+id/delitel1"
android:layout_width="234dp"
android:layout_height="2dp"
android:layout_alignStart="#+id/textView7"
android:layout_below="#+id/button6"
android:layout_marginTop="10dp"
android:background="#004f9b" />
<TextView
android:id="#+id/textView14"
android:layout_width="135dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignTop="#+id/delitel1"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="Language:"
android:textSize="20dp"
android:visibility="invisible" />
</RelativeLayout>
So, you need to give different id to each RelativeLayouts. Here there are android:id="#+id/digital_layout" and android:id="#+id/analog_layout".
And in code need to write handler for your spinner:
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
digital_layout.setVisibility(View.INVISIBLE);
analog_layout.setVisibility(View.VISIBLE);
break;
case 1:
digital_layout.setVisibility(View.VISIBLE);
analog_layout.setVisibility(View.INVISIBLE);
break;
etc.
}
}
Are there any android library that do:
Shows drawables in either a grid or scroll view.
Allows user to select one of the images and shows his selection.
Allows me to get the selected drawable and show it to the user.
Please see the screenshots to see what I'm trying to achieve.
I have done something like you are thinking about. I will show you that if is useful to you (I think will be, but you must adapt this):
Context: I have created a simple game based in Space Invaders. My game has a main menu (main activity) where the player can press 'PLAY' or scan elect some 'OPTIONS', though a popup.
App main menu image
1 screen: Main menu.
2 screen: Popup appears when I press 'OPTIONS' button. This popup let me choose graphic options (change game background, ally spaceship model and enemy spaship model).
MainActivity.java
optionBoton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
soundMenu.start();
LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View viewPopup = inflater.inflate(R.layout.popup_activity, null);
popup = new PopupWindow(
viewPopup,
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
popup.showAtLocation(layoutPrincipal, Gravity.BOTTOM, 0, 0);
ImageButton closePop = (ImageButton) vistaPopup.findViewById(R.id.volver_boton);
closePop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
soundMenu.start();
popup.dismiss();
optionBoton.setVisibility(View.VISIBLE);
}
});
optionBoton.setVisibility(View.INVISIBLE);
}
});
This code will be inside onCreate method.
Now, you need to design your own XML popup (if you decide to create a popup). Showing a popup is easy, clean and dynamic way to change the graphic options.
popup_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#839ceaac"
android:orientation="vertical">
<ImageButton
android:id="#+id/volver_boton"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_marginRight="2dp"
android:layout_marginTop="3dp"
android:background="#android:color/transparent"
android:scaleType="centerCrop"
android:src="#drawable/wcerrar" />
<TextView
android:id="#+id/options_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="6dp"
android:layout_marginTop="5dp"
android:text="Opciones gráficas"
android:textSize="20dp"
android:textStyle="bold" />
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_below="#id/options_title"
android:background="#9dc8a6" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/options_title"
android:layout_marginBottom="8dp"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="#+id/back_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3dp"
android:text="Fondo"
android:textSize="15sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/back_1"
android:layout_width="40dp"
android:layout_height="60dp"
android:layout_below="#id/back_title"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3dp"
android:adjustViewBounds="true"
android:onClick="uploadBack"
android:scaleType="centerCrop"
android:src="#drawable/fondo" />
<ImageView
android:id="#+id/back_2"
android:layout_width="40dp"
android:layout_height="60dp"
android:layout_below="#id/back_1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3dp"
android:adjustViewBounds="true"
android:onClick="uploadBack"
android:scaleType="centerCrop"
android:src="#drawable/fondo1" />
<ImageView
android:id="#+id/back_3"
android:layout_width="40dp"
android:layout_height="60dp"
android:layout_below="#id/back_2"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3dp"
android:adjustViewBounds="true"
android:onClick="uploadBack"
android:scaleType="centerCrop"
android:src="#drawable/fondo3" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="#+id/spaceship_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3dp"
android:text="Spaceship"
android:textSize="15sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/spaceship_1"
android:layout_width="40dp"
android:layout_height="60dp"
android:layout_below="#id/spaceship_title"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3dp"
android:adjustViewBounds="true"
android:onClick="uploadSpaceship"
android:src="#drawable/diseno11" />
<ImageView
android:id="#+id/spaceship_2"
android:layout_width="40dp"
android:layout_height="60dp"
android:layout_below="#id/spaceship_1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3dp"
android:adjustViewBounds="true"
android:onClick="uploadSpaceship"
android:src="#drawable/diseno21" />
<ImageView
android:id="#+id/spaceship_3"
android:layout_width="40dp"
android:layout_height="60dp"
android:layout_below="#id/spaceship_2"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3dp"
android:adjustViewBounds="true"
android:onClick="uploadSpaceship"
android:src="#drawable/diseno31" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="#+id/enemy_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3dp"
android:text="Enemies"
android:textSize="15sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/enemy_1"
android:layout_width="40dp"
android:layout_height="60dp"
android:layout_below="#id/enemy_title"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3dp"
android:adjustViewBounds="true"
android:onClick="uploadEnemy"
android:rotation="180"
android:src="#drawable/enemigodiseno11" />
<ImageView
android:id="#+id/enemy_2"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_below="#id/enemy_1"
android:layout_marginBottom="3dp"
android:layout_centerHorizontal="true"
android:src="#drawable/enemigodiseno21"
android:onClick="uploadEnemy"/>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
Anyway, if you choose to don't use a popup, I will show you how you can send some data to another activity in Android development:
When you press on a graphic option, look at the call "android:onclick". This allows to call a java method to capture the option selected.
Look a simple example of this:
Method example
public void uploadBack(View view) {
switch (view.getId()) {
case R.id.back_1:
result[0] = "back";
break;
case R.id.back_2:
result[0] = "back1";
break;
case R.id.back_3:
result[0] = "back3";
break;
}
}
As you can see, I have an array of Strings with 3 positions. When I click an ImageView, I call the method wich changes the result[i] value. When I press 'PLAY' and I call the game activity, I use putExtra method to send these options and change the game settings.
I hope this serve as a help for your problem.
Have a nice day!
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" >
<Button
android:id="#+id/insertButtonVIEW"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="insert" />
<Button
android:id="#+id/removeButtonVIEW"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="remove" />
</LinearLayout>
</RelativeLayout>
Sometimes, depending on a situaiton, I am hiding removeButtonVIEW using the method setVisibility(View.INVISIBLE);
When that happens, I want to my insertButtonVIEW to take the whole width of the screen (when the two are present, each one takes 50% of the screen).
Since this might or might not happen, I am doing these changes programmatically.
I tried the following methd which worked, however, it is causing for all the interface to go jumbo mumbo, as everything's location on the screen is getting mixed.
modifyButton.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
Any hints ?
UPDATE 1 after applying the answer :
I am using the following code to set the button to invisible.
Bundle extras;
extras = getIntent().getExtras();
String answer = extras.getString("answer");
if(answer.equalsIgnoreCase("yes")
{
insertButton.setEnabled(true);
insertButton.setText("INSERT");
removeButton.setVisibility(View.INVISIBLE);
}
else
{
//whatever
}
// I Have modify your code now try this one and you don't required set LayoutParams ().
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" >
<Button
android:id="#+id/insertButtonVIEW"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="insert" />
<Button
android:id="#+id/removeButtonVIEW"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="remove" />
</LinearLayout>
</RelativeLayout>
if(answer.equalsIgnoreCase("yes")
{
insertButton.setEnabled(true);
insertButton.setText("INSERT");
removeButton.setVisibility(View.GONE);
}
else
{
//whatever
}
Use
setVisibility(View.GONE);
*Remove this *
modifyButton.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
And set
android:layout_width="0dp"
in both button
I am trying to show a pair of hidden buttons (using setVisibility(View.VISIBLE), within a RelativeLayout), but it doesn't always work. The button shows OK on a Galaxy Tab 10.1" but not in a smaller tablet (not sure which model), nor on an Android 4.0 emulator.
I randomly discovered that, for a certain TextView t, the following code causes the buttons to become visible:
t.setText(t.getText());
...
button.setVisibility(View.VISIBLE);
t is located in the same RelativeLayout but is not related to the buttons (their locations are independent and non-overlapping).
Edit: In case some Android dev wants to track this down...
I was able to reduce the code to the following layout that exhibits the problem on an Android 4.0.3 emulator but not a Galaxy Tab. I found that I need a SurfaceView or the problem does not occur (for example, change it to TextView and the problem disappears).
<?xml version="1.0" encoding="utf-8"?>
<!-- layout/test.xml -->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<SurfaceView
android:id="#+id/mapCtrl"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/bottomPanel"
android:text="Placeholder"
android:layout_marginTop="18dip" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="#string/map_mode_title" />
<!--=================================================-->
<!-- Bottom bar: current road name and current speed -->
<LinearLayout
android:id="#+id/bottomPanel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#f228"
android:orientation="horizontal"
android:textColor="#ffff" >
<Button
android:id="#+id/btnNavMode"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="3dip"
android:textColor="#fff"
android:text="Switch to\nNav Mode" />
<RelativeLayout
android:id="#+id/currentStreetPanel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:clickable="true"
android:orientation="vertical" >
<TextView
android:id="#+id/currentStreetHdg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Current street"
android:textColor="#fff"
android:textSize="10dip" />
<TextView
android:id="#+id/currentStreet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/currentStreetHdg"
android:layout_marginTop="-8dip"
android:singleLine="true"
android:text="Current street"
android:textColor="#fff"
android:textSize="30dip" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/RelativeLayout2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#ff606060"
android:orientation="vertical" >
<TextView
android:id="#+id/yourSpeedHdg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="3dip"
android:text="Your speed"
android:textColor="#fff"
android:textSize="10dip" />
<TextView
android:id="#+id/speed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/yourSpeedHdg"
android:layout_marginLeft="3dip"
android:layout_marginTop="-8dip"
android:text="0"
android:textColor="#fff"
android:textSize="30dip" />
<TextView
android:id="#+id/speedUnit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/speed"
android:layout_marginLeft="5dip"
android:layout_toRightOf="#+id/speed"
android:text="kph"
android:textColor="#fff"
android:textSize="18dip" />
</RelativeLayout>
</LinearLayout>
<!--================-->
<!-- On-map buttons -->
<Button
android:id="#+id/btnClearRoute"
android:background="#F00"
android:textColor="#fff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear\nroute"/>
<ZoomControls
android:id="#+id/zoomControls"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/mapCtrl"
android:layout_centerHorizontal="true"
android:layout_marginBottom="-25dip"
android:orientation="horizontal" />
<Button
android:id="#+id/btnFindRoute"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/mapCtrl"
android:layout_alignParentRight="true"
android:layout_marginRight="2dip"
android:layout_marginBottom="65dip"
android:text="Route to selected location"
android:textSize="17dip"/>
<Button
android:id="#+id/btnUnselect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/btnFindRoute"
android:layout_alignTop="#+id/btnFindRoute"
android:layout_alignParentLeft="true"
android:layout_marginLeft="2dip"
android:text="Unselect" />
<LinearLayout
android:id="#+id/showMePanel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/btnFindRoute"
android:layout_alignRight="#+id/btnFindRoute"
android:layout_alignLeft="#+id/btnFindRoute"
android:padding="4dip"
android:background="#bbbb"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show me..."
android:textColor="#fff"/>
<Button
android:id="#+id/btnShowVehicle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="My car"/>
<Button
android:id="#+id/btnShowRoute"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="The route"/>
<Button
android:id="#+id/btnShowDestination"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Destination"/>
<Button
android:id="#+id/btnShowMap"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="The map"/>
</LinearLayout>
</RelativeLayout>
The Activity class simply toggles the visibility of the two buttons when any of the buttons are clicked. Again, on some devices it works, on others it does not.
package mentor.simplegps;
import android.app.*;
import android.os.Bundle;
import android.view.*;
import android.widget.*;
public class TestActivity extends Activity implements View.OnClickListener
{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.test);
boilerplate();
setVisibilities();
}
Button _btnShowMap, _btnShowVehicle, _btnShowRoute, _btnShowDestination;
Button _btnUnselect, _btnFindRoute, _btnNavMode;
TextView _title;
void boilerplate()
{
_btnUnselect = attachBtn(R.id.btnUnselect);
_btnShowMap = attachBtn(R.id.btnShowMap);
_btnShowVehicle = attachBtn(R.id.btnShowVehicle);
_btnShowRoute = attachBtn(R.id.btnShowRoute);
_btnShowDestination = attachBtn(R.id.btnShowDestination);
_btnFindRoute = attachBtn(R.id.btnFindRoute);
_btnNavMode = attachBtn(R.id.btnNavMode);
_title = (TextView)findViewById(R.id.title);
}
private Button attachBtn(int btnId) {
Button b = (Button)findViewById(btnId);
b.setOnClickListener(this);
return b;
}
boolean haveSel;
public void onClick(View v)
{
haveSel = !haveSel;
setVisibilities();
}
void setVisibilities()
{
_btnFindRoute.setVisibility(haveSel ? View.VISIBLE : View.INVISIBLE);
_btnUnselect.setVisibility (haveSel ? View.VISIBLE : View.INVISIBLE);
// Fixes the problem
//_title.setText(_title.getText());
}
}
SurfaceView is the sole culprit (of course, this also applies to GLSurfaceView, RSSurfaceView and VideoView, all of which inherits from SurfaceView). It exposes lots of weird behaviours when dealing with other views on top of it. Playing with View.setVisibility() is one of those issues. Clearly, SurfaceView has not been designed to be used with other views (even though the official doc says it ought to be) but as a standalone view for videos, games or OpenGL stuffs.
For the visibility issue, I've found that using View.GONE instead of View.INVISIBLE resolve it. If you don't want to use GONE, try changing the focus for example (and back to the one that had focus before), or changing other states. The goal is to wake up the underlying UI system somehow.
In short: when something weird happens with your views and you have a SurfaceView (or subclass) somewhere, try replacing it with something else so you don't lose hours searching what you're doing wrong when you're doing it right (and no false beliefs). This way, you know SurfaceView is to blame and you can hack around it with beautiful comments to piss on it without qualms.
For the record: I had this problem, tried a bunch of random stuff (thanks Alex!), and in my case what solved it was doing seekBar.requestLayout() directly after the setVisible on the very seekbar that was refusing to show.
This is my Solution
setAlpha(0)
btnName.setAlpha(0)
Is working for all views like => Buttons - Images - Texts and ...
In my case View.VISIBLE/View.GONE was not working always. When I switched my toggle to View.VISIBLE/View.INVISIBLE it started to work as intended.
I (annoyingly) had similar difficulty with having a button on top of a SurfaceView preview and had to put the Button in a RelativeLayout and make the RelativeLayout VISIBLE/INVISIBLE. Might be worth a shot for anyone else having the same issue.
...And I also had to programatically call the layout to be brought to from: buttonLayout.bringToFront() right after findViewById.
I'm new to android development(v 4.0, API 14) and stuck in implementing orientation change.
I've added the following line in manifest file
android:configChanges="orientation|keyboardHidden|screenSize">
But it looks like screenSize parameter is not working so I have to manually write these two methods:
public void ChangetoLandscape() {
ed.setTranslationX(300.0f);
btnindex.setTranslationX(300.0f);
btngainer.setTranslationX(300.0f);
btnloser.setTranslationX(300.0f);
lsym.setTranslationX(200.0f);
ltable.setTranslationX(300.0f);
tv1.setTranslationX(290.0f);
tv2.setTranslationX(300.0f);
tv4.setTranslationX(300.0f);
mySimpleXYPlot.setScaleX(3.0f);
mySimpleXYPlot.setScaleY(0.8f);
mySimpleXYPlot.setPlotMarginLeft(50.0f);
mySimpleXYPlot.setTranslationX(60.0f);
mySimpleXYPlot.setTranslationY(-70.0f);
sv1.setTranslationY(-80.0f);
}
public void ChangetoPortrait() {
//NOTE THAT IF I DON'T WRITE BELOW CODE,
//SWITCHING TO PORTRAIT MODE AFTER LANDSCAPE MODE RUINS THE WHOLE LAYOUT.
ed.setTranslationX(-1.0f);
btnindex.setTranslationX(-1.0f);
btngainer.setTranslationX(-1.0f);
btnloser.setTranslationX(-1.0f);
lsym.setTranslationX(-1.0f);
ltable.setTranslationX(-1.0f);
tv1.setTranslationX(-1.0f);
tv2.setTranslationX(-1.0f);
tv4.setTranslationX(-1.0f);
mySimpleXYPlot.setScaleX(1.0f);
mySimpleXYPlot.setScaleY(1.0f);
mySimpleXYPlot.setPlotMarginLeft(1.0f);
mySimpleXYPlot.setTranslationX(-1.0f);
mySimpleXYPlot.setTranslationY(-1.0f);
sv1.setTranslationY(-1.0f);
}
.
I'm using RelativeLayout, so I've to manually translate each view to specified position on orientation change.
main.xml
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="#+id/MainLayout">"
<EditText
android:id="#+id/txt1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:hint="Search"
android:layout_marginLeft="220dp"
android:selectAllOnFocus="true"
android:cursorVisible="false" >
</EditText>
<Button
android:id="#+id/btnindex"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:text="Index"
android:layout_marginLeft="140dp"
android:layout_below="#id/txt1"
android:textSize="12dp"/>
<Button
android:id="#+id/btngainer"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:text="Gainer"
android:layout_below="#id/txt1"
android:layout_toRightOf="#id/btnindex"
android:textSize="12dp"/>
<Button
android:id="#+id/btnloser"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:text="Loser"
android:layout_below="#id/txt1"
android:layout_toRightOf="#id/btngainer"
android:textSize="12dp"/>
<TextView
android:id="#+id/tv1"
android:layout_width="75dp"
android:layout_height="wrap_content"
android:layout_marginLeft="150dp"
android:layout_marginTop="100dp"
android:textStyle="bold"
android:gravity="center"
android:textSize="10dp"
android:textColor="#00ff00"
android:text="SYMBOL"/> <!-- 2 similar textviews -->
<ListView
android:id="#+id/ltable"
android:layout_height="wrap_content"
android:layout_marginLeft="150dp"
android:layout_marginTop="70dp"
android:layout_alignParentRight="true"
android:layout_width="match_parent"
android:layout_below="#id/txt1">
</ListView>
<com.androidplot.xy.XYPlot
android:id="#+id/mySimpleXYPlot"
android:layout_width="140dp"
android:layout_height="200dp"
android:layout_alignTop="#+id/btnindex"
title="Index"/>
<ScrollView android:id="#+id/sv1"
android:layout_width="300dp"
android:layout_height="100dp"
android:layout_marginTop="250dp">
<LinearLayout android:id="#+id/LinearLayout02"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:orientation="vertical">
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tv1"
android:text="Scrollview Item 1"/> <!-- 5 similar textviews -->
</LinearLayout>
</ScrollView>
</RelativeLayout>
.
But as showed in below screenshot, in landscape mode, graph which is plotted using android plot is not getting properly displayed. The reason behind this is I've used setScaleX() method bcoz setWidth() is not available. I don't want graph to look stretched. Instead it's width should be increased.
ScrollView is getting displayed properly in portrait mode but in landscape mode, it is not visivle according to it's height which is 100dp.
Edit: After changing android:configChanges="orientation|keyboardHidden|screenSize" to
android:configChanges="orientation|screenSize",
I get this when I switch to landscape mode:
use LayoutParams to place the graph at appropriate location instead of scale() function.