RelativeLayout Params addRule without ID - java

I want to add the following rule to a TextView within my RelativeLayout
TextView anyView = new TextView(anyContext);
RelativeLayout.LayoutParams layout;
..
layout.addRule(RelativeLayout.LEFT_OF, anyView.getID());
This only works if I add the ID of the element. The problem is that I do not have and do not want to have any IDs specified, because my view can be used multiple times within the same ViewGroup later.
So either I have to assign that rule by reference or I need to create new IDs dynamically so that I don't create multiple Views with the same ID.
Any suggestions what to do in this situation?
I tried to copy from this xml:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentRight="true"
android:id="#+id/gridLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A lot of text for some width"/>
</GridLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="123"
android:layout_centerVertical="true"
android:gravity="center_horizontal"
android:layout_toLeftOf="#+id/gridLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>

try this
RelativeLayout.LayoutParams pAC = new RelativeLayout.LayoutParams(
your width,
your height);
pAC.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
anyView .setLayoutParams(pAC);

Related

Widget ListView loading view

I'm trying to display a dynamic list on a widget and so far i've been able to display my 3 headers (which are simply textview inside a linearlayout) but for my items (2 textview in one linearlayout inside another linear layout) the view displays Loading... instead of the content (a task label).
I've already bumped getViewTypeCount to 2 in my RemoteViewsFactory to match the number of views returned by the getViewAt.
I can't figure out what is wrong inside this layout that would cause a problem:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="3dp"
android:clickable="true"
android:focusable="true"
style="#style/SelectableItemBackground"
android:id="#+id/widget_item"
>
<LinearLayout
android:paddingLeft="22dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="•"
android:paddingRight="6dp"
android:textSize="17dp"
android:textColor="#color/colorText" />
<TextView
android:id="#+id/widget_item_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="17dp"
android:textColor="#color/colorText" />
</LinearLayout>
Thank you for reading :)
To anyone reading this, i have been able to solve it by suppressing the style attribute

How to use a ScrollView without keyboard altering view?

I have limited space in one of my views and I am trying to implement a Scroll View which can help to resolve the limited space issue. However when I try to implement the scroll view it messes-up the keyboard, every time I now try to edit a EditText it causes the elements within the view to move up with the keyboard.
Is there any other solutions or work arounds which I can try to implement?
I have tried the solution at Move layouts up when soft keyboard is shown? which suggest to add the following code to the Manifest android:windowSoftInputMode="stateHidden|adjustResize which prevents the keyboard from jumping up at runtime.
I have also tried to set the gravity of the scrollview and use different layouts example: linear, constraint, relative-layouts. I have tried to make a custom keyboard class and to implement it by setting it in the relevant view activity class (Not my code but tried to implement it)
Here is the solution for this fix
- android:windowSoftInputMode="adjustResize" in Manifest File
- Make a class "CommentKeyBoardFix.Java" and copy and paste the below code.
public class CommentKeyBoardFix
{
private View mChildOfContent;
private int usableHeightPrevious;
private FrameLayout.LayoutParams frameLayoutParams;
private Rect contentAreaOfWindowBounds = new Rect();
public CommentKeyBoardFix(Activity activity)
{
FrameLayout content = activity.findViewById(android.R.id.content);
mChildOfContent = content.getChildAt(0);
mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(this::possiblyResizeChildOfContent);
frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
}
private void possiblyResizeChildOfContent()
{
int usableHeightNow = computeUsableHeight();
if (usableHeightNow != usableHeightPrevious)
{
int heightDifference = 0;
if (heightDifference > (usableHeightNow /4))
{
// keyboard probably just became visible
frameLayoutParams.height = usableHeightNow - heightDifference;
}
else
{
// keyboard probably just became hidden
frameLayoutParams.height = usableHeightNow;
}
mChildOfContent.layout(contentAreaOfWindowBounds.left, contentAreaOfWindowBounds.top, contentAreaOfWindowBounds.right, contentAreaOfWindowBounds.bottom);
mChildOfContent.requestLayout();
usableHeightPrevious = usableHeightNow;
}
}
private int computeUsableHeight()
{
mChildOfContent.getWindowVisibleDisplayFrame(contentAreaOfWindowBounds);
return contentAreaOfWindowBounds.height();
}
}
And then call this class in your Activity or Fragment
setContentView(R.layout.your_comments_layout)
CommentKeyBoardFix(this) ---> For Kotlin
or
new CommentKeyBoardFix(this) ---> For Java
This gives a static member error which I don't know how to solve.
However none of these solved the problem.
My current layout file which is causing the error.
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true"
android:id="#+id/main_layout1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/freq_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Freq"
android:textSize="14sp" />
<EditText
android:id="#+id/freq"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:digits="0123456789.,"
android:inputType="numberDecimal"
android:textAlignment="center"
android:textColor="#color/black" />
<Spinner
android:id="#+id/spinner_r1_unit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:tooltipText="unit"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="5dp">
<TextView
android:id="#+id/duty_cycle_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/duty_cycle"
android:textSize="14sp"
android:layout_marginLeft="5dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/duty_cycle_percentage"
android:layout_marginLeft="5dp"
android:text="50%"/>
</LinearLayout>
<SeekBar
android:id="#+id/seekBar"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:max="100"
android:progress="50" />
<android.support.v7.widget.AppCompatButton
android:layout_width="100dp"
android:layout_below="#id/main_layout1"
android:layout_height="wrap_content"
android:text="Design"
android:layout_marginLeft="30dp"
android:id="#+id/button_design"
android:layout_marginTop="10dp"
android:background="#drawable/white_rounded_button"
/>
<ScrollView
android:layout_width="200dp"
android:layout_height="100dp"
>
</ScrollView>
</LinearLayout>
</merge>
If I remove the scroll view it solves the problem however, I need to add a scroll view since this .xml file is used as a snippet in a different .xml file where the space is very limited.
Is there any other possible work-around of the implementation of a scroll view? Is there any good explanation to why this scroll view is causing this bug and considering the space issue what other alternatives are there that I can take into account, which can deliver similar results to that of a scroll view, without causing the keyboard error?

emptyView always showing even if list is not empty

I wish someone could take a look on that and let me know what is wrong.this is my emptyView container in the activity_main
<ListView
android:id="#+id/display_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RelativeLayout
android:id="#+id/empty_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<TextView
android:id="#+id/empty_shelter_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:fontFamily="sans-serif-medium"
android:paddingTop="16dp"
android:text="#string/empty_view_title_text"
android:textAppearance="?android:textAppearanceMedium" />
<TextView
android:id="#+id/empty_subtitle_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/empty_shelter_title"
android:layout_centerHorizontal="true"
android:fontFamily="sans-serif"
android:paddingTop="8dp"
android:text="#string/empty_view_sub_text"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#A2AAB0" />
</RelativeLayout>
I have set emptyView in each fragment, and even though when items are showing in the list it is still showing the empty view fields.
//set empty activity
ListView listView = rootView.findViewById(R.id.display_view);
View emptyView = rootView.findViewById(R.id.empty_view);
listView.setEmptyView(emptyView);
//create a new cursor adapter
mCursorAdapter = new PlatformCursorAdapter(getActivity(),null);
listView.setAdapter(mCursorAdapter);
I will highly appreciate it if you can tell me what i did wrong.
Try hiding the empty_view in xml.
<RelativeLayout
android:id="#+id/empty_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_centerInParent="true">
my friends you have a send a null in adapter. send a list
mCursorAdapter = new PlatformCursorAdapter(getActivity(),null);
and other way are used to check a listview is null hide the
display_view view and show the empty_view .

Unable to access TextView of the the Custom ActionBar

I am using different layout and showing into action bar. I want to change value of TextView programatically which I used in layout.
Here is my xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/splashscreen_background">
<TextView
android:id="#android:id/text1"
android:layout_width="?attr/actionBarSize"
android:layout_height="match_parent"
android:textColor="#ff0000"
android:textSize="10sp"
android:textStyle="bold"
android:background="#034552"
android:text="M:101"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center"
android:minHeight="?android:attr/listPreferredItemHeight"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"/>
<TextView
android:id="#android:id/text2"
android:layout_width="?attr/actionBarSize"
android:layout_height="match_parent"
android:textColor="#ff0000"
android:textSize="10sp"
android:textStyle="bold"
android:background="#034552"
android:text="N:101"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center"
android:minHeight="?android:attr/listPreferredItemHeight"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"/>
</RelativeLayout>
And here is java file code:
ActionBar ab = getSupportActionBar();
ab.setDisplayShowHomeEnabled(false);
ab.setDisplayShowTitleEnabled(false);
LayoutInflater li = LayoutInflater.from(this);
View customView = li.inflate(R.layout.test, null);
TextView tv = (TextView)customView.findViewById(R.id.text1);
ab.setCustomView(customView);
ab.setDisplayShowCustomEnabled(true);
Can any one suggest me how can I change the value of TextView programmatically.
In xml, replace android:id="#android:id/text1" with android:id="#+id/text1". Same goes for TextView with id text2 ..
and in java, you will be able to programmatically set it like this:
TextView tv = (TextView)customView.findViewById(R.id.text1);
tv.setText("Your Text");
To findout what's the difference between #android:id and #+id, Please see this:
https://stackoverflow.com/a/9530028/2534007

How to right-align elements in table layout in Java?

Please have a look at the following XML file
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:stretchColumns="*"
android:padding="5dp" >
<TableRow
android:id="#+id/tableRow0"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Words To Remove" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_span="2"
android:padding="5dp"
>
</ScrollView>
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<Button
android:id="#+id/removeWordsBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Acerto"
/>
</TableRow>
</TableLayout>
Inside the scrollview of this, I am adding table rows with textviews and buttons dynamically like below
ScrollView scrollView = (ScrollView)findViewById(R.id.scrollView);
TableLayout internalTableLayout = new TableLayout(this);
for(int i=0;i<words.size();i++)
{
TableRow r = new TableRow(this);
//r.setId(i);
TextView t = new TextView(this);
t.setGravity(Gravity.RIGHT);
CheckBox c = new CheckBox(this);
r.addView(t);
r.addView(c);
internalTableLayout.addView(r);
}
scrollView.addView(internalTableLayout);
}
But, what I get is the following
As you can see, the dynamically generated elements are left aligned. How can I make these right aligned?
Solution 1:
Use a RelativeLayout, make use of it's attributes:
Replace the children with android:layout_alignParentLeft="true" for the TextView and android:layout_alignParentRight="true" for the CheckBox.
That positions the children relative to the parents borders. You may also want to add android:layout_centerVertical="true" to each child to center the two vertical within each list item.
Solution 2:
If you want it for the table,
use myTable.setColumnStretchable(2, true); in Java.
and android:stretchColumns="2" in XML.
Solution 3:
Use CheckedTextView. I think this will serve your need.
Hope that helps.

Categories