I have a question about resize layout. This is my activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
requestWindowFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.log_in);
}
How can I set size my R.layout.log_in in this activity? I want set size to 400x300 but in activity not xml.
I don't know what are your requirements but I prefer to set the dimension on the xml (if they're suppose to be fixed).
Anyway, this can be useful.
The code used in the link:
// Gets linearlayout
LinearLayout layout = (LinearLayout)findViewById(R.id.numberPadLayout);
// Gets the layout params that will allow you to resize the layout
LayoutParams params = layout.getLayoutParams();
// Changes the height and width to the specified *pixels*
params.height = 300;
params.width = 400;
Related
I want to display instruction activity when user opens the app for the first time. I am using shared preference for that. Whatever I am doing so far is working. But I think my way of achieving this is not right.
What I am following:
I am drawing a transparent instruction image(with instructions) in photoshop.
Checking if user is opening that page for the first time(using shared preference).
Displaying that particular image in an activity with translucent theme
private void showFrontPageGuideIfFirstTime(){
if(!prefKeeper.getBoolean(PreferenceKey.FRONT_GUIDE)){
Intent intent = new Intent(this, ShowGuide.class);
intent.putExtra(BACKGROUND_KEY, R.drawable.front_page_png);
this.startActivity(intent);
prefKeeper.putBoolean(PreferenceKey.FRONT_GUIDE, true);
}
}
And my instruction page looks something like(made in photoshop):
The Instruction Image
But I think by this way it would not work in all smart phone screens.
Where am I wrong, and what would be the best way of doing this?
Implementation as below
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:id="#+id/framelayout"
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"
tools:context="in.excogitation.example_mvptdd.MainActivity">
<!-- Include your layout here-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!"/>
</FrameLayout>
In your activity.java
public class MainActivity extends AppCompatActivity {
FrameLayout frameLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Parent FrameLayout
frameLayout = (FrameLayout) findViewById(R.id.framelayout);
// Dynamically create a relativelayout which will be appended to framelayout
final RelativeLayout relativeLayout = new RelativeLayout(getApplicationContext());
relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams
.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
relativeLayout.setBackgroundColor(Color.DKGRAY);
relativeLayout.setAlpha(0.7f);
relativeLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// remove the whole relativelayout on click
frameLayout.removeView(relativeLayout);
}
});
RelativeLayout.LayoutParams params;
// Place 1st 30x40 ImageView at (50,60) coordinates
params = new RelativeLayout.LayoutParams(100, 100);
params.leftMargin = 20;
params.topMargin = 50;
final ImageView imageView1 = new ImageView(getApplicationContext());
imageView1.setImageDrawable(getResources().getDrawable(R.drawable.ic_action_star));
// Add 1st imageview to relative layout
relativeLayout.addView(imageView1, params);
// Place 2nd 30x40 ImageView at (100,60) coordinates
params = new RelativeLayout.LayoutParams(120, 120);
params.leftMargin = 800;
params.topMargin = 450;
final ImageView imageView2 = new ImageView(getApplicationContext());
imageView2.setImageDrawable(getResources().getDrawable(R.drawable.ic_action_star));
// Add 2nd imageview to relative layout
relativeLayout.addView(imageView2, params);
// finally add it ot the framelayout
frameLayout.addView(relativeLayout);
}
}
Ofcourse you should modify this code with your own images and colors and interactions. Its just a simple working version that is better than loading a whole image upfront when you all you want is smaller helper images on a translucent background for the instructions.
Also you in this way you make things more Android-ish and editable. You can add more children to the relative layout like a textview to include instructions.
Screenshot on load of app and hence the relative layout as an overlay.
Screenshot on click/touch , the relative layout is removed.
In the following code , Why I can not set a custom size for my relativeLayout object? The resolution of my device is 240 * 320.
RelativeLayout relativeLayout;
#Override
protected void onCreate(Bundle bundle){
super.onCreate(bundle);
relativeLayout = new RelativeLayout(this);
relativeLayout.getLayoutParams().width=240;
relativeLayout.getLayoutParams().height=320;
setContentView(relativeLayout);}
You should try to use a layout parameter like this:
RelativeLayout relativeLayout = new RelativeLayout(this);
//You can put the background in another color to check were is the layout
//relativeLayout.setBackgroundResource(android.R.color.black);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(240, 320);
relativeLayout.setLayoutParams(lp);
setContentView(relativeLayout);
I am trying to display a grid of buttons using gridLayout at runtime. I am trying to fit the gridLayout to fit the entire screen's width. I am able to do that for 3x3 and 5x5 button grid (using my nexus 4). But when I go for 7x7, I lose the last column of buttons i.e the last column of GridLayout. I seriously cant understand whats going wrong here.
Here's what I am doing.. I take the grid size from settings activity and this is the number of columns in GridLayout (numColumns). I use LayoutParams and get the width and height and set the buttons' width and height to (layoutWidth/numColumns) and (layoutHeight/numColumns). Please help me here guyz.. I have pasted the code below.
public class StartGameActivity extends ActionBarActivity {
#TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_game);
SharedPreferences sharedPref= PreferenceManager.getDefaultSharedPreferences(this);
int numColumns=Integer.parseInt(sharedPref.getString("Grid", "3").toString());
// create a RelativeLayout
RelativeLayout relativeLayout = new RelativeLayout(this);
//LinearLayout linearLayout = new LinearLayout(this);
// define the RelativeLayout layout parameters.
//LinearLayout.LayoutParams linearLayoutparams=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
DisplayMetrics metrics=new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width=metrics.widthPixels;
int height=metrics.heightPixels;
RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(width,height);
RelativeLayout.LayoutParams relativeLayoutParams1 = new RelativeLayout.LayoutParams(width,height);
GridLayout gridLayout=new GridLayout(this);
GridLayout.LayoutParams gridLayoutParams = new GridLayout.LayoutParams();
gridLayoutParams.setGravity(Gravity.FILL_HORIZONTAL);
gridLayout.setColumnCount(numColumns);
gridLayout.setRowCount(numColumns);
gridLayout.setMinimumHeight(height);
gridLayout.setMinimumWidth(width);
for(int i=0;i<numColumns*numColumns;i++)
{
Button button=new Button(this);
button.setWidth(width/numColumns);
button.setHeight(width/numColumns);
button.setMinWidth(width/numColumns);
button.setMinHeight(width/numColumns);
button.setGravity(Gravity.FILL);
//buttonForEveryRow++;
//columIndex++;
gridLayout.addView(button);
}
relativeLayout.addView(gridLayout,relativeLayoutParams1);
setContentView(relativeLayout,relativeLayoutParams);
}
}
You should set the parameters of the button only after it has been added to it's parent layout.
So inside your FOR loop, remove all the button.set.. lines and add the following lines at the end(after the gridLayout.addView line) :
ViewGroup.LayoutParams par = button.getLayoutParams();
par.width=width/numColumns;
par.height=width/numColumns;
button.setLayoutParams(par);
http://developer.android.com/reference/android/view/View.html#getLayoutParams%28%29 :
All views should have layout parameters. These supply parameters to the parent of this view specifying how it should be arranged.
I have an activity with ListView that has:
android:theme="#android:style/Theme.Dialog"
in Manifest.
When I open it and when it has only one line in ListView, the window that opens is very small.
How do I make the window take the whole screen?
Use this in your onCreate method of the Activity to make it full screen.
#Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.myxml);
LayoutParams params = getWindow().getAttributes();
params.height = LayoutParams.MATCH_PARENT;
params.width = LayoutParams.MATCH_PARENT;
getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
}
I have found that setting the window size does work, but you have to do it a bit later. In this example the window width is set to 90% of the display width, and it is done in onStart() rather than onCreate():
#Override
protected void onStart() {
super.onStart();
// In order to not be too narrow, set the window size based on the screen resolution:
final int screen_width = getResources().getDisplayMetrics().widthPixels;
final int new_window_width = screen_width * 90 / 100;
LayoutParams layout = getWindow().getAttributes();
layout.width = Math.max(layout.width, new_window_width);
getWindow().setAttributes(layout);
}
Similar to the answer from PravinCG but it can be done with one line in onCreate()...
getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
Use the suggested code before setcontentview() call. It will work.
Just a small update. Used MATCH_PARENT instead of the deprecated FILL_PARENT. PravinCG's answer worked great for me.
Yeezz ! I figured it out ! The problem is that the margin sizes are not calculated in the window widht. So If you set the layout margin to 0 and move that part to the padding of the layout the problem will be solved.
Can anyone help me how to set the width of TextView to wrap_content through code and not from XML?
I am dynamically creating a TextView in code ,so is there anyway to how to set its width to wrap_content through code?
TextView pf = new TextView(context);
pf.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
For different layouts like ConstraintLayout and others, they have their own LayoutParams, like so:
pf.setLayoutParams(new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
or
parentView.addView(pf, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
There is another way to achieve same result. In case you need to set only one parameter, for example 'height':
TextView textView = (TextView)findViewById(R.id.text_view);
ViewGroup.LayoutParams params = textView.getLayoutParams();
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
textView.setLayoutParams(params);
Solution for change TextView width to wrap content.
textView.getLayoutParams().width = ViewGroup.LayoutParams.WRAP_CONTENT;
textView.requestLayout();
// Call requestLayout() for redraw your TextView when your TextView is already drawn (laid out) (eg: you update TextView width when click a Button).
// If your TextView is drawing you may not need requestLayout() (eg: you change TextView width inside onCreate()). However if you call it, it still working well => for easy: always use requestLayout()
// Another useful example
// textView.getLayoutParams().width = 200; // For change `TextView` width to 200 pixel
A little update on this post: if you are using ktx within your Android project, there is a little helper method that makes updating LayoutParams a lot easier.
If you want to update e.g. only the width you can do that with the following line in Kotlin.
tv.updateLayoutParams { width = WRAP_CONTENT }
I am posting android Java base multi line edittext.
EditText editText = findViewById(R.id.editText);/* edittext access */
ViewGroup.LayoutParams params = editText.getLayoutParams();
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
editText.setLayoutParams(params); /* Gives as much height for multi line*/
editText.setSingleLine(false); /* Makes it Multi line */
I think this code answer your question
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)
holder.desc1.getLayoutParams();
params.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
holder.desc1.setLayoutParams(params);