How to create horizontal layout programmatically in java? - java

I have XML file where I display information using LinearLayout vertical. All what I need to do is to create horizontal LinearLayout and attach there an image and a TextView. Then I need to display this horizontal LinearLayout on my main vertical LinearLayout. How can I do this? I need to do this programmatically in java

First create a LinearLayout with orientation = vertical then create another LinearLayout inside this with orientation = horizontal with your image and textview. Like this
<LinearLayout
android:orientation="vertical"
>
//Your other views
<LinearLayout
android:orientation="horizontal"
>
// Your image and textview
</LinearLayout>
</LinearLayout>

Related

How to Fix Scrollview for Linear Layout Containing Textview

I currently have a program that uses a linear layout that contains a Textview that is populated with x amount of values (dependent on arrayList size determined in separate activity) but Scrollview is not working as I anticipated.
Since Scrollview can only have one direct child, I have the Linear Layout with the TextView nested within; however when I wrap both of these within Scrollview, the Linear Layout is still not scrollable
//This is what I am using to populate the Linear Layout that I'm trying to make scrollable
TextView DisplayString = (TextView)findViewById(R.id.stringNumsCon1);
LinearLayout LinContainer = (LinearLayout)findViewById(R.id.LinLay);
Intent intent = getIntent();
ArrayList<String> timerVals = (ArrayList<String>)getIntent().getSerializableExtra("timerVals");
DisplayString.setTextSize(15);
DisplayString.setTextColor(Color.BLACK);
for(int i=0; i<timerVals.size(); i++){
DisplayString.append(timerVals.get(i));
DisplayString.append("\n");
}
//This is how I am trying to make it scrollable within the xml
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/LinLay"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="391dp">
<TextView
android:id="#+id/stringNumsCon1"
android:layout_width="match_parent"
android:layout_height="391dp"
android:orientation="vertical">
</TextView>
</LinearLayout>
</ScrollView>
First Activity (used to create arraylist that will populate Linear Layout on next activity)
Portion of second activity I wish to make scrollable (Blue lines delineating what part of page I want scrollable, the rest I want to remain static)
I expected this to make the Linear Layout portion scrollable, but I can see data is cut off and inaccessible when actually implementing this code
You won't need the ScrollView, neither the LinearLayout. Remove them from your layout.
All you will need is set in your TextView in xml:
android:scrollbars = "vertical"
so your final xml layout would be:
<TextView
android:id="#+id/stringNumsCon1"
android:layout_width="match_parent"
android:layout_height="391dp"
android:orientation="vertical"
android:scrollbars = "vertical" />
then set the movement method in onCreate of your activity/fragment as shown below:
Java
DisplayString.setMovementMethod(new ScrollingMovementMethod());
Kotlin
DisplayString.movementMethod = ScrollingMovementMethod()
P.S.1: You can find the original answer here
P.S.2: Consider to use camel case naming convention for your variable names. E.g. use displayString instead of DisplayString.

RecyclerView scroll offset gets reset when adding unrelated view programmatically

Whenever I add a TextView on top of RecyclerView, the horizontal scroll offset resets to start.
I have a LinearLayout which contains two items - TextViewContainer and the RecyclerView. After the RecyclerView has initialized, I update the horizontal scroll offset by 100 pixels the following way:
recyclerView.scrollTo( 100, 0 ).
Then whenever I add a TextView to the TextViewContainer, the RecyclerView offset gets reset to the start for whatever reason, even though they're not related in any way.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainLayout"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textViewContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
<RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
I'm adding the TextView the following way:
LinearLayout container = rootView.findViewById(
R.layout.textViewContainer );
TextView textView = new TextView(container.getContext());
textView.setLayoutParams(new
LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
textView.setGravity(Gravity.CENTER);
textView.setTextSize( 20.5f );
textView.setText("TEST" );
container.removeAllViews();
container.addView(textView);
I'm expecting for the RecyclerView stay at the 100 pixel horizontal offset and NOT to get reset back to 0 when I add a TextView above in a separate container.
The textViewContainer is unrelated to the RecyclerView, I don't understand why it's resetting to the start as soon as I add the TextView to the Text View Container.
The only reason I could think of why this is happening is because each item in the RecyclerView is wider than the screen width and maybe that's why Android is trying to reset the horizontal scroll offset since there has been changes in the main layout, not sure..
Is there any way I can prevent this from happening? Thanks!

Hide View element with zero height and width in android studio

I have two ListView and I want them to share the same layout position so when I click a button one ListView hides.
Maybe this is not possible or there is a better way like fragments?
Use FrameLayout. This layout view overlies two views over each other.
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/list1"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
<ListView
android:id="#+id/list2"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</FrameLayout>
For showing the first page (i.e. the first ListView):
findViewById(R.id.list1).setVisibility(View.VISIBLE);
findViewById(R.id.list2).setVisibility(View.GONE);
And for the second page:
findViewById(R.id.list1).setVisibility(View.GONE);
findViewById(R.id.list2).setVisibility(View.VISIBLE);
fragments are the easy way to do this IF you don't plan on changing the data in your views.
Make a button and
/*create fragment of the opposite view, probably through a boolean field and an if block
then*/
getSupportFragmentManager.beginTransaction().replace(/*your fragments*/).commit().
in your onclicklistener.

Android align two buttons on same line

I need to align button,and textview in on same line,button aligned right side and textview also aligned right side,
I tried lot of methods but aligned first textview then aligned button,
how to solve this problem please any one help and solve my problem in programatically,
aligned success in layout xml design but I need it programatically.
place both views inside your layout and set orientation to "horizontal".
<LinearLayout>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
linear layout can also be nested into one another!
if you want to implement more lines of views then you could just define a vertical layout (the main.xml will by default define one for you when first created) and inside that vertical linear layout just insert how many horizontal linear layouts (like the one I written above) as you wish.
Something like this should work. You should modify this to fit your needs (i.e. set the proper text size, width, height, etc).
TextView tv = new TextView(this);
tv.setText("Text");
Button bt = new Button(this);
bt.setText("Button");
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
ll.addView(tv);
ll.addView(bt);
setContentView(ll);

I can't scroll my customized view

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical">
<ScrollView android:id="#+id/scroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:fillViewport="true">
<adam.music.testfont.NwcDrawingArea android:id="#+id/NwcDrawingArea"
android:layout_height="2000dp"
android:layout_width="2000dp"
/>
</ScrollView>
</LinearLayout>
I created Android Project and edited main.xml as above.
And then I customized view to draw image.
To see the effect of scrolling customized view I set width and height as above.
Now I can see the image in a customized view but it doesn't scroll.
Could you help me what the problem is?
Should I add something more?
There's an interplay between ScrollView and its contents. For example, if, instead of your NwcDrawingArea widget, you inserted this TextView into your ScrollView:
<TextView android:text="8This is a lot of text 7This is a lot of text 6This is a lot of text 5This is a lot of text 4This is a lot of text 3This is a lot of text 2This is a lot of text 1This is a lot of text"
android:layout_width="10dp"
android:layout_height="2000dp"
android:background="#FF00FF00"
/>
You'll see a skinny green vertical TextView whose text is longer than the screen, and the ScrollView shows scrollbars that let you see the hidden part of the TextView to the extent of the text. Now, change the TextView layout_width="2000dp". The TextView becomes a full-screen green area that has a single line of text that runs off the right side of the screen. ScrollView, of course, shows no horizontal scroll bars. However, ScrollView shows no vertical scroll bars either, even though we sized the TextView to be much longer than the screen. ScrollView is attempting to determine the visually interesting portion of its contents, so you need to understand the behavior for whatever widget you are subclassing.
For example, ScrollView respects the layout sizes of LinearLayout and RelativeLayout in the way that you expected it to behave with TextView. In fact, it is common to make a LinearLayout or RelativeLayout -- rather than a view such as TextView -- the child of a ScrollView. Drop your TextView in a fixed-height LinearLayout and you should get the behavior that you expected.

Categories