(My first post, btw)
My problem is so newbie-esque that I can't find anyone who has asked it and oh have I tried. So here goes..:
My main screen has an imageview. Click it and it sends you to a different layout from where a bunch of images (imagebuttons) can be selected (works fine).
Pressing any of these imagebuttons should send that image to my main imageview (crashing)
I made an imageview inside this layout where the ImButtons are and sent the images there instead. This is working fine so the switch statements are ok.
I have tried to make my main imageview a "public" in my main.java but still crashing.
So the question:
How do I send an image from one layout/class to another?
Grateful in advance.
Jakob
Some code:
My secondary layout:
package egen.helt.min;
import android.app.Activity;``
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
public class FartSelect extends Activity implements OnClickListener {
public ImageView VælgMax;
MediaPlayer mpButtonClick;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.fartselect);
VælgMax = (ImageView) findViewById(R.id.ivValgtMax);
ImageButton skilt30 = (ImageButton) findViewById(R.id.ib30s);
ImageButton skilt40 = (ImageButton) findViewById(R.id.ib40s);
ImageButton skilt50 = (ImageButton) findViewById(R.id.ib50s);
ImageButton skilt60 = (ImageButton) findViewById(R.id.ib60s);
ImageButton skilt70 = (ImageButton) findViewById(R.id.ib70s);
ImageButton skilt80 = (ImageButton) findViewById(R.id.ib80s);
ImageButton skilt90 = (ImageButton) findViewById(R.id.ib90s);
ImageButton skilt100 = (ImageButton) findViewById(R.id.ib100s);
ImageButton skilt110 = (ImageButton) findViewById(R.id.ib110s);
ImageButton skilt120 = (ImageButton) findViewById(R.id.ib120s);
ImageButton skilt130 = (ImageButton) findViewById(R.id.ib130s);
skilt30.setOnClickListener(this);
skilt40.setOnClickListener(this);
skilt50.setOnClickListener(this);``
skilt60.setOnClickListener(this);
skilt70.setOnClickListener(this);
skilt80.setOnClickListener(this);
skilt90.setOnClickListener(this);
skilt100.setOnClickListener(this);
skilt110.setOnClickListener(this);
skilt120.setOnClickListener(this);
skilt130.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.ib30s:
VælgMax.setImageResource(R.drawable.skilt30s);
break;
case R.id.ib40s:
VælgMax.setImageResource(R.drawable.skilt40s);
break;
case R.id.ib50s:
VælgMax.setImageResource(R.drawable.skilt50s);
break;
case R.id.ib60s:
VælgMax.setImageResource(R.drawable.skilt60s);
break;
case R.id.ib70s:
VælgMax.setImageResource(R.drawable.skilt70s);
break;
case R.id.ib80s:
VælgMax.setImageResource(R.drawable.skilt80s);
break;
case R.id.ib90s:
VælgMax.setImageResource(R.drawable.skilt90s);
break;
case R.id.ib100s:
VælgMax.setImageResource(R.drawable.skilt100s);
break;
case R.id.ib110s:
VælgMax.setImageResource(R.drawable.skilt110s);
break;
case R.id.ib120s:
VælgMax.setImageResource(R.drawable.skilt120s);
break;
case R.id.ib130s:
VælgMax.setImageResource(R.drawable.skilt130s);
break;
}
}
}
And here is FartSelect.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/ivValgteMax"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/blanktskiltsk" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="400dp" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/ib30s"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:background="#android:color/black"
android:gravity="center"
android:src="#drawable/skilt30s" />
<ImageButton
android:id="#+id/ib40s"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:background="#android:color/black"
android:gravity="center"
android:src="#drawable/skilt40s" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/ib50s"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:background="#android:color/black"
android:gravity="center"
android:src="#drawable/skilt50s" />
<ImageButton
android:id="#+id/ib60s"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:background="#android:color/black"
android:gravity="center"
android:src="#drawable/skilt60s" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/ib70s"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:background="#android:color/black"
android:gravity="center"
android:src="#drawable/skilt70s" />
<ImageButton
android:id="#+id/ib80s"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:background="#android:color/black"
android:gravity="center"
android:src="#drawable/skilt80s" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/ib90s"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:background="#android:color/black"
android:gravity="center"
android:src="#drawable/skilt90s" />
<ImageButton
android:id="#+id/ib100s"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:background="#android:color/black"
android:gravity="center"
android:src="#drawable/skilt100s" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/ib110s"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:background="#android:color/black"
android:gravity="center"
android:src="#drawable/skilt110s" />
<ImageButton
android:id="#+id/ib120s"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:background="#android:color/black"
android:gravity="center"
android:src="#drawable/skilt120s" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/ib130s"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:background="#android:color/black"
android:gravity="center"
android:src="#drawable/skilt130s" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/ib30sk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:background="#android:color/black"
android:gravity="center"
android:src="#drawable/skilt30sk" />
<ImageButton
android:id="#+id/ib40sk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:background="#android:color/black"
android:gravity="center"
android:src="#drawable/skilt40sk" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/ib50sk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:src="#drawable/skilt50sk" />
<ImageButton
android:id="#+id/ib60sk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:src="#drawable/skilt60sk" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/ib70sk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:src="#drawable/skilt70sk" />
<ImageButton
android:id="#+id/ib80sk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:src="#drawable/skilt80sk" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/ib90sk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:src="#drawable/skilt90sk" />
<ImageButton
android:id="#+id/ib100sk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:src="#drawable/skilt100sk" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/ib110sk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:src="#drawable/skilt110sk" />
<ImageButton
android:id="#+id/ib120sk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:src="#drawable/skilt120sk" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/ib130sk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:src="#drawable/skilt130sk" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/ib30"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:src="#drawable/skilt30" />
<ImageButton
android:id="#+id/ib40"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:src="#drawable/skilt40" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/ib50"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:src="#drawable/skilt50" />
<ImageButton
android:id="#+id/ib60"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:src="#drawable/skilt60" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/ib70"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:src="#drawable/skilt70" />
<ImageButton
android:id="#+id/ib80"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:src="#drawable/skilt80" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/ib90"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:src="#drawable/skilt90" />
<ImageButton
android:id="#+id/ib100"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:src="#drawable/skilt100" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/ib110"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:src="#drawable/skilt110" />
<ImageButton
android:id="#+id/ib120"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:src="#drawable/skilt120" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/ib130"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:src="#drawable/skilt130" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
Within the same layout the imageview can change. Just not when sent to an imageview on main layout.
You're not supposed to sent images between activities, if that's what you mean. You could probably do it, but it would definitely be slow through something like an intent. From what I understand, you want to go to Activity B and then click an ImageButton, then return back to Activity A and have that displayed in the ImageView. Couldn't you just pass in the path of the image through an intent (i.e. as a String)? Alternatively, you could just pass in the name as a String and then display that image in the next Activity by setting the source of the ImageView in Activity A.
If the image was created temporarily or something, and you needed to pass it, you could just save it to a private file (MODE_PRIVATE) and then access it in the next activity.
Sorry if I'm thinking something off topic.
EDIT
Look at this for more information.
EDIT 2
You can use intents to pass Strings between activities: (i.e. the image path/name)
String myImageName = "image1";
Intent picIntent = new Intent(this, NextActivity.class);
picIntent.putExtra("name", myImageName); //this has to correspond to the below name
startActivity(intent);
You can retrieve it in the next activity:
String thePictureName = getIntent().getStringExtra("name"); //correspond to above name
You don't send an ImageView from one layout to another.
You may either,
Remove the ImageView on layout and add to the other layout (by inflating).
Hide on first layout and show on other layout.
Set up an intent i for the FartSelector
startActivity(i);
Create an OnActivityResult(int result_code) and check for the FartSelector's code
In fart selector, pack a new intent with the desired image data on click
Call setResult(newIntent); and finish(); after creating and packing the intent
Picking up where we left off at 3, unpack the intent and use it in your ImageView.
Your problem is this
VælgMax = (ImageView) findViewById(R.id.ivValgtMax);
ivValgtMax is not an id that is defined in fartlayout therefore that code will give you null. I assume ivValgtMax refers to an ImageView from another layout.
If I understand you correctly, you want to launch the Activity B (which has the images) from another Activity A (which has the main ImageView), select an image while in Activity B, and return the selected image to Activity A after the user has clicked.
If all of this is correct, we can proceed. The way you want to launch Activity B from A is to use something like
Intent intent = new Intent(this, ActivityB.class);
startActivityForResult(intent, IMAGE_SELECTION_VALUE);
where IMAGE_SELECTION_VALUE is a static value that you assign an integer to, just make it something unique.
In Activity B, once the user finishes clicking, your goal is to return the selection to Activity A. To do this, you need to create an intent and set the result.
public void onClick(View v) {
... your code ...
Intent resultIntent = new Intent(null);
resultIntent.putExtra(IMAGE_FIELD_NAME, userSelection);
setResult(Activity.RESULT_OK, resultIntent);
finish();
}
In the above code, IMAGE_FIELD_NAME will be a static String that you can use to find the correct image (we will find how to use it in the next step). userSelection will be the data that you send back to Activity A. You can use v.getId() for this and move your switch statement to Activity A.
Lastly, you will want to override onActivityResult in Activity A. This will allow you to get the result from Activity B and is where we will use IMAGE_FIELD_NAME.
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case IMAGE_SELECTION_VALUE:
if (resultCode == Activity.RESULT_OK) {
int imageId = data.getIntExtra(IMAGE_FIELD_NAME);
// now you can update your ImageView here
}
break;
default:
break;
}
}
Related
I have a linear layout for buttons so when i run the app on different mobiles some of the buttons get hidden. As my app is ready in terms of code so i want some short technique to make it compatible for all screen sizes.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
tools:context="com.example.android.hiha.MainActivity"
android:background="#drawable/main_activity_cover">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="#layout/tool_bar"
android:id="#+id/toolbar"></include>
<RelativeLayout
android:id="#+id/btn_list_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_below="#id/toolbar">
<include
layout="#layout/button_list"
android:id="#+id/button_list"></include>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/btn_list_layout"
android:layout_alignParentBottom="true"
android:gravity="bottom">
<include
layout="#layout/bottom_section_main"
android:id="#+id/bottom_section_main"></include>
</RelativeLayout>
</RelativeLayout>
</ScrollView>
button_list.xml (landscape)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp">
<ImageView
android:id="#+id/about_btn"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/about"
android:layout_gravity="center"
android:elevation="4dp"/>
<TextView
android:id="#+id/about_txt_view"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="About"
android:textStyle="bold"
android:gravity="center"
android:textColor="#FFFFFF"
android:layout_gravity="center"
android:layout_marginBottom="20dp"/>
<ImageView
android:id="#+id/digital_lib_btn"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/digital_library"
android:layout_gravity="center"
android:elevation="4dp"
android:onClick="onClick"/>
<TextView
android:id="#+id/digital_lib_txt_view"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="Digital Library"
android:textStyle="bold"
android:gravity="center"
android:textColor="#FFFFFF"
android:layout_gravity="center"
android:layout_marginBottom="20dp"/>
<ImageView
android:id="#+id/blogs_btn"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/blog"
android:layout_gravity="center"
android:elevation="4dp"
android:onClick="onClick"/>
<TextView
android:id="#+id/blogs_txt_view"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="Forums/Blogs"
android:textStyle="bold"
android:gravity="center"
android:textColor="#FFFFFF"
android:layout_gravity="center"
android:layout_marginBottom="10dp"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp">
<ImageView
android:id="#+id/affltd_organization_btn"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/organization"
android:layout_gravity="center"
android:elevation="4dp"
android:onClick="onClick"/>
<TextView
android:id="#+id/affltd_organization_txt_view"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="Organizations"
android:textStyle="bold"
android:gravity="center"
android:textColor="#FFFFFF"
android:layout_gravity="center"
android:layout_marginBottom="20dp"/>
<ImageView
android:id="#+id/gallery_btn"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/gallery"
android:layout_gravity="center"
android:elevation="4dp"
android:onClick="onClick"/>
<TextView
android:id="#+id/gallery_txt_view"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="Gallery"
android:textStyle="bold"
android:gravity="center"
android:textColor="#FFFFFF"
android:layout_gravity="center"
android:layout_marginBottom="20dp"/>
<ImageView
android:id="#+id/feedback_btn"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/feedback"
android:layout_gravity="center"
android:elevation="4dp"
android:onClick="onClick"/>
<TextView
android:id="#+id/feedback_txt_view"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="Feedback"
android:textStyle="bold"
android:gravity="center"
android:textColor="#FFFFFF"
android:layout_gravity="center"
android:layout_marginBottom="10dp"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp">
<ImageView
android:id="#+id/scientific_proof_btn"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/scientific_proof"
android:layout_gravity="center"
android:elevation="4dp"
android:onClick="onClick"/>
<TextView
android:id="#+id/scientific_proof_txt_view"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="Scientific Proof"
android:textStyle="bold"
android:gravity="center"
android:textColor="#FFFFFF"
android:layout_gravity="center"
android:layout_marginBottom="20dp"/>
<ImageView
android:id="#+id/tender_btn"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/tender"
android:layout_gravity="center"
android:elevation="4dp"
android:onClick="onClick"/>
<TextView
android:id="#+id/tender_txt_view"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="Tenders"
android:textStyle="bold"
android:gravity="center"
android:textColor="#FFFFFF"
android:layout_gravity="center"
android:layout_marginBottom="20dp"/>
<ImageView
android:id="#+id/contact_us_btn"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/contact"
android:layout_gravity="center"
android:elevation="4dp"
android:onClick="onClick"/>
<TextView
android:id="#+id/contact_us_txt_view"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="Contact Us"
android:textStyle="bold"
android:gravity="center"
android:textColor="#FFFFFF"
android:layout_gravity="center"
android:layout_marginBottom="10dp"/>
</LinearLayout>
</LinearLayout>
MainActivity.java
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
ImageView about_btn;
ImageView feedbackBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_PORTRAIT) {
setContentView(R.layout.activity_main);
} else {
setContentView(R.layout.activity_main);
}
about_btn=(ImageView)findViewById(R.id.about_btn);
about_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Create custom dialog object
final Dialog dialog = new Dialog(MainActivity.this);
// Include dialog.xml file
dialog.setContentView(R.layout.about_dialog);
// Set dialog title
dialog.setTitle("Choose one");
dialog.show();
ImageView sarasvati_btn = (ImageView) dialog.findViewById(R.id.sarasvati_btn);
sarasvati_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(MainActivity.this,Sarasvati_Activity.class);
startActivity(i);
dialog.dismiss();
}
});
ImageView about_btn_dialog=(ImageView) dialog.findViewById(R.id.about_btn_dialog);
about_btn_dialog.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(MainActivity.this,About_Us_Activity.class);
startActivity(i);
dialog.dismiss();
}
});
}
});
feedbackBtn=(ImageView)findViewById(R.id.feedback_btn);
facebook=(ImageView)findViewById(R.id.facebook_icon);
twitter=(ImageView)findViewById(R.id.twitter_icon);
youtube=(ImageView)findViewById(R.id.youtube_icon);
instagram=(ImageView)findViewById(R.id.instagram_icon);
}
public void onClick(View v){
switch (v.getId()){
case R.id.feedback_btn:
Intent j=new Intent(this,FeedbackActivity.class);
startActivity(j);
break;
case R.id.contact_us_btn:
Intent k=new Intent(this,Contact_Us_Activity.class);
startActivity(k);
break;
case R.id.affltd_organization_btn:
Intent gs = new Intent(this,AffiliatedOrganizationsActivity.class);
startActivity(gs);
break;
case R.id.gallery_btn:
Intent l=new Intent(this,Gallery.class);
startActivity(l);
break;
case R.id.scientific_proof_btn:
Intent sc=new Intent(this,Scientific_Evidences_Actiivity.class);
startActivity(sc);
break;
case R.id.blogs_btn:
Intent blogs=new Intent(this,BlogActivity.class);
startActivity(blogs);
break;
case R.id.tender_btn:
Intent tender=new Intent(this,WebViewActivity.class);
tender.putExtra("URL","url");
startActivity(tender);
break;
case R.id.digital_lib_btn:
Intent lib=new Intent(this,DigitalLibraryActivity.class);
startActivity(lib);
break;
default:
break;
}
}
#Override
public void onBackPressed() {
//Execute your code here
finish();
}
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
int orientation;
if (getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_PORTRAIT) {
orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
// or = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
}else {
orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
}
// Add code if needed
setRequestedOrientation(orientation);
}
}
Percent Relative Layout
https://developer.android.com/reference/android/support/percent/PercentRelativeLayout.html
Constraint Layout
https://developer.android.com/training/constraint-layout/index.html
Use layout_weight. Research about them. I have started using layout_weight and they adjust to any screen size. Weights are used in LinearLayouts and TableLayouts. There may be more but those are the two i have used them in. If you want space in between items, go to the layout text screen and type
<View
and press enter. it should guide you from there. Views also can have weight too.
do not give any fix size to button or TextView
as per above code you give
<ImageView
android:id="#+id/about_btn"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/about"
android:layout_gravity="center"
android:elevation="4dp"/>``
Imageview you give fix size 50dp for(height and width).Image view Hight&Width never change when you run different mobile phone.
so most of use Wrap_content and Match_Parent
I didn't have this problem until now. I don't know what is the problem here because in the same layout i have several buttons and only buttons which are in LinearLayout won't respond.
This is the layout where my buttons won't respond onClick (EDITED):
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:background="#drawable/texture"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="600dp"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<EditText
android:id="#+id/input_first_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:ems="10"
android:inputType="text"
android:textSize="#dimen/text_size">
<requestFocus />
</EditText>
<EditText
android:id="#+id/input_last_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/input_first_name"
android:ems="10"
android:inputType="text"
android:textSize="#dimen/text_size" />
<EditText
android:id="#+id/input_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/input_last_name"
android:ems="10"
android:inputType="number"
android:textSize="#dimen/text_size" />
<ImageView
android:id="#+id/profile_image"
android:layout_width="140dp"
android:layout_height="140dp"
android:layout_below="#+id/input_age"
android:layout_centerHorizontal="true"
android:src="#drawable/add" />
<Button
android:id="#+id/save_button"
style="#style/MyCustomButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#+id/slider"
android:layout_toLeftOf="#+id/edit_button"
android:onClick="run"
android:text="#string/save_button" />
<Button
android:id="#+id/edit_button"
style="#style/MyCustomButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/slider"
android:text="#string/edit_button" />
<Button
android:id="#+id/delete_button"
style="#style/MyCustomButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/slider"
android:layout_toLeftOf="#+id/save_button"
android:layout_toStartOf="#+id/save_button"
android:text="#string/delete_button" />
<Button
android:id="#+id/edit_birthday_date"
style="#style/MyCustomButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/profile_image"
android:layout_centerHorizontal="true" />
<LinearLayout
android:id="#+id/button_container1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/cake_image"
android:layout_marginTop="10dp"
android:clickable="true"
android:weightSum="3"
android:orientation="horizontal">
<Button
android:id="#+id/button_movie"
style="#style/MyCustomButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1" />
<Button
android:id="#+id/button_books"
style="#style/MyCustomButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"/>
<Button
android:id="#+id/button_tech"
style="#style/MyCustomButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:id="#+id/button_container2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/button_container1"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:weightSum="3"
android:clickable="true">
<Button
android:id="#+id/button_body_care"
style="#style/MyCustomButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"/>
<Button
android:id="#+id/button_clothes"
style="#style/MyCustomButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1" />
<Button
android:id="#+id/button_accessories"
style="#style/MyCustomButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:id="#+id/button_container3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/button_container2"
android:layout_marginTop="10dp"
android:clickable="true"
android:orientation="horizontal">
<Button
android:id="#+id/button_games"
style="#style/MyCustomButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/edit_button"
android:layout_alignTop="#+id/slider"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
<ImageView
android:id="#+id/cake_image"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignStart="#+id/edit_birthday_date"
android:layout_below="#+id/edit_birthday_date"
android:layout_marginTop="20dp"
android:src="#drawable/birthday_cake" />
<TextView
android:id="#+id/turning_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/cake_image"
android:layout_toEndOf="#+id/cake_image"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<com.daimajia.slider.library.SliderLayout
android:id="#+id/slider"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="#+id/cake_image" />
</RelativeLayout>
</ScrollView>
I have set for every linear layout visibility when one button which is working is clicked to visible, so i think that is not the problem.
This is the code in java:
if (getMovieCategory.equals("movies")) {
buttonCategoryMovie.setText("MOVIES");
buttonCategoryMovie.setTextColor(Color.parseColor("#ffffff"));
buttonCategoryMovie.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_cancelar, 0, 0, 0);
buttonCategoryMovie.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String movies = "movies";
dbh.updateCategoryMovies(birthdayId, movies);
}
});
} else {
buttonCategoryMovie.setText("MOVIES");
buttonCategoryMovie.setTextColor(Color.parseColor("#ffffff"));
buttonCategoryMovie.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_add, 0, 0, 0);
buttonCategoryMovie.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dbh.deleteMovies(getMovieCategory);
}
});
}
Here everything is working except onClick. I'm getting no errors. My button doesn't respond on click.
try by handling setOnClickListener out side the if condition.
.You're coding with a lot of redundancy and risking introducing errors, some of which may not be immediately obvious. Here's a leaner and clearer version of your posted code sample, using one onClick method to handle your if(){else} conditions.
buttonCategoryMovie.setText("MOVIES");
buttonCategoryMovie.setTextColor(Color.parseColor("#ffffff"));
buttonCategoryMovie.setCompoundDrawablesWithIntrinsicBounds(
getMovieCategory.equals("movies") ? R.drawable.ic_cancelar : R.drawable.ic_add, 0,0,0
);
buttonCategoryMovie.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (getMovieCategory.equals("movies")) {
String movies = "movies";
dbh.updateCategoryMovies(birthdayId, movies);
} else {
dbh.deleteMovies(getMovieCategory);
}
}
});
EDIT 1: I mistakenly left out your String movies = "movies" and have added it back in. If you're only going to use this String once, to pass it as a parameter for dbh.updateCategoryMovies(birthdayId, movies), then you could simply call dbh.updateCategoryMovies(birthdayId, "movies") and delete String movies = "movies"
EDIT 2: Without seeing your entire source code, I don't know if you're at all changing the value of getMovieCategory somewhere. If not, then the below sample will change it on every click of the button (once you launch the app to test the sample, your buttonCategoryMovie will be a generic Button; once you start clicking the button, it should toggle between showing your R.drawable.ic_cancelar and R.drawable.ic_add resources. If the button toggles between these two drawables, then you at least know that the button is in fact receiving the onClick.
Remember that, if the value of getMovieCategory doesn't change on each click, your button will always perform only one of the sets of onClick actions and it'll look like nothing's happening.
If it works for you as I've described, then you're on your way ;)
buttonCategoryMovie.setText("MOVIES");
buttonCategoryMovie.setTextColor(Color.parseColor("#ffffff"));
buttonCategoryMovie.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (getMovieCategory.equals("movies")) {
getMoviesCategory = "not movies"; // this is here just to test
buttonCategoryMovie.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_cancelar, 0, 0, 0);
String movies = "movies";
dbh.updateCategoryMovies(birthdayId, movies);
} else {
getMoviesCategory = "movies"; // this is here just to test
buttonCategoryMovie.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_add, 0, 0, 0);
dbh.deleteMovies(getMovieCategory);
}
}
});
Try changing android:visibility="gone" to android:visibility="visible" in your LinearLayout.
And again add android:clickable="true" to your LinearLayout
I have an linear layout in my application,In that i am using one root layout(invitation_single) and one nested layout(hidden).when i am onclick root layout at run time the nested layout visible(it contains"yes,no,maybe" buttons)successfully.Now my need is if i have similar layout values(consider event 2) immediately below the previous layout values(consider event 1),when i am on click "event 2" i need to hide nested layout of "event 1"(atpresent when i am click button the nested layout gone)automatically.how can i achieve this..here is my layout code,
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/invitation_single"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="?android:dividerVertical"
android:dividerPadding="5dp"
android:showDividers="middle"
tools:context=".MainActivity">
<ImageButton
android:id="#+id/image"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/ic_action_event" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:clickable="false"
android:focusable="true"
android:orientation="vertical">
<TextView
android:id="#+id/invitation_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="0dp"
android:paddingTop="3dp"
android:textColor="#color/black"
android:textSize="18sp" />
<TextView
android:id="#+id/invitation_place"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="0dp"
android:textColor="#color/black"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/hidden"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginLeft="-270dp"
android:layout_marginTop="60dp"
android:layout_weight="1"
android:clickable="true"
android:focusable="true"
android:orientation="horizontal"
android:paddingTop="1dp"
android:visibility="gone"
android:weightSum="3">
<Button
android:id="#+id/yesbutton"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="7dp"
android:layout_weight="1"
android:background="#color/blue"
android:text="Yes"
android:textColor="#color/black"></Button>
<Button
android:id="#+id/nobutton"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="25dp"
android:layout_weight="1"
android:background="#color/blue"
android:text="No"
android:textColor="#color/black"></Button>
<Button
android:id="#+id/buttonmaybe"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="25dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:background="#color/blue"
android:text="Maybe"
android:textColor="#color/black"></Button>
</LinearLayout>
</LinearLayout>
my programming code is below,
final LinearLayout first = (LinearLayout) convertView.findViewById(R.id.invitation_single);
final LinearLayout second = (LinearLayout) convertView.findViewById(R.id.hidden);
first.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.invitation_single:
second.setVisibility(View.VISIBLE);
break;
}
}
});
is this what you're looking for?
first.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.invitation_single:
second.setVisibility(second.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
break;
}
}
});
I am inflating a layout inside a dialog, which consist of a scrollview but the layout is not scrolling. I have referred so many questions from stackoverflow but then also its not working. My layout.xml is given below:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:background="#color/White"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/White"
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="5dp"
android:text="Update available"
android:textColor="#000000" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:gravity="center"
android:text="#string/upgrade_msg"
android:textColor="#000000" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="300dp"
android:layout_margin="3dp"
android:layout_weight="0.02"
android:src="#drawable/updateimg2" />
<Button
android:id="#+id/btn_upgrade"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tv_forget_pwd"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="#color/White"
android:gravity="center"
android:text="Upgrade Now"
android:textColor="#color/Black" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:scaleType="fitXY"
android:src="#drawable/line_y_h" />
<Button
android:id="#+id/btn_remind"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tv_forget_pwd"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="#color/White"
android:gravity="center"
android:text="Remind Me Later"
android:textColor="#color/Blue" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:scaleType="fitXY"
android:src="#drawable/line_y_h" />
</LinearLayout>
</ScrollView>
Dialog code
public void show_Alert_version_custom(String str, final String url,
final String exitStatus)// use for
{
final Dialog dialog = new Dialog(Splash.this);
dialog.setContentView(R.layout.version_update);
dialog.setTitle(getString(R.string.alert_title));
Button dialogButton2 = (Button) dialog.findViewById(R.id.btn_remind);
dialogButton = (Button) dialog.findViewById(R.id.btn_upgrade);
ImageView viewLine = (ImageView) dialog.findViewById(R.id.imageView2);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {}
});
// if button is clicked, close the custom dialog
dialogButton2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {}
});
dialog.show();
}
android:fillViewport="true" in ScrollView solved my problem. Now its scrolling even inside dialog.
You can use simply below code on ScollView. it's working fine.
<ScrollView>
<LinearLayout android:orientation="vertical"
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true">
</LinearLayout>
</ScrollView>
I'm trying to make a simple app to show pictures. The images get scaled to fit the screen then there is a next and prev button below the image. I would like the next and prev buttons to be at the button, but they keep getting drawn at the button of the image. Thus they move up and down, depending how big the picture is, each time you display a new image.
I found the following solution here, but it is not working for me:
android:gravity="bottom"
android:layout_alignParentBottom="true"
This is the xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/backFeetGallery"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/viewimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/background" />
<LinearLayout
android:id="#+id/linearLayout4"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:gravity="bottom"
android:orientation="horizontal" >
<Button
android:id="#+id/butLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12px"
android:text=" Left "
android:textColor="#ff0000ff" />
<Button
android:id="#+id/butFavrest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12px"
android:text=" Favrets "
android:textColor="#ff0000ff" />
<Button
android:id="#+id/butEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12px"
android:text=" email "
android:textColor="#ff0000ff" />
<Button
android:id="#+id/butRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12px"
android:text=" Right "
android:textColor="#ff0000ff" />
</LinearLayout>
</LinearLayout>
source code
public class cFeetView extends cBaseView implements OnClickListener {
cFileNames mFileNames;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.feet);
// add listeners
View mLeft = findViewById(R.id.butRight);
mLeft.setOnClickListener(this);
// add listeners
View mRight = findViewById(R.id.butLeft);
mRight.setOnClickListener(this);
mFileNames=new cFileNames();
mFileNames.Start();
DrawFeet();
}
public void DrawFeet()
{
int screenHeight;
ImageView picImage = (ImageView) findViewById(R.id.viewimage);// R.id.viewpic);
try {
String FileName = "canon20.png";
FileName=mFileNames.Current();
AssetManager assetManager = getAssets();
InputStream inputStream;
inputStream = assetManager.open(FileName);
Bitmap icon = BitmapFactory.decodeStream(inputStream);
int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
screenHeight = getWindowManager().getDefaultDisplay().getHeight();
int bw = icon.getWidth();
int bh=icon.getHeight();
float t = (float) screenWidth / (float) bw;
int iConHeight=(int)((float)bh*t);
picImage.setImageBitmap(icon);
// scale it
picImage.getLayoutParams().width = screenWidth;
picImage.getLayoutParams().height =iConHeight;
Bitmap scaledIcon = Bitmap.createScaledBitmap(icon, screenWidth, iConHeight, false);
} catch (IOException e) {
}
}
// set the top and buttom margins
public void onClick(View v)
{
Intent i;
switch(v.getId())
{
case R.id.butLeft:
mFileNames.Pre();
DrawFeet();
break;
case R.id.butRight:
mFileNames.Next();
DrawFeet();
break;
}
}
} // end class
Instead of LinearLayout, use RelativeLayout in your xml file.
LinearLayout is used only to arrange them in horizontal or vertical directions.
In order to use alignParentBottom you need to use RelativeLayout. And you dont need the gravity attribute.
My suggestion for your particular scenario is that you you have a RelativeLayout and put all your buttons inside a LinearLayout that is aligned at bottom and the ImageView above it.
Psedo:
<RelativeLayout>
<ImageView />
<LinearLayout
android:layout_below="#ImageView_id"
android:layout_alignParentBottom=true>
</LinearLayout>
</RelativeLayout>
Edited with your xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/backFeetGallery"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/viewimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/background" />
<LinearLayout
android:id="#+id/linearLayout4"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_below="#id/viewimage"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:orientation="horizontal" >
<Button
android:id="#+id/butLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12px"
android:text=" Left "
android:textColor="#ff0000ff" />
<Button
android:id="#+id/butFavrest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12px"
android:text=" Favrets "
android:textColor="#ff0000ff" />
<Button
android:id="#+id/butEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12px"
android:text=" email "
android:textColor="#ff0000ff" />
<Button
android:id="#+id/butRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12px"
android:text=" Right "
android:textColor="#ff0000ff" />
</LinearLayout>
</RelativeLayout>
You need to have everything in RelativeLayout. For example, look at this sample code which I made for one button which is aligned to bottom center of screen.
<?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:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Button" />
And here is the complete layout file for your layout
<?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:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Button" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Button" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Button" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/button2"
android:text="Button" />
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/button3"
android:text="Button" />
<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="38dp"
android:src="#android:drawable/gallery_thumb" />
This is how it looks,