textView scroll at first line - android

i want to scroll my TextView at some portion of text that i know, and show it on top. this is my scrollView and TextView:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:scrollbars="vertical"
android:id="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"/>
</ScrollView>
My text is an array of strings:
ScrollView scroll= (ScrollView) getActivity().findViewById(R.id.scroll);
TextView text = (TextView) getActivity().findViewById(R.id.text);
String book ="";
for(String line: lines){
book += line;
}
text.setText(book);
this is when i want to scroll:
String find = book[myindex];
int go = text.indexOf(find);
makeScroll(go);
this is my runnable:
private void makeScroll(final int go){
scrollRegole.post(new Runnable() {
public void run() {
scrollRegole.scrollTo(0, go);
}
});
}
this is not work, why? thanks!

Ok, the code is fine. It scrolls to the current (x,y) position of your "scrollView" as says:
http://developer.android.com/reference/android/widget/ScrollView.html#scrollTo(int, int)
So I think that is an interpretation trouble. Is not the same a "line" on the textView that a "y" coordinate on the ScrollView. So if you want to get the row number 15 for example, "go variable" cant be 15, it should be something proportional to the "TextSize" + padding + margin, etc...
Do you know what I mean?
Hope it helps.
EDIT:: Here is a test that I tried and it work fine if you know which is the row that you want. Maybe the trouble is that you didn't calculate the line properly
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;
public class MainActivity extends Activity {
ScrollView scroll;
TextView text;
EditText editText;
Button but;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.text);
scroll = (ScrollView) findViewById(R.id.scroll);
but = (Button) findViewById(R.id.button1);
editText = (EditText) findViewById(R.id.editText1);
but.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int go = Integer.valueOf(editText.getText().toString());
makeScroll(go);
}
});
}
private void makeScroll(final int go) {
scroll.post(new Runnable() {
public void run() {
scroll.scrollTo(0, go * text.getLineHeight());
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
and the xml
<LinearLayout 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:onClick="handleClick"
android:orientation="vertical"
tools:context=".PlayActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="number" />
</LinearLayout>
<ScrollView
android:id="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" >
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="sdqwerhdf adf qwer hdfashdf aE HFASDJFH ASKDFH QWLIULER HASDLIDLF HASDKJFH SDJF HAWEUJRH AS DKJFHL AWIUE4R YAS DJHFWIERUHF AUDF JHAWHWER ASJDFH ASDFHAS JEHLRUAIKYHE KURF ASJKDFHJ ASDFKJAHSDF ASDFASDF ASDF ASDFASDFASDF QWETR DGFsdqwerhdf adf qwer hdfashdf aE HFASDJFH ASKDFH QWLIULER HASDLIDLF HASDKJFH SDJF HAWEUJRH AS DKJFHL AWIUE4R YAS DJHFWIERUHF AUDF JHAWHWER ASJDFH ASDFHAS JEHLRUAIKYHE KURF ASJKDFHJ ASDFKJAHSDF ASDFASDF ASDF ASDFASDFASDF QWETR DGF"
android:textSize="20dp" />
</ScrollView>
</LinearLayout>
EDIT:: EDIT:
In order to get the line, here is a possible solution:
Android TextView and getting line of text
As I said at the commentary, firstly get a vector that contains every line, then look for the word. And finally move to the current row with the previous code.

this is how i resolved my problem:
this is my new ScrollView:
<ScrollView
android:id="#+id/scroll"
android:layout_width="0dp"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:layout_weight="3" >
<LinearLayout
android:id="#+id/containerTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
here i add many TextView into my container:
if(scroll == null){
scroll = (ScrollView) getActivity().findViewById(R.id.scroll);
}
if(layout == null)
layout = (LinearLayout) getActivity().findViewById(R.id.containerTextView);
layout.removeAllViews();
for(String line: lines){
TextView text = new TextView(getActivity());
text.setText(line);
text.setTextSize(...));
layout.addView(text);
}
this is when i want to scroll:
private void calculateScroll(final LinearLayout layout, final int indexLine){
layout.addOnLayoutChangeListener(new OnLayoutChangeListener(){
#Override
public void onLayoutChange(View paramView, int paramInt1,
int paramInt2, int paramInt3, int paramInt4, int paramInt5,
int paramInt6, int paramInt7, int paramInt8) {
int y = 0;
for(int x=0; x< indexLine; x++){
y += layout.getChildAt(x).getHeight();
}
makeScroll(y);
}
});
}
private void makeScroll(final int go){
scrollRegole.post(new Runnable() {
public void run() {
scroll.scrollTo(0, go);
}
});
}
here if i want to show lines[3] (for exsampe..) i know the correct height. My problem now is remove space between TextView.. but it works!

Related

Android ConstraintLayout - moving View fails

My goal is to dynamically create a grid of TextViews in a ConstraintLayout. I've seen many examples but there's an issue that I'm just not seeing. I've broken the task into two steps:
creating the View
positioning the View
I seem to have mastered the first but not the second. Usually I can modify a View's ConstraintSet if it is defined in XML and positioned at startup, but never when I've created it programmatically. When I create a View I'm careful to set an id, layout_width and layout_height. Method's I've tried for creating a new View:
creating it and setting parameters from a new LayoutParameters object
creating it and setting parameters from another View.
inflating it from an XML template.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity"
android:id="#+id/activity_main" >
<Button
android:id="#+id/makeView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10sp"
android:text="Make View"
android:onClick="makeView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/placeView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10sp"
android:text="Place View"
android:onClick="placeView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="#+id/makeView" />
<TextView
android:id="#+id/anchorView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20sp"
android:layout_marginStart="20sp"
android:text="View 00"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.textviewonthefly;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.ConstraintSet;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
final String TAG = "DEBUGME";
final int START = ConstraintSet.START;
final int END = ConstraintSet.END;
final int TOP = ConstraintSet.TOP;
int counter = 0;
View anchorView;
TextView nextView;
ConstraintLayout mainLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainLayout = (ConstraintLayout) findViewById(R.id.activity_main);
anchorView = (TextView) findViewById(R.id.anchorView);
}
public void makeView(View view) {
nextView = new TextView(this);
nextView.setLayoutParams(anchorView.getLayoutParams());
int nextViewId = nextView.generateViewId();
nextView.setId(nextViewId);
int ViewCount = ++counter;
String newName = String.format(Locale.US, "View %02d", ViewCount);
nextView.setText(newName);
this.mainLayout.addView(nextView);
}
public void placeView(View view) {
ConstraintSet set = new ConstraintSet();
set.clone(this.mainLayout);
set.clear(nextView.getId());
set.connect(nextView.getId(), TOP, anchorView.getId(), TOP);
set.connect(nextView.getId(), START, anchorView.getId(), END);
set.applyTo(this.mainLayout);
anchorView = nextView;
}
}
Solved.
clear(nextView.getId()); was too aggressive. I replaced that line with more specific directives:
clear(nextView.getId(), END);
clear(nextView.getId(), BOTTOM);
It's now working as intended, with a little revision my app creates a horizontal chain as I Add and Place:
[View 00][View 01][View 02]

Add value of an edittext to a textview with button click

Im currently working on a small project. Which has a textview, a button and a edittext. The textview is set to 1 and when the button is clicked the textview goes up by 1. I can fill the edittext with any number (including -1 for example). I need to get the value from the edittext and add it to the current value in the textview with the click of the same button. How do i do this? this sounds really easy but i cant seem to find a solution.
MainActivity
package com.example.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int numberText = 1;
EditText editTextID;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView textViewID = (TextView) findViewById(R.id.textViewID);
textViewID.setText("1");
}
public void changeValue(View view){
numberText+=1;
TextView textViewID = (TextView) findViewById(R.id.textViewID);
textViewID.setText(numberText+"");
}
}
Activity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context="com.example.myapplication.MainActivity">
<TextView
android:id="#+id/textViewID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.238" />
<Button
android:id="#+id/PlusID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="changeValue"
android:text="#string/plus"
tools:ignore="MissingConstraints"
android:layout_marginTop="50dp"
app:layout_constraintTop_toBottomOf="#+id/textViewID"
android:layout_marginLeft="0dp"
app:layout_constraintLeft_toLeftOf="#+id/textViewID"
android:layout_marginRight="0dp"
app:layout_constraintRight_toRightOf="#+id/textViewID" />
<EditText
android:id="#+id/editTextID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintTop_toBottomOf="#+id/PlusID"
android:layout_marginLeft="0dp"
app:layout_constraintLeft_toLeftOf="#+id/PlusID"
android:layout_marginRight="0dp"
app:layout_constraintRight_toRightOf="#+id/PlusID" />
</android.support.constraint.ConstraintLayout>
You can get EditText value as follows within changeValue:
EditText editTextID = (EditText ) findViewById(R.id.editTextID);
try {
int editValue = Integer.parseInt(editTextID.getText().toString());
} catch (NumberFormatException e) {
// edit text value not a valid integer
}
Then it should be simple to add editValue to numberText and display in the TextView.
Here is how:
public void changeValue(View view) {
// Retrieve the content from EditText
editTextID = (EditText) findViewById(R.id.edit_text);
String content = editTextID.toString();
// Then the value of that content
// Wrap below statement in a try-catch for a NumberFormatException
int value1 = Integer.parseInt(content);
// Add it to value from TextView
TextView textViewID = (TextView) findViewById(R.id.textViewID);
// Wrap below statement in a try-catch for a NumberFormatException
int value2 = Integer.parseInt(textViewID.toString());
// Finally set the correct value
int value = value1 + value2;
textViewID.setText(value + "");
}
Also, store the references to TextView and EditText as class fields rather than calling findViewById() again.
int currentNum=Integer.parseInt(textView.getText().toString());
int addedNum=Integer.valueOf(editText.getText().toString());
currentNum+=addedNum;
textView.setText(String.valueOf(currentNum));
Try initialising the required views first & storing the View instances... It would make your task a lot easier.
public class MainActivity extends AppCompatActivity {
private TextView displayValueTextView;
private EditText valueEditText;
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
private void initViews(){
displayValueTextView = (TextView) findViewById(R.id.tv_value);
valueEditText = (EditText) findViewById(R.id.et_input);
button = (Button) findViewById(R.id.button);
}
}
Here's the XML layout so you don't get confused about the Views and their associated Id's;
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/rl_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.myapplication.MainActivity">
<TextView
android:id="#+id/tv_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="26sp"
android:layout_centerInParent="true"
android:maxLines="1"
android:text="Hello World"/>
<EditText
android:id="#+id/et_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:ems="9"
android:hint="#string/hint_text"
android:maxLines="1"/>
<Button
android:id="#+id/button"
android:text="#string/plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="changeValue"
android:layout_toRightOf="#+id/et_input"
android:layout_alignBottom="#+id/et_input"/>
</RelativeLayout>
The onClick attribute of the Button is set to changeValue this is our method that will change the value of our TextView, dependent on what number is on your EditText. This method is shown below:
public void changeValue(View view) {
double numberToAdd;
double currentValue;
try {
numberToAdd = Double.parseDouble(valueEditText.getText().toString());
} catch (Exception ex) {
ex.printStackTrace();
Toast.makeText(this, "Edit Text must contain a number.", Toast.LENGTH_LONG).show();
valueEditText.setText("");
numberToAdd = 0;
}
try {
currentValue = Double.parseDouble(displayValueTextView.getText().toString());
} catch (Exception ex) {
ex.printStackTrace();
currentValue = 0;
Toast.makeText(this, "Current value is not a number. It is now set to 0.", Toast.LENGTH_LONG).show();
displayValueTextView.setText("0");
}
double finalValue = currentValue + numberToAdd;
displayValueTextView.setText(finalValue + "");
}
The numberToAdd variable is the number we are adding to our current TextView... We must;
Extract the value the EditText holds by getting it's text and parsing it as a double (this will allow us to handle decimals numbers in the EditText).
We then extract the current value of our TextView (if it is not a number an exception will be thrown and the textview will default to 0).
Finally we add the two values and set the TextView as the string representing the added values.

Android RelativeLayout - show items side by side when space but on separate lines when insufficient space?

I'm laying out an app which presents the results of a search in a ListView. I've defined each item to have a custom layout as follows:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="5dp"
android:paddingRight="?android:attr/scrollbarSize" >
<TextView
android:id="#+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ellipsize="end"
android:textAppearance="?android:attr/textAppearanceSearchResultTitle"
android:textIsSelectable="false" />
<TextView
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/title"
android:layout_toRightOf="#+id/subtitle"
android:gravity="bottom|right"
android:textAppearance="?android:attr/textAppearanceSearchResultSubtitle"
android:textIsSelectable="false" />
<TextView
android:id="#+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/title"
android:gravity="bottom|left"
android:textAppearance="?android:attr/textAppearanceSearchResultSubtitle"
android:textIsSelectable="false" />
</RelativeLayout>
This looks great when subtitle and date are of appropriate length to fit on a single line, however it looks awful if the subtitle consumes most of the line and forces date to take a very thin width and so wrap vertically.
What I'd like to do is have them appear side-by-side when there's space but on separate lines if there isn't. I've tried fiddling with the various layout_* attributes and the gravity to no avail and the question isn't very Google-able (at least, I can't think of the right words to search for). Can anyone point me towards the combination of layout rules that I need to achieve this? Or perhaps a different container if one would be more appropriate?
I believe the code below will do what you want but I can't see a way to do this only in the xml layout.
Basically I have added a textChangedListener to two different TextViews that have the two different layout options I believe you are looking for, both inside their own relative layout with the date displaying textview. When the subtitle is set the first of these is used to hold the text, if it requires more than a single line the second TextView is used and in either case the other has its visibility option set to GONE.
In my example I use a seperate thread to change the subtitle, hopefully this doesn't confuse things too much.
The layout xml is as follows:
<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=".MainActivity" >
<TextView
android:id="#+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ellipsize="end"
android:textAppearance="?android:attr/textAppearanceSearchResultTitle"
android:background="#FF009999"
android:textIsSelectable="false"
android:text="#string/my_title" />
<RelativeLayout
android:id="#+id/resizingTextContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/title" >
<TextView
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textAppearance="?android:attr/textAppearanceSearchResultSubtitle"
android:textIsSelectable="false"
android:textColor="#FFFFFFFF"
android:background="#FF000000" />
<TextView
android:id="#+id/subtitle1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/date"
android:textAppearance="?android:attr/textAppearanceSearchResultSubtitle"
android:textIsSelectable="false"
android:textColor="#FFFFFF"
android:background="#FF00FF00" />
<TextView
android:id="#+id/subtitle2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/date"
android:textAppearance="?android:attr/textAppearanceSearchResultSubtitle"
android:textIsSelectable="false"
android:visibility="gone"
android:textColor="#FFFFFF"
android:background="#FF00FF00" />
</RelativeLayout>
<Button
android:id="#+id/startTestBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/resizingTextContainer"
android:layout_centerHorizontal="true"
android:text="Click To Begin"
/>
</RelativeLayout>
And the main activity code with the textview switching logic:
package com.example.code.examples.changelayoutwithtextlength;
import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
TextWatcher textChangeDisplayCheck = new TextWatcher(){
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
displayLatestSubtitle(s);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView DateTextView = (TextView)findViewById(R.id.date);
DateTextView.setText("29th April 2013");
Button b = (Button)findViewById(R.id.startTestBtn);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
v.setEnabled(false);
Thread thread = new testSubtitleThread();
thread.start();
}
});
TextView tv = (TextView)findViewById(R.id.subtitle1);
tv.addTextChangedListener(textChangeDisplayCheck);
TextView tv2 = (TextView)findViewById(R.id.subtitle2);
tv2.addTextChangedListener(textChangeDisplayCheck);
}
private void displayLatestSubtitle(CharSequence newSubtitle)
{
TextView tv = (TextView)findViewById(R.id.subtitle1);
TextView tv2 = (TextView)findViewById(R.id.subtitle2);
tv.removeTextChangedListener(textChangeDisplayCheck);
tv2.removeTextChangedListener(textChangeDisplayCheck);
tv.setVisibility(View.GONE);
tv2.setVisibility(View.GONE);
tv2.setText("");
tv.setText(newSubtitle);
if(tv.getLineCount() > 1)
{
tv.setText("");
tv2.setText(newSubtitle);
tv2.setVisibility(View.VISIBLE);
}
else
{
tv.setVisibility(View.VISIBLE);
}
tv.addTextChangedListener(textChangeDisplayCheck);
tv2.addTextChangedListener(textChangeDisplayCheck);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class testSubtitleThread extends Thread
{
String[] subtitles = new String[] { "a short one", "a really long winded subtitle that will take over more than the allowed space", "tiny",
"Really, really, really, really, really, really, really, really, really, really, really, really, really, really, really, really, really, long.",
".....", "text just to long to fit on my device"};
private android.os.Handler handler = new android.os.Handler()
{
#Override
public void handleMessage(android.os.Message msg)
{
if(msg.what < subtitles.length)
{
TextView tv = (TextView)findViewById(R.id.subtitle1);
tv.setText(subtitles[msg.what]);
}
else
{
Button b = (Button)findViewById(R.id.startTestBtn);
b.setEnabled(true);
}
}
};
#Override
public void run()
{
for(int i = 0; i <= subtitles.length; i++)
{
handler.sendEmptyMessage(i);
try
{
Thread.sleep(3000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
I hope this helps.
In your layout, I would define two Views per item (subtitle and date), one in the same line and one in the line below.
Then I would check the length of those 2 fields (or their sum) and I would write this piece of code:
if (length > MAX_LENGTH_OF_LINE) > {
findViewById(R.id.subtitle_line_below).setVisibility(View.VISIBLE);
findViewById(R.id.subtitle_same_line).setVisibility(View.GONE);
} else {
findViewById(R.id.subtitle_line_below).setVisibility(View.GONE);
findViewById(R.id.subtitle_same_line).setVisibility(View.VISIBLE);
}

ScrollView Layout does not fill the whole screen

I got an Activity with two Fragments (one list one normal).
And the normal Fragment inflates a Scrollview containing a LineaLayout (vertical) and this layout contains TextViews.
The ScrollView and layout_width and layout_height are match_parent, so I think the whole screen should be used. But on the bottom there is still a "gap".
I hope you can help me.
ScrollView.xml
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/titlescreen_bg"
android:orientation="vertical"
android:paddingTop="60dp"
tools:context=".MainActivity" >
<TextView
android:id="#+id/tv_headline"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingBottom="60dp"
android:paddingTop="60dp"
android:textIsSelectable="false"
android:textSize="#dimen/fontsize_slogan_titlescreen" />
<TextView
android:id="#+id/tv_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:paddingBottom="30dp"
android:paddingTop="30dp"
android:textIsSelectable="false"
android:textSize="#dimen/fontsize_slogan_titlescreen" />
</LinearLayout>
</ScrollView>
the fragment inflating this layout.
package wak.iage.layout;
import wak.iage.R;
import android.app.Fragment;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MenuContentFragment extends Fragment
{
LinearLayout.LayoutParams relativeParams = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
LinearLayout topLayout = null;
TextView body = null;
TextView head = null;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.menu_content_main, container);
return v;
}
public void changeText(String title, String content) {
topLayout = (LinearLayout) getActivity().findViewById(
R.id.LinearLayout1);
head = (TextView) getActivity().findViewById(R.id.tv_headline);
body = (TextView) getActivity().findViewById(R.id.tv_content);
if (body == null) {
topLayout.removeViews(1, topLayout.getChildCount() - 1);
body = new TextView(getActivity().getApplicationContext());
body.setPadding(0, 30, 0, 20);
body.setTextColor(Color.BLACK);
body.setTextSize(22);
body.setGravity(Gravity.CENTER_HORIZONTAL);
topLayout.addView(body, relativeParams);
}
body.setText(content);
head.setText(title);
}
public void addGlossary() {
if (body != null) {
topLayout.removeView(body);
}
int i = 0;
for (int id : GLOSSARY) {
TextView glossary = new TextView(getActivity()
.getApplicationContext());
glossary.setText(getString(id));
glossary.setTextColor(Color.BLACK);
if (i % 2 == 0) {
glossary.setTypeface(Typeface.DEFAULT_BOLD);
glossary.setTextSize(22);
glossary.setPadding(0, 10, 0, 10);
}
topLayout.addView(glossary, relativeParams);
i += 1;
}
}
public static final int[] GLOSSARY = {
R.string.GlossaryAndroidOSTitle, R.string.GlossaryAndroidOSContent,
R.string.GlossaryAppTitle, R.string.GlossaryAppContent,
R.string.GlossaryCloudTitle, R.string.GlossaryCloudContent,
R.string.GlossaryDonwloadTitle, R.string.GlossaryDonwloadContent,
R.string.GlossaryFacebookTitle, R.string.GlossaryFacebookContent,
R.string.GlossaryGPSTitle, R.string.GlossaryGPSContent,
R.string.GlossaryHomescreenTitle,
R.string.GlossaryHomescreenContent, R.string.GlossaryPasswordTitle,
R.string.GlossaryPasswordContent, R.string.GlossaryRouterTitle,
R.string.GlossaryRouterContent, R.string.GlossarySDTitle,
R.string.GlossaySDContent, R.string.GlossayStandbyTitle,
R.string.GlossayStandbyContent, R.string.GlossaryTabletTitle,
R.string.GlossaryTabletContent, R.string.GlossaryTouchscreenTitle,
R.string.GlossaryTouchscreenContent, R.string.GlossayWidgetsTitle,
R.string.GlossayWidgetsContent, R.string.GlossayWLANTitle,
R.string.GlossayWLANContent };
}
Thanks a lot.
Edit: Even the proble is already fixed with: android:fillViewPort="true", I want to show you the problem.
But I don't have enough reputation to post a picture.
Sorry!
If i'm not mistaken, the ViewGroup's height (LinearLayout's height in your case), that is the (only) child inside a ScrollView, is always interpreted as wrap_content, since that content can be larger than the ScrollView's height (hence the scrollbars).
This also means that if the content is smaller, the ScrollView's content (child) may not necessarily stretch to fill the screen.
In order to visually help you fix this, we need to see a screenshot of your problem.
Maybe setting android:fillViewport="true" on the ScrollView will fix your issue:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:fadeScrollbars="false"
android:scrollbars="vertical" >
In your ScrollView add an attribute ie.
android:fillViewport="true"
inflater.inflate(R.layout.menu_content_main, container);
should be
inflater.inflate(R.layout.menu_content_main, container, false);
Replace ScrollView with NestedScrollView it will also solve the problem of nested scrolling.
<androidx.core.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.core.widget.NestedScrollView>
I had a similar problem and could only fix it with a Helper Class. I found the original code online and this is my implementation of it.
Java class:
public class ImageViewHelper extends android.support.v7.widget.AppCompatImageView {
public ImageViewHelper(Context context) {
super(context);
}
public ImageViewHelper(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ImageViewHelper(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Drawable d = getDrawable();
if (d != null) {
int w = MeasureSpec.getSize(widthMeasureSpec);
int h = w * d.getIntrinsicHeight() / d.getIntrinsicWidth();
setMeasuredDimension(w, h);
}
else super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
XML:
<com.example.app.ImageViewHelper
android:id="#+id/img"
android:src="#drawable/start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true" />

How did Google manage to do this? Slide ActionBar in Android application

I really want to implement this (the side navigation) in an app of my own, does anyone know how Google managed to do this?
They seem to have pulled the current window aside and put in a fly-in navigation of their own.
In fact, there's a way to do this. Even without implementing your own ActionBar.
Just have a look at the hierachyviewer! (Located in the tools directory)
There's the DecorView, and a LinearLayout as a child. This LinearLayout contains both the ActionBar and the other content. So, you can simply apply some FrameLayout.LayoutParams to this LinearLayout and get some space on the left side this way. Then, you can fill this space with your menu-ListView and overlay the other content with a FrameLayout, that, when it's clicked, collapses the menu. So, here's some code:
First, the class for collapsing / expanding (SlideMenu.java):
package your.cool.app;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
public class SlideMenu {
//just a simple adapter
public static class SlideMenuAdapter extends ArrayAdapter<SlideMenu.SlideMenuAdapter.MenuDesc> {
Activity act;
SlideMenu.SlideMenuAdapter.MenuDesc[] items;
class MenuItem {
public TextView label;
public ImageView icon;
}
static class MenuDesc {
public int icon;
public String label;
}
public SlideMenuAdapter(Activity act, SlideMenu.SlideMenuAdapter.MenuDesc[] items) {
super(act, R.id.menu_label, items);
this.act = act;
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = act.getLayoutInflater();
rowView = inflater.inflate(R.layout.menu_listitem, null);
MenuItem viewHolder = new MenuItem();
viewHolder.label = (TextView) rowView.findViewById(R.id.menu_label);
viewHolder.icon = (ImageView) rowView.findViewById(R.id.menu_icon);
rowView.setTag(viewHolder);
}
MenuItem holder = (MenuItem) rowView.getTag();
String s = items[position].label;
holder.label.setText(s);
holder.icon.setImageResource(items[position].icon);
return rowView;
}
}
private static boolean menuShown = false;
private static View menu;
private static LinearLayout content;
private static FrameLayout parent;
private static int menuSize;
private static int statusHeight = 0;
private Activity act;
SlideMenu(Activity act) {
this.act = act;
}
//call this in your onCreate() for screen rotation
public void checkEnabled() {
if(menuShown)
this.show(false);
}
public void show() {
//get the height of the status bar
if(statusHeight == 0) {
Rect rectgle = new Rect();
Window window = act.getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
statusHeight = rectgle.top;
}
this.show(true);
}
public void show(boolean animate) {
menuSize = Functions.dpToPx(250, act);
content = ((LinearLayout) act.findViewById(android.R.id.content).getParent());
FrameLayout.LayoutParams parm = (FrameLayout.LayoutParams) content.getLayoutParams();
parm.setMargins(menuSize, 0, -menuSize, 0);
content.setLayoutParams(parm);
//animation for smooth slide-out
TranslateAnimation ta = new TranslateAnimation(-menuSize, 0, 0, 0);
ta.setDuration(500);
if(animate)
content.startAnimation(ta);
parent = (FrameLayout) content.getParent();
LayoutInflater inflater = (LayoutInflater) act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
menu = inflater.inflate(R.layout.menu, null);
FrameLayout.LayoutParams lays = new FrameLayout.LayoutParams(-1, -1, 3);
lays.setMargins(0,statusHeight, 0, 0);
menu.setLayoutParams(lays);
parent.addView(menu);
ListView list = (ListView) act.findViewById(R.id.menu_listview);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//handle your menu-click
}
});
if(animate)
menu.startAnimation(ta);
menu.findViewById(R.id.overlay).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
SlideMenu.this.hide();
}
});
Functions.enableDisableViewGroup((LinearLayout) parent.findViewById(android.R.id.content).getParent(), false);
((ExtendedViewPager) act.findViewById(R.id.viewpager)).setPagingEnabled(false);
((ExtendedPagerTabStrip) act.findViewById(R.id.viewpager_tabs)).setNavEnabled(false);
menuShown = true;
this.fill();
}
public void fill() {
ListView list = (ListView) act.findViewById(R.id.menu_listview);
SlideMenuAdapter.MenuDesc[] items = new SlideMenuAdapter.MenuDesc[5];
//fill the menu-items here
SlideMenuAdapter adap = new SlideMenuAdapter(act, items);
list.setAdapter(adap);
}
public void hide() {
TranslateAnimation ta = new TranslateAnimation(0, -menuSize, 0, 0);
ta.setDuration(500);
menu.startAnimation(ta);
parent.removeView(menu);
TranslateAnimation tra = new TranslateAnimation(menuSize, 0, 0, 0);
tra.setDuration(500);
content.startAnimation(tra);
FrameLayout.LayoutParams parm = (FrameLayout.LayoutParams) content.getLayoutParams();
parm.setMargins(0, 0, 0, 0);
content.setLayoutParams(parm);
Functions.enableDisableViewGroup((LinearLayout) parent.findViewById(android.R.id.content).getParent(), true);
((ExtendedViewPager) act.findViewById(R.id.viewpager)).setPagingEnabled(true);
((ExtendedPagerTabStrip) act.findViewById(R.id.viewpager_tabs)).setNavEnabled(true);
menuShown = false;
}
}
Some helping methods (for me, in static Functions.java):
public static int dpToPx(int dp, Context ctx) {
Resources r = ctx.getResources();
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics());
}
//originally: http://stackoverflow.com/questions/5418510/disable-the-touch-events-for-all-the-views
//modified for the needs here
public static void enableDisableViewGroup(ViewGroup viewGroup, boolean enabled) {
int childCount = viewGroup.getChildCount();
for (int i = 0; i < childCount; i++) {
View view = viewGroup.getChildAt(i);
if(view.isFocusable())
view.setEnabled(enabled);
if (view instanceof ViewGroup) {
enableDisableViewGroup((ViewGroup) view, enabled);
} else if (view instanceof ListView) {
if(view.isFocusable())
view.setEnabled(enabled);
ListView listView = (ListView) view;
int listChildCount = listView.getChildCount();
for (int j = 0; j < listChildCount; j++) {
if(view.isFocusable())
listView.getChildAt(j).setEnabled(false);
}
}
}
}
Then, the layouts:
Layout of the menu (res/layout/menu.xml)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="250dip"
android:background="#color/darkblack">
<ListView
android:id="#+id/menu_listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#color/dividerblack"
android:dividerHeight="2dip" />
</LinearLayout>
<FrameLayout
android:id="#+id/overlay"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
Layout of the listitems (res/layout/menu_listitem.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent" >
<ImageView
android:id="#+id/menu_icon"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginRight="5dip"
android:layout_marginLeft="10dip"
android:layout_marginTop="10dip"
android:layout_marginBottom="10dip" />
<TextView
android:id="#+id/menu_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textSize="24dp"
android:layout_marginTop="10dip"
android:layout_marginBottom="10dip" />
</LinearLayout>
How to use it:
In your onCreate():
private SlideMenu slidemenu;
#Override
public void onCreate(Bundle savedInstanceState) {
//your onCreate code
slidemenu = new SlideMenu(this);
slidemenu.checkEnabled();
}
In the handler for your ActionBar homebutton:
slidemenu.show();
That's it!
And now, a little screenshot of it in action:
As far as I know, it is working. If you experience any problems or my explanations are not clear, please contact me!
EDIT: ExtendedViewPager & ExtendedPagerStrip:
ExtendedViewPager:
package your.cool.app;
//source: http://blog.svpino.com/2011/08/disabling-pagingswiping-on-android.html
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
public class ExtendedViewPager extends ViewPager {
private boolean enabled;
public ExtendedViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
this.enabled = true;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (this.enabled) {
return super.onTouchEvent(event);
}
return false;
}
#Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (this.enabled) {
return super.onInterceptTouchEvent(event);
}
return false;
}
public void setPagingEnabled(boolean enabled) {
this.enabled = enabled;
}
}
ExtendedPagerTabStrip:
package your.cool.app;
//source: http://blog.svpino.com/2011/08/disabling-pagingswiping-on-android.html
import android.content.Context;
import android.support.v4.view.PagerTabStrip;
import android.util.AttributeSet;
import android.view.MotionEvent;
public class ExtendedPagerTabStrip extends PagerTabStrip {
private boolean enabled;
public ExtendedPagerTabStrip(Context context, AttributeSet attrs) {
super(context, attrs);
this.enabled = true;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (this.enabled) {
return super.onTouchEvent(event);
}
return false;
}
#Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (this.enabled) {
return super.onInterceptTouchEvent(event);
}
return false;
}
public void setNavEnabled(boolean enabled) {
this.enabled = enabled;
}
}
I use this SlideMenu for an Activity with a ViewPager with PagerTabStrip for tabs like Talk, Market etc. You can't disable these Views in an easy way, so the two classes above just extend them to stop the onTouch event when disabled.
There are several attempts at doing this, however I have yet to find a lib or source code on how to implement it successfully with actionbar accross all api levels. One promising lib is here
https://github.com/jfeinstein10/SlidingMenu
here is a video of the example app.
here is the Google Play app link.
This does work with ActionbarSherlock. You will have to build the SlidingMenu library with ABS to get it working. Works and looks great!
Did a roundup of the original implementation and added XML parsing as well as autodetection of a possibly present actionbar, so it works with the native as well as a support action bar such as ActionBarSherlock.
The whole thing is now a library project together with an example app and is described over at Sliding Menu for android Thanks to scirocco for the initial idea and code!
If you are using API level greater that 11 you can use a much simpler approach inspired by the answer given by #Scirocco
// get content parent that is basically the whole
// app screen (viewed from hierarchy viewer)
final LinearLayout content =
(LinearLayout) findViewById(android.R.id.content).getParent();
// make new value animator with range from 0 to 1
final ValueAnimator animator = ValueAnimator.ofFloat(0, 1);
// set custom duration
animator.setDuration(500);
// on update is called for every value in the
// given range in time frame defined by the duration
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
// get the current value
float value = ((Float) (animation.getAnimatedValue())).floatValue();
// translate by that value, minus means translate left
content.setTranslationX(-250 * value);
}
});
// start the animator
animator.start();
// make or inflate custom view for test purposes
Button textView = new Button(this);
textView.setText("TestButton");
// add it to the frame layout that is the parent of the content on position 0
FrameLayout parent = (FrameLayout) content.getParent();
parent.addView(textView, 0);
The idea here is to use ValueAnimator that transforms and not just animates the main layout with the Action bar, so you can interact with the inflated view you want to use as a sliding panel.
You should replace the hardcoded values with something that is of use to your app.
I hope this helps :)
Well currently im working on a project and came across Sliding menu,i googled but gets very disappointed to see that no one has given some piece of code or some hint for how to start making a sliding menu,but every one has given link to some github's projects/libraries to use,I decided to do it myself and finally i have my own Sliding Menu Ready...
I have Spent two days on it
1. on making animations of sliding
2. on making it work with all screen resolutions
Its really easy and simple once you get some idea about Animations, i have read some where,its not sensible to re-invent the Wheel(people who are refering to github source code of sliding menu),but i beleif that you should atleast once try to make your own so you get a idea how it actually works and functions :P
so this is a picture of how my sliding menu will going to work
1.Find.xml //later in the code it will be refer as findLayout
<?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" >
<RelativeLayout
android:id="#+id/find_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="60dp"
android:padding="2dp"
android:background="#drawable/main_header">
<Button
android:id="#+id/filter"
android:layout_width="40dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="#drawable/filter_button" />
<TextView
android:id="#+id/city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/filter"
android:layout_marginLeft="20dp"
android:layout_marginTop="3dp"
android:text="Islamabad"
android:textSize="22sp"
android:textStyle="bold"
android:textColor="#android:color/primary_text_dark"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/city"
android:layout_alignLeft="#+id/city">
<TextView
android:id="#+id/interested_in"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="Men and Women"
android:textSize="12sp"
android:textColor="#android:color/primary_text_dark"/>
<ImageView
android:id="#+id/separator"
android:layout_width="2dp"
android:layout_height="18dp"
android:layout_toRightOf="#+id/interested_in"
android:layout_marginLeft="4dp"
android:src="#drawable/separator_1"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:layout_toRightOf="#+id/separator"
android:layout_centerVertical="true"
android:text="18-24 years"
android:textSize="12sp"
android:textColor="#android:color/primary_text_dark"/>
<ImageView
android:id="#+id/separator_1"
android:layout_width="2dp"
android:layout_height="18dp"
android:layout_toRightOf="#+id/age"
android:layout_marginLeft="4dp"
android:src="#drawable/separator_1"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:layout_toRightOf="#+id/separator_1"
android:layout_centerVertical="true"
android:text=">30km"
android:textSize="12sp"
android:textColor="#android:color/primary_text_dark" />
</RelativeLayout>
</RelativeLayout>
<GridView
android:id="#+id/users_grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/header"
android:numColumns="4">
</GridView>
</RelativeLayout>
<include
layout="#layout/filter"/> //here i included the filter.xml, which is on top of find.xml layout and is initially invisible
</RelativeLayout>
2.Filter.xml //later in code refer as FilterLayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/filter_layout"
android:visibility="invisible"
android:layout_width="260dp"
android:layout_height="match_parent"
android:background="#drawable/grey_bg" >
<ImageView
android:id="#+id/profile_pic"
android:layout_width="match_parent"
android:layout_height="220dp"
android:src="#drawable/pic"/>
<RelativeLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="55dp"
android:paddingLeft="10dp"
android:paddingTop="5dp"
android:layout_below="#+id/profile_pic"
android:background="#drawable/light_blue_header">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:text="Raja Babar"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#android:color/primary_text_dark"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/name"
android:layout_alignLeft="#+id/name">
<TextView
android:id="#+id/gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="Male"
android:textSize="12sp"
android:textColor="#android:color/primary_text_dark" />
<ImageView
android:id="#+id/seperator"
android:layout_width="2dp"
android:layout_height="20dp"
android:layout_toRightOf="#+id/gender"
android:layout_marginLeft="5dp"
android:src="#drawable/separator_1"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/seperator"
android:layout_marginLeft="5dp"
android:layout_centerVertical="true"
android:text="22 years"
android:textSize="12sp"
android:textColor="#android:color/primary_text_dark" />
</RelativeLayout>
</RelativeLayout>
<ScrollView
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_below="#+id/header"
android:layout_marginTop="15dp"
android:layout_centerHorizontal="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/filter_options"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/filter_options"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#android:color/primary_text_light"/>
<RelativeLayout
android:id="#+id/interested_in_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="15dp"
android:paddingRight="40dp"
android:layout_below="#+id/filter_options"
android:background="#drawable/interested_in_field">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="#string/gender"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#android:color/primary_text_light"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="#string/women_men"
android:textSize="18sp"
android:textColor="#33b9cd" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/age_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="15dp"
android:paddingRight="40dp"
android:layout_below="#+id/interested_in_layout"
android:background="#drawable/age_field_1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="#string/age"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#android:color/primary_text_light"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="18-24 years"
android:textSize="18sp"
android:textColor="#33b9cd"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="15dp"
android:paddingRight="40dp"
android:layout_below="#+id/age_layout"
android:background="#drawable/distance_field">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="#string/distance"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#android:color/primary_text_light"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text=">30km"
android:textSize="18sp"
android:textColor="#33b9cd"/>
</RelativeLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
In find.xml i have included filter.xml initially which is invisible
Now FilterAnimation.java
package matchat.helpers;
import com.s3.matchat.R;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.RelativeLayout;
public class FilterAnimation implements AnimationListener
{
Context context;
RelativeLayout filterLayout, otherLayout;
private Animation filterSlideIn, filterSlideOut, otherSlideIn, otherSlideOut;
private static int otherLayoutWidth, otherLayoutHeight;
private boolean isOtherSlideOut = false;
private int deviceWidth;
private int margin;
public FilterAnimation(Context context)
{
this.context = context;
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
deviceWidth = displayMetrics.widthPixels; // as my animation is x-axis related so i gets the device width and will use that width,so that this sliding menu will work fine in all screen resolutions
}
public void initializeFilterAnimations(RelativeLayout filterLayout)
{
this.filterLayout = filterLayout;
filterSlideIn = AnimationUtils.loadAnimation(context, R.anim.filter_slide_in);
filterSlideOut = AnimationUtils.loadAnimation(context, R.anim.filter_slide_out);
}
public void initializeOtherAnimations(RelativeLayout otherLayout)
{
this.otherLayout = otherLayout;
otherLayoutWidth = otherLayout.getWidth();
otherLayoutHeight = otherLayout.getHeight();
otherSlideIn = AnimationUtils.loadAnimation(context, R.anim.other_slide_in);
otherSlideIn.setAnimationListener(this);
otherSlideOut = AnimationUtils.loadAnimation(context, R.anim.other_slide_out);
otherSlideOut.setAnimationListener(this);
}
public void toggleSliding()
{
if(isOtherSlideOut) //check if findLayout is already slided out so get so animate it back to initial position
{
filterLayout.startAnimation(filterSlideOut);
filterLayout.setVisibility(View.INVISIBLE);
otherLayout.startAnimation(otherSlideIn);
}
else //slide findLayout Out and filterLayout In
{
otherLayout.startAnimation(otherSlideOut);
filterLayout.setVisibility(View.VISIBLE);
filterLayout.startAnimation(filterSlideIn);
}
}
#Override
public void onAnimationEnd(Animation animation)
{
if(isOtherSlideOut) //Now here we will actually move our view to the new position,because animations just move the pixels not the view
{
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(otherLayoutWidth, otherLayoutHeight);
otherLayout.setLayoutParams(params);
isOtherSlideOut = false;
}
else
{
margin = (deviceWidth * 80) / 100; //here im coverting device percentage width into pixels, in my other_slide_in.xml or other_slide_out.xml you can see that i have set the android:toXDelta="80%",so it means the layout will move to 80% of the device screen,to work across all screens i have converted percentage width into pixels and then used it
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(otherLayoutWidth, otherLayoutHeight);
params.leftMargin = margin;
params.rightMargin = -margin; //same margin from right side (negavite) so that our layout won't get shrink
otherLayout.setLayoutParams(params);
isOtherSlideOut = true;
dimOtherLayout();
}
}
#Override
public void onAnimationRepeat(Animation animation)
{
}
#Override
public void onAnimationStart(Animation animation)
{
}
private void dimOtherLayout()
{
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.5f);
alphaAnimation.setFillAfter(true);
otherLayout.startAnimation(alphaAnimation);
}
}
Now Find.java
package main.matchat.activities;
import matchat.helpers.FilterAnimation;
import com.s3.matchat.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.View.OnClickListener;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.Button;
import android.widget.RelativeLayout;
public class Find extends Activity implements OnClickListener
{
RelativeLayout filterLayout, findLayout;
Button btFilter;
FilterAnimation filterAnimation;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.find);
filterLayout = (RelativeLayout)findViewById(R.id.filter_layout);
findLayout = (RelativeLayout)findViewById(R.id.find_layout);
btFilter = (Button)findViewById(R.id.filter);
btFilter.setOnClickListener(this);
filterAnimation = new FilterAnimation(this);
initializeAnimations();
}
private void initializeAnimations()
{ //Setting GlobolLayoutListener,when layout is completely set this function will get called and we can have our layout onbject with correct width & height,else if you simply try to get width/height of your layout in onCreate it will return 0
final ViewTreeObserver filterObserver = filterLayout.getViewTreeObserver();
filterObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener()
{
#Override
public void onGlobalLayout()
{
filterLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
int deviceWidth = displayMetrics.widthPixels;
int filterLayoutWidth = (deviceWidth * 80) / 100; //here im coverting device percentage width into pixels, in my other_slide_in.xml or other_slide_out.xml you can see that i have set the android:toXDelta="80%",so it means the layout will move to 80% of the device screen,to work across all screens i have converted percentage width into pixels and then used it
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(filterLayoutWidth, RelativeLayout.LayoutParams.MATCH_PARENT);
filterLayout.setLayoutParams(params);//here im setting the layout params for my filter.xml because its has width 260 dp,so work it across all screen i first make layout adjustments so that it work across all screens resolution
filterAnimation.initializeFilterAnimations(filterLayout);
}
});
final ViewTreeObserver findObserver = findLayout.getViewTreeObserver();
findObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener()
{
#Override
public void onGlobalLayout()
{
findLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
filterAnimation.initializeOtherAnimations(findLayout);
}
});
}
#Override
public void onClick(View v)
{
int id = v.getId();
switch(id)
{
case R.id.filter:
filterAnimation.toggleSliding();
break;
}
}
}
Here are the animations res/anim
1.filter_slide_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/decelerate_interpolator">
<translate
android:fromXDelta="-100%"
android:toXDelta="0%"
android:duration="1000"
android:fillEnabled="true" />
</set>
2.filter_slide_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/decelerate_interpolator">
<translate
android:fromXDelta="0%"
android:toXDelta="-100%"
android:duration="1000"/>
</set>
3.other_slide_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/decelerate_interpolator" >
<translate
android:fromXDelta="0%"
android:toXDelta="-80%"
android:duration="1000"
android:fillEnabled="true"/>
</set>
4.other_slide_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/decelerate_interpolator">
<translate
android:fromXDelta="0%"
android:toXDelta="80%"
android:duration="1000"
android:fillEnabled="true"/>
</set>
There you go a complete working and functional Sliding Menu, and you can customized it to meet your requirements,if any one still have some problems setting up,feel free to ask,i feel pleasure to help you out :)
I've created my own solution for sliding away the view and revealing a menu underneath, as many other solutions appeared to not work on older Android versions or lacked proper instructions on how to get it to work.
My solution has the following features:
Provides support for sliding away a view to reveal a menu that lies underneath it
Both the menu and the view above can be any custom View
Supported on old Android versions (tested to work at least on Android 2.2)
Works with PhoneGap / Cordova projects
The solution uses a custom layout, called SlidingMenuLayout, that you are expected to add 2 views to. The first view you add is the menu, the second is the main view.
The simplest way to add the layout to your existing project is to override your Activity's setContentView() method:
#Override
public void setContentView(View view) {
SlidingMenuLayout layout = new SlidingMenuLayout(this);
layout.setLayoutParams(new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT,
0.0F));
layout.addView(new MenuView(this));
layout.addView(view);
super.setContentView(layout);
}
In this example, MenuView is the view that will actually show the menu. It is up to you to implement this view.
Finally, you can add a button (typically in the top left corner of your main view), that calls openMenu() or closeMenu() on the layout as appropriate.
The code for SlidingMenuLayout is found on the GitHub project page.
For those of you who uses the SlidingMenu library (https://github.com/jfeinstein10/SlidingMenu) there is a way to jack it in and it seems to work! With help of #Scirocco put this in your onCreate for the activity:
ViewGroup decorView = (ViewGroup) getWindow().getDecorView();
mSlidingMenu = new SlidingMenu(this);
ViewGroup mainContent = (ViewGroup) decorView.getChildAt(0);
decorView.removeView(mainContent);
mSlidingMenu.setContent(mainContent);
decorView.addView(mSlidingMenu);
mMenu = (LinearLayout) View.inflate(this, R.layout.menuview, null);
mSlidingMenu.setMenu(mMenu);
mSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
mSlidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
basically what it does is replacing the linearlayout in decor view with the slidingmenu instead.
Notice: Ive only tested it lightly but it seems to work.
public class ImprovedSlidingPaneLayout extends SlidingPaneLayout {
Context context;
FrameLayout left;
FrameLayout right;
Boolean canOpen = true;
public ImprovedSlidingPaneLayout(Context context) {
super(context);
this.context = context;
this.left = new FrameLayout(context);
this.right = new FrameLayout(context);
this.addView(left);
this.addView(right);
}
public ImprovedSlidingPaneLayout(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (canOpen)
return super.onInterceptTouchEvent(ev);
else
return false;
}
public ImprovedSlidingPaneLayout canOpen(Boolean canOpen) {
this.canOpen = canOpen;
return this;
}
public ImprovedSlidingPaneLayout makeActionBarSlide(Window window){
ViewGroup decorView = (ViewGroup) window.getDecorView();
ViewGroup mainContent = (ViewGroup) decorView.getChildAt(0);
decorView.removeView(mainContent);
setContentView(mainContent);
decorView.addView(this);
return this;
}
public ImprovedSlidingPaneLayout setMenuView(View view){
if((left.getChildCount()== 1)){
left.removeView(left.getChildAt(0));
}
left.addView(view);
return this;
}
public ImprovedSlidingPaneLayout setContentView(View view){
if((right.getChildCount()== 1)){
right.removeView(right.getChildAt(0));
}
right.addView(view);
return this;
}
public ImprovedSlidingPaneLayout setMenuWidth(int width){
left.setLayoutParams(new SlidingPaneLayout.LayoutParams(width, ViewGroup.LayoutParams.MATCH_PARENT));
return this;
}
}
this is my class extends SlidingPaneLayout. Can slide with actio

Categories

Resources