I am having problem getting a layout's ID.
splash.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/splash_img1" >
</LinearLayout>
.java file
#Override
protected void onCreate(Bundle savedStateInstance) {
super.onCreate(savedStateInstance);
setContentView(R.layout.splash);
int imgId[] = new int[] { R.drawable.splash_img1, R.drawable.splash_img2 };
Random random = new Random();
int result = random.nextInt(imgId.length);
LinearLayout layout = (LinearLayout) findViewById(R.layout.splash);
layout.setBackgroundDrawable(getResources().getDrawable(imgId[result]));
}
Whenever I run this, I get a Gradle build error of "undefined layout".
you can get the layout by R.id.layout_id.
add id to your layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/splash_layout" // <-- this to your code.
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/splash_img1" >
</LinearLayout>
add this to your .java file
#Override
protected void onCreate(Bundle savedStateInstance) {
super.onCreate(savedStateInstance);
setContentView(R.layout.splash);
int imgId[] = new int[] { R.drawable.splash_img1, R.drawable.splash_img2 };
Random random = new Random();
int result = random.nextInt(imgId.length);
// edit this part.
LinearLayout layout = (LinearLayout) findViewById(R.id.splash_layout);
layout.setBackgroundDrawable(getResources().getDrawable(imgId[result]));
Hope this helps :)
Related
I have a FrameLayout which is the child of ScrollView,
I set an image as frame layout background and draw dynamic EditText on random coordinates of framelayout by setting x and y coordinates of EditText. The issue is when an edittext behind the keyboard gets focus, the layout doesn't scroll up. I have to scroll it manually and as soon as i start typing the layout scroll back to original position.
Instead of FrameLayout, if i take LinearLayout with vertical orientation and add edittext dynamically(without setting coordinates), it works perfectly.
"AsjustPan" is already set in manifest file.
Here is the test layout and java file which works:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/sv_root"
android:fillViewport="true"
android:scrollbars="none"
tools:context=".MainActivity">
<LinearLayout
android:id="#+id/fl_canvas"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
</ScrollView>
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout layout_canvas = findViewById(R.id.layout_canvas);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ppd);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, getResources().getDisplayMetrics().widthPixels, 1908, false);
layout_canvas.setBackground(new BitmapDrawable(getResources(), scaledBitmap));
for(int i=0; i<50; i++){
EditText editText = new EditText(this);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,100);
layoutParams.topMargin = 10;
editText.setLayoutParams(layoutParams);
layout_canvas.addView(editText);
}
}
}
Below is the test layout and java file which doesn't works:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/sv_root"
android:fillViewport="true"
android:scrollbars="none"
tools:context=".MainActivity">
<FrameLayout
android:id="#+id/layout_canvas"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</FrameLayout>
</ScrollView>
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FrameLayout layout_canvas = findViewById(R.id.layout_canvas);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ppd);
Bitmap sBitmap = Bitmap.createScaledBitmap(bitmap, getResources().getDisplayMetrics().widthPixels, 1908, false);
layout_canvas.setBackground(new BitmapDrawable(getResources(), sBitmap));
int y = 0;
for(int i=0; i<60; i++){
EditText editText = new EditText(this);
editText.setX(20);
editText.setY(y);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 80);
layout_canvas.addView(editText, layoutParams);
y = y + 100;
}
}
}
Can someone help me with this issue. Thanks
With the following Java and XML code, I was wondering how to retrieve the dimensions in pixels of an inner/child layout (LinearLayout, whereas the parent is RelativeLayout with paddings of 20dp) properly:
Java code:
public class TestActivity extends Activity {
private LinearLayout linLay;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
linLay = (LinearLayout)findViewById(R.id.linLay);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
linLay.setLayoutParams(lp);
getDimensions();
}
private void getDimensions() {
System.out.println(linLay.getHeight());
System.out.println(linLay.getWidth());
}
}
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:padding="20dp" >
<LinearLayout
android:id="#+id/linLay"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
</LinearLayout>
</RelativeLayout>
... Since I get outputs of 0 for the dimensions, I am aware that the LinearLayout needs to be set first on the screen before I could retrieve the dimensions... But what am I doing wrong here?
It didn't get its dimensions at the time you try to output them.You can just add a delay or just use method below.
private void getDimensions() {
ViewTreeObserver vto = linLay.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
#Override
public voidonGlobalLayout() {
linLay.getViewTreeObserver().removeGlobalOnLayoutListener(this);
int height =linLay.getMeasuredHeight();
int width =linLay.getMeasuredWidth();
System.out.println(height);
System.out.println(width);
}
});
}
I'm working on an android application and i have this problem. I want to add checkboxes dynamically in a scroll view which will be displayed in a dialog.
The problem is layout is null when going through this expression ScrollView layout = (ScrollView) findViewById(R.layout.custom);
Here is the function.
private void openCustomMenu() {
// TODO Auto-generated method stub
ScrollView layout = (ScrollView) findViewById(R.layout.custom);
layout.removeAllViews();
for (String name : StopNames){
CheckBox checkbox = new CheckBox(this);
checkbox.setText(name);
checkbox.setTextColor(getResources().getColor(R.color.white));
layout.addView(checkbox);
}
final Dialog dialog = new Dialog(this);
dialog.setContentView(layout);
dialog.setTitle("Bus Stops");
}
And here my xml file.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/custom"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</ScrollView>
Thank you for you help !
EDIT : new XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/custom">
</LinearLayout>
</ScrollView>
and new function.
private void openCustomMenu() {
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Bus Stops");
LinearLayout layout = (LinearLayout) dialog.findViewById(R.id.custom);
layout.removeAllViews();
for (String name : StopNames){
CheckBox checkbox = new CheckBox(this);
checkbox.setText(name);
checkbox.setTextColor(getResources().getColor(R.color.white));
layout.addView(checkbox);
}
}
change findViewById(R.layout.custom); to findViewById(R.id.custom); , so in your code, do this :
ScrollView layout = (ScrollView) findViewById(R.id.custom);
you are trying to get the id from the layouts and not from the ids.
I just want to click to button and to change the Background image. here is my different codes
array.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="image">
<item>#drawable/photo</item>
<item>#drawable/photo1</item>
<item>#drawable/photo2</item>
<item>#drawable/photo3</item>
<item>#drawable/photo4</item>
</string-array>
</resources>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_above="#+id/back"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#drawable/photo"
tools:context=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="146dp"
android:text="Button" />
</RelativeLayout>
and here is my activity code:
final LinearLayout background = (LinearLayout) findViewById(R.id.back);
Button button = (Button) findViewById(R.id.button1);
Resources res = getResources();
final TypedArray myImages = res.obtainTypedArray(R.array.image);
final Random random = new Random();
button.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v) {
int randomInt = random.nextInt(myImages.length());
background.setBackgroundResource(myImages.getResourceId(randomInt,-1));
int drawableID = myImages.getResourceId(randomInt,-1);
background.setBackgroundResource(drawableID);
}
});
it doesn't work , can you help me
Thanks in advance
In xml, you are using RelativeLayout, but in activity code, you are LinearLayout.
You are casting wrong layout in your code. You have taken the RelativeLayout in your xml file where as you are casting it into LinearLayout in your java file.
In your java file change the LinearLayout to RelativeLayout.
final LinearLayout background = (LinearLayout) findViewById(R.id.back);
write this:
final RelativeLayout background = (RelativeLayout) findViewById(R.id.back);
If the case is not the LinearLayout do this:
Change the definition of your array to
final String[] myImages = res.getStringArray(R.array.image);
And then, replace your onClick method for this:
public void onClick(View v) {
int randomInt = random.nextInt(myImages.length);
int drawableID = myImages.getResources().getIdentifier(myImages[randomInt], "drawable", this.getPackageName());
background.setBackgroundResource(drawableID);
}
I am new to android and just trying to modify a basic example.
main.xml is like
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<Button android:layout_width="228dp" android:layout_height="wrap_content" android:id="#+id/addButton" android:text="#string/addBtn"></Button>
</LinearLayout>
And want to add TextField on Click of the button. How to add Child to view?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn=(Button)findViewById(R.id.addButton);
btn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
EditText editText = new EditText(v.getContext());
//Append the editText to View
}
How to append it to current view/layout?
Something like this would help you i think.
ParentView.post(new Runnable() {
public void run() {
ParentView.addView(newView,0);
LinearLayout.LayoutParams rlparams = (LinearLayout.LayoutParams)rldeal.getLayoutParams();
rlparams.width = LinearLayout.LayoutParams.WRAP_CONTENT;
rlparams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
newView.setLayoutParams(rlparams);
}
});
Try to use include and merge:
http://mfarhan133.wordpress.com/2010/10/01/reusing-layout-include-merge-tag-for-androd/