Zoom controls for several images in a relative layout - android

I'm new with android, and I have to make improvements for an existing app. Inside this app we have a relative layout with a group of 15 imageViews, 1 dynamic imageView and two textViews to which I have to program zoom controls for zooming in and out. The last couple of days I've been reading continuously this website to try to find an answer in the existing questions without any results because lots of questions are without an answer.
My biggest problem is that I don't know which frame or layout to use. All the images are placed and working well in the relative layout, but I don't seem to get the zoom controls working with the content and I've read in different threads and questions that the RelativeLayout doesn't work with zoom at all. I thought of maybe using a FrameLayout but I think it has the same problem.
This is the XML code from the layout:
<RelativeLayout
android:id="#+id/relativeLayoutMachine"
android:layout_width="1000dp"
android:layout_height="550dp"
android:layout_above="#+id/relativeLayout1"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#color/brm_back_1" >
<ImageView
android:id="#+id/imageView01"
android:layout_width="82dp"
android:layout_height="67dp"
android:layout_marginLeft="409dp"
android:layout_marginTop="83dp"
android:contentDescription="#id/imageVeuwBandInloopKoker"
android:src="#drawable/lva_bandinloopkoker" />
<ImageView
android:id="#+id/imageView02"
android:layout_width="82dp"
android:layout_height="67dp"
android:layout_marginLeft="409dp"
android:layout_marginTop="149dp"
android:contentDescription="#id/imageViewTussenKoker"
android:src="#drawable/lva_tussenkoker" />
<ImageView
android:id="#+id/imageView03"
android:layout_width="172dp"
android:layout_height="88dp"
android:layout_marginLeft="401dp"
android:layout_marginTop="216dp"
android:contentDescription="#id/imageViewHMI_Graph_04_Flap"
android:src="#drawable/lva_flap_open" />
<ImageView
android:id="#+id/imageView04"
android:layout_width="281dp"
android:layout_height="61dp"
android:layout_marginLeft="401dp"
android:layout_marginTop="303dp"
android:contentDescription="#id/imageViewHMI_Graph_03_Ram"
android:src="#drawable/lva_ram_retour" />
<ImageView
android:id="#+id/imageView05"
android:layout_width="110dp"
android:layout_height="47dp"
android:layout_marginLeft="571dp"
android:layout_marginTop="257dp"
android:contentDescription="#id/imageViewHMI_Graph_01_Unit"
android:src="#drawable/lva_unit" />
<ImageView
android:id="#+id/imageView06"
android:layout_width="51dp"
android:layout_height="54dp"
android:layout_marginLeft="431dp"
android:layout_marginTop="28dp"
android:contentDescription="#id/imageViewHMI_Graph_15_Feeder"
android:src="#drawable/lva_feeder_off" />
<ImageView
android:id="#+id/imageView07"
android:layout_width="90dp"
android:layout_height="40dp"
android:layout_marginLeft="585dp"
android:layout_marginTop="218dp"
android:contentDescription="#id/imageViewHMI_Graph_02_Pump"
android:src="#drawable/lva_main_motor_off" />
<ImageView
android:id="#+id/imageView08"
android:layout_width="199dp"
android:layout_height="124dp"
android:layout_marginLeft="157dp"
android:layout_marginTop="240dp"
android:contentDescription="#id/imageViewChannel"
android:src="#drawable/lva_channel" />
<ImageView
android:id="#+id/imageView09"
android:layout_width="82dp"
android:layout_height="166dp"
android:layout_marginLeft="410dp"
android:layout_marginTop="50dp"
android:contentDescription="#id/imageViewHMI_Graph_013_Turbo"
android:src="#drawable/lva_turbo_off"
android:visibility="invisible" />
<ImageView
android:id="#+id/imageView10"
android:layout_width="88dp"
android:layout_height="67dp"
android:layout_marginLeft="402dp"
android:layout_marginTop="149dp"
android:contentDescription="#id/imageViewHMI_Graph_012_Perforator"
android:src="#drawable/lva_perforator_off"
android:visibility="invisible" />
<ImageView
android:id="#+id/ImageView11"
android:layout_width="88dp"
android:layout_height="67dp"
android:layout_marginLeft="408dp"
android:layout_marginTop="149dp"
android:contentDescription="#id/ImageViewHMI_Graph_10_Ruffler"
android:src="#drawable/lva_ruffler_off"
android:visibility="invisible" />
<ImageView
android:id="#+id/ImageView12"
android:layout_width="174dp"
android:layout_height="72dp"
android:layout_marginLeft="362dp"
android:layout_marginTop="146dp"
android:contentDescription="#id/ImageViewHMI_Graph_11_Prepress"
android:src="#drawable/lva_prepress_open"
android:visibility="invisible" />
<ImageView
android:id="#+id/imageView13"
android:layout_width="46dp"
android:layout_height="68dp"
android:layout_marginLeft="356dp"
android:layout_marginTop="297dp"
android:contentDescription="#id/imageViewHMI_Graph_07_Needles_Hor"
android:src="#drawable/lva_needles_hor_none" />
<ImageView
android:id="#+id/imageView14"
android:layout_width="44dp"
android:layout_height="111dp"
android:layout_marginLeft="356dp"
android:layout_marginTop="187dp"
android:contentDescription="#id/imageViewHMI_Graph_05_Needles_Vert"
android:src="#drawable/lva_needles_vert_high" />
<ImageView
android:id="#+id/imageView15"
android:layout_width="10dp"
android:layout_height="24dp"
android:layout_marginLeft="371dp"
android:layout_marginTop="272dp"
android:contentDescription="#id/imageViewHMI_Graph_06_Knotter_Vert"
android:src="#drawable/lva_motor_off" />
<FrameLayout
android:id="#+id/frameLayoutBale"
android:layout_width="140dp"
android:layout_height="51dp"
android:layout_marginLeft="19dp"
android:layout_marginTop="307dp" >
<ImageView
android:id="#+id/imageViewAnimation"
android:layout_width="140dp"
android:layout_height="51dp"
android:layout_gravity="right"
android:contentDescription="#id/imageViewHMI_Graph_Bale"
android:scaleType="fitXY"
android:src="#drawable/baal" />
</FrameLayout>
<TextView
android:id="#+id/textView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="110dp"
android:layout_marginTop="316dp"
android:gravity="right"
android:text="#string/percentage"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/brm_back_2" />
<TextView
android:id="#+id/textView02"
android:layout_width="142dp"
android:layout_height="wrap_content"
android:layout_marginLeft="22dp"
android:layout_marginTop="271dp"
android:gravity="center"
android:text="#string/puntjes"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/brm_back_2" />
<ZoomControls
android:id="#+id/zoomControls"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/textCurrMaterial" />
</RelativeLayout>
This is the code I used on my main to declare the zoom controls:
btnZoomControls = (ZoomControls) findViewById(R.id.zoomControls);
btnZoomControls.setOnZoomInClickListener(new View.OnClickListener() {public void onClick(View v) {setZoomIn(); }});
btnZoomControls.setOnZoomOutClickListener(new View.OnClickListener() {public void onClick(View v) {setZoomOut(); }});
And this are the functions for zooming in and out
private void setZoomIn()
{
m_ZoomController.getContainer();
m_ZoomController.getZoomControls();
m_ZoomController.setZoomInEnabled(true);
}
private void setZoomOut()
{
m_ZoomController.getContainer();
m_ZoomController.getZoomControls();
m_ZoomController.setZoomOutEnabled(true);
}
Had any of you a similar problem like mine, and how you found a solution to fix it?

Actually, You have two major ways for zooming:
1) Call setScaleX() and setScaleY() (since API 11) on whole layout or on particular view. Per my experience, images scaling works fine, but text on some versions produces weird behaviour (this scaling get applied by view group during draw process and so, quite fast). This way is quite smooth and fast for not very big layouts;
2) Change layout parameters on every view (You might need, probably, to provide new pictures for image views during scaling). This way is not so fast, but should work on almost all android revisions.

Related

Android studio error: resource not found Exception

i have a problem with my app: i'm using crashlytics by Fabric, (my app is pubblishes in the play store) to have log of possible crash, and i have this error ".. MainActivity: android.content.res.Resource$NotFoundException:Resource ID#0x7f09001b"
(Crashlytics say to me that the device is a samsung galaxy s4 with Android 5.1 and root )
CrashLytics screen
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Fabric.with(this, new Crashlytics());
setContentView(R.layout.activity_main); <---- LINE 102 THAT GIVE
THE ERROR , ACCORDING TO
CRASHLYTICS
ping = (ImageButton)findViewById(R.id.ping);
internet =(TextView)findViewById(R.id.Internet);
banda5Ghz =(TextView)findViewById(R.id.BandeSupp);
SpeedNet =(TextView)findViewById(R.id.VR);
ipv6 =(TextView)findViewById(R.id.showIPv6);
txVsegnale =(TextView)findViewById(R.id.intensità);
aggiorna = (ImageButton)findViewById(R.id.Aggiorna);
share = (ImageButton)findViewById(R.id.sharebtn);
clipboard = (ImageButton)findViewById(R.id.clipboard);
rating = (ImageButton)findViewById(R.id.ratebtn);
IP =(TextView)findViewById(R.id.IP);
showIPv4 =(TextView)findViewById(R.id.showIPv4);
// TextViewWifi =(TextView)findViewById(R.id.TextViewWifi);
TextViewwifi_info = (TextView)findViewById(R.id.wifi_info);
MacA = (TextView)findViewById(R.id.MacAdd);
//switch gestione notifiche
notifiche = (Switch)findViewById(R.id.switchNot);`
......
The same device crash 2 times in a short time, with same error.
My app is an app that show infos on your wifi connection, such as IP, network velocity, etc etc, also have system notification and widgets and a service that run in background .
I have also done different layout for different devices with different screen size and different screen dpi
My Layouts
No other logs from other device with same problem .I want to know if is a specific problem of this device ( may be related to root access given to this device ) or is a problem of my application.
Not sure if it is linked to this, but, i was tryeing to add split screen function to my app, and , adding android:resizeableActivity="true" to manifest, make app crash in the same code line, so, i solve by settings to false and not include split screen feature in my app.
I CONFIRMED THAT TRIEING TO USE SPLIT SCREEN GIVE ME THE SAME ERROR, SAME RESOURCE NOT FOUND EXCEPTION, how can i see which resource have this id ?
android.content.res.Resources$NotFoundException: Resource ID #0x7f09001b
THE APP Crash only in this case , no other crash, do i have to define a minimum width and weight to use split Screen ?
`<?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="com.ShowMyIp.utente.ShowMyIp.MainActivity">
<TextView
android:id="#+id/Internet"
android:layout_width="match_parent"
android:layout_height="225dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#drawable/roundcorner"
android:layout_marginTop="8dp"
android:paddingTop="27dp"
android:textColor="#646464"
android:textSize="13sp"
android:layout_marginLeft="7dp"
android:layout_marginRight="8dp"
android:textStyle="italic" />
<TextView
android:id="#+id/Internet2"
android:layout_width="match_parent"
android:layout_height="195dp"
android:layout_alignEnd="#+id/internet4"
android:layout_below="#+id/internet4"
android:layout_marginTop="14dp"
android:layout_marginLeft="7dp"
android:background="#drawable/roundcorner" />
<TextView
android:id="#+id/internet4"
android:layout_width="match_parent"
android:layout_height="195dp"
android:layout_alignStart="#+id/Internet"
android:layout_below="#+id/Internet"
android:layout_marginRight="8dp"
android:layout_marginTop="17dp"
android:background="#drawable/roundcorner"
android:textSize="13sp" />
<TextView
android:id="#+id/MacAdd"
android:layout_width="275dp"
android:layout_height="35dp"
android:layout_alignStart="#+id/txtMac"
android:layout_below="#+id/BandeSupp"
android:layout_marginTop="35dp"
android:textAlignment="viewStart"
android:textColor="#android:color/black"
android:textSize="15sp" />
<TextView
android:id="#+id/IP"
android:layout_width="150dp"
android:layout_height="45dp"
android:layout_alignBaseline="#+id/Internet"
android:layout_alignBottom="#+id/Internet"
android:layout_alignStart="#+id/internet4"
android:layout_marginStart="45dp"
android:text="IP Address"
android:textColor="#E1E140"
android:textSize="18sp"
android:textStyle="italic" />
<TextView
android:id="#+id/showIPv4"
android:layout_width="230dp"
android:layout_height="45dp"
android:layout_alignStart="#+id/IP"
android:layout_below="#+id/intensità"
android:textColor="#android:color/black"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/wifi_info"
android:layout_width="220dp"
android:layout_height="55dp"
android:layout_alignStart="#+id/showIPv4"
android:layout_below="#+id/showIPv4"
android:text="Non Connesso"
android:textColor="#android:color/black"
android:textSize="16sp"
android:textStyle="italic" />
<TextView
android:id="#+id/velocita"
android:layout_width="155dp"
android:layout_height="45dp"
android:layout_above="#+id/VR"
android:layout_alignEnd="#+id/txvIPV6"
android:text="Velocità rete"
android:textColor="#E1E140"
android:textSize="18sp"
android:textStyle="italic" />
<TextView
android:id="#+id/VR"
android:layout_width="135dp"
android:layout_height="45dp"
android:layout_alignBottom="#+id/wifi_info"
android:layout_alignEnd="#+id/velocita"
android:textColor="#android:color/black"
android:textSize="16sp"
android:textStyle="italic" />
<ImageButton
android:id="#+id/Aggiorna"
android:layout_width="200dp"
android:layout_height="75dp"
android:layout_alignEnd="#+id/intensità"
android:layout_alignTop="#+id/Internet2"
android:layout_marginEnd="21dp"
android:layout_marginTop="56dp"
android:background="#android:color/transparent"
android:scaleType="fitCenter"
android:textColor="#android:color/white"
app:srcCompat="#drawable/btn_aggiorna" />
<TextView
android:id="#+id/intensità"
android:layout_width="190dp"
android:layout_height="65dp"
android:layout_alignEnd="#+id/Internet"
android:layout_alignTop="#+id/IP"
android:layout_marginEnd="21dp"
android:textColor="#android:color/black"
android:textSize="18sp" />
<TextView
android:id="#+id/txtMac"
android:layout_width="140dp"
android:layout_height="35dp"
android:layout_alignStart="#+id/showIPv4"
android:layout_alignTop="#+id/internet4"
android:layout_marginTop="28dp"
android:text="MAC Address"
android:textColor="#E1E140"
android:textSize="18sp" />
<Switch
android:id="#+id/switchNot"
android:layout_width="145dp"
android:layout_height="85dp"
android:layout_alignStart="#+id/MacAdd"
android:layout_alignTop="#+id/Aggiorna"
android:layout_marginStart="28dp"
android:textColor="#E1E140"
android:textSize="18sp"
tools:text="Notifiche" />
<TextView
android:id="#+id/txvIPV6"
android:layout_width="75dp"
android:layout_height="45dp"
android:layout_alignBottom="#+id/txtMac"
android:layout_alignEnd="#+id/Aggiorna"
android:text="IPV6"
android:textColor="#E1E140"
android:textSize="18sp" />
<TextView
android:id="#+id/showIPv6"
android:layout_width="220dp"
android:layout_height="45dp"
android:layout_alignBottom="#+id/MacAdd"
android:layout_alignStart="#+id/Aggiorna"
android:textAlignment="viewStart"
android:textColor="#android:color/black"
android:textSize="17sp" />
<ImageButton
android:id="#+id/ratebtn"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_alignEnd="#+id/Internet2"
android:layout_alignParentBottom="true"
android:layout_marginBottom="22dp"
android:layout_marginEnd="10dp"
android:background="#android:color/transparent"
app:srcCompat="#drawable/rating_button"
android:scaleType="fitCenter"
/>
<ImageButton
android:id="#+id/sharebtn"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_alignTop="#+id/ratebtn"
android:layout_marginEnd="23dp"
android:layout_toStartOf="#+id/ratebtn"
android:background="#android:color/transparent"
android:scaleType="fitCenter"
app:srcCompat="#drawable/share_button" />
<ImageButton
android:id="#+id/clipboard"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_alignTop="#+id/sharebtn"
android:layout_toStartOf="#+id/VR"
android:background="#android:color/transparent"
android:scaleType="fitCenter"
app:srcCompat="#drawable/clipboard" />
<TextView
android:id="#+id/BandeSupp"
android:layout_width="130dp"
android:layout_height="55dp"
android:layout_alignTop="#+id/txvIPV6"
android:layout_toEndOf="#+id/showIPv4"
android:textColor="#android:color/black"
android:visibility="invisible"
android:textSize="17sp" />
<ImageButton
android:id="#+id/ping"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_alignBottom="#+id/clipboard"
android:layout_alignEnd="#+id/switchNot"
android:background="#android:color/transparent"
android:visibility="invisible"
app:srcCompat="#drawable/ping" />
</RelativeLayout>`
here is my layout (large-port-mdpi)
App still crash entering in split screen mode, with "no found exception ..." ; i've made another simple app , following Android developer guide to make multiview support, and this app work, no problem when entering in split screen, , i only added to the Manifest same code i wrote for this app " android:resizeableActivity="true"". Anyone know about to resolve this error? why my app, dosen't find layout resource entering in split screen mode??
Check your MainActivity Layout Xml in Text Format, There Has to be Line with a Yellow Warning on it, Or a Red Line.
Check the Layout Xml, And Then,
Check If You Missed Something Like LinearLayout Being RelativeLayout and you Declared it with this mistake.
Then Check If You've Used Anything like below:
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) myRelativeLayout.getLayoutParams();
LinearLayout.LayoutParams Param1 = (LinearLayout.LayoutParams) myLinearLayout.getLayoutParams();
If Yes, Reply me and I'll tell you what to do.

Any Skin Selector Android library

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!

How to change position of ImageButton in Android

How can I change the position of ImageButton when I click on it.
There are 6 ImageButton here: ibWolf1, ibWolf2, ibWolf3, ibSheep1, ibSheep2, ibSheep3; 1 ImageView ivWood and 1 Button btnGoAndBack. When I click ibSheep3, ibSheep3 will change position like this:
And when I continue to click on ibWolf3 it will change position like this:
That means the image will appear above the wood image. So how to do this?
Here is my game_play_layout.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">
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Go"
android:textSize="30dp"
android:background="#3300FF00"
android:id="#+id/btnGoAndBack"
android:layout_below="#+id/ivWood"
android:layout_toLeftOf="#+id/ibSheep2"
android:layout_toStartOf="#+id/ibSheep2" />
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/wolf"
android:scaleType="centerCrop"
android:id="#+id/ibWolf1"
android:layout_above="#+id/ibSheep2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/wolf"
android:scaleType="centerCrop"
android:id="#+id/ibWolf2"
android:layout_below="#+id/ibWolf1"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/wolf"
android:scaleType="centerCrop"
android:id="#+id/ibWolf3"
android:layout_below="#+id/ibWolf2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/sheep"
android:scaleType="centerCrop"
android:id="#+id/ibSheep1"
android:layout_above="#+id/ibWolf2"
android:layout_toLeftOf="#+id/ibWolf1"
android:layout_toStartOf="#+id/ibWolf1" />
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/sheep"
android:scaleType="centerCrop"
android:id="#+id/ibSheep2"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/ibWolf2"
android:layout_toStartOf="#+id/ibWolf2" />
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/sheep"
android:scaleType="centerCrop"
android:id="#+id/ibSheep3"
android:layout_below="#+id/ibSheep2"
android:layout_toLeftOf="#+id/ibWolf3"
android:layout_toStartOf="#+id/ibWolf3" />
<ImageView
android:layout_width="200dp"
android:layout_height="30dp"
android:src="#drawable/wood"
android:scaleType="centerCrop"
android:id="#+id/ivWood"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/ibSheep2"
android:layout_toStartOf="#+id/ibSheep2"/>
</RelativeLayout>
One easy solution is that,put two images of sheep and wolf above go button and at first make the visibility invisible or gone.Now when user clicks on the sheep or the wolf images make the visibility of the imagebutton gone and make the image visible of the image above go button.
Otherwise you have to calculate the position of the go button and create a imagebutton progmatically and place it over there.
Hope it clears your view.

Custom Layout Manager using predefined and developer-defined child view(s)

Revised Question
When one looks online in tutorials, or by Google IO presentations, there are always just 2 kinds of UI customizations that one can do (but there are several implementations of doing each type). These customizations are:
Creating a custom view that is a composite of existing views, or your own
Creating a custom layout that is your own
However, what if I wanted to create a hybrid of these two? Specifically a Custom View that allow child views to be laid out how I define? There is nothing that I have found online (no tutorials, no videos) or even anything saying if this is even feasible.
Idea
Here is the Contacts App that has something very similar to what I want to do. You notice that every list item has a very similar layout. Nothing special here. I can create composite custom view with styleables to set the primary and secondary text, and the main and action icons.
However, my secret sauce comes with the idea of changing the primary text (the blacked-out region) to some other view in XML! This view could be a TextView, RatingBar, ProgressBar, VideoView, SurfaceView, etc...
Examples
TextView
Here is an example of using my custom view that takes in a developer-defined TextView.
<com.example.customview.BaseViewLayout
...
custom:imageMainIcon="..."
custom:imageActionIcon="..."
custom:secondaryText="Home">
<TextView
...
android:text="User-defined TextView"
... />
</com.example.customview.BaseViewLayout>
VideoView
Here is an example of using my same custom view that takes in a developer-defined VideoView.
<com.example.customview.BaseViewLayout
...
custom:imageMainIcon="..."
custom:imageActionIcon="..."
custom:secondaryText="Home">
<VideoView
... />
</com.example.customview.BaseViewLayout>
Code
BaseCustomLayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/detail_list_item"
android:layout_width="450dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:minHeight="72dp"
android:background="#color/material_blue_grey_800" >
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="24dp"
android:layout_marginBottom="24dp"
android:src="#drawable/ic_not_available_white"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/imageView" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="72dp"
android:layout_marginStart="72dp"
android:layout_marginRight="72dp"
android:layout_marginEnd="72dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:layout_toRightOf="#+id/imageView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<ViewStub
android:layout_height="20dp"
android:layout_width="wrap_content"
android:id="#+id/view_stub_main_view"
android:inflatedId="#+id/inflated_main_view" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="20dp"
android:paddingTop="4dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="16dp"
android:text="Item Type"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="16dp"
android:textStyle="bold"
android:text=" · "/>
<TextView
android:layout_width="wrap_content"
android:layout_height="16dp"
android:text="Extra"/>
</LinearLayout>
</LinearLayout>
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:src="#drawable/ic_not_available_white"
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:padding="8dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:id="#+id/imageView2" />
</RelativeLayout>
So, how does a person go about developing a custom view like this?

Saving state of the view

Is there an easy way to save a whole viewgroup with its data together? For example if initially i have this view and during the app run some of the view's background image changes, some views become invisible, or its' text changes, etc. How can I save the state of the view, in case user quits app?
<RelativeLayout android:layout_width="wrap_content" android:id="#+id/water_room_common"
android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android"
>
<!-- storage layout -->
<View android:id="#+id/gap" android:layout_width="wrap_content"
android:layout_height="0dip" android:layout_alignParentBottom="true" />
<RelativeLayout android:id="#+id/storage"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_above="#id/gap" android:layout_marginBottom="10dp"
android:layout_marginRight="27dp" android:layout_marginLeft="10dp"
android:layout_toLeftOf="#id/bag" android:orientation="horizontal"
>
<ImageButton android:id="#+id/ImageButton02"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignTop="#+id/ImageButton01"
android:layout_marginRight="16dp" android:layout_toLeftOf="#+id/ImageButton01"
android:background="#drawable/single_square" android:onClick="itemSelected" />
<ImageButton android:id="#+id/ImageButton03"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignTop="#+id/ImageButton01"
android:layout_centerHorizontal="true" android:layout_marginRight="16dp"
android:layout_toLeftOf="#+id/ImageButton02" android:background="#drawable/single_square"
android:onClick="itemSelected"
/>
<ImageButton android:id="#+id/ImageButton04"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignTop="#+id/ImageButton01"
android:layout_marginRight="16dp" android:layout_toLeftOf="#+id/ImageButton03"
android:adjustViewBounds="true" android:background="#drawable/single_square"
android:onClick="itemSelected"
/>
<ImageButton android:id="#+id/ImageButton05"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignTop="#+id/ImageButton01"
android:layout_marginRight="16dp" android:layout_toLeftOf="#+id/ImageButton04"
android:adjustViewBounds="true" android:background="#drawable/single_square"
android:onClick="itemSelected"
/>
<ImageButton android:id="#+id/ImageButton06"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignTop="#+id/ImageButton01"
android:layout_marginRight="16dp" android:layout_toLeftOf="#+id/ImageButton05"
android:adjustViewBounds="true" android:background="#drawable/single_square"
android:onClick="itemSelected"
/>
<ImageButton android:id="#+id/ImageButton01"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true" android:background="#drawable/single_square"
android:onClick="itemSelected"
/>
</RelativeLayout>
<ImageButton android:id="#+id/bag" android:layout_width="65dp"
android:layout_height="65dp" android:layout_alignBottom="#+id/storage"
android:layout_alignParentRight="true" android:layout_marginRight="10dp"
android:adjustViewBounds="true"
android:onClick="bagClicked"/>
<ImageButton android:id="#+id/left" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignLeft="#+id/storage"
android:layout_centerVertical="true" android:background="#null"
android:onClick="previousWall" android:src="#drawable/left" />
<ImageButton android:id="#+id/right" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignRight="#id/bag"
android:layout_centerVertical="true" android:background="#null"
android:onClick="nextWall" android:src="#drawable/right" />
</RelativeLayout>
See this answer. You need to override onSaveInstanceState for transient changes or override onPause and onResume for permanent changes.
Read the API guide here
Update
This is a summary of this training
Picture this scenario. You have a notepad app. On the homescreen, the user has a list of recently created notes (NoteList Activity) and the user can choose to create a new note or edit an existing note and changes will be stored in a database.
When the user clicks on an item brings another activity (NoteEditActivity) to the foreground. If the user is in the process of editting a note and the NoteEditActivity loses focus for some reason (e.g a call comes in or the user just decides to switch to another app without clicking the "confirm button") the NoteEditActivity goes from being focused to being paused and then shortly after it is stopped.
To prevent the user from loosing the current edit, you need to call onPause where you would want to save any changes to the database. Now if the app is in the stopped (onStop) and the user returns to the edit it will not go through onCreate but rather through onResume which is where you want to "resume" from where the user left.
This picture should further explain.
Notice how onResume gets called every time the activity is about to be focused and not onCreate.
You can follow the training above for better understanding.

Categories

Resources