What I want to do is be able to insert a new ImageView into a LinearLayout which is inside a TableRow, without adjusting the size of the table row. My code is below.
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addToGrid(7,7);
}
private void addToGrid(int x,int y)
{
TableLayout table = (TableLayout)findViewById(R.id.table);
TableRow row=(TableRow)table.getChildAt(x);
LinearLayout box=(LinearLayout)row.getChildAt(y);
ImageView iv = new ImageView(this);
iv.setBackgroundResource(R.drawable.location);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(70, 70);
iv.setLayoutParams(layoutParams);
box.addView(iv);
}
What I want is the following(but with the missing ImageView):
What I am getting is this:
Although I can't show the entire layout file as it is far too large, it goes like this in general:
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/table">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="14">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/border"></LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/border" />
Note that I am adding the ImageView(the map icon) to one of the Linearlayouts.
Any help would be greatly appreciated!
Related
I am trying to programatically change the width of an ImageView without resizing the image. The ImageView originally shows the entire image, but when the width of the image is decreased, a portion of the image should be cut off, like this:
However, when my code reduces the width of the ImageView from 350dp to 200dp, the image is automatically resized instead:
How do I ensure that the ImageView does not resize the image when I decrease its width? Below is my code:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/photo_1_preview"
android:layout_width="350dp"
android:layout_height="350dp"
android:adjustViewBounds="false"
android:background="#drawable/android"/>
</LinearLayout>
MainActivity.java:
package com.example.changingimageviewwidth;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Reduce the width of the image preview box
ImageView imagePreviewBox = findViewById(R.id.photo_1_preview);
imagePreviewBox.getLayoutParams().width = 250;
}
}
Use android:scaleType="matrix" for the ImageView and put the desired image in android:src.
NOTE: In this case, I set the LinearLayout to be 400x400 because the image size is 400x400 px and wrap the ImageView inside of a LinearLayout just for clarity.
main.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:background="#333333"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_height="400px"
android:layout_width="400px"
android:orientation="vertical"
android:background="#0000FF">
<ImageView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:src="#drawable/android"
android:scaleType="matrix"
android:id="#+id/iv"/>
</LinearLayout>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView iv = findViewById(R.id.iv);
ViewGroup.LayoutParams params = iv.getLayoutParams();
params.width = 200; //200 pixels.
//Not necessary in my test. You may need to call the following
//method if you set the params outside of onCreate(), onCreateView().
//iv.requestLayout();
}
}
Output:
Set the scale type to CENTER_CROP. That will center the image in the view, and crop out anything that doesn't fit.
To get your desired behavior, simply set android:scaleType="fitStart" to your ImageView in xml. It will not change the aspect ratio of the src, but aligns it to the start of the ImageView, just as you mentioned.
The problem you're facing is because the drawable is set as a background of ImageView instead of being set as a source
After you fix that, the right scaleType to choose for your use-case is fitStart which will maintain the original aspect ratio for the bitmap and align it to the top-left of the ImageView
Please use the updated layout file -
<?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/photo_1_preview"
android:layout_width="350dp"
android:layout_height="350dp"
android:adjustViewBounds="false"
android:scaleType="fitStart"
android:src="#drawable/android"/>
</LinearLayout>
Try with the following code.
//activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/darker_gray"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/image_view"
android:layout_width="300dp"
android:layout_height="300dp"
android:scaleType="fitCenter"
android:src="#drawable/dumy" />
</LinearLayout>
//MainActivity.java
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.image_view);
imageView.getLayoutParams().width = dpToPixels(150);
imageView.requestLayout();
imageView.invalidate();
}
private int dpToPixels(int size) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, size, getResources().getDisplayMetrics());
}
}
I am trying to understand how to add views programatically (with java) in Android applications. I created a simple layout (below) to practice that but I struggle to solve the following error:
java.lang.IllegalStateException: Could not find method addingView() in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button'
Screenshot from the app:
My XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:id="#+id/activity_main"
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"
tools:context="com.example.android.view_create_test_1.MainActivity">
<LinearLayout
android:id="#+id/top"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:background="#555555">
</LinearLayout>
<RelativeLayout
android:id="#+id/bottom"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#116677">
<Button
android:text="Add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:layout_centerInParent="true"
android:onClick="addingView"/>
</RelativeLayout>
</LinearLayout>
My JAVA:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void addingView (){
LinearLayout top = (LinearLayout) findViewById(R.id.top);
LinearLayout newLayout = new LinearLayout (this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
newLayout.setLayoutParams(new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, 50));
top.addView(newLayout,params);
}
}
Please help me out how to solve the error. Thanks
Your method should come like this
public void addingView (View view){
LinearLayout top = (LinearLayout) findViewById(R.id.top);
LinearLayout newLayout = new LinearLayout (this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
newLayout.setLayoutParams(new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, 50));
top.addView(newLayout,params);
}
When you specify an onclick method in Xml, it must contain the corresponding method in your Activity. However you must include the correct parameters. THe View parameter is required.
Change your addingView method declaration to:
public void addingView (View v){
Your error is however of changeColor(View), which i assume else where, you have done the same thing to the changeColor declaration. Add the View parameter to this method also
I am having a layout with a single editText in it,below the editText I added a add button. Now my question is when I click the add button I need to get another editText below the editText and has to repeat the same.Please anyone help, Thank you.
activity_main.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">
<LinearLayout
android:id="#+id/ll_edit_texts_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:id="#+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD" />
</LinearLayout>
and put following code in your Activity
final LinearLayout llEditTextsContainer = (LinearLayout) view.findViewById(R.id.ll_edit_texts_container);
Button buttonAdd = (Button) view.findViewById(R.id.buttonAdd);
buttonAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
EditText editText = new EditText(getActivity());
//editText.setId(); you should set id smartly if you wanted to use data from this edittext
llEditTextsContainer.addView(editText);
}
});
First of all create a container view in your main layout which is going to hold the new Edittexts.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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"
tools:context="com.example.vuclip.dynamictextedit.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/addTextView"
android:text="Add EditText"
android:onClick="onClick"
/>
<TableLayout
android:id="#+id/containerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</TableLayout>
Here, on pressing the button, new Edittexts will be created and added into the TableLayout.
Now create a new Layout which will hold your edittext(I called this file new_layout.xml).
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="#+id/newEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
Now add this layout into your main activity.
public class MainActivity extends AppCompatActivity {
TableLayout container;
static int rowIndex = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
container = (TableLayout) findViewById(R.id.containerLayout);
}
public void onClick(View view) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View newRowView = inflater.inflate(R.layout.new_layout,null);
container.addView(newRowView,rowIndex);
rowIndex++;
}}
I need to show n number of Edit text added dynamically below a static Edit text.
I am adding the ET but some how it is not visible. What am doing wrong??
Here is my code:
private void addEditTextView(int numberOfViews){
// Using layout params same as above static ET
ViewGroup.LayoutParams layoutParams = staticEditText.getLayoutParams();
for (int index = 0; index <= numberOfViews; index++) {
final EditText newET = new EditText(getActivity());
//Added below 2 lines just to make sure width and height are coming
layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
newET.setLayoutParams(layoutParams);
newET.setHint("Select ME");
newET.setFocusable(false);
newET.setId(index);
newET.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//TODO set time
Log.d("Clicked...",newET.getId()+"");
}
});
//parentLayout is Linear Layout with Vertical orientation
parentLayout.addView(newET);
}
}
XML code :
<LinearLayout
android:id="#+id/parentLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/staticEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:background="#drawable/rectangle_bg_dark_gray_border"
android:focusable="false"
android:hint="staticEditText"
android:padding="7dp" />
</LinearLayout>
UPDATE : parentLayout.getChildCount() is coming correctly. New views are getting added but not visible!
Hi try adding a Scrollview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/parentLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- EDITTEXT here -->
</LinearLayout>
</ScrollView>
</LinearLayout>
Hey, I want to change the TypeFace of one of the TextViews to an external font.. How can I do that?
Code:
public class English extends Activity {
<...>
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.quran_english);
listview=(ListView) findViewById(R.id.QuranEnglishListView);
<..add into list..>
SimpleAdapter adapter = new SimpleAdapter(
this,
list,
R.layout.quran_english_row,
new String[] {"Arabic","English"},
new int[] {R.id.QuranEnglishTextViewArabic,R.id.QuranEnglishTextViewEnglish});
listview.setAdapter(adapter);
xml for each row (quran_english_row.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical">
<LinearLayout android:gravity="right" android:layout_height="wrap_content" android:id="#+id/linearLayout1" android:layout_width="fill_parent">
<TextView android:textSize="21px" android:text="TextView" android:id="#+id/QuranEnglishTextViewArabic" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
<TextView android:textSize="21px" android:text="TextView" android:id="#+id/QuranEnglishTextViewEnglish" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
xml for the layout (quran_english.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical">
<ListView android:id="#+id/QuranEnglishListView" android:layout_width="fill_parent" android:layout_height="fill_parent"></ListView>
</LinearLayout>
This crashes because of NullPointer:
((TextView) findViewById(R.id.QuranEnglishTextViewArabic)).setTypeface(Typeface.createFromAsset(getAssets(),"dejavusans.ttf"));
So how can I reach the TextView that is in the row to change its TypeFace? Thanks
You can try to get the layout for each list item by
View rowView = adapter.getView(...);
Then get the TextView of the layout by
TextView txt = (TextView)rowView .findViewById(R.id.QuranEnglishTextViewEnglish);
txt.setTypeFace(...);
and set your TypeFaces. Haven't tested that yet, but i guess it could work.
But I recommend extending your own BaseAdapter. Then create another list-row class which provides the inflated layouts for your BaseAdapter. There you'll be able to set the typefaces easily for every TextView.
try this links it works http://techdroid.kbeanie.com/2011/04/using-custom-fonts-on-android.html see steps and class MyTextView and MyButton shown above layout